Gates
from mpqp.gates import *
Our gates class definitions are very declarative: if the gate operates on only
one qubit, it takes
SingleQubitGate
as
parent, if it is a rotation gate, it takes
RotationGate
as parent,
etc. This allows us to factorize a lot of common behaviors.1
If you are not a library developer, the most important section of this page for you is very likely the Native Gates one.
The Gate class
- class Gate(targets, label=None)[source]
Bases:
Instruction
,ABC
Represent a unitary operator acting on qubit(s).
A gate is an measurement and the main component of a circuit. The semantics of a gate is defined using
GateDefinition
.- Parameters
targets (list[int]) – List of indices referring to the qubits on which the gate will be applied.
label (Optional[str]) – Label used to identify the gate.
- inverse()[source]
Computing the inverse of this gate.
- Returns
The gate corresponding to the inverse of this gate.
- Return type
Example
>>> Z(0).inverse() Z(0) >>> gate = CustomGate(UnitaryMatrix(np.diag([1,1j])),[0]) >>> pprint(gate.inverse().to_matrix()) [[1, 0 ], [0, -1j]]
- is_equivalent(other)[source]
Determine if the gate in parameter is equivalent to this gate.
The equivalence of two gate is only determined from their matricial semantics (and thus ignores all other aspects of the gate such as the target qubits, the label, etc….)
- Parameters
other (Gate) – the gate to test if it is equivalent to this gate
- Returns
True
if the two gates’ matrix semantics are equal.- Return type
bool
Example
>>> X(0).is_equivalent(CustomGate(UnitaryMatrix(np.array([[0,1],[1,0]])),[1])) True
- minus(other, targets=None)[source]
Compute the subtraction of two gates. It normalizes the subtraction to ensure it is unitary.
This operation is shorthanded by the
-
operator.- Parameters
other (Gate) – The gate to subtract to this gate.
targets (Optional[list[int]]) – Qubits on which this new gate will operate. If not given, the targets of the two gates multiplied must be the same and the resulting gate will have this same targets.
- Returns
The subtraction of
self
andother
.- Return type
Example
>>> (X(0).minus(Z(0))).to_matrix() array([[-0.70710678, 0.70710678], [ 0.70710678, 0.70710678]])
- plus(other, targets=None)[source]
Compute the sum of two gates. It normalizes the result to ensure it is unitary.
This operation is shorthanded by the
+
operator.- Parameters
other (Gate) – The gate to add to this gate.
targets (Optional[list[int]]) – Qubits on which this new gate will operate. If not given, the targets of the two gates multiplied must be the same and the resulting gate will have this same targets.
- Returns
The sum of
self
andother
.- Return type
Example
>>> (X(0).plus(Z(0))).to_matrix() array([[ 0.70710678, 0.70710678], [ 0.70710678, -0.70710678]])
- power(exponent)[source]
Compute the exponentiation \(G^{exponent}\) of this gate G.
- Parameters
exponent (float) – Number representing the exponent.
- Returns
The gate elevated to the exponent in parameter.
- Return type
Examples
>>> swap_gate = SWAP(0,1) >>> pprint((swap_gate.power(2)).to_matrix()) [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] >>> pprint((swap_gate.power(-1)).to_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]] >>> pprint((swap_gate.power(0.75)).to_matrix()) [[1, 0 , 0 , 0], [0, 0.14645+0.35355j, 0.85355-0.35355j, 0], [0, 0.85355-0.35355j, 0.14645+0.35355j, 0], [0, 0 , 0 , 1]]
- product(other, targets=None)[source]
Compute the composition of self and the other gate.
This operation is shorthanded by the
*
operator.- Parameters
other (Gate) – Rhs of the product.
targets (Optional[list[int]]) – Qubits on which this new gate will operate. If not given, the targets of the two gates multiplied must be the same and the resulting gate will have this same targets.
- Returns
The product of the two gates concerned.
- Return type
Example
>>> pprint((X(0).product(Z(0))).to_matrix()) [[0, -1], [1, 0 ]]
- scalar_product(scalar)[source]
Multiply this gate by a scalar. It normalizes the result to ensure it is unitary.
- Parameters
scalar (complex) – The number to multiply the gate’s matrix by.
- Returns
The result of the multiplication, the targets of the resulting gate will be the same as the ones of the initial gate.
- Return type
Example
>>> pprint((X(0).scalar_product(1j)).to_matrix()) [[0 , 1j], [1j, 0 ]]
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
- Return type
Matrix
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- to_matrix(desired_gate_size=0)[source]
Return the matricial semantics to this gate. Considering connections’ order and position, in contrast with
to_canonical_matrix()
.- Parameters
desired_gate_size (int) – The total number for qubits needed for the gate representation. If not provided, the minimum number of qubits required to generate the matrix will be used.
- Returns
A numpy array representing the unitary matrix of the gate.
- Return type
Matrix
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0, 1).to_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]] >>> pprint(TOF([1,3], 2).to_matrix()) [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1, 0, 0]]
- class InvolutionGate(targets, label=None)[source]
Bases:
Gate
,ABC
Gate who’s inverse is itself.
- Parameters
targets (list[int]) – List of indices referring to the qubits on which the gate will be applied.
label (Optional[str]) – Label used to identify the gate.
- class SingleQubitGate(target, label=None)[source]
Bases:
Gate
,ABC
Abstract class for gates operating on a single qubit.
- Parameters
target (int) – Index or referring to the qubit on which the gate will be applied.
label (Optional[str]) – Label used to identify the gate.
- classmethod range(start_or_end, end=None, step=1)[source]
Apply the gate to a range of qubits.
- Parameters
start_or_end (int) – If
end
is not defined, this value is treated as the end value of the range, and the range starts from0
. Otherwise, it is treated as the start value.end (Optional[int]) – The upper bound of the range (exclusive).
step (int) – The step or increment between indices in the range.
- Returns
A list of gate instances applied to the qubits in the specified range.
Examples
>>> H.range(3) [H(0), H(1), H(2)] >>> S.range(1, 4) [S(1), S(2), S(3)] >>> Z.range(7, step=2) [Z(0), Z(2), Z(4), Z(6)]
- nb_qubits = 1
Controlled Gates
- class ControlledGate(controls, targets, non_controlled_gate, label=None)[source]
Bases:
Gate
,ABC
Abstract class representing a controlled gate, that can be controlled by one or several qubits.
- Parameters
controls (list[int]) – List of indices referring to the qubits used to control the gate.
targets (list[int]) – List of indices referring to the qubits on which the gate will be applied.
non_controlled_gate (Gate) – The original, non controlled, gate.
label (Optional[str]) – Label used to identify the gate.
- to_matrix(desired_gate_size=0)[source]
Return the matricial semantics to this gate. Considering connections’ order and position, in contrast with
to_canonical_matrix()
.- Parameters
desired_gate_size (int) – The total number for qubits needed for the gate representation. If not provided, the minimum number of qubits required to generate the matrix will be used.
- Returns
A numpy array representing the unitary matrix of the gate.
- Return type
Matrix
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0, 1).to_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]] >>> pprint(TOF([1,3], 2).to_matrix()) [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1, 0, 0]]
- controls
See parameter description.
- non_controlled_gate
See parameter description.
Parametrized Gates
Some gate (such as CNOT for instance) do not need any parameters, but in order to have a universal set of gates, one needs at least one parametrized gate. This module defines the abstract class needed to define these gates as well as a way to handle symbolic variables.
More on the topic of symbolic variable can be found in the VQA page.
- class ParametrizedGate(definition, targets, parameters, label=None)[source]
Bases:
Gate
,ABC
Abstract class to factorize behavior of parametrized gate.
- Parameters
definition (GateDefinition) – Provide a definition of the gate (matrix, gate combination, …).
targets (list[int]) – List of indices referring to the qubits on which the gate will be applied.
parameters (list[Expr | float]) – List of parameters used to define the gate.
label (Optional[str]) – Label used to identify the measurement.
- subs(values, remove_symbolic=False)[source]
Substitutes the parameters of the instruction with complex values. Optionally, also removes all symbolic variables such as \(\pi\) (needed for circuit execution, for example).
Since we use
sympy
for gate parameters,values
can in fact be anything thesubs
method fromsympy
would accept.- Parameters
values (dict[Expr | str, Complex]) – Mapping between the variables and the replacing values.
remove_symbolic (bool) – Whether symbolic values should be replaced by their numeric counterparts.
- Returns
The circuit with the replaced parameters.
- Return type
Example
>>> theta = symbols("θ") >>> print(Rx(theta, 0).subs({theta: np.pi})) ┌───────┐ q: ┤ Rx(π) ├ └───────┘
- definition
See parameter description.
- parameters
See parameter description.
Native Gates
Native gates is the set of all gates natively supported in OpenQASM. Since we rely on this standard, all of them are indeed implemented. In addition, this module contains a few abstract classes used to factorize the behaviors common to a lot of gates.
You will find bellow the list of available native gates:
to-be-generated
- class CNOT(control, target)[source]
Bases:
InvolutionGate
,ControlledGate
,NoParameterGate
Two-qubit Controlled-NOT gate.
\(\begin{pmatrix}1&0&0&0\\0&1&0&0\\0&0&0&1\\0&0&1&0\end{pmatrix}\)
- Parameters
control (int) – index referring to the qubit used to control the gate
target (int) – index referring to the qubit on which the gate will be applied
Example
>>> pprint(CNOT(0, 1).to_matrix()) [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]
- braket_gate
alias of
CNot
- qiskit_gate
alias of
CXGate
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- nb_qubits = 2
Size of the gate.
- qiskit_string: str = 'cx'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'CNOT'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class CP(theta, control, target)[source]
Bases:
RotationGate
,ControlledGate
Two-qubit Controlled-P gate.
- Parameters
theta (Expr | float) – Parameter representing the phase to apply.
control (int) – Index referring to the qubit used to control the gate.
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(CP(0.5, 0, 1).to_matrix()) [[1, 0, 0, 0 ], [0, 1, 0, 0 ], [0, 0, 1, 0 ], [0, 0, 0, 0.87758+0.47943j]]
- braket_gate
alias of
CPhaseShift
- qiskit_gate
alias of
CPhaseGate
- inverse()[source]
Computing the inverse of this gate.
- Returns
The gate corresponding to the inverse of this gate.
- Return type
Example
>>> Z(0).inverse() Z(0) >>> gate = CustomGate(UnitaryMatrix(np.diag([1,1j])),[0]) >>> pprint(gate.inverse().to_matrix()) [[1, 0 ], [0, -1j]]
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- nb_qubits = 2
- qiskit_string: str = 'cp'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'CNOT;PH'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class CRk(k, control, target)[source]
Bases:
RotationGate
,ControlledGate
Two-qubit Controlled-Rk gate.
\(\begin{pmatrix}1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&0&e^{i\pi/2^{k-1}}\end{pmatrix}\)
- Parameters
k (Expr | int) – Parameter used in the definition of the phase to apply.
control (int) – Index referring to the qubit used to control the gate.
target (int) – Index referring to the qubit on which the gate will be applied.
Examples
>>> pprint(CRk(4, 0, 1).to_matrix()) [[1, 0, 0, 0 ], [0, 1, 0, 0 ], [0, 0, 1, 0 ], [0, 0, 0, 0.92388+0.38268j]]
>>> k = symbols("k") >>> pprint(CRk(k, 0, 1).to_matrix()) [[1, 0, 0, 0 ], [0, 1, 0, 0 ], [0, 0, 1, 0 ], [0, 0, 0, 1.0*exp(2.0*I*pi/2**k)]]
- braket_gate
alias of
CPhaseShift
- qiskit_gate
alias of
CPhaseGate
- inverse()[source]
Computing the inverse of this gate.
- Returns
The gate corresponding to the inverse of this gate.
- Return type
Example
>>> Z(0).inverse() Z(0) >>> gate = CustomGate(UnitaryMatrix(np.diag([1,1j])),[0]) >>> pprint(gate.inverse().to_matrix()) [[1, 0 ], [0, -1j]]
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- to_other_language(language=Language.QISKIT, qiskit_parameters=None)[source]
Transforms this instruction into the corresponding object in the language specified in the
language
arg.By default, the instruction is translated to the corresponding one in Qiskit, since it is the interface we use to generate the OpenQASM code.
In the future, we will generate the OpenQASM code on our own, and this method will be used only for complex objects that are not tractable by OpenQASM (like hybrid structures).
- Parameters
language (Language) – Enum representing the target language.
qiskit_parameters (Optional[set['Parameter']]) – We need to keep track of the parameters passed to qiskit in order not to define twice the same parameter. Defaults to
set()
.
- Returns
The corresponding instruction (gate or measure) in the target language.
- property k: Expr | float
See corresponding argument.
- nb_qubits = 2
Size of the gate.
- qiskit_string: str = 'cp'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'CNOT;PH'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- property theta: Expr | float
Value of the rotation angle, parametrized by
k
with the relation \(\theta = \frac{\pi}{2^{k-1}}\).
- class CRk_dagger(k, control, target)[source]
Bases:
RotationGate
,ControlledGate
Two-qubit Controlled-Rk-dagger gate.
\(\begin{pmatrix}1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&0&e^{-i\pi/2^{k-1}}\end{pmatrix}\)
- Parameters
k (Expr | int) – Parameter used in the definition of the phase to apply.
control (int) – Index referring to the qubit used to control the gate.
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(CRk_dagger(4, 0, 1).to_matrix()) [[1, 0, 0, 0 ], [0, 1, 0, 0 ], [0, 0, 1, 0 ], [0, 0, 0, 0.92388-0.38268j]]
- braket_gate
alias of
CPhaseShift
- qiskit_gate
alias of
CPhaseGate
- inverse()[source]
Computing the inverse of this gate.
- Returns
The gate corresponding to the inverse of this gate.
- Return type
Example
>>> Z(0).inverse() Z(0) >>> gate = CustomGate(UnitaryMatrix(np.diag([1,1j])),[0]) >>> pprint(gate.inverse().to_matrix()) [[1, 0 ], [0, -1j]]
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- to_other_language(language=Language.QISKIT, qiskit_parameters=None)[source]
Transforms this instruction into the corresponding object in the language specified in the
language
arg.By default, the instruction is translated to the corresponding one in Qiskit, since it is the interface we use to generate the OpenQASM code.
In the future, we will generate the OpenQASM code on our own, and this method will be used only for complex objects that are not tractable by OpenQASM (like hybrid structures).
- Parameters
language (Language) – Enum representing the target language.
qiskit_parameters (Optional[set['Parameter']]) – We need to keep track of the parameters passed to qiskit in order not to define twice the same parameter. Defaults to
set()
.
- Returns
The corresponding instruction (gate or measure) in the target language.
- property k: Expr | int
See corresponding argument.
- nb_qubits = 2
Size of the gate.
- qiskit_string: str = 'cp'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'CNOT;PH'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- property theta: Expr | float
Value of the rotation angle, parametrized by
k
with the relation \(\theta = -\frac{\pi}{2^{k-1}}\).
- class CZ(control, target)[source]
Bases:
InvolutionGate
,ControlledGate
,NoParameterGate
Two-qubit Controlled-Z gate.
\(\begin{pmatrix}1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&0&-1\end{pmatrix}\)
- Parameters
k – Parameter used in the definition of the phase to apply.
control (int) – Index referring to the qubit used to control the gate.
target (int) – Index referring to the qubit on which the gate will be applied.
Examples
>>> pprint(CZ(0, 1).to_matrix()) [[1, 0, 0, 0 ], [0, 1, 0, 0 ], [0, 0, 1, 0 ], [0, 0, 0, -1]]
- braket_gate
alias of
CZ
- qiskit_gate
alias of
CZGate
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- nb_qubits = 2
Size of the gate.
- qiskit_string: str = 'cz'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'CSIGN'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class H(target)[source]
Bases:
OneQubitNoParamGate
,InvolutionGate
One qubit Hadamard gate. \(\frac{1}{\sqrt{2}}\begin{pmatrix}1&1\\1&-1\end{pmatrix}\)
- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(H(0).to_matrix()) [[0.70711, 0.70711 ], [0.70711, -0.70711]]
- braket_gate
alias of
H
- qiskit_gate
alias of
HGate
- qiskit_string: str = 'h'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'H'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class Id(target, label=None)[source]
Bases:
OneQubitNoParamGate
,InvolutionGate
One qubit identity gate.
\(\begin{pmatrix}1&0\\0&1\end{pmatrix}\)
- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
label (Optional[str]) –
Example
>>> pprint(Id(0).to_matrix()) [[1, 0], [0, 1]]
- braket_gate
alias of
I
- qiskit_gate
alias of
IGate
- to_other_language(language=Language.QISKIT, qiskit_parameters=None)[source]
Transforms this instruction into the corresponding object in the language specified in the
language
arg.By default, the instruction is translated to the corresponding one in Qiskit, since it is the interface we use to generate the OpenQASM code.
In the future, we will generate the OpenQASM code on our own, and this method will be used only for complex objects that are not tractable by OpenQASM (like hybrid structures).
- Parameters
language (Language) – Enum representing the target language.
qiskit_parameters (Optional[set['Parameter']]) – We need to keep track of the parameters passed to qiskit in order not to define twice the same parameter. Defaults to
set()
.
- Returns
The corresponding instruction (gate or measure) in the target language.
- qiskit_string: str = 'id'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'I'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class NativeGate(targets, label=None)[source]
Bases:
Gate
,SimpleClassReprABC
The standard on which we rely, OpenQASM, comes with a set of gates supported by default. More complicated gates can be defined by the user. This abstract class represent all those gates supported by default.
- Parameters
targets (list[int]) – List of indices referring to the qubits on which the gate will be applied.
label (Optional[str]) – Label used to identify the gate.
- braket_gate = None
- native_gate_options = {'disable_symbol_warn': True}
- qasm2_gate
Decorator yo unite the
classmethod
andproperty
decorators.
- qiskit_gate = None
- qiskit_string: str
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class NoParameterGate(targets, label=None)[source]
Bases:
NativeGate
,SimpleClassReprABC
Abstract class describing native gates that do not depend on parameters.
- Parameters
targets (list[int]) – List of indices referring to the qubits on which the gate will be applied.
label (Optional[str]) – Label used to identify the gate.
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
- Return type
Matrix
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- to_other_language(language=Language.QISKIT, qiskit_parameters=None)[source]
Transforms this instruction into the corresponding object in the language specified in the
language
arg.By default, the instruction is translated to the corresponding one in Qiskit, since it is the interface we use to generate the OpenQASM code.
In the future, we will generate the OpenQASM code on our own, and this method will be used only for complex objects that are not tractable by OpenQASM (like hybrid structures).
- Parameters
language (Language) – Enum representing the target language.
qiskit_parameters (Optional[set['Parameter']]) – We need to keep track of the parameters passed to qiskit in order not to define twice the same parameter. Defaults to
set()
.
- Returns
The corresponding instruction (gate or measure) in the target language.
- braket_gate = None
- matrix: ndarray[Any, dtype[complex64]]
Matricial semantics of the gate.
- qiskit_gate = None
- qlm_aqasm_keyword: str
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class OneQubitNoParamGate(target)[source]
Bases:
SingleQubitGate
,NoParameterGate
,SimpleClassReprABC
Abstract Class describing one-qubit native gates that do not depend on parameters.
- Parameters
target (int) – Index referring to the qubits on which the gate will be applied.
- class P(theta, target)[source]
Bases:
RotationGate
,SingleQubitGate
One qubit parametrized Phase gate. Consist in a rotation around Z axis.
\(\begin{pmatrix}1&0\\0&e^{i\theta}\end{pmatrix}\)
- Parameters
theta (Expr | float) – Parameter representing the phase to apply.
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(P(np.pi/3, 1).to_matrix()) [[1, 0 ], [0, 0.5+0.86603j]]
- braket_gate
alias of
PhaseShift
- qiskit_gate
alias of
PhaseGate
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
- Return type
Matrix
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- qiskit_string: str = 'p'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'PH'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class Rk(k, target)[source]
Bases:
RotationGate
,SingleQubitGate
One qubit Phase gate of angle \(\frac{2i\pi}{2^k}\).
\(\begin{pmatrix}1&0\\0&e^{i\pi/2^{k-1}}\end{pmatrix}\)
- Parameters
k (Expr | int) – Parameter used in the definition of the phase to apply.
target (int) – Index referring to the qubit on which the gate will be applied.
Examples
>>> pprint(Rk(5, 0).to_matrix()) [[1, 0 ], [0, 0.98079+0.19509j]]
>>> pprint(Rk(k, 0).to_matrix()) [[1, 0 ], [0, 1.0*exp(2.0*I*pi/2**k)]]
- braket_gate
alias of
PhaseShift
- qiskit_gate
alias of
PhaseGate
- inverse()[source]
Computing the inverse of this gate.
- Returns
The gate corresponding to the inverse of this gate.
- Return type
Example
>>> Z(0).inverse() Z(0) >>> gate = CustomGate(UnitaryMatrix(np.diag([1,1j])),[0]) >>> pprint(gate.inverse().to_matrix()) [[1, 0 ], [0, -1j]]
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- to_other_language(language=Language.QISKIT, qiskit_parameters=None)[source]
Transforms this instruction into the corresponding object in the language specified in the
language
arg.By default, the instruction is translated to the corresponding one in Qiskit, since it is the interface we use to generate the OpenQASM code.
In the future, we will generate the OpenQASM code on our own, and this method will be used only for complex objects that are not tractable by OpenQASM (like hybrid structures).
- Parameters
language (Language) – Enum representing the target language.
qiskit_parameters (Optional[set['Parameter']]) – We need to keep track of the parameters passed to qiskit in order not to define twice the same parameter. Defaults to
set()
.
- Returns
The corresponding instruction (gate or measure) in the target language.
- property k: Expr | int
See corresponding argument.
- qiskit_string: str = 'p'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'PH'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- property theta: Expr | float
Value of the rotation angle, parametrized by
k
with the relation \(\theta = \frac{\pi}{2^{k-1}}\).
- class Rk_dagger(k, target)[source]
Bases:
RotationGate
,SingleQubitGate
One qubit Phase gate of angle \(-\frac{2i\pi}{2^k}\).
\(\begin{pmatrix}1&0\\0&e^{-i\pi/2^{k-1}}\end{pmatrix}\)
- Parameters
k (Expr | int) – Parameter used in the definition of the phase to apply.
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(Rk_dagger(5, 0).to_matrix()) [[1, 0 ], [0, 0.98079-0.19509j]]
- braket_gate
alias of
PhaseShift
- qiskit_gate
alias of
PhaseGate
- inverse()[source]
Computing the inverse of this gate.
- Returns
The gate corresponding to the inverse of this gate.
- Return type
Example
>>> Z(0).inverse() Z(0) >>> gate = CustomGate(UnitaryMatrix(np.diag([1,1j])),[0]) >>> pprint(gate.inverse().to_matrix()) [[1, 0 ], [0, -1j]]
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- to_other_language(language=Language.QISKIT, qiskit_parameters=None)[source]
Transforms this instruction into the corresponding object in the language specified in the
language
arg.By default, the instruction is translated to the corresponding one in Qiskit, since it is the interface we use to generate the OpenQASM code.
In the future, we will generate the OpenQASM code on our own, and this method will be used only for complex objects that are not tractable by OpenQASM (like hybrid structures).
- Parameters
language (Language) – Enum representing the target language.
qiskit_parameters (Optional[set['Parameter']]) – We need to keep track of the parameters passed to qiskit in order not to define twice the same parameter. Defaults to
set()
.
- Returns
The corresponding instruction (gate or measure) in the target language.
- property k: Expr | float
See corresponding argument.
- qiskit_string: str = 'p'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'PH'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- property theta: Expr | float
Value of the rotation angle, parametrized by
k
with the relation \(\theta = -\frac{\pi}{2^{k-1}}\).
- class RotationGate(theta, target)[source]
Bases:
NativeGate
,ParametrizedGate
,SimpleClassReprABC
Many gates can be classified as a simple rotation gate, around a specific axis (and potentially with a control qubit). All those gates have in common a single parameter:
theta
. This abstract class helps up factorize this behavior, and simply having to tweak the matrix semantics and qasm translation of the specific gate.- Parameters
theta (Expr | float) – Angle of the rotation.
target (int) – Index referring to the qubits on which the gate will be applied.
- inverse()[source]
Computing the inverse of this gate.
- Returns
The gate corresponding to the inverse of this gate.
- Return type
Example
>>> Z(0).inverse() Z(0) >>> gate = CustomGate(UnitaryMatrix(np.diag([1,1j])),[0]) >>> pprint(gate.inverse().to_matrix()) [[1, 0 ], [0, -1j]]
- to_other_language(language=Language.QISKIT, qiskit_parameters=None)[source]
Transforms this instruction into the corresponding object in the language specified in the
language
arg.By default, the instruction is translated to the corresponding one in Qiskit, since it is the interface we use to generate the OpenQASM code.
In the future, we will generate the OpenQASM code on our own, and this method will be used only for complex objects that are not tractable by OpenQASM (like hybrid structures).
- Parameters
language (Language) – Enum representing the target language.
qiskit_parameters (Optional[set['Parameter']]) – We need to keep track of the parameters passed to qiskit in order not to define twice the same parameter. Defaults to
set()
.
- Returns
The corresponding instruction (gate or measure) in the target language.
- property theta
Rotation angle (in radians).
- class Rx(theta, target)[source]
Bases:
RotationGate
,SingleQubitGate
One qubit rotation around the X axis.
\(\begin{pmatrix}\cos(\theta/2)&-i\sin(\theta/2)\\-i\sin(\theta/2)&\cos(\theta/2)\end{pmatrix}\)
- Parameters
theta (Expr | float) – Parameter representing the angle of the gate.
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(Rx(np.pi/5, 1).to_matrix()) [[0.95106 , -0.30902j], [-0.30902j, 0.95106 ]]
- braket_gate
alias of
Rx
- qiskit_gate
alias of
RXGate
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- qiskit_string: str = 'rx'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'RX'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class Ry(theta, target)[source]
Bases:
RotationGate
,SingleQubitGate
One qubit rotation around the Y axis.
\(\begin{pmatrix}\cos(\theta/2)&-\sin(\theta/2)\\\sin(\theta/2)&\cos(\theta/2)\end{pmatrix}\)
- Parameters
theta (Expr | float) – Parameter representing the angle of the gate.
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(Ry(np.pi/5, 1).to_matrix()) [[0.95106, -0.30902], [0.30902, 0.95106 ]]
- braket_gate
alias of
Ry
- qiskit_gate
alias of
RYGate
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- qiskit_string: str = 'ry'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'RY'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class Rz(theta, target)[source]
Bases:
RotationGate
,SingleQubitGate
One qubit rotation around the Z axis.
\(\begin{pmatrix}e^{i\theta/2}&0\\0&e^{-i\theta/2}\end{pmatrix}\)
- Parameters
theta (Expr | float) – Parameter representing the angle of the gate.
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(Rz(np.pi/5, 1).to_matrix()) [[0.95106-0.30902j, 0 ], [0 , 0.95106+0.30902j]]
- braket_gate
alias of
Rz
- qiskit_gate
alias of
RZGate
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- qiskit_string: str = 'rz'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'RZ'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class S(target)[source]
Bases:
OneQubitNoParamGate
One qubit S gate. It’s equivalent to
P(pi/2)
. It can also be defined as the square-root of the Z (Pauli) gate.\(\begin{pmatrix}1&0\\0&i\end{pmatrix}\)
- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(S(0).to_matrix()) [[1, 0 ], [0, 1j]]
- braket_gate
alias of
S
- qiskit_gate
alias of
SGate
- qiskit_string: str = 's'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'S'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class SWAP(a, b)[source]
Bases:
InvolutionGate
,NoParameterGate
Two-qubit SWAP gate.
\(\begin{pmatrix}1&0&0&0\\0&0&1&0\\0&1&0&0\\0&0&0&1\end{pmatrix}\)
- Parameters
a (int) – First target of the swapping operation.
b (int) – Second target of the swapping operation.
Example
>>> pprint(SWAP(0, 1).to_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- braket_gate
alias of
Swap
- qiskit_gate
alias of
SwapGate
- to_matrix(desired_gate_size=0)[source]
Constructs the matrix representation of a SWAP gate for two qubits.
- Parameters
nb_qubits – The total number for qubits gate representation. If not provided, the minimum number of qubits required to generate the matrix will be used.
desired_gate_size (int) –
- Returns
The matrix representation of the SWAP gate.
- Return type
npt.NDArray[np.complex64]
- nb_qubits = 2
Size of the gate.
- qiskit_string: str = 'swap'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'SWAP'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class T(target)[source]
Bases:
OneQubitNoParamGate
One qubit T gate. It is also referred to as the \(\pi/4\) gate because it consists in applying the phase gate with a phase of \(\pi/4\).
\(\begin{pmatrix}1&0\\0&e^{i\pi/4}\end{pmatrix}\)
The T gate can also be defined as the fourth-root of the Z (Pauli) gate.
- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(T(0).to_matrix()) [[1, 0 ], [0, 1.0*exp(0.25*I*pi)]]
- braket_gate
alias of
T
- qiskit_gate
alias of
TGate
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- qiskit_string: str = 't'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'T'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class TOF(control, target)[source]
Bases:
InvolutionGate
,ControlledGate
,NoParameterGate
Three-qubit Controlled-Controlled-NOT gate, also known as Toffoli Gate.
\(\begin{pmatrix}1&0&0&0&0&0&0&0\\0&1&0&0&0&0&0&0\\0&0&1&0&0&0&0&0\\0&0&0&1&0&0&0&0\\0&0&0&0&1&0&0&0\\0&0&0&0&0&1&0&0\\0&0&0&0&0&0&0&1\\0&0&0&0&0&0&1&0\end{pmatrix}\)
- Parameters
control (list[int]) – List of indices referring to the qubits used to control the gate.
target (int) – Index referring to the qubit on which the gate will be applied.
Examples
>>> pprint(TOF([0, 1], 2).to_matrix()) [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0]]
- braket_gate
alias of
CCNot
- qiskit_gate
alias of
CCXGate
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- nb_qubits = 3
Size of the gate.
- qiskit_string: str = 'ccx'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'CCNOT'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class U(theta, phi, gamma, target)[source]
Bases:
NativeGate
,ParametrizedGate
,SingleQubitGate
Generic one qubit unitary gate. It is parametrized by 3 Euler angles.
\(\begin{pmatrix}\cos(\theta/2)&-e^{i\gamma}\sin(\theta/2)\\e^{i\phi}\sin(\theta/2)&e^{i(\gamma+\phi)}\cos(\theta/2)\end{pmatrix}\)
- Parameters
theta (Expr | float) – Parameter representing the first angle of the gate U.
phi (Expr | float) – Parameter representing the second angle of the gate U.
gamma (Expr | float) – Parameter representing the third angle of the gate U.
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(U(np.pi/3, 0, np.pi/4, 0).to_matrix()) [[0.86603, -0.35355-0.35355j], [0.5 , 0.61237+0.61237j ]]
- braket_gate
alias of
U
- qiskit_gate
alias of
UGate
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- to_other_language(language=Language.QISKIT, qiskit_parameters=None)[source]
Transforms this instruction into the corresponding object in the language specified in the
language
arg.By default, the instruction is translated to the corresponding one in Qiskit, since it is the interface we use to generate the OpenQASM code.
In the future, we will generate the OpenQASM code on our own, and this method will be used only for complex objects that are not tractable by OpenQASM (like hybrid structures).
- Parameters
language (Language) – Enum representing the target language.
qiskit_parameters (Optional[set['Parameter']]) – We need to keep track of the parameters passed to qiskit in order not to define twice the same parameter. Defaults to
set()
.
- Returns
The corresponding instruction (gate or measure) in the target language.
- property gamma
See corresponding argument.
- property phi
See corresponding argument.
- qiskit_string: str = 'u'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'U'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- property theta
See corresponding argument.
- class X(target)[source]
Bases:
OneQubitNoParamGate
,InvolutionGate
One qubit X (NOT) Pauli gate.
\(\begin{pmatrix}0&1\\1&0\end{pmatrix}\)
- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(X(0).to_matrix()) [[0, 1], [1, 0]]
- braket_gate
alias of
X
- qiskit_gate
alias of
XGate
- qiskit_string: str = 'x'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'X'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class Y(target)[source]
Bases:
OneQubitNoParamGate
,InvolutionGate
One qubit Y Pauli gate.
\(\begin{pmatrix}0&-i\\i&0\end{pmatrix}\)
- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(Y(0).to_matrix()) [[0 , -1j], [1j, 0 ]]
- braket_gate
alias of
Y
- qiskit_gate
alias of
YGate
- qiskit_string: str = 'y'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'Y'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- class Z(target)[source]
Bases:
OneQubitNoParamGate
,InvolutionGate
One qubit Z Pauli gate.
\(\begin{pmatrix}1&0\\0&-1\end{pmatrix}\)
- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> pprint(Z(0).to_matrix()) [[1, 0 ], [0, -1]]
- braket_gate
alias of
Z
- qiskit_gate
alias of
ZGate
- qiskit_string: str = 'z'
Keyword corresponding to the gate in
qiskit
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- qlm_aqasm_keyword: str = 'Z'
Keyword(s) corresponding to the gate in
myQLM
. This needs to be available at the class level and is not enforced by the type checker so be careful about it!
- NATIVE_GATES = [CNOT, CP, CRk, CRk_dagger, CZ, H, Id, P, Rk, Rk_dagger, Rx, Ry, Rz, S, SWAP, T, TOF, U, X, Y, Z]
All concrete native gates.
- 1
This in fact is somewhat twisting the way inheritance usually works in python, to make it into a feature existing in other languages, such as traits in rust.
The GateDefinition
- class GateDefinition[source]
Bases:
ABC
Abstract class used to handle the definition of a Gate.
A quantum gate can be defined in several ways, and this class allows us to define it as we prefer. It also handles the translation from one definition to another.
This said, for now only one way of defining the gates is supported, using their matricial semantics.
Example
>>> gate_matrix = np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> gate_definition = UnitaryMatrix(gate_matrix) >>> custom_gate = CustomGate(gate_definition, [0,1])
- inverse()[source]
Compute the inverse of the gate.
- Returns
A GateDefinition representing the inverse of the gate defined.
- Return type
Example
>>> UnitaryMatrix(np.array([[1, 0], [0, -1]])).inverse() UnitaryMatrix(array([[ 1., 0.], [-0., -1.]]))
- is_equivalent(other)[source]
Determines if this definition is equivalent to the other.
- Parameters
other (GateDefinition) – The definition we want to know if it is equivalent.
- Return type
bool
Example
>>> d1 = UnitaryMatrix(np.array([[1, 0], [0, -1]])) >>> d2 = UnitaryMatrix(np.array([[2, 0], [0, -2.0]]) / 2) >>> d1.is_equivalent(d2) True
- subs(values, remove_symbolic=False, disable_symbol_warn=False)[source]
- Parameters
values (dict[Expr | str, Complex]) –
remove_symbolic (bool) –
disable_symbol_warn (bool) –
- Return type
- class UnitaryMatrix(definition, disable_symbol_warn=False)[source]
Bases:
GateDefinition
Definition of a gate using its matrix.
- Parameters
definition (Matrix) – Matrix defining the unitary gate.
disable_symbol_warn (bool) – Boolean used to enable/disable warning concerning unitary checking with symbolic variables.
- subs(values, remove_symbolic=False, disable_symbol_warn=False)[source]
Substitute some symbolic variables in the definition by complex values.
- Parameters
values (dict[Expr | str, Complex]) – Mapping between the symbolic variables and their complex attributions.
remove_symbolic (bool) – Some values such as pi are kept symbolic during circuit manipulation for better precision, but must be replaced by their complex counterpart for circuit execution, this arguments fills that role. Defaults to False.
disable_symbol_warn (bool) – This method returns a
UnitaryMatrix
, which raises a warning in case the matrix used to build it has symbolic variables. This is because this class performs verifications on the matrix to ensure it is indeed unitary, but those verifications cannot be done on symbolic variables. This argument disables this check because in some contexts, it is undesired. Defaults to False.
- to_matrix()[source]
Returns the matrix corresponding to this gate definition. Considering connections’ order and position, in contrast with
to_canonical_matrix()
.- Return type
Matrix
- matrix
See parameter
definition
’s description.
- property nb_qubits: int
Custom Gates
In some cases, we need to manipulate unitary operations that are not defined
using native gates (by the corresponding unitary matrix for instance). For those
cases, you can use mpqp.core.instruction.gates.custom_gate.CustomGate
to add your custom unitary operation to the circuit, which will be decomposed
and executed transparently.
- class CustomGate(definition, targets, label=None)[source]
Bases:
Gate
Custom gates allow you to define your own unitary gates.
- Parameters
definition (UnitaryMatrix) – The GateDefinition describing the gate.
targets (list[int]) – The qubits on which the gate operates.
label (Optional[str]) – The label of the gate. Defaults to None.
- Raises
ValueError – the target qubits must be contiguous and in order, and must match the size of the UnitaryMatrix
Example
>>> u = UnitaryMatrix(np.array([[0,-1],[1,0]])) >>> cg = CustomGate(u, [0]) >>> print(run(QCircuit([X(0), cg]), IBMDevice.AER_SIMULATOR)) Result: IBMDevice, AER_SIMULATOR State vector: [-1, 0] Probabilities: [1, 0] Number of qubits: 1
Note
For the moment, only ordered and contiguous target qubits are allowed when instantiating a CustomGate.
- to_canonical_matrix()[source]
Return the “base” matricial semantics to this gate. Without considering potential column and row permutations needed if the targets of the gate are not sorted.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_canonical_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0,1).to_canonical_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
- to_matrix(desired_gate_size=0)[source]
Return the matricial semantics to this gate. Considering connections’ order and position, in contrast with
to_canonical_matrix()
.- Parameters
desired_gate_size (int) – The total number for qubits needed for the gate representation. If not provided, the minimum number of qubits required to generate the matrix will be used.
- Returns
A numpy array representing the unitary matrix of the gate.
Example
>>> m = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> pprint(CustomGate(m, [1, 2]).to_matrix()) [[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]] >>> pprint(SWAP(0, 1).to_matrix()) [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]] >>> pprint(TOF([1,3], 2).to_matrix()) [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1, 0, 0]]
- to_other_language(language=Language.QISKIT, qiskit_parameters=None)[source]
Transforms this instruction into the corresponding object in the language specified in the
language
arg.By default, the instruction is translated to the corresponding one in Qiskit, since it is the interface we use to generate the OpenQASM code.
In the future, we will generate the OpenQASM code on our own, and this method will be used only for complex objects that are not tractable by OpenQASM (like hybrid structures).
- Parameters
language (Language) – Enum representing the target language.
qiskit_parameters (Optional[set['Parameter']]) – We need to keep track of the parameters passed to qiskit in order not to define twice the same parameter. Defaults to
set()
.
- Returns
The corresponding instruction (gate or measure) in the target language.
- definition
See parameter description.
- property matrix: Union[ndarray[Any, dtype[complex64]], ndarray[Any, dtype[object_]]]