pyquil.quilatom module

class pyquil.quilatom.Add(op1: Union[pyquil.quilatom.Expression, int, float, complex], op2: Union[pyquil.quilatom.Expression, int, float, complex])[source]

Bases: pyquil.quilatom.BinaryExp

associates: ClassVar[str] = 'both'
static fn(a: Union[pyquil.quilatom.Expression, int, float, complex], b: Union[pyquil.quilatom.Expression, int, float, complex]) Union[pyquil.quilatom.Add, int, float, complex][source]
operator: ClassVar[str] = ' + '
precedence: ClassVar[int] = 1
class pyquil.quilatom.BinaryExp(op1: Union[pyquil.quilatom.Expression, int, float, complex], op2: Union[pyquil.quilatom.Expression, int, float, complex])[source]

Bases: pyquil.quilatom.Expression

associates: ClassVar[str]
static fn(a: Union[pyquil.quilatom.Expression, int, float, complex], b: Union[pyquil.quilatom.Expression, int, float, complex]) Union[pyquil.quilatom.BinaryExp, int, float, complex][source]
operator: ClassVar[str]
precedence: ClassVar[int]
class pyquil.quilatom.Div(op1: Union[pyquil.quilatom.Expression, int, float, complex], op2: Union[pyquil.quilatom.Expression, int, float, complex])[source]

Bases: pyquil.quilatom.BinaryExp

associates: ClassVar[str] = 'left'
static fn(a: Union[pyquil.quilatom.Expression, int, float, complex], b: Union[pyquil.quilatom.Expression, int, float, complex]) Union[pyquil.quilatom.Div, int, float, complex][source]
operator: ClassVar[str] = '/'
precedence: ClassVar[int] = 2
class pyquil.quilatom.Expression[source]

Bases: object

Expression involving some unbound parameters. Parameters in Quil are represented as a label like ‘%x’ for the parameter named ‘x’. An example expression therefore may be ‘%x*(%y/4)’.

Expressions may also have function calls, supported functions in Quil are sin, cos, sqrt, exp, and cis.

This class overrides all the Python operators that are supported by Quil.

class pyquil.quilatom.FormalArgument(name: str)[source]

Bases: pyquil.quilatom.QuilAtom

Representation of a formal argument associated with a DEFCIRCUIT or DEFGATE … AS PAULI-SUM or DEFCAL form.

property index: NoReturn
out() str[source]
class pyquil.quilatom.Frame(qubits: Sequence[Union[int, pyquil.quilatom.Qubit, pyquil.quilatom.FormalArgument]], name: str)[source]

Bases: pyquil.quilatom.QuilAtom

Representation of a frame descriptor.

name: str

The name of the frame.

out() str[source]
qubits: Tuple[Union[pyquil.quilatom.Qubit, pyquil.quilatom.FormalArgument], ...]

A tuple of qubits on which the frame exists.

class pyquil.quilatom.Function(name: str, expression: Union[pyquil.quilatom.Expression, int, float, complex], fn: Callable[[Union[int, float, complex]], Union[int, float, complex]])[source]

Bases: pyquil.quilatom.Expression

Supported functions in Quil are sin, cos, sqrt, exp, and cis

class pyquil.quilatom.Label(label_name: str)[source]

Bases: pyquil.quilatom.QuilAtom

Representation of a label.

Parameters

label_name – The label name.

out() str[source]
class pyquil.quilatom.LabelPlaceholder(prefix: str = 'L')[source]

Bases: pyquil.quilatom.QuilAtom

out() str[source]
class pyquil.quilatom.MemoryReference(name: str, offset: int = 0, declared_size: Optional[int] = None)[source]

Bases: pyquil.quilatom.QuilAtom, pyquil.quilatom.Expression

Representation of a reference to a classical memory address.

Parameters
  • name – The name of the variable

  • offset – Everything in Quil is a C-style array, so every memory reference has an offset.

  • declared_size – The optional size of the named declaration. This can be used for bounds checking, but isn’t. It is used for pretty-printing to quil by deciding whether to output memory references with offset 0 as either e.g. ro[0] or beta depending on whether the declared variable is of length >1 or 1, resp.

out() str[source]
class pyquil.quilatom.Mul(op1: Union[pyquil.quilatom.Expression, int, float, complex], op2: Union[pyquil.quilatom.Expression, int, float, complex])[source]

Bases: pyquil.quilatom.BinaryExp

associates: ClassVar[str] = 'both'
static fn(a: Union[pyquil.quilatom.Expression, int, float, complex], b: Union[pyquil.quilatom.Expression, int, float, complex]) Union[pyquil.quilatom.Mul, int, float, complex][source]
operator: ClassVar[str] = '*'
precedence: ClassVar[int] = 2
class pyquil.quilatom.Parameter(name: str)[source]

Bases: pyquil.quilatom.QuilAtom, pyquil.quilatom.Expression

Parameters in Quil are represented as a label like ‘%x’ for the parameter named ‘x’.

out() str[source]
class pyquil.quilatom.Pow(op1: Union[pyquil.quilatom.Expression, int, float, complex], op2: Union[pyquil.quilatom.Expression, int, float, complex])[source]

Bases: pyquil.quilatom.BinaryExp

associates: ClassVar[str] = 'right'
static fn(a: Union[pyquil.quilatom.Expression, int, float, complex], b: Union[pyquil.quilatom.Expression, int, float, complex]) Union[pyquil.quilatom.Pow, int, float, complex][source]
operator: ClassVar[str] = '^'
precedence: ClassVar[int] = 3
class pyquil.quilatom.Qubit(index: int)[source]

Bases: pyquil.quilatom.QuilAtom

Representation of a qubit.

Parameters

index – Index of the qubit.

out() str[source]
class pyquil.quilatom.QubitPlaceholder[source]

Bases: pyquil.quilatom.QuilAtom

property index: NoReturn
out() str[source]
classmethod register(n: int) List[pyquil.quilatom.QubitPlaceholder][source]

Return a ‘register’ of n QubitPlaceholders.

>>> qs = QubitPlaceholder.register(8) # a qubyte
>>> prog = Program(H(q) for q in qs)
>>> address_qubits(prog).out()
H 0
H 1
...
>>>

The returned register is a Python list of QubitPlaceholder objects, so all normal list semantics apply.

Parameters

n – The number of qubits in the register

class pyquil.quilatom.QuilAtom[source]

Bases: object

Abstract class for atomic elements of Quil.

out() str[source]
class pyquil.quilatom.Sub(op1: Union[pyquil.quilatom.Expression, int, float, complex], op2: Union[pyquil.quilatom.Expression, int, float, complex])[source]

Bases: pyquil.quilatom.BinaryExp

associates: ClassVar[str] = 'left'
static fn(a: Union[pyquil.quilatom.Expression, int, float, complex], b: Union[pyquil.quilatom.Expression, int, float, complex]) Union[pyquil.quilatom.Sub, int, float, complex][source]
operator: ClassVar[str] = ' - '
precedence: ClassVar[int] = 1
class pyquil.quilatom.TemplateWaveform(duration: float)[source]

Bases: pyquil.quilatom.QuilAtom

duration: float

The duration [seconds] of the waveform.

num_samples(rate: float) int[source]

The number of samples in the reference implementation of the waveform.

Note: this does not include any hardware-enforced alignment (cf. documentation for samples).

Parameters

rate – The sample rate, in Hz.

Returns

The number of samples.

samples(rate: float) numpy.ndarray[source]

A reference implementation of waveform sample generation.

Note: this is close but not always exactly equivalent to the actual IQ values produced by the waveform generators on Rigetti hardware. The actual ADC process imposes some alignment constraints on the waveform duration (in particular, it must be compatible with the clock rate).

Parameters

rate – The sample rate, in Hz.

Returns

An array of complex samples.

class pyquil.quilatom.WaveformReference(name: str)[source]

Bases: pyquil.quilatom.QuilAtom

Representation of a Waveform reference.

name: str

The name of the waveform.

out() str[source]
pyquil.quilatom.format_parameter(element: Union[pyquil.quilatom.Expression, pyquil.quilatom.MemoryReference, numpy.int64, int, float, complex]) str[source]

Formats a particular parameter. Essentially the same as built-in formatting except using ‘i’ instead of ‘j’ for the imaginary number.

Parameters

element – The parameter to format for Quil output.

pyquil.quilatom.qubit_index(qubit: Union[pyquil.quilatom.Qubit, pyquil.quilatom.QubitPlaceholder, pyquil.quilatom.FormalArgument, int]) int[source]

Get the index of a QubitDesignator.

Parameters

qubit – the qubit designator.

Returns

An int that is the qubit index.

pyquil.quilatom.quil_cis(expression: Union[pyquil.quilatom.Expression, int, float, complex]) pyquil.quilatom.Function[source]
pyquil.quilatom.quil_cos(expression: Union[pyquil.quilatom.Expression, int, float, complex]) pyquil.quilatom.Function[source]
pyquil.quilatom.quil_exp(expression: Union[pyquil.quilatom.Expression, int, float, complex]) pyquil.quilatom.Function[source]
pyquil.quilatom.quil_sin(expression: Union[pyquil.quilatom.Expression, int, float, complex]) pyquil.quilatom.Function[source]
pyquil.quilatom.quil_sqrt(expression: Union[pyquil.quilatom.Expression, int, float, complex]) pyquil.quilatom.Function[source]
pyquil.quilatom.substitute(expr: Union[pyquil.quilatom.Expression, int, float, complex], d: Mapping[pyquil.quilatom.Parameter, Union[int, float, complex]]) Union[pyquil.quilatom.Expression, int, float, complex][source]

Using a dictionary of substitutions d try and explicitly evaluate as much of expr as possible.

Parameters
  • expr – The expression whose parameters are substituted.

  • d – Numerical substitutions for parameters.

Returns

A partially simplified Expression or a number.

pyquil.quilatom.substitute_array(a: Union[Sequence[pyquil.quilatom.Expression], numpy.ndarray], d: Mapping[pyquil.quilatom.Parameter, Union[int, float, complex]]) numpy.ndarray[source]

Apply substitute to all elements of an array a and return the resulting array.

Parameters
  • a – The expression array to substitute.

  • d – Numerical substitutions for parameters.

Returns

An array of partially substituted Expressions or numbers.

pyquil.quilatom.unpack_classical_reg(c: Union[pyquil.quilatom.MemoryReference, Tuple[str, int], List[Any], str]) pyquil.quilatom.MemoryReference[source]

Get the address for a classical register.

Parameters

c – A list of length 2, a pair, a string (to be interpreted as name[0]), or a MemoryReference.

Returns

The address as a MemoryReference.

pyquil.quilatom.unpack_qubit(qubit: Union[pyquil.quilatom.Qubit, pyquil.quilatom.QubitPlaceholder, pyquil.quilatom.FormalArgument, int]) Union[pyquil.quilatom.Qubit, pyquil.quilatom.QubitPlaceholder, pyquil.quilatom.FormalArgument][source]

Get a qubit from an object.

Parameters

qubit – the qubit designator to unpack.

Returns

A Qubit or QubitPlaceholder instance