paddle_quantum.mbqc.qobject
This module contains the commonly used class in quantum information, e.g. quantum state, quantum circuit and measurement patterns.
- class paddle_quantum.mbqc.qobject.State(vector=None, system=None)
Bases:
object
Define the quantum state.
- vector
the column vector of the quantum state.
- Type:
Tensor
- system
the list of system labels of the quantum state.
- Type:
list
- class paddle_quantum.mbqc.qobject.Circuit(width)
Bases:
object
Define the quantum circuit.
Note
This class is similar to
UAnsatz
, one can imitate the use ofUAnsatz
to instantiate this class and construct the circuit diagram.Warning
The current version supports the quantum operations(gates and measurement) in
H, X, Y, Z, S, T, Rx, Ry, Rz, Rz_5, U, CNOT, CNOT_15, CZ
.- width
The width of the circuit(number of qubits).
- Type:
int
- h(which_qubit)
Add a
Hadamard
gate.The matrix form:
\[\begin{split}\frac{1}{\sqrt{2}}\begin{bmatrix} 1&1\\1&-1 \end{bmatrix}\end{split}\]- Parameters:
which_qubit (int) – The number(No.) of the qubit that the gate applies to.
Code example:
from paddle_quantum.mbqc.qobject import Circuit width = 1 cir = Circuit(width) which_qubit = 0 cir.h(which_qubit) print(cir.get_circuit())
[['h', [0], None]]
- x(which_qubit)
Add a
Pauli X
gate.The matrix form:
\[\begin{split}\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\end{split}\]- Parameters:
which_qubit (int) – The number(No.) of the qubit that the gate applies to.
Code example:
from paddle_quantum.mbqc.qobject import Circuit width = 1 cir = Circuit(width) which_qubit = 0 cir.x(which_qubit) print(cir.get_circuit())
[['x', [0], None]]
- y(which_qubit)
Add a
Pauli Y
gate.The matrix form:
\[\begin{split}\begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}\end{split}\]- Parameters:
which_qubit (int) – The number(No.) of the qubit that the gate applies to.
Code example:
from paddle_quantum.mbqc.qobject import Circuit width = 1 cir = Circuit(width) which_qubit = 0 cir.y(which_qubit) print(cir.get_circuit())
[['y', [0], None]]
- z(which_qubit)
Add a
Pauli Z
gate.The matrix form:
\[\begin{split}\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\end{split}\]- Parameters:
which_qubit (int) – The number(No.) of the qubit that the gate applies to.
Code example:
from paddle_quantum.mbqc.qobject import Circuit width = 1 cir = Circuit(width) which_qubit = 0 cir.z(which_qubit) print(cir.get_circuit())
[['z', [0], None]]
- s(which_qubit)
Add a
S
gate.The matrix form:
\[\begin{split}\begin{bmatrix} 1&0\\0& i \end{bmatrix}\end{split}\]- Parameters:
which_qubit (int) – The number(No.) of the qubit that the gate applies to.
Code example:
from paddle_quantum.mbqc.qobject import Circuit width = 1 cir = Circuit(width) which_qubit = 0 cir.s(which_qubit) print(cir.get_circuit())
[['s', [0], None]]
- t(which_qubit)
Add
T
gate.The matrix form:
\[\begin{split}\begin{bmatrix} 1&0\\0& e^{i\pi/ 4} \end{bmatrix}\end{split}\]- Parameters:
which_qubit (int) – The number(No.) of the qubit that the gate applies to.
Code example:
from paddle_quantum.mbqc.qobject import Circuit width = 1 cir = Circuit(width) which_qubit = 0 cir.t(which_qubit) print(cir.get_circuit())
[['t', [0], None]]
- rx(theta, which_qubit)
Add a rotation gate in x direction.
The matrix form:
\[\begin{split}\begin{bmatrix} \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]- Parameters:
theta (Tensor) – rotation angle
which_qubit (int) – The number(No.) of the qubit that the gate applies to.
Code example:
from paddle import to_tensor from paddle_quantum.mbqc.qobject import Circuit width = 1 cir = Circuit(width) which_qubit = 0 angle = to_tensor([1], dtype='float64') cir.rx(angle, which_qubit) print(cir.get_circuit())
[['rx', [0], Tensor(shape=[1], dtype=float64, place=CPUPlace, stop_gradient=True, [1.])]]
- ry(theta, which_qubit)
Add a rotation gate in y direction.
The matrix form:
\[\begin{split}\begin{bmatrix} \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]- Parameters:
theta (Tensor) – rotation angle
which_qubit (int) – The number(No.) of the qubit that the gate applies to.
Code example:
from paddle import to_tensor from paddle_quantum.mbqc.qobject import Circuit width = 1 cir = Circuit(width) which_qubit = 0 angle = to_tensor([1], dtype='float64') cir.ry(angle, which_qubit) print(cir.get_circuit())
[['ry', [0], Tensor(shape=[1], dtype=float64, place=CPUPlace, stop_gradient=True, [1.])]]
- rz(theta, which_qubit)
Add a rotation gate in z direction.
The matrix form:
\[\begin{split}\begin{bmatrix} 1 & 0 \\ 0 & e^{i\theta} \end{bmatrix}\end{split}\]- Parameters:
theta (Tensor) – rotation angle
which_qubit (int) – The number(No.) of the qubit that the gate applies to.
Code example:
from paddle import to_tensor from paddle_quantum.mbqc.qobject import Circuit width = 1 cir = Circuit(width) which_qubit = 0 angle = to_tensor([1], dtype='float64') cir.rz(angle, which_qubit) print(cir.get_circuit())
[['rz', [0], Tensor(shape=[1], dtype=float64, place=CPUPlace, stop_gradient=True, [1.])]]
- rz_5(theta, which_qubit)
Add a rotation gate in the z direction(the measurement pattern corresponding to this gate is composed of five qubits).
The matrix form:
\[\begin{split}\begin{bmatrix} 1 & 0 \\ 0 & e^{i\theta} \end{bmatrix}\end{split}\]- Parameters:
theta (Tensor) – rotation angle
which_qubit (int) – The number(No.) of the qubit that the gate applies to.
Code example:
from paddle import to_tensor from paddle_quantum.mbqc.qobject import Circuit width = 1 cir = Circuit(width) which_qubit = 0 angle = to_tensor([1], dtype='float64') cir.rz(angle, which_qubit) print(cir.get_circuit())
[['rz_5', [0], Tensor(shape=[1], dtype=float64, place=CPUPlace, stop_gradient=True, [1.])]]
- u(params, which_qubit)
Add a general single qubit gate.
Warning
Different from the 3 parameters of the U3 gate in
UAnsatz
class, the unitary here adopts aRz Rx Rz
decomposition.The decomposition takes the form:
\[U(\alpha, \beta, \gamma) = Rz(\gamma) Rx(\beta) Rz(\alpha)\]- Parameters:
params (list) – three rotation angles of the unitary gate
which_qubit (int) – The number(No.) of the qubit that the gate applies to.
Code example:
from paddle import to_tensor from numpy import pi from paddle_quantum.mbqc.qobject import Circuit width = 1 cir = Circuit(width) which_qubit = 0 alpha = to_tensor([pi / 2], dtype='float64') beta = to_tensor([pi], dtype='float64') gamma = to_tensor([- pi / 2], dtype='float64') cir.u([alpha, beta, gamma], which_qubit) print(cir.get_circuit())
[['u', [0], [Tensor(shape=[1], dtype=float64, place=CPUPlace, stop_gradient=True, [1.57079633]), Tensor(shape=[1], dtype=float64, place=CPUPlace, stop_gradient=True, [3.14159265]), Tensor(shape=[1], dtype=float64, place=CPUPlace, stop_gradient=True, [-1.57079633])]]]
- cnot(which_qubits)
Add a CNOT gate.
When
which_qubits
is[0, 1]
,the matrix form is:\[\begin{split}\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}\end{split}\]- Parameters:
which_qubits (list) – A two element list contains the qubits that the CNOT gate applies to, the first element is the control qubit, the second is the target qubit.
Code example:
from paddle_quantum.mbqc.qobject import Circuit width = 2 cir = Circuit(width) which_qubits = [0, 1] cir.cnot(which_qubits) print(cir.get_circuit())
[['cnot', [0, 1], None]]
- cnot_15(which_qubits)
Add a CNOT gate(the measurement pattern corresponding to this gate is composed of 15 qubits).
When
which_qubits
is[0, 1]
,the matrix form is:\[\begin{split}\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}\end{split}\]- Parameters:
which_qubits (list) – A two element list contains the qubits that the CNOT gate applies to, the first element is the control qubit, the second is the target qubit.
Code example:
from paddle_quantum.mbqc.qobject import Circuit width = 2 cir = Circuit(width) which_qubits = [0, 1] cir.cnot_15(which_qubits) print(cir.get_circuit())
[['cnot_15', [0, 1], None]]
- cz(which_qubits)
Add a controlled-Z gate.
When
which_qubits
is[0, 1]
,the matrix form is:\[\begin{split}\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{bmatrix}\end{split}\]- Parameters:
which_qubits (list) – A two element list contains the qubits that the CZ gate applies to, the first element is the control qubit, the second is the target qubit.
Code example:
from paddle_quantum.mbqc.qobject import Circuit width = 2 cir = Circuit(width) which_qubits = [0, 1] cir.cz(which_qubits) print(cir.get_circuit())
[['cz', [0, 1], None]]
- measure(which_qubit=None, basis_list=None)
Measure the output state of the quantum circuit.
Note
Unlike the measurement operations in the
UAnsatz
class, besides the default measurement in Z basis, the users can define the measurement ways themselves by providing the measurement basis and the qubits to be measured.Warning
There are three kinds of inputs: 1. which_qubit=None,basis_list=None(default): measure all the qubits in Z basis. 2. which_qubit=input, basis_list=None: measure the input qubit in Z basis. 3. which_qubit=input1, basis_list=input2: measure the input1 qubit by the basis in input2. If the users want to define the measurement basis themselves, the input format looks like:
[angle,plane,domain_s,domain_t]
. By the way,in the current version paddle_quantum, theplane
parameter can only takeXY
orYZ
.- Parameters:
which_qubit (int, optional) – the qubit to be measured
basis_list (list, optional) – measurement basis
- is_valid()
Check that if the circuit is valid.
We require that for each qubit in the quantum circuit, at least one quantum gate should be applied to it.
- Returns:
the boolean values of whether the quantum circuit is valid.
- Return type:
bool
- get_width()
Return the width of the quantum circuit.
- Returns:
the width of the quantum circuit.
- Return type:
int
- get_circuit()
Return the list of quantum circuits.
- Returns:
the list of quantum circuits
- Return type:
list
- get_measured_qubits()
Return the list of measured qubits in the quantum circuit.
- Returns:
the list of measured qubits in the quantum circuit.
- Return type:
list
- print_circuit_list()
Print the list of the circuit.
- Returns:
the strings to be printed
- Return type:
string
Code example:
from paddle_quantum.mbqc.qobject import Circuit from paddle import to_tensor from numpy import pi n = 2 theta = to_tensor([pi], dtype="float64") cir = Circuit(n) cir.h(0) cir.cnot([0, 1]) cir.rx(theta, 1) cir.measure() cir.print_circuit_list()
-------------------------------------------------- Current circuit -------------------------------------------------- Gate Name Qubit Index Parameter -------------------------------------------------- h [0] None cnot [0, 1] None rx [1] 3.141592653589793 m [0] [0.0, 'YZ', [], []] m [1] [0.0, 'YZ', [], []] --------------------------------------------------
- class paddle_quantum.mbqc.qobject.Pattern(name, space, input_, output_, commands)
Bases:
object
Define the measurement pattern.
see more details of the measurement pattern in [The measurement calculus, arXiv: 0704.1263].
- name
the name of the measurement pattern.
- Type:
str
- space
the list contains all the nodes of the measurement pattern.
- Type:
list
- input_
the list contains the input nodes of the measurement pattern.
- Type:
list
- output_
the list contains the output nodes of the measurement pattern.
- Type:
list
- commands
the list contains the commands of the measurement pattern.
- Type:
list
- class CommandE(which_qubits)
Bases:
object
Define the class
CommandE
corresponding to some entanglement commands.Note
The entanglement command here corresponds to the controlled-Z gate.
- which_qubits
A two-element list contains the labels of the node
- Type:
list
- that the entanglement command applies to.
- class CommandM(which_qubit, angle, plane, domain_s, domain_t)
Bases:
object
Define the
CommandM
class corresponding to the measurement commands.CommandM
has 5 attributes, including:1.which_qubit: the qubit to be measured. 2.angle: the original measurement angle. 3.plane: the measurement plane. 4.domain_s: the node list corresponding to domain s. 5.domain_t: the node list corresponding to domain t.
The original angle \(\alpha\) is transformed into \(\theta\) as: .. math:
\theta = (-1)^s \times \alpha + t \times \pi
after considering the node dependence in the domain.
Note
Domain s(domain t) is the concept in MBQC containing the influence on the measurement angles induced by Pauli X(Pauli Z) operator. Both of them record the dependence of the measurement node on the measurement results of other nodes.
Warning
Only measurements in “XY” and “YZ” planes are allowed in this version.
- which_qubit
the qubit to be measured.
- Type:
any
- angle
the original measurement angle.
- Type:
Tensor
- plane
the measurement plane.
- Type:
str
- domain_s
the node list corresponding to domain s.
- Type:
list
- domain_t
the node list corresponding to domain t.
- Type:
list
- class CommandX(which_qubit, domain)
Bases:
object
Define the
CommandX
class used for correcting the byproduct induced by Pauli X.- which_qubit
the label of the qubit that the correcting operator applies to.
- Type:
any
- domain
the list contains the dependence relation.
- Type:
list
- class CommandZ(which_qubit, domain)
Bases:
object
Define the
CommandZ
class used for correcting the byproduct induced by Pauli Z.- which_qubit
the label of the qubit that the correcting operator applies to.
- Type:
any
- domain
the list contains the dependence relation.
- Type:
list
- class CommandS(which_qubit, domain)
Bases:
object
Define the
CommandS
class used for signal shifting.Note
Signal shifting is a class of special operations used for eliminating the measurement operations’ dependence on the nodes in domain t and simplify the measurement patterns on some conditions.
- which_qubit
the labels of the nodes applied by the signal shifting operations.
- Type:
any
- domain
the list contains the dependence relation.
- Type:
list
- print_command_list()
Print the information of commands in the
Pattern
class.Code example:
from paddle_quantum.mbqc.qobject import Circuit from paddle_quantum.mbqc.mcalculus import MCalculus n = 1 cir = Circuit(n) cir.h(0) pat = MCalculus() pat.set_circuit(cir) pattern = pat.get_pattern() pattern.print_command_list()
----------------------------------------------------------- Current Command List ----------------------------------------------------------- Command: E which_qubits: [('0/1', '0/1'), ('0/1', '1/1')] ----------------------------------------------------------- Command: M which_qubit: ('0/1', '0/1') plane: XY angle: 0.0 domain_s: [] domain_t: [] ----------------------------------------------------------- Command: X which_qubit: ('0/1', '1/1') domain: [('0/1', '0/1')] -----------------------------------------------------------