quairkit.circuit¶
The source file of the Circuit class.
-
class quairkit.circuit.Circuit(num_systems=
None
, system_dim=2
)¶ Quantum circuit.
- Parameters:¶
Note
when the number of system is unknown and system_dim is an int, the circuit is a dynamic quantum circuit.
- property num_qubits : int¶
Number of qubits.
- property num_qutrits : int¶
Number of qutrits.
- property isdynamic : bool¶
Whether the circuit is dynamic
- property num_systems : int¶
Number of systems.
- property system_dim : list[int] | int¶
Dimension of systems.
- unitary_matrix()¶
Get the unitary matrix form of the circuit.
- property gate_history : list[dict[str, str | list[int] | Tensor]]¶
List of gates information of circuit
- Returns:¶
history of quantum gates of circuit
- property depth : int¶
Depth of gate sequences.
- Returns:¶
depth of this circuit
Note
The measurement is omitted, and all gates are assumed to have depth 1. See Niel’s answer in the [StackExchange](https://quantumcomputing.stackexchange.com/a/5772).
- property system_history : list[list[tuple[dict[str, str | list[int] | Tensor], int]]]¶
gate information on each system
- Returns:¶
list of gate history on each system
Note
The entry
system_history[i][j][0/1]
returns the gate information / gate index of the j-th gate applied on the i-th system.
-
plot(save_path=
None
, dpi=100
, show=True
, output=False
, scale=1.0
, tex=False
)¶ display the circuit using matplotlib
- Parameters:¶
- save_path : str | None¶
the save path of image
- dpi : int | None¶
dots per inches, here is resolution ratio
- show : bool | None¶
whether execute
plt.show()
- output : bool | None¶
whether return the
matplotlib.figure.Figure
instance- scale : float | None¶
scale coefficient of figure, default to 1.0
- tex : bool | None¶
a bool flag which controls latex fonts of gate display, default to
False
.
- Returns:¶
a
matplotlib.figure.Figure
instance orNone
depends onoutput
- Return type:¶
None | Figure
Note
Using
plt.show()
may cause a distortion, but it will not happen in the figure saved. If the depth is too long, there will be some patches unable to display. Settingtex = True
requires that you have TeX and the other dependencies properly installed on your system. See https://matplotlib.org/stable/gallery/text_labels_and_annotations/tex_demo.html for more details.
-
h(qubits_idx=
'full'
)¶ Add single-qubit Hadamard gates.
The matrix form of such a gate is:
-
s(qubits_idx=
'full'
)¶ Add single-qubit S gates.
The matrix form of such a gate is:
-
sdg(qubits_idx=
'full'
)¶ Add single-qubit S dagger (S inverse) gates.
The matrix form of such a gate is:
-
t(qubits_idx=
'full'
)¶ Add single-qubit T gates.
The matrix form of such a gate is:
-
tdg(qubits_idx=
'full'
)¶ Add single-qubit T dagger (T inverse) gates.
The matrix form of such a gate is:
-
x(qubits_idx=
'full'
)¶ Add single-qubit X gates.
The matrix form of such a gate is:
-
y(qubits_idx=
'full'
)¶ Add single-qubit Y gates.
The matrix form of such a gate is:
-
z(qubits_idx=
'full'
)¶ Add single-qubit Z gates.
The matrix form of such a gate is:
-
p(qubits_idx=
'full'
, param=None
, param_sharing=False
)¶ Add single-qubit P gates.
The matrix form of such a gate is:
-
rx(qubits_idx=
'full'
, param=None
, param_sharing=False
)¶ Add single-qubit rotation gates about the x-axis.
The matrix form of such a gate is:
-
ry(qubits_idx=
'full'
, param=None
, param_sharing=False
)¶ Add single-qubit rotation gates about the y-axis.
The matrix form of such a gate is:
-
rz(qubits_idx=
'full'
, param=None
, param_sharing=False
)¶ Add single-qubit rotation gates about the z-axis.
The matrix form of such a gate is:
-
u3(qubits_idx=
'full'
, param=None
, param_sharing=False
)¶ Add single-qubit rotation gates.
The matrix form of such a gate is:
-
cnot(qubits_idx=
'cycle'
)¶ Add CNOT gates.
For a 2-qubit quantum circuit, when qubits_idx is [0, 1], the matrix form of such a gate is:
-
cy(qubits_idx=
'cycle'
)¶ Add controlled Y gates.
For a 2-qubit quantum circuit, when qubits_idx is [0, 1], the matrix form of such a gate is:
-
cz(qubits_idx=
'linear'
)¶ Add controlled Z gates.
For a 2-qubit quantum circuit, when qubits_idx is [0, 1], the matrix form of such a gate is:
-
swap(qubits_idx=
'linear'
)¶ Add SWAP gates.
The matrix form of such a gate is:
-
cp(qubits_idx=
'cycle'
, param=None
, param_sharing=False
)¶ Add controlled P gates.
For a 2-qubit quantum circuit, when qubits_idx is [0, 1], the matrix form of such a gate is:
-
crx(qubits_idx=
'cycle'
, param=None
, param_sharing=False
)¶ Add controlled rotation gates about the x-axis.
For a 2-qubit quantum circuit, when qubits_idx is [0, 1], the matrix form of such a gate is:
-
cry(qubits_idx=
'cycle'
, param=None
, param_sharing=False
)¶ Add controlled rotation gates about the y-axis.
For a 2-qubit quantum circuit, when qubits_idx is [0, 1], the matrix form of such a gate is:
-
crz(qubits_idx=
'cycle'
, param=None
, param_sharing=False
)¶ Add controlled rotation gates about the z-axis.
For a 2-qubit quantum circuit, when qubits_idx is [0, 1], the matrix form of such a gate is:
-
cu(qubits_idx=
'cycle'
, param=None
, param_sharing=False
)¶ Add controlled single-qubit rotation gates.
For a 2-qubit quantum circuit, when qubits_idx is [0, 1], the matrix form of such a gate is:
-
rxx(qubits_idx=
'linear'
, param=None
, param_sharing=False
)¶ Add RXX gates.
The matrix form of such a gate is:
-
ryy(qubits_idx=
'linear'
, param=None
, param_sharing=False
)¶ Add RYY gates.
The matrix form of such a gate is:
-
rzz(qubits_idx=
'linear'
, param=None
, param_sharing=False
)¶ Add RZZ gates.
The matrix form of such a gate is:
-
ms(qubits_idx=
'cycle'
)¶ Add Mølmer-Sørensen (MS) gates.
The matrix form of such a gate is:
-
cswap(qubits_idx=
'cycle'
)¶ Add CSWAP (Fredkin) gates.
The matrix form of such a gate is:
-
ccx(qubits_idx=
'cycle'
)¶ Add CCX (Toffoli) gates.
The matrix form of such a gate is:
-
universal_two_qubits(qubits_idx=
'linear'
, param=None
, param_sharing=False
)¶ Add universal two-qubit gates. One of such a gate requires 15 parameters.
-
universal_three_qubits(qubits_idx=
'linear'
, param=None
, param_sharing=False
)¶ Add universal three-qubit gates. One of such a gate requires 81 parameters.
-
universal_qudits(system_idx, param=
None
, param_sharing=False
)¶ Add universal qudit gates. One of such a gate requires
parameters, where is the gate dimension.
-
oracle(oracle, system_idx, gate_name=
'O'
, latex_name=None
, plot_width=None
)¶ Add an oracle gate.
- Parameters:¶
- oracle : Tensor¶
Unitary oracle to be implemented.
- system_idx : List[int] | int¶
Indices of the systems on which the gates are applied.
- gate_name : str | None¶
name of this oracle.
- latex_name : str | None¶
latex name of this oracle, default to be the gate name.
- plot_width : float | None¶
width of this gate in circuit plot, default to be proportional with the gate name.
-
control_oracle(oracle, system_idx, proj=
None
, gate_name='O'
, latex_name=None
, plot_width=None
)¶ Add a controlled oracle gate.
- Parameters:¶
- oracle : Tensor¶
Unitary oracle to be implemented.
- system_idx : List[List[int] | int]¶
Indices of the systems on which the gates are applied. The first element in the list is the control system, defaulting to the
state as the control qubit, while the remaining elements represent the oracle system.- proj : Tensor¶
Projector matrix for the control qubit. Defaults to
None
- gate_name : str | None¶
name of this oracle.
- latex_name : str | None¶
latex name of this oracle, default to be the gate name.
- plot_width : float | None¶
width of this gate in circuit plot, default to be proportional with the gate name.
-
param_oracle(generator, num_acted_param, system_idx, param=
None
, gate_name='P'
, latex_name=None
, plot_width=None
)¶ Add a parameterized oracle gate.
- Parameters:¶
- generator : Callable[[Tensor], Tensor]¶
function that generates the oracle.
- num_acted_param : int¶
the number of parameters required for a single operation.
- system_idx : List[int] | int¶
indices of the system on which this gate acts on.
- param : Tensor | float¶
input parameters of quantum parameterized gates. Defaults to
None
i.e. randomized.- gate_name : str | None¶
name of this oracle.
- latex_name : str | None¶
latex name of this oracle, default to be the gate name.
- plot_width : float | None¶
width of this gate in circuit plot, default to be proportional with the gate name.
-
measure(system_idx=
None
, post_selection=None
, if_print=False
, measure_basis=None
)¶ - Parameters:¶
- system_idx : Iterable[int] | int | str¶
list of systems to be measured. Defaults to all qubits.
- post_selection : int | str¶
the post selection result after measurement. Defaults to
None
meaning preserving all measurement outcomes.- if_print : bool¶
whether print the information about the collapsed state. Defaults to
False
.- measure_basis : Tensor | None¶
The basis of the measurement. The quantum state will collapse to the corresponding eigenstate.
Note
When desired_result is None, collapse is equivalent to mid-circuit measurement.
-
superposition_layer(qubits_idx=
None
)¶ Add layers of Hadamard gates.
-
weak_superposition_layer(qubits_idx=
None
)¶ Add layers of Ry gates with a rotation angle
.
-
linear_entangled_layer(qubits_idx=
None
, depth=1
)¶ Add linear entangled layers consisting of Ry gates, Rz gates, and CNOT gates.
-
real_entangled_layer(qubits_idx=
None
, depth=1
)¶ Add strongly entangled layers consisting of Ry gates and CNOT gates.
-
complex_entangled_layer(qubits_idx=
None
, depth=1
)¶ Add strongly entangled layers consisting of single-qubit rotation gates and CNOT gates.
-
real_block_layer(qubits_idx=
None
, depth=1
)¶ Add weakly entangled layers consisting of Ry gates and CNOT gates.
-
complex_block_layer(qubits_idx=
None
, depth=1
)¶ Add weakly entangled layers consisting of single-qubit rotation gates and CNOT gates.
-
bit_flip(prob, qubits_idx=
'full'
)¶ Add bit flip channels.
-
phase_flip(prob, qubits_idx=
'full'
)¶ Add phase flip channels.
-
bit_phase_flip(prob, qubits_idx=
'full'
)¶ Add bit phase flip channels.
-
amplitude_damping(gamma, qubits_idx=
'full'
)¶ Add amplitude damping channels.
-
generalized_amplitude_damping(gamma, prob, qubits_idx=
'full'
)¶ Add generalized amplitude damping channels.
-
phase_damping(gamma, qubits_idx=
'full'
)¶ Add phase damping channels.
-
depolarizing(prob, qubits_idx=
'full'
)¶ Add depolarizing channels.
- generalized_depolarizing(prob, qubits_idx)¶
Add a general depolarizing channel.
-
pauli_channel(prob, qubits_idx=
'full'
)¶ Add Pauli channels.
-
reset_channel(prob, qubits_idx=
'full'
)¶ Add reset channels.
-
thermal_relaxation(const_t, exec_time, qubits_idx=
'full'
)¶ Add thermal relaxation channels.
- Parameters:¶
- choi_channel(choi_repr, system_idx)¶
Add custom channels in the Choi representation.
- kraus_channel(kraus_oper, system_idx)¶
Add custom channels in the Kraus representation.
- stinespring_channel(stinespring_repr, system_idx)¶
Add custom channels in the Stinespring representation.
- locc(local_unitary, system_idx)¶
Add a one-way local operation and classical communication (LOCC) protocol comprised of unitary operations.