mimiqcircuits.operations.operator

AbstractOperator base class.

Classes

AbstractOperator()

Base class for all N-qubit operators.

Exceptions

UnexpectedSymbolics(op)

Raised when a numeric matrix is requested for an operator whose parameters are still symbolic.

class AbstractOperator[source]

Bases: Operation

Base class for all N-qubit operators.

An operator is anything with a matrix representation, see matrix(). It does not need to be unitary: Gate is the unitary subclass, while operators such as SigmaMinus, Projector0 or DiagonalOp are not unitary.

Non-unitary operators cannot be pushed to a circuit on their own. They are used to define Kraus channels (see Kraus) and to compute expectation values (see ExpectationValue).

Examples

>>> from mimiqcircuits import *
>>> # a non-unitary operator, used here to measure an expectation value
>>> op = SigmaMinus()
>>> op.isunitary()
False
>>> c = Circuit()
>>> c.push(ExpectationValue(op), 0, 0)
1-qubit, 1-zvar circuit with 1 instruction:
└── ⟨SigmaMinus(1)⟩ @ q[0], z[0]

See also

matrix(), isunitary()

opname()[source]

Return the name of the operator’s class, for example "SigmaMinus".

iswrapper()[source]

Whether the operator wraps another operation.

True for composite operators such as Control, Power, Inverse or Parallel, False for plain operators and gates.

Returns:

bool

static isunitary()[source]

Whether the operator is unitary.

Returns:

True for gates, False for general operators.

Return type:

bool

supports_canonical_rewrite()[source]

Whether CanonicalRewrite may decompose this operator.

Returns:

True for gates, False for other operators.

Return type:

bool

matrix()[source]

Return the SymEngine matrix representation of the operator.

Operators fully determined by their type (parameter-free gates and named wrappers such as GateT or GateCX) build their matrix once per class and return a fresh copy of it on each call, so mutating the result never affects later calls. Numeric parametric operators build a SymEngine matrix through _matrix() and then evaluate each non-zero entry to a floating-point scalar. Symbolic operators return the SymEngine expressions without simplifying them; callers that want a simplified form apply their own simplification.

Returns:

The matrix representation.

Return type:

symengine.Matrix

Examples

>>> from mimiqcircuits import *
>>> GateX().matrix()
[0, 1.0]
[1.0, 0]
inverse()[source]

Return the inverse of the operator.

General operators are not unitary and have no inverse here, so this always raises. Gates override it.

Raises:

NotImplementedError – Always.

rescale(scale)[source]

Return a copy of the operator multiplied by scale.

Every parameter in parnames is multiplied by scale, which is the right thing for operators linear in their parameters (projectors, sigmas, DiagonalOp, Operator). Operators that are not linear in their parameters override this.

Raises:

NotImplementedError – If the operator has no parameters to scale.

rescale_inplace(scale)[source]

Multiply the operator by scale in place and return it.

In-place counterpart of rescale().

Raises:

NotImplementedError – If the operator has no parameters to scale.

power(n)[source]

Return the operator raised to the power n.

Powers of general operators are not supported, so this always raises. Gates override it.

Parameters:

n – The exponent.

Raises:

NotImplementedError – Always.

unwrappedmatrix()[source]

Return the numeric matrix of the operator as a NumPy array.

Use this instead of matrix() when plain numbers are needed, for example to feed a simulator. Gates with a fast numeric path (see mimiqcircuits.numerics) skip SymEngine entirely; other operators build the SymEngine matrix and convert it.

Operators fully determined by their type return a cached, read-only array. Copy it before modifying it.

Returns:

The matrix, with dtype complex128.

Return type:

numpy.ndarray

Raises:

UnexpectedSymbolics – If any parameter is still symbolic.

Examples

>>> from mimiqcircuits import *
>>> GateX().unwrappedmatrix()
array([[0.+0.j, 1.+0.j],
       [1.+0.j, 0.+0.j]])
evaluate(d)[source]

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

Operators without parameters return themselves. Otherwise a new operator of the same type is built from the substituted parameters. Symbols missing from d stay symbolic.

Parameters:

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

Returns:

The operator with the symbols substituted.

Return type:

AbstractOperator

Examples

>>> from symengine import symbols
>>> from mimiqcircuits import *
>>> theta = symbols('theta')
>>> # bind theta to a number
>>> GateRX(theta).evaluate({theta: 0.5})
RX(0.5)