mimiqcircuits.operations.operators.custom

Classes

Operator(mat)

N-qubit operator given by a \(2^N \times 2^N\) matrix.

class Operator(mat)[source]

Bases: AbstractOperator

N-qubit operator given by a \(2^N \times 2^N\) matrix.

The matrix does not have to be unitary. Only one- and two-qubit operators are supported. The matrix is stored as a SymEngine matrix in the mat attribute.

Parameters:

mat (symengine.Matrix | sympy.Matrix | numpy.ndarray) – The square matrix of the operator, of size 2 or 4.

Raises:
  • TypeError – If mat is not a SymEngine, SymPy or NumPy matrix.

  • ValueError – If mat is not square, its size is not a power of 2, or it acts on more than two qubits.

See also

AbstractOperator, ExpectationValue, Kraus

Examples

>>> from mimiqcircuits import *
>>> from symengine import Matrix
>>> op = Operator(Matrix([[1, 2], [3, 4]]))
>>> op
1-qubit Operator:
├── 1 2
└── 3 4
>>> op = Operator(Matrix([[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 1]]))
>>> op
2-qubit Operator:
├── 1 0 0 1
├── 0 0 0 0
├── 0 0 0 0
└── 1 0 0 1

Operators can be used for expectation values:

>>> c = Circuit()
>>> c.push(ExpectationValue(Operator(Matrix([[0, 1], [0, 0]]))), 1, 1)
2-qubit, 2-zvar circuit with 1 instruction:
└── ⟨Operator([[0, 1], [0, 0]])⟩ @ q[1], z[1]
static is_valid_power_of_2(n)[source]

Return whether n is a positive power of 2.

opname()[source]

Return "Operator".

property parnames

("mat",).

Type:

Names of the parameters

iswrapper()[source]

Operators do not wrap another operation. Returns None.

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

Return a pretty-printed representation.

__str__()[source]

Return Operator(<matrix>) for one qubit and Operator(...) for two.

pretty_print()[source]

Return the matrix as a multi-line string, one row per line.

opsquared()[source]

Return \(O^\dagger O\) as a new Operator.