mimiqcircuits.operations.operation

Base Operation class.

Classes

Operation()

Abstract base class for everything that can be pushed to a circuit.

class Operation[source]

Bases: ABC

Abstract base class for everything that can be pushed to a circuit.

An operation acts on a fixed number of qubits, classical bits and complex-valued z-variables (see num_qubits, num_bits and num_zvars). Gates, measurements, resets, noise channels, classical operations and annotations are all operations. An operation only says what is applied; an Instruction pairs it with the targets it is applied to.

Examples

>>> from mimiqcircuits import *
>>> # operations know how many targets of each kind they need
>>> op = Measure()
>>> op.num_qubits, op.num_bits, op.num_zvars
(1, 1, 0)
>>> # pushing an operation to a circuit creates an instruction
>>> c = Circuit()
>>> c.push(op, 0, 0)
1-qubit, 1-bit circuit with 1 instruction:
└── M @ q[0], c[0]
classmethod allow_bit_aliasing()[source]

Whether an instruction may list the same classical bit more than once.

False for most operations. Operations that read and write the same bit, such as the classical logic operations, return True.

classmethod allow_zvar_aliasing()[source]

Whether an instruction may list the same z-variable more than once.

False for most operations. Arithmetic operations whose output may also be one of their inputs return True.

property num_qubits

Number of qubits the operation acts on.

Read-only: assigning to it raises ValueError.

property num_ancillas

Number of clean ancillas of the operation.

The last num_ancillas qubit targets must be in |0> when the operation starts, and are returned in |0>. On those inputs the operation acts as the one it was built for, which only touches the other qubits.

It is set on GateDecl, BlockDecl, Block and calls to them, and is 0 for every other operation. decompose() sets it when it wraps a decomposition that uses clean ancillas, and marks the ancillas of each call with AncillaAlloc and AncillaFree.

property num_qregs

Number of quantum registers (groups of qubit targets) the operation takes.

Read-only: assigning to it raises ValueError.

property num_bits

Number of classical bits the operation acts on.

Read-only: assigning to it raises ValueError.

property num_zvars

Number of complex-valued z-variables the operation acts on.

Read-only: assigning to it raises ValueError.

property num_cregs

Number of classical registers (groups of bit targets) the operation takes.

Read-only: assigning to it raises ValueError.

property qregsizes

Size of each quantum register, as a list with one entry per register.

Read-only: assigning to it raises ValueError.

property cregsizes

Size of each classical register, with one entry per register.

Empty or None when the operation has no classical register. Read-only: assigning to it raises ValueError.

property zregsizes

Size of each z-variable register, with one entry per register.

Empty or None when the operation has no z-variable register. Read-only: assigning to it raises ValueError.

property name

Short name of the operation type.

For a wrapper, and for a named gate defined through one, this is the name of the wrapper: GateCX().name is "Control". str(op) gives the name shown when printing a circuit. Read-only: assigning to it raises ValueError.

property parnames

Names of the operation’s parameters, as a tuple of strings.

Read-only: assigning to it raises ValueError.

getparams()[source]

Return the values of the operation’s parameters.

Returns:

One value per name in parnames, in the same order. Values may be numbers or symbolic expressions.

Return type:

list

Examples

>>> from mimiqcircuits import *
>>> # GateU has a fourth parameter, the global phase, defaulting to 0
>>> g = GateU(0.1, 0.2, 0.3)
>>> g.parnames
('theta', 'phi', 'lmbda', 'gamma')
>>> g.getparams()
[0.1, 0.2, 0.3, 0.0]
is_symbolic()[source]

Whether any parameter contains a free symbol.

For wrappers such as Control or Power, the wrapped operation is checked too.

Returns:

True if at least one parameter is symbolic.

Return type:

bool

Examples

>>> from mimiqcircuits import *
>>> from symengine import Symbol
>>> GateRX(Symbol("theta")).is_symbolic()
True
>>> GateRX(0.5).is_symbolic()
False
getparam(pn)[source]

Return the value of the parameter named pn.

Parameters:

pn (str) – Parameter name, one of parnames.

Returns:

The parameter value, a number or a symbolic expression.

Raises:

ValueError – If the operation has no parameter named pn.

Examples

>>> from mimiqcircuits import *
>>> GateRX(0.5).getparam("theta")
0.5
copy()[source]

Return a shallow copy of the operation.

The copy shares its attributes (for example a wrapped operation) with the original. Use deepcopy() for an independent copy.

Returns:

A new operation of the same type.

Return type:

Operation

deepcopy()[source]

Return a deep copy of the operation and all its attributes.

Returns:

A new operation equal to the original that shares no mutable state with it.

Return type:

Operation

abstract iswrapper()[source]

Whether the operation wraps another operation.

True for composite operations such as Control, Power, Inverse, Parallel or IfStatement, False otherwise.

Returns:

bool

isopalias()[source]

Whether the operation is a named alias of a wrapper.

Some named gates are defined as a wrapper around another operation, for example GateS as GateZ to the power 1/2. Such gates return True. Not every such gate reports it: GateSDG and GateCX return False.

Returns:

True for a named alias of a wrapper that reports it, False otherwise.

Return type:

bool

supports_canonical_rewrite()[source]

Whether CanonicalRewrite may decompose this operation.

Operations opt in: only those the canonical rewrite knows how to handle return True.

Returns:

bool

numparams()[source]

Return the number of parameters of the operation.

Returns:

The length of getparams().

Return type:

int

decompose()[source]

Decompose the operation one level into simpler operations.

The result is a new circuit acting on qubits 0 to num_qubits - 1 (and likewise for bits and z-variables). Operations without a decomposition return a circuit holding only themselves.

Returns:

The decomposed operation.

Return type:

Circuit

Examples

>>> from mimiqcircuits import *
>>> # GateSWAP decomposes into three CX gates
>>> GateSWAP().decompose()
2-qubit circuit with 3 instructions:
├── CX @ q[0], q[1]
├── CX @ q[1], q[0]
└── CX @ q[0], q[1]
evaluate(d)[source]

Substitute values for the symbols in the operation’s parameters.

Operations without symbolic parameters may return themselves.

Parameters:

d (dict) – Map from symbols (or their names) to values.

Returns:

The operation with the symbols substituted.

Return type:

Operation

isidentity()[source]

Whether the operation is known to act as the identity.

Returns:

True only when the operation is known to be the identity. False does not prove the opposite.

Return type:

bool

classmethod isunitary()[source]

Whether the operation acts unitarily on the quantum state.

Returns:

True for gates, annotations, Barrier, and the operations that only read the state into a z-variable, such as ExpectationValue or Amplitude. False for everything else: measurements, resets, noise channels, classical operations, and containers such as Block or IfStatement.

Return type:

bool

get_operation()[source]

Return the operation that this one wraps, or the operation itself.

Wrappers such as Control or Power return the operation they wrap; other operations return themselves.

Returns:

Operation

listvars()[source]

List the symbolic variables in the parameters of the operation.

Wrappers such as Control, Inverse, Power, Parallel, Repeat, IfStatement and WhileStatement give the variables of the operation they wrap, and a Block those of all its instructions.

Returns:

Each symengine.Symbol once, in no particular order. The list is empty when the operation has no symbolic parameters.

Return type:

list

Examples

>>> from mimiqcircuits import *
>>> from symengine import Symbol
>>> theta = Symbol("theta")
>>> GateRX(theta).listvars()
[theta]
>>> Control(2, GateRY(2 * theta)).listvars()
[theta]
>>> GateH().listvars()
[]