mimiqcircuits.operations.gates.custom

Custom gate definition.

Classes

GateCustom(matrix)

Custom gate defined by a unitary matrix.

class mimiqcircuits.operations.gates.custom.GateCustom(matrix)[source]

Bases: Gate

Custom gate defined by a unitary matrix.

The matrix must be square with a power of two number of rows, and it acts on log2(rows) qubits.

Examples

>>> from mimiqcircuits import Circuit, GateCustom
>>> import numpy as np
>>> matrix = np.array([[1, 0, 0, 0],[0, 1, 0, 0],[0, 0, 0, -1j],[0, 0, 1j, 0]])
>>> c = Circuit()
>>> c.push(GateCustom(matrix), 0, 1)
2-qubit circuit with 1 instruction:
└── Custom(...) @ q[0:1]
__init__(matrix)[source]
matrix()[source]

Numerically evaluate entries where possible, otherwise keep symbolic.

Deliberately not inherited from AbstractOperator: that version memoises on the class for parameter-free operators, which is right for a named gate but would make every GateCustom share whichever matrix was built first. unwrappedmatrix below is overridden for the same reason.

property num_qubits
property parnames
getparams()[source]
getparam(pn)[source]
inverse()[source]

Raise an error, as non-unitary operators cannot be inverted.

This method is not implemented for non-unitary operators and will raise a NotImplementedError if called.

Raises:

NotImplementedError – If the method is called.

static is_unitary(matrix, tol=1e-08)[source]
pretty_print()[source]
evaluate(d)[source]

Substitute the symbolic parameters of the operator with numerical values.

This method evaluates the operator’s symbolic parameters using the values provided in the dictionary d. If the operator has no parameters, it returns the same instance. Otherwise, it creates a new instance of the operator with updated numerical parameters.

Parameters:

d (dict) – A dictionary where keys are symbolic parameter names and values are values for substitution.

Example

>>> from symengine import *
>>> from mimiqcircuits import *
>>> theta = symbols('theta')
>>> op = GateRX(theta)
>>> evaluated_op = op.evaluate({'theta': 0.5})
>>> print(evaluated_op)
RX(0.5)
unwrappedmatrix()[source]

Numeric matrix representation as a numpy.ndarray[complex128].

Simulators and anything else that wants raw numbers (e.g. TensorWeaver) should call this instead of matrix(). It bypasses SymEngine entirely for gates that override _matrix_numeric() — see mimiqcircuits.numerics — and otherwise falls back to building the SymEngine matrix and casting it.

Operators fully determined by their type cache the result at class level as a read-only array.

Raises:

UnexpectedSymbolics – If any parameter is still symbolic.

Returns:

complex128 matrix.

Return type:

numpy.ndarray