mimiqcircuits.operations.gates.custom¶
Custom gate definition.
Classes
|
Custom gate defined by a unitary matrix. |
- class GateCustom(matrix)[source]¶
Bases:
GateCustom gate defined by a unitary matrix.
The matrix must be square with \(2^n\) rows, \(n \geq 1\); the gate then acts on \(n\) qubits. Qubits are ordered big-endian: the first target qubit is the most significant bit of the row index. Numeric matrices are checked for unitarity with an absolute tolerance of
1e-8. The check is skipped when an entry is a plain symbol, so a symbolic matrix can be filled in later withevaluate().- Parameters:
matrix (numpy.ndarray or symengine.Matrix) – The unitary matrix.
- Raises:
TypeError – If
matrixis neither a NumPy array nor a SymEngine matrix.ValueError – If the matrix is not square, not unitary, or its size is not a power of two greater than one.
Examples
>>> from mimiqcircuits import * >>> import numpy as np >>> # a controlled-Y written out as a 4x4 matrix >>> matrix = np.array([[1, 0, 0, 0],[0, 1, 0, 0],[0, 0, 0, -1j],[0, 0, 1j, 0]]) >>> g = GateCustom(matrix) >>> g.num_qubits 2 >>> c = Circuit() >>> c.push(g, 0, 1) 2-qubit circuit with 1 instruction: └── Custom(...) @ q[0:1] >>> # a single qubit custom gate decomposes into one GateU >>> GateCustom(np.array([[0, 1], [1, 0]])).decompose() 1-qubit circuit with 1 instruction: └── U(3.141592653589793, 0.0, -3.141592653589793, 0.0) @ q[0]
- matrix()[source]¶
Return the gate matrix as a SymEngine matrix.
Entries that can be converted to a complex number are returned as complex numbers; symbolic entries are kept as they are.
- Returns:
A new matrix on each call.
- Return type:
symengine.Matrix
- property num_qubits¶
Number of qubits the gate acts on, \(\log_2\) of the matrix size.
- property parnames¶
the 1-based linear indices of the matrix entries.
- Type:
Parameter names
- getparam(pn)[source]¶
Return the matrix entry with 1-based column-major index
pn.- Raises:
IndexError – If
pnis outside1..rows*cols.
- inverse()[source]¶
Return the inverse of the gate.
Gates with a known inverse return it directly (for example
GateS().inverse()isGateSDG()); otherwise the gate is wrapped inInverse.
- static is_unitary(matrix, tol=1e-08)[source]¶
Return whether \(U U^\dagger\) equals the identity within
tol.- Parameters:
matrix (numpy.ndarray or symengine.Matrix) – The matrix to check.
tol (float) – Absolute tolerance on each entry. Defaults to
1e-8.
- Returns:
Trueif the matrix is unitary.- Return type: