Unitary Gates¶
Unitary gates are fundamental components of quantum circuits. Here we explain how to work with unitary gates in MIMIQ.
Mathematical background¶
State vector and probability¶
In quantum mechanics, every transformation applied to a quantum state must be unitary (in a closed system). To understand why, we can expand the quantum state as
where \(\ket{\psi_i}\) are orthonormal basis states. For this state, the following condition must hold true:
Since \(|c_i|^2\) corresponds to the probability of measuring state \(\ket{\psi_i}\), this condition simply says that the probabilities must add up to one. Unitary gates preserve this normalization condition, see below.
Unitary transformation¶
An alternative way to compute the probability is through the inner product. Given two states in Hilbert space, \(\ket{\alpha}\) and \(\ket{\psi}\), the squared inner product \(|\braket{\alpha|\psi}|^2\) reflects the probability of measuring the system in state \(\ket{\alpha}\). Thus, the normalization condition can be written as \(|\braket{\psi|\psi}|^2 = 1\). In other words, the length of the state vector in complex space must be one.
When evolving the state \(\ket{\psi}\) using an operator U, the normalization condition becomes (omitting the square):
To fulfill this, the operator U must satisfy the condition:
An operator that fulfills this requirement is called a unitary operator and its matrix representation is unitary too.
Unitary gates in MIMIQ.¶
MIMIQ offers a large number of gates to build quantum circuits. For an overview, type the following line in your Python session:
help(GATES)
Similarly, to get more information about a specific gate, you can type the following command in your Python session using the gate of your choice:
help(GateID)
There are different categories of gates depending on the number of targets, parameters etc. We discuss how to implement them in the following.
Single-qubit gates¶
List of single-qubit gates: GateID, GateX, GateY, GateZ, GateH, GateS, GateSDG, GateT, GateTDG, GateSX, GateSXDG. GateSY, GateSYDG.
GateX()).push() function.>>> circuit = Circuit()
>>> circuit.push(GateX(), 0)
1-qubit circuit with 1 instruction:
└── X @ q[0]
Single-qubit parametric gates¶
List of single-qubit parametric gates: GateU, GateP, GateRX, GateRY, GateRZ, GateR, GateU1, GateU2, GateU3, Delay().
`GateU(0.5, 0.5, 0.5)` or `GateU1(0.5)`), if you are unsure of the expected number of parameters use the help() function in your Python interactive session and give it the oject you are interested in (ex: help(GateU)).push() function and give the index of the target qubit.>>> circuit = Circuit()
>>> circuit.push(GateRX(math.pi/2), 0)
1-qubit circuit with 1 instruction:
└── RX(1.5707963267948966) @ q[0]
Two qubit gates¶
List of two qubits gates: GateCX, GateCY, GateCZ, GateCH, GateSWAP, GateISWAP, GateCS, GateCSDG, GateCSX, GateCSXDG, GateECR, GateDCX.
GateCX()).push() function to add it to the circuit.>>> circuit = Circuit()
>>> circuit.push(GateCH(), 0, 1)
2-qubit circuit with 1 instruction:
└── CH @ q[0], q[1]
Two-qubit parametric gates¶
List of two qubits parametric gates : GateCP, GateCU, GateCRX, GateCRY, GateCRZ, GateRXX, GateRYY, GateRZZ,
GateRZX, GateXXplusYY, GateXXminusYY.
GateCU(math.pi, math.pi, math.pi)).push() function. Again, check each gate’s documentation to understand the qubit ordering; for controlled gates the first qubit corresponds to the control qubit, the second to the target.>>> circuit = Circuit()
>>> circuit.push(GateRXX(math.pi/2), 0, 1)
2-qubit circuit with 1 instruction:
└── RXX(1.5707963267948966) @ q[0:1]
Multi-qubit gates¶
List of multi-qubit gates: GateCCX, GateC3X, GateCCP, GateCSWAP.
For the multi-qubit controlled gates you will need to give the index of each qubit to the push() function. As usual, first the control qubits, then the targets; check the specific documentation of each gate.
>>> circuit = Circuit()
>>> circuit.push(GateC3X(), 0, 1, 2, 3)
4-qubit circuit with 1 instruction:
└── C₃X @ q[0:2], q[3]
Generalized gates¶
Some common gate combinations are available as generalized gates: PauliString, QFT, PhaseGradient, Diffusion, PolynomialOracle.
Generalized gates can be applied to a variable number of qubits.
It is highly recommended to check their docstrings to understand their usage help(QFT).
Here is an example of use:
>>> circuit = Circuit()
>>> circuit.push(PhaseGradient(10), *range(0, 10))
10-qubit circuit with 1 instruction:
└── PhaseGradient @ q[0:9]
These gates target a variable number of gates, so you have to specify in the constructor how many target qubits will be used, and give to the push() function one index per target qubit.
More about generalized gates on special operations.
Custom Gates¶
If you need to use a specific unitary gate that is not provided by MIMIQ, you can use GateCustom` to create your own unitary gate.
Note
Only one qubit or two qubits gates can be created using MIMIQ’s GateCustom.
Note
Avoid using GateCustom if you can define the same gate using a pre-defined gate from MIMIQ’s library, as it could impact negatively peformance.
To create a custom unitary gate you first have to define the matrix of your gate in Python:
# define the matrix for a 2 qubits gate
>>> custom_matrix = np.array([[np.exp(1j*math.pi/3), 0, 0, 0], [0, np.exp(1j*math.pi/5), 0, 0 ], [0, 0, np.exp(1j*math.pi/7), 0], [0, 0, 0, np.exp(1j*math.pi/11)]])
Then you can create your unitary gate and use it like any other gate using push()
>>> circuit = Circuit()
>>> custom_gate = GateCustom(custom_matrix)
>>> circuit.push(custom_gate, 0, 1)
2-qubit circuit with 1 instruction:
└── Custom(...) @ q[0:1]
Composition: Control, Power, Inverse, Parallel¶
Gates in MIMIQ can be combined to create more complex gates using Control, Power, Inverse, Parallel.
Control¶
A controlled version of every gate can be built using the control() function.
For example, CX can be built with the following instruction:
>>> CX = control(1, GateX())
The first argument indicates the number of control qubits and is completely up to the user. For example a CCCCCX can be built with the following instruction:
>>> CCCCCX = control(5, GateX())
Details
A wrapper for GateCX is already provided by MIMIQ. Whenever possible, it is recommended to use the gates already provided by the framework instead of creating your own composite gate to prevent performances loss.
Be careful when adding the new control gate to your circuit. When using the push() function, the first expected indices should be the control qubits specified in Control and the last indices the target qubits of the gate, for instance:
>>> circuit = Circuit()
>>> circuit.push(CCCCCX, 0, 1, 2, 3, 4, 5)
6-qubit circuit with 1 instruction:
└── C₅X @ q[0:4], q[5]
Power¶
To raise the power of a gate you can use the power() function.
For example, \(\sqrt{\mathrm{GateS}} = \mathrm{GateT}\), therefore, the following instruction can be used to generate the GateS:
>>> power(GateS(), 1/2)
T
The power method will attempt to realize simplifications whenever it can, for example asking for the square of GateX will directly give you GateID.
Inverse¶
To get the inverse of an operator you can use the inverse() method.
Remember that the inverse of a unitary matrix is the same as the adjoint (conjugate transpose), so this is a simple way to get the adjoint of a gate.
For example here is how to get the inverse of a GateH
>>> inv_H = inverse(GateH())
Parallel¶
To create a composite gate applying a specific gate to multiple qubits at once you can use the parallel() method.
>>> circuit = Circuit()
>>> X_gate_4 = parallel(4, GateX())
>>> circuit.push(X_gate_4, 0, 1, 2, 3)
4-qubit circuit with 1 instruction:
└── ⨷ ⁴ X @ q[0], q[1], q[2], q[3]
>>> circuit.draw()
┌─┐
q[0]: ╶┤X├────────────────────────────────────────────────────────────────────╴
├─┤
q[1]: ╶┤X├────────────────────────────────────────────────────────────────────╴
├─┤
q[2]: ╶┤X├────────────────────────────────────────────────────────────────────╴
├─┤
q[3]: ╶┤X├────────────────────────────────────────────────────────────────────╴
└─┘
To check the number of repetition of your custom parallel gate you can use the num_repeats() method:
>>> X_gate_4.num_repeats
4
Be careful when using a multi-qubit gate with parallel() as the index of the targeted qubits in push() can become confusing.
for example see below the parallel applicatoin of a CX gate:
>>> circuit = Circuit()
>>> double_CX = Parallel(2, GateCX())
>>> circuit.push(double_CX, 0, 1, 2, 3)
4-qubit circuit with 1 instruction:
└── ⨷ ² CX @ q[0], q[1], q[2], q[3]
>>> circuit.draw()
┌────┐
q[0]: ╶┤0 ├─────────────────────────────────────────────────────────────────╴
│ CX│
q[1]: ╶┤1 ├─────────────────────────────────────────────────────────────────╴
├────┤
q[2]: ╶┤0 ├─────────────────────────────────────────────────────────────────╴
│ CX│
q[3]: ╶┤1 ├─────────────────────────────────────────────────────────────────╴
└────┘
Here the index 0 and 1 correspond to the control and target of the first CX gate and 2 and 3 correspond to the second CX gate.
Extract information of unitary gates¶
MIMIQ priovides a few methods to extract information about the unitary gates.
Matrix¶
To get the matrix of a unitary gate you can use the matrix():
>>> GateCX().matrix()
[1.0, 0, 0, 0]
[0, 1.0, 0, 0]
[0, 0, 0, 1.0]
[0, 0, 1.0, 0]
Number of targets¶
Another way to know how many qubits, bits or z-variables are targeted by one unitary gate you can use num_qubits(), num_bits() and num_zvars(), respectively.
>>> GateCX().num_qubits, GateCX().num_bits, GateCX().num_zvars
(2, 0, 0)
>>> Measure().num_qubits, Measure().num_bits, Measure().num_zvars
(1, 1, 0)
>>> Amplitude("01").num_qubits,Amplitude("01").num_bits, Amplitude("01").num_zvars
(0, 0, 1)
See non-unitary operations and statistical operations pages for more information on Measure and Amplitude.
Reference¶
- class GateID[source]
Bases:
GateSingle qubit Identity gate.
Matrix Representation
\[\begin{split}\operatorname{ID} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}\end{split}\]Examples
>>> from mimiqcircuits import * >>> GateID() ID >>> GateID().matrix() [1.0, 0] [0, 1.0] >>> c = Circuit().push(GateID(), 0) >>> c 1-qubit circuit with 1 instruction: └── ID @ q[0] >>> GateID().power(2), GateID().inverse() (ID, ID) >>> GateID().decompose() 1-qubit circuit with 1 instruction: └── U(0, 0, 0, 0.0) @ q[0]
- 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.
- class GateX[source]
Bases:
GateSingle qubit Pauli-X gate.
Matrix Representation
\[\begin{split}\operatorname{X} = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\end{split}\]Examples
>>> from mimiqcircuits import * >>> GateX() X >>> GateX().matrix() [0, 1.0] [1.0, 0] >>> c = Circuit().push(GateX(), 0) >>> c 1-qubit circuit with 1 instruction: └── X @ q[0] >>> GateX().power(2), GateX().inverse() (ID, X) >>> GateX().decompose() 1-qubit circuit with 1 instruction: └── U(pi, 0, pi, 0.0) @ q[0]
- 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.
- class GateY[source]
Bases:
GateSingle qubit Pauli-Y gate.
Matrix Representation
\[\begin{split}\operatorname{Y} = \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}\end{split}\]Examples
>>> from mimiqcircuits import * >>> GateY() Y >>> GateY().matrix() [0, 0.0 - 1.0*I] [0.0 + 1.0*I, 0] >>> c = Circuit().push(GateY(), 0) >>> c 1-qubit circuit with 1 instruction: └── Y @ q[0] >>> GateY().power(2), GateY().inverse() (ID, Y) >>> GateY().decompose() 1-qubit circuit with 1 instruction: └── U(pi, (1/2)*pi, (1/2)*pi, 0.0) @ q[0]
- 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.
- class GateZ[source]
Bases:
GateSingle qubit Pauli-Z gate.
Matrix Representation
\[\begin{split}\operatorname{Z} = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}\end{split}\]Examples
>>> from mimiqcircuits import * >>> GateZ() Z >>> GateZ().matrix() [1.0, 0] [0, -1.0] >>> c = Circuit().push(GateZ(), 0) >>> c 1-qubit circuit with 1 instruction: └── Z @ q[0] >>> GateZ().power(2), GateZ().inverse() (ID, Z) >>> GateZ().decompose() 1-qubit circuit with 1 instruction: └── P(pi) @ q[0]
- 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.
- class GateH[source]
Bases:
GateSingle qubit Hadamard gate, \((X + Z)/\sqrt{2}\).
Matrix Representation
\[\begin{split}\operatorname{H} = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}\end{split}\]Examples
>>> from mimiqcircuits import * >>> GateH() H >>> GateH().matrix() [0.707106781186548, 0.707106781186548] [0.707106781186548, -0.707106781186548] >>> c = Circuit().push(GateH(), 0) >>> c 1-qubit circuit with 1 instruction: └── H @ q[0] >>> GateH().power(2), GateH().inverse() (ID, H) >>> GateH().decompose() 1-qubit circuit with 1 instruction: └── U((1/2)*pi, 0, pi, 0.0) @ q[0]
- 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.
- class GateCX(num_controls=None, operation=None, *args, **kwargs)[source]
Bases:
ControlTwo qubit Controlled-X gate (or CNOT).
By convention, the first qubit is the control and the second is the target.
Matrix Representation
\[\begin{split}\operatorname{CX} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix}\end{split}\]Examples
>>> from mimiqcircuits import * >>> GateCX(), GateCX().num_controls, GateCX().num_targets (CX, 1, 1) >>> GateCX().matrix() [1.0, 0, 0, 0] [0, 1.0, 0, 0] [0, 0, 0, 1.0] [0, 0, 1.0, 0] >>> c = Circuit().push(GateCX(), 0, 1) >>> c 2-qubit circuit with 1 instruction: └── CX @ q[0], q[1] >>> GateCX().power(2), GateCX().inverse() (CID, CX) >>> GateCX().decompose() 2-qubit circuit with 1 instruction: └── CX @ q[0], q[1]
- __init__(num_controls=1, operation=None)[source]
Initialize a CX gate.
- Parameters:
num_controls – Ignored, always 1 for CX.
operation – Ignored, always GateX() for CX.
- supports_canonical_rewrite()[source]
Return
False: CX is a primitive of the canonical gate set.
- class GateRX(theta)[source]
Bases:
GateSingle qubit rotation around the \(\hat{x}\) axis.
Matrix Representation
\[\begin{split}\operatorname{RX}(\theta) = \begin{pmatrix} \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{pmatrix}\end{split}\]- Parameters:
theta – Rotation angle in radians, numeric or symbolic.
Examples
>>> from mimiqcircuits import * >>> from symengine import * >>> theta = Symbol('theta') >>> GateRX(theta) RX(theta) >>> GateRX(theta).matrix() [cos((1/2)*theta), -I*sin((1/2)*theta)] [-I*sin((1/2)*theta), cos((1/2)*theta)] >>> c = Circuit().push(GateRX(theta), 0) >>> c 1-qubit circuit with 1 instruction: └── RX(theta) @ q[0] >>> GateRX(theta).power(2), GateRX(theta).inverse() (RX(2*theta), RX(-theta)) >>> GateRX(theta).decompose() 1-qubit circuit with 1 instruction: └── U(theta, (-1/2)*pi, (1/2)*pi, 0.0) @ q[0]
- 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.
- class GateRY(theta)[source]
Bases:
GateSingle qubit rotation around the \(\hat{y}\) axis.
Matrix Representation
\[\begin{split}\operatorname{RY}(\theta) = \begin{pmatrix} \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{pmatrix}\end{split}\]- Parameters:
theta – Rotation angle in radians, numeric or symbolic.
Examples
>>> from mimiqcircuits import * >>> from symengine import * >>> theta = Symbol('theta') >>> GateRY(theta) RY(theta) >>> GateRY(theta).matrix() [cos((1/2)*theta), -sin((1/2)*theta)] [sin((1/2)*theta), cos((1/2)*theta)] >>> c = Circuit().push(GateRY(theta), 0) >>> c 1-qubit circuit with 1 instruction: └── RY(theta) @ q[0] >>> GateRY(theta).power(2), GateRY(theta).inverse() (RY(2*theta), RY(-theta)) >>> GateRY(theta).decompose() 1-qubit circuit with 1 instruction: └── U(theta, 0, 0, 0.0) @ q[0]
- 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.
- class GateRZ(lmbda)[source]
Bases:
GateSingle qubit rotation around the \(\hat{z}\) axis.
Matrix Representation
\[\begin{split}\operatorname{RZ}(\lambda) = \begin{pmatrix} e^{-i\frac{\lambda}{2}} & 0 \\ 0 & e^{i\frac{\lambda}{2}} \end{pmatrix}\end{split}\]- Parameters:
lmbda – Rotation angle \(\lambda\) in radians, numeric or symbolic.
Examples
>>> from mimiqcircuits import * >>> from symengine import * >>> lmbda = Symbol('lambda') >>> GateRZ(lmbda) RZ(lambda) >>> GateRZ(lmbda).matrix() [exp(-1/2*I*lambda), 0] [0, exp(1/2*I*lambda)] >>> c = Circuit().push(GateRZ(lmbda), 0) >>> c 1-qubit circuit with 1 instruction: └── RZ(lambda) @ q[0] >>> GateRZ(lmbda).power(2), GateRZ(lmbda).inverse() (RZ(2*lambda), RZ(-lambda)) >>> GateRZ(lmbda).decompose() 1-qubit circuit with 1 instruction: └── U(0, 0, lambda, (-1/2)*lambda) @ q[0]
- 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.
- class GateU(theta, phi, lmbda, gamma=0.0)[source]
Bases:
GateSingle qubit generic unitary gate with a global phase.
Any single qubit unitary can be written as a
GateU. The fourth anglegammasets a global phase.Matrix Representation
\[\begin{split}\operatorname{U}(\theta, \phi, \lambda, \gamma) = \mathrm{e}^{i\gamma} \begin{pmatrix} \cos\left(\frac{\theta}{2}\right) & -\mathrm{e}^{i\lambda}\sin\left(\frac{\theta}{2}\right)\\ \mathrm{e}^{i\phi}\sin\left(\frac{\theta}{2}\right) & \mathrm{e}^{i(\phi+\lambda)}\cos\left (\frac{\theta}{2}\right) \end{pmatrix}\end{split}\]- Parameters:
theta – Euler angle \(\theta\) in radians.
phi – Euler angle \(\phi\) in radians.
lmbda – Euler angle \(\lambda\) in radians.
gamma – Global phase \(\gamma\) in radians. Defaults to
0.0.
Examples
>>> from mimiqcircuits import * >>> from symengine import * >>> theta, phi, lmbda, gamma = symbols('theta phi lambda gamma') >>> GateU(theta, phi, lmbda, gamma) U(theta, phi, lambda, gamma) >>> GateU(theta, phi, lmbda, gamma).matrix() [exp(I*gamma)*cos((1/2)*theta), -exp(I*(gamma + lambda))*sin((1/2)*theta)] [exp(I*(gamma + phi))*sin((1/2)*theta), exp(I*(gamma + lambda + phi))*cos((1/2)*theta)] >>> c = Circuit().push(GateU(theta, phi, lmbda, gamma), 0) >>> c 1-qubit circuit with 1 instruction: └── U(theta, phi, lambda, gamma) @ q[0] >>> GateU(theta, phi, lmbda, gamma).power(2), GateU(theta, phi, lmbda, gamma).inverse() (U(theta, phi, lambda, gamma)**2, U(-theta, -lambda, -phi, -gamma)) >>> GateU(theta, phi, lmbda, gamma).decompose() 1-qubit circuit with 1 instruction: └── U(theta, phi, lambda, gamma) @ q[0]
Note
A power of a
GateUwith symbolic angles is wrapped inPower. With numeric angles it is computed numerically and returned as a newGateU.- 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.
- supports_canonical_rewrite()[source]
Return
False: U is a primitive of the canonical gate set.
- convert_to_numeric(matrix)[source]
Convert a nested list of SymEngine numbers to a
complex128array.- Parameters:
matrix – Rows of numeric SymEngine expressions, as returned by
matrix().tolist().- Returns:
The same entries as complex floats.
- Return type:
- 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
- getparams()[source]
Return the matrix entries as a list, in column-major order.
- 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:
- pretty_print()[source]
Return a multi-line string showing the matrix row by row.
- control(*args)[source]
Return a controlled operation, or a lazy expression for one.
- Parameters:
*args –
One of the following.
(num_controls, op): returnop.control(num_controls).(op,): return the lazy expressioncontrol(?, op).(num_controls,)with anint: return the lazy expressioncontrol(num_controls, ?).
- Returns:
The controlled operation or a lazy expression.
- Return type:
- Raises:
TypeError – If a single argument is neither an
Operationnor anint, or the number of arguments is not 1 or 2.
Examples
>>> from mimiqcircuits import * >>> control(2, GateX()) C₂X >>> # fix the number of controls, give the operation later >>> lazy_ctrl = control(2) >>> lazy_ctrl(GateX()) C₂X
- power(*args)[source]
Return a power of an operation, or a lazy expression for one.
- Parameters:
*args –
One of the following.
(op, exponent): returnop.power(exponent).(op,): return the lazy expressionpower(op, ?).(exponent,)with anint: return the lazy expressionpower(?, exponent). A non-integer exponent cannot be given alone.
- Returns:
The powered operation or a lazy expression.
- Return type:
- Raises:
TypeError – If a single argument is neither an
Operationnor anint, or the number of arguments is not 1 or 2.
Examples
>>> from mimiqcircuits import * >>> power(GateX(), 0.5) SX >>> # fix the exponent, give the operation later >>> power(2)(GateS()) Z
- inverse(op)[source]
Return the inverse of an operation; same as
op.inverse().- Parameters:
- Returns:
The inverse.
- Return type:
Examples
>>> from mimiqcircuits import * >>> op = inverse(GateS()) >>> op S†
- parallel(*args)[source]
Return an operation repeated in parallel on separate qubits, or a lazy expression for one.
- Parameters:
*args –
One of the following.
(num_repeats, op): returnop.parallel(num_repeats).(op,): return the lazy expressionparallel(?, op).(num_repeats,)with anint: return the lazy expressionparallel(num_repeats, ?).
- Returns:
The parallel operation or a lazy expression.
- Return type:
- Raises:
TypeError – If a single argument is neither an
Operationnor anint, or the number of arguments is not 1 or 2.
Examples
>>> from mimiqcircuits import * >>> parallel(3, GateH()) ⨷ ³ H >>> # fix the number of repetitions, give the operation later >>> parallel(3)(GateH()) ⨷ ³ H