Gates
Note: you can import all gates from the root mpqp module, but we also
provide a shorthand to import all the gates at once:
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,ABCRepresent 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(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:
Trueif the two gates’ matrix semantics are equal.- Return type:
bool
Example
>>> X(0).is_equivalent(CustomGate(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 (list[int] | None) – 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
selfandother.- 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 (list[int] | None) – 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
selfandother.- 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 (list[int] | None) – 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 ]]
- abstractmethod 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:
Example
>>> m = 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_dict()[source]
Serialize the gate to a dictionary.
- Returns:
A dictionary representation of the gate.
- Return type:
dict
- 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:
Example
>>> m = 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,ABCGate 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,ABCAbstract 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
endis 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 (int | None) – 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,ABCAbstract class representing a controlled gate, that can be controlled by one or several qubits.
- Parameters:
controls (Union[list[int], int]) – List of indices referring to the qubits used to control the gate.
targets (Union[list[int], 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:
Example
>>> m = 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,ABCAbstract 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)[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
sympyfor gate parameters,valuescan in fact be anything thesubsmethod fromsympywould accept.- Parameters:
values (dict[Expr | str, Complex]) – Mapping between the variables and the replacing values.
- 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,NoParameterGateTwo-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]]
- 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 = 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]]
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,ControlledGateTwo-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]]
- 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(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 = 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]]
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,ControlledGateTwo-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)]]
- 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(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 = 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
languagearg.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
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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
kwith the relation \(\theta = \frac{\pi}{2^{k-1}}\).
- class CRk_dagger(k, control, target)[source]
Bases:
RotationGate,ControlledGateTwo-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]]
- 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(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 = 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
languagearg.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
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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
kwith the relation \(\theta = -\frac{\pi}{2^{k-1}}\).
- class CZ(control, target)[source]
Bases:
InvolutionGate,ControlledGate,NoParameterGateTwo-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]]
- 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 = 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]]
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,InvolutionGateOne 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]]
- qiskit_gate
alias of
HGate
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,InvolutionGateOne 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]]
- 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
languagearg.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
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,SimpleClassReprABCThe 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
- cirq_gate = None
- qasm2_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,SimpleClassReprABCAbstract 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:
Example
>>> m = 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
languagearg.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[tuple[Any, ...], dtype[complex128]]
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, label=None)[source]
Bases:
SingleQubitGate,NoParameterGate,SimpleClassReprABCAbstract 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.
label (Optional[str])
- class P(theta, target)[source]
Bases:
RotationGate,SingleQubitGateOne 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]]
- 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:
Example
>>> m = 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]]
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,SingleQubitGateOne 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)]]
- 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(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 = 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
languagearg.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
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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
kwith the relation \(\theta = \frac{\pi}{2^{k-1}}\).
- class Rk_dagger(k, target)[source]
Bases:
RotationGate,SingleQubitGateOne 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]]
- 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(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 = 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
languagearg.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
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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
kwith the relation \(\theta = -\frac{\pi}{2^{k-1}}\).
- class RotationGate(theta, target)[source]
Bases:
NativeGate,ParametrizedGate,SimpleClassReprABCMany 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(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
languagearg.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,SingleQubitGateOne 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 ]]
- 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 = 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]]
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,SingleQubitGateOne 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 ]]
- 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 = 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]]
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,SingleQubitGateOne 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]]
- 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 = 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]]
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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:
OneQubitNoParamGateOne 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]]
- qiskit_gate
alias of
SGate
- 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(np.diag([1,1j]),[0]) >>> pprint(gate.inverse().to_matrix()) [[1, 0 ], [0, -1j]]
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,NoParameterGateTwo-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]]
- 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.complex128]
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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 S_dagger(target)[source]
Bases:
OneQubitNoParamGateOne qubit S adjoint gate. It’s equivalent to
P(-pi/2).\(\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_dagger(0).to_matrix()) [[1, 0 ], [0, -1j]]
- qiskit_gate
alias of
SdgGate
- 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(np.diag([1,1j]),[0]) >>> pprint(gate.inverse().to_matrix()) [[1, 0 ], [0, -1j]]
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- qiskit_string: str = 'sdg'
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 = 'DAG(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 T(target)[source]
Bases:
OneQubitNoParamGateOne 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)]]
- 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 = 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]]
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,NoParameterGateThree-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]]
- 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 = 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]]
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,SingleQubitGateGeneric 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 ]]
- 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 = 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
languagearg.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
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- property gamma
See corresponding argument.
- property phi
See corresponding argument.
- qasm2_gate = 'u3'
- qiskit_string: str = 'u3'
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,InvolutionGateOne 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]]
- qiskit_gate
alias of
XGate
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,InvolutionGateOne 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 ]]
- qiskit_gate
alias of
YGate
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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,InvolutionGateOne 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]]
- qiskit_gate
alias of
ZGate
- braket_gate
Decorator yo unite the
classmethodandpropertydecorators.
- cirq_gate
Decorator yo unite the
classmethodandpropertydecorators.
- 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, S_dagger, T, TOF, U, X, Y, Z]
All concrete native gates.
The GateDefinition
- class GateDefinition[source]
Bases:
ABCAbstract 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]]) >>> custom_gate = CustomGate(gate_matrix, [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
- abstractmethod to_canonical_matrix()[source]
Returns the matrix corresponding to this gate definition.
- Return type:
- class UnitaryMatrix(definition)[source]
Bases:
GateDefinitionDefinition of a gate using its matrix.
- Parameters:
definition (Matrix) – Matrix defining the unitary gate.
- Raises:
ValueError – Matrices defining gates have to be unitary.
ValueError – The unitary matrix of a gate acting on qubits must have dimensions that are power of two.
- subs(values)[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.
- 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
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(matrix, targets, label=None)[source]
Bases:
GateCustom gates allow you to define your own unitary gates.
- Parameters:
matrix (Matrix) – The matrix describing the gate.
targets (Union[list[int], 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 matrix
ValueError – Target qubits must be ordered and contiguous for a CustomGate.
Example
>>> u = 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
- decompose()[source]
Returns the circuit made of native gates equivalent to this gate.
The circuit follows the quantum Shannon decomposition which decomposes any unitary matrix into Ry,Rz and CNOT gates.
Example
>>> U = np.array([[0,1], [1,0]]) >>> gate = CustomGate(U, [0]) >>> print(gate.decompose()) global phase: π/2 ┌─────────┐┌───────┐┌──────────┐ q: ┤ Rz(π/2) ├┤ Ry(π) ├┤ Rz(-π/2) ├ └─────────┘└───────┘└──────────┘
- Return type:
- subs(values)[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
sympyfor gate parameters,valuescan in fact be anything thesubsmethod fromsympywould accept.- Parameters:
values (dict[Expr | str, Complex]) – Mapping between the variables and the replacing values.
- Returns:
The circuit with the replaced parameters.
- Return type:
Example
>>> theta = symbols("θ") >>> print(Rx(theta, 0).subs({theta: np.pi})) ┌───────┐ q: ┤ Rx(π) ├ └───────┘
- 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 = 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 = 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, printing=False)[source]
Transforms this instruction into the corresponding object in the language specified in the
languagearg.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().printing (bool)
- Returns:
The corresponding instruction (gate or measure) in the target language.
- definition
See parameter description.
- property free_symbols: set[Basic]
Controlled Custom Gates
- class CustomControlledGate(controls, gate, label=None)[source]
Bases:
ControlledGateClass used to define a custom controlled gate.
- Parameters:
controls (list[int] | int) – Indices referring to the qubits used to control the gate.
gate (Gate) – The original, non controlled, instance of the gate.
label (str | None) – Label used to identify the gate.
Examples
>>> circuit = QCircuit(2) >>> circuit.add(CustomControlledGate(0, Y(1))) >>> pprint(circuit.to_matrix()) [[1, 0, 0 , 0 ], [0, 1, 0 , 0 ], [0, 0, 0 , -1j], [0, 0, 1j, 0 ]] >>> print(circuit) q_0: ──■── ┌─┴─┐ q_1: ┤ Y ├ └───┘ >>> circuit = QCircuit(3) >>> circuit.add(CustomControlledGate([0,2], CustomGate(np.array([[1,0],[0,-1]]),[1]))) >>> print(circuit) q_0: ─────■───── ┌────┴────┐ q_1: ┤ Unitary ├ └────┬────┘ q_2: ─────■─────
- 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(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 = 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
languagearg.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 (set[Parameter] | None) – 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.
- Return type:
Any