Operations

MimiqCircuits provides a rich set of operations to build your quantum circuits. From standard unitary gates to advanced noise channels and classical control flow, explore the available tools below.

Unitary Gates

Standard single and multi-qubit gates (X, H, CX, etc.).

Unitary Gates
Classical Operations

Manipulate classical bits logic (AND, XOR) and registers.

Classical Operations
Non-Unitary Ops

Measurements, resets, and conditional logic.

Non-unitary Operations
Noise Models

Define custom noise models and apply them to your circuits.

Noisy simulations on MIMIQ
Qubit Loss

Model qubit loss, reload lost qubits, and process loss-aware circuits.

Qubit Loss on MIMIQ
Symbolic Ops

Work with parameterized circuits using symbolic variables.

Symbolic Operations in MIMIQ
Statistical Ops

Operations for extracting statistical properties.

Statistical Operations
Special Ops

Specialized operations and structures.

Special Operations
Z-Operations

Operations on the Z-register.

Z-register Operations

Reference

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()
[]
class Gate[source]

Bases: AbstractOperator

Base class for unitary gates.

A gate acts on qubits only, has a unitary matrix, and can be inverted, raised to a power, controlled, repeated and run in parallel. Subclasses provide the matrix and the number of qubits.

power(), control(), parallel() and repeat() called without their count return a LazyExpr that takes it later.

Examples

>>> from mimiqcircuits import *
>>> GateX().control(2)
C₂X
>>> GateX().power(0.5)
SX
>>> GateH().parallel(3)
⨷ ³ H
>>> # without the count, a lazy expression is returned
>>> GateX().control()
lazy control(?, X)
iswrapper()[source]

Return False: a plain gate does not wrap another operation.

rescale(scale)[source]

Return a RescaledGate wrapping this gate with factor scale.

rescale_inplace(scale)[source]

Raise, as a gate cannot be rescaled in place. Use rescale().

inverse()[source]

Return the inverse of the gate.

Gates with a known inverse return it directly (for example GateS().inverse() is GateSDG()); otherwise the gate is wrapped in Inverse.

power(*args)[source]

Return the gate raised to a power.

Gates with a known power return it directly (for example GateZ().power(0.5) is GateS(), and GateRX(a).power(p) is GateRX(p * a)); otherwise the gate is wrapped in Power.

Parameters:

*args – The exponent. Without it, return a LazyExpr that takes the exponent later.

Raises:
  • ValueError – If more than one argument is given.

  • TypeError – If the exponent is not a real number.

control(*args)[source]

Return the gate controlled by num_controls qubits.

Gates with a named controlled form return it directly (for example GateX().control(1) is GateCX()); otherwise the gate is wrapped in Control. The controls are the first qubits.

Parameters:

*args – The number of controls. Without it, return a LazyExpr that takes the number later.

Raises:

ValueError – If more than one argument is given.

parallel(*args)[source]

Return a Parallel applying the gate to separate qubit groups.

Parameters:

*args – The number of repetitions. Without it, return a LazyExpr that takes the number later.

Raises:

ValueError – If more than one argument is given.

repeat(*args)[source]

Return a Repeat applying the gate several times in sequence on the same qubits.

Parameters:

*args – The number of repetitions. Without it, return a LazyExpr that takes the number later.

Raises:

ValueError – If more than one argument is given.

evaluate(d)[source]

Substitute symbols in the gate parameters and return the result.

Parameters:

d (dict) – Map from symbols to the values to substitute.

Returns:

self if nothing changed, otherwise a new gate of the same type with the substituted parameters.

Return type:

Gate

gettypekey()[source]

Return the key used to look up registered decompositions: the gate class.

static isunitary()[source]

Return True: gates are unitary.