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
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) >>> CustomGate(UnitaryMatrix(np.diag([1,1j])),[0]).inverse().to_matrix() array([[1.-0.j, 0.-0.j], [0.-0.j, 0.-1.j]])
- 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.
- 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 subtraction to ensure it is unitary.
- 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
targets. (must be the same and the resulting gate will have this same) –
- 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) >>> (swap_gate.power(2)).to_matrix() array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) >>> (swap_gate.power(-1)).to_matrix() array([[1., 0., 0., 0.], [0., 0., 1., 0.], [0., 1., 0., 0.], [0., 0., 0., 1.]]) >>> (swap_gate.power(0.75)).to_matrix() # not implemented yet array([[1. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [0. +0.j , 0.14644661+0.35355339j, 0.85355339-0.35355339j, 0. +0.j ], [0. +0.j , 0.85355339-0.35355339j, 0.14644661+0.35355339j, 0. +0.j ], [0. +0.j , 0. +0.j , 0. +0.j , 1. +0.j ]])
- product(other, targets=None)[source]
Compute the composition of self and the other gate.
- 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
>>> (X(0).product(Z(0))).to_matrix() array([[ 0, -1], [ 1, 0]])
- scalar_product(scalar)[source]
Multiply this gate by a scalar. It normalizes the subtraction 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
>>> (X(0).scalar_product(1j)).to_matrix() array([[0.+0.j, 0.+1.j], [0.+1.j, 0.+0.j]])
- to_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
The gate definition
- 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])
- 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
- class UnitaryMatrix(definition, disable_symbol_warn=False)[source]
Bases:
GateDefinition
Definition of a gate using it’s 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.
- matrix
See parameter
definition
’s description.
Controlled Gates
- class ControlledGate(controls, targets, non_controlled_gate=None, 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 (Optional[Gate]) – The original, non controlled, gate.
label (Optional[str]) – Label used to identify the gate.
- 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 example for circuit execution).
Since we use
sympy
for gates’ 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) – If symbolic values should be replaced by their numeric counterpart.
- 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
,NoParameterGate
,ControlledGate
Two-qubit Controlled-NOT gate.
- 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
>>> CNOT(0, 1).to_matrix() array([[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_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
- nb_qubits = 2
- class CRk(k, control, target)[source]
Bases:
RotationGate
,ControlledGate
Two-qubit Controlled-Rk gate.
- 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
>>> print(clean_matrix(CRk(4, 0, 1).to_matrix())) [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0.9238795+0.3826834j]]
- braket_gate
alias of
CPhaseShift
- qiskit_gate
alias of
CPhaseGate
- to_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
- property k: Expr | float
See corresponding argument.
- nb_qubits = 2
- 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
,NoParameterGate
,ControlledGate
Two-qubit Controlled-Z gate.
- 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
>>> print(clean_matrix(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_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
- nb_qubits = 2
- class H(target)[source]
Bases:
OneQubitNoParamGate
,InvolutionGate
One qubit Hadamard gate.
- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> H(0).to_matrix() array([[ 0.70710678, 0.70710678], [ 0.70710678, -0.70710678]])
- braket_gate
alias of
H
- qiskit_gate
alias of
HGate
- matrix: ndarray[Any, dtype[complex64]] = array([[ 0.70710678, 0.70710678], [ 0.70710678, -0.70710678]])
Matricial semantics of the gate.
- class Id(target)[source]
Bases:
OneQubitNoParamGate
,InvolutionGate
One qubit identity gate.
- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> Id(0).to_matrix() array([[1.+0.j, 0.+0.j], [0.+0.j, 1.+0.j]], dtype=complex64)
- braket_gate
alias of
I
- qiskit_gate
alias of
IGate
- 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.
- native_gate_options = {'disable_symbol_warn': True}
- 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_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[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
Corresponding
qiskit
’s gate class.
- 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.
- 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
>>> P(np.pi/3, 1).to_matrix() array([[1. +0.j , 0. +0.j ], [0. +0.j , 0.5+0.8660254j]])
- braket_gate
alias of
PhaseShift
- qiskit_gate
alias of
PhaseGate
- to_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
- class Rk(k, target)[source]
Bases:
RotationGate
,SingleQubitGate
One qubit Phase gate of angle \(\frac{2i\pi}{2^k}\).
- 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
>>> Rk(5, 0).to_matrix() array([[1. +0.j , 0. +0.j ], [0. +0.j , 0.98078528+0.19509032j]])
- braket_gate
alias of
PhaseShift
- qiskit_gate
alias of
PhaseGate
- to_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
- property k: Expr | float
See corresponding argument.
- 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.
- 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
- qiskit_gate = None
- property theta
- class Rx(theta, target)[source]
Bases:
RotationGate
,SingleQubitGate
One qubit rotation around the X axis
- 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
>>> Rx(np.pi/5, 1).to_matrix() array([[0.95105652+0.j , 0. -0.30901699j], [0. -0.30901699j, 0.95105652+0.j ]])
- braket_gate
alias of
Rx
- qiskit_gate
alias of
RXGate
- to_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
- class Ry(theta, target)[source]
Bases:
RotationGate
,SingleQubitGate
One qubit rotation around the Y axis
- 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
>>> Ry(np.pi/5, 1).to_matrix() array([[ 0.95105652, -0.30901699], [ 0.30901699, 0.95105652]])
- braket_gate
alias of
Ry
- qiskit_gate
alias of
RYGate
- to_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
- class Rz(theta, target)[source]
Bases:
RotationGate
,SingleQubitGate
One qubit rotation around the Z axis
- 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
>>> print(clean_matrix(Rz(np.pi/5, 1).to_matrix())) [[0.9510565-0.309017j, 0], [0, 0.9510565+0.309017j]]
- braket_gate
alias of
Rz
- qiskit_gate
alias of
RZGate
- to_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
- 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.- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> S(0).to_matrix() array([[1.+0.j, 0.+0.j], [0.+0.j, 0.+1.j]])
- braket_gate
alias of
S
- qiskit_gate
alias of
SGate
- matrix: ndarray[Any, dtype[complex64]] = array([[1.+0.j, 0.+0.j], [0.+0.j, 0.+1.j]])
Matricial semantics of the gate.
- class SWAP(a, b)[source]
Bases:
InvolutionGate
,NoParameterGate
Two-qubit SWAP gate.
- Parameters
a (int) – First target of the swapping operation.
b (int) – Second target of the swapping operation.
Example
>>> SWAP(0, 1).to_matrix() array([[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
- nb_qubits = 2
- 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\).
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
>>> T(0).to_matrix() array([[1, 0], [0, exp(0.25*I*pi)]], dtype=object)
- braket_gate
alias of
T
- qiskit_gate
alias of
TGate
- to_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
- class TOF(control, target)[source]
Bases:
InvolutionGate
,NoParameterGate
,ControlledGate
Three-qubit Controlled-Controlled-NOT gate, also known as Toffoli Gate
- 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.
Example
>>> print(clean_matrix(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_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
- nb_qubits = 3
- class U(theta, phi, gamma, target)[source]
Bases:
NativeGate
,ParametrizedGate
,SingleQubitGate
Generic one qubit unitary gate. It is parametrized by 3 Euler angles.
- 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
>>> U(np.pi/3, 0, np.pi/4, 0).to_matrix() array([[ 0.8660254 +0.j , -0.35355339-0.35355339j], [ 0.5 +0.j , 0.61237244+0.61237244j]])
- to_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
>>> gd = UnitaryMatrix( ... np.array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) ... ) >>> CustomGate(gd, [1, 2]).to_matrix() array([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]) >>> SWAP(0,1).to_matrix() array([[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.
- qlm_aqasm_keyword = 'U'
- property theta
See corresponding argument.
- class X(target)[source]
Bases:
OneQubitNoParamGate
,InvolutionGate
One qubit X (NOT) Pauli gate.
- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> X(0).to_matrix() array([[0, 1], [1, 0]])
- braket_gate
alias of
X
- qiskit_gate
alias of
XGate
- class Y(target)[source]
Bases:
OneQubitNoParamGate
,InvolutionGate
One qubit Y Pauli gate.
- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> Y(0).to_matrix() array([[ 0.+0.j, -0.-1.j], [ 0.+1.j, 0.+0.j]])
- braket_gate
alias of
Y
- qiskit_gate
alias of
YGate
- matrix: ndarray[Any, dtype[complex64]] = array([[ 0.+0.j, -0.-1.j], [ 0.+1.j, 0.+0.j]])
Matricial semantics of the gate.
- class Z(target)[source]
Bases:
OneQubitNoParamGate
,InvolutionGate
One qubit Z Pauli gate.
- Parameters
target (int) – Index referring to the qubit on which the gate will be applied.
Example
>>> Z(0).to_matrix() array([[ 1, 0], [ 0, -1]])
- braket_gate
alias of
Z
- qiskit_gate
alias of
ZGate
- matrix: ndarray[Any, dtype[complex64]] = array([[ 1, 0], [ 0, -1]])
Matricial semantics of the gate.
- 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.