qpandalite.circuit_builder package#
Submodules#
qpandalite.circuit_builder.qcircuit module#
Quantum circuit builder with OriginIR and OpenQASM 2.0 output.
This module provides a Circuit class for building quantum circuits programmatically. It supports various quantum gates, controlled operations, dagger (adjoint) blocks, and measurement operations. The circuit can be exported to OriginIR or OpenQASM format.
- Key exports:
Circuit: Main quantum circuit builder class. OpcodeType: Type alias for opcode tuples.
- class qpandalite.circuit_builder.qcircuit.Circuit[源代码]
基类:
objectQuantum circuit builder that generates OriginIR and OpenQASM output.
- used_qubit_list
Qubits referenced in the circuit.
- Type:
list[int]
- circuit_str
Raw string builder used by context managers.
- Type:
str
- max_qubit
Highest qubit index used.
- Type:
int
- qubit_num
Total number of qubits.
- Type:
int
- cbit_num
Total number of classical bits.
- Type:
int
- measure_list
Qubits scheduled for measurement.
- Type:
list[int]
- opcode_list
Internal list of gate opcodes.
- Type:
list[OpCode]
- add_circuit(other)[源代码]
Add all gates from another circuit into this circuit.
- 参数:
other (Circuit)
- 返回类型:
None
- add_gate(operation, qubits, cbits=None, params=None, dagger=False, control_qubits=None)[源代码]
Add a gate to the circuit.
- 参数:
operation (str)
qubits (int | list[int])
cbits (int | list[int] | None)
params (float | list[float] | tuple[float, ...] | None)
dagger (bool)
control_qubits (int | list[int])
- 返回类型:
None
- barrier(*qubits)[源代码]
Insert a barrier across the specified qubits.
- 参数:
*qubits (int) -- Qubit indices to include in the barrier.
- 返回类型:
None
- cbit_num: int
- property circuit: str
Generate the circuit in OriginIR format.
- circuit_str: str
- cnot(controller, target)[源代码]
Apply CNOT (controlled-X) gate.
- 参数:
controller (int) -- Control qubit index.
target (int) -- Target qubit index.
- 返回类型:
None
- control(*args)[源代码]
Return a context manager that wraps gates in a CONTROL block.
All gates added inside the
withblock will be executed only when all specified control qubits are in state|1>.- 参数:
*args (int) -- One or more control qubit indices.
- 返回:
A
CircuitControlContextcontext manager.- 抛出:
ValueError -- No control qubits were supplied.
- 返回类型:
CircuitControlContext
- copy()[源代码]
Return a deep copy of this circuit.
- 返回类型:
Circuit
- cswap(q1, q2, q3)[源代码]
Apply CSWAP (Fredkin) gate to three qubits.
- 参数:
q1 (int)
q2 (int)
q3 (int)
- 返回类型:
None
- cx(controller, target)[源代码]
Apply CX gate (alias for CNOT).
- 参数:
controller (int) -- Control qubit index.
target (int) -- Target qubit index.
- 返回类型:
None
- cz(q1, q2)[源代码]
Apply controlled-Z gate to two qubits.
- 参数:
q1 (int)
q2 (int)
- 返回类型:
None
- dagger()[源代码]
Return a context manager that wraps gates in a DAGGER block.
All gates added inside the
withblock will be conjugate-transposed (adjoint).- 返回:
A
CircuitDagContextcontext manager.- 返回类型:
CircuitDagContext
- property depth: int
Calculate the depth of the quantum circuit.
- h(qn)[源代码]
Apply single-qubit Hadamard gate to qubit.
- 参数:
qn (int)
- 返回类型:
None
- identity(qn)[源代码]
Apply the identity (no-op) gate to qubit.
- 参数:
qn (int)
- 返回类型:
None
- iswap(q1, q2)[源代码]
Apply iSWAP gate to two qubits.
- 参数:
q1 (int)
q2 (int)
- 返回类型:
None
- max_qubit: int
- measure(*qubits)[源代码]
Schedule qubits for measurement.
Appends the given qubits to the measurement list. Multiple calls accumulate measurements; classical bit indices are assigned in the order qubits are added.
- 参数:
*qubits (int) -- One or more qubit indices to measure.
- 抛出:
ValueError -- Called inside an active CONTROL or DAGGER context block.
- 返回类型:
None
- measure_list: list[int]
- opcode_list: list[tuple[str, int | list[int], int | list[int] | None, float | list[float] | tuple[float, ...] | None, bool, int | list[int]]]
- property originir: str
Generate the circuit in OriginIR format.
- phase2q(q1, q2, theta1, theta2, thetazz)[源代码]
Apply two-qubit phase gate with local and ZZ terms.
- 参数:
q1 (int) -- First qubit index.
q2 (int) -- Second qubit index.
theta1 (float) -- Local phase angle for q1 in radians.
theta2 (float) -- Local phase angle for q2 in radians.
thetazz (float) -- ZZ interaction angle in radians.
- 返回类型:
None
- property qasm: str
Generate the circuit in OpenQASM format.
- qubit_num: int
- record_qubit(qubits)[源代码]
Record the qubits used in the circuit.
- 参数:
qubits (int | list[int])
- 返回类型:
None
- remapping(mapping)[源代码]
Create a new circuit with qubits remapped according to mapping.
- 参数:
mapping (dict[int, int])
- 返回类型:
Circuit
- rphi(qn, theta, phi)[源代码]
Apply RPhi rotation gate.
- 参数:
qn (int) -- Target qubit index.
theta (float) -- Polar rotation angle in radians.
phi (float) -- Azimuthal angle in radians.
- 返回类型:
None
- rx(qn, theta)[源代码]
Apply RX rotation gate.
- 参数:
qn (int) -- Target qubit index.
theta (float) -- Rotation angle in radians.
- 返回类型:
None
- ry(qn, theta)[源代码]
Apply RY rotation gate.
- 参数:
qn (int) -- Target qubit index.
theta (float) -- Rotation angle in radians.
- 返回类型:
None
- rz(qn, theta)[源代码]
Apply RZ rotation gate.
- 参数:
qn (int) -- Target qubit index.
theta (float) -- Rotation angle in radians.
- 返回类型:
None
- s(qn)[源代码]
Apply S (phase) gate to qubit.
- 参数:
qn (int)
- 返回类型:
None
- sdg(qn)[源代码]
Apply S-dagger (inverse phase) gate to qubit.
- 参数:
qn (int)
- 返回类型:
None
- set_control(*args)[源代码]
Manually open a CONTROL block (low-level API; prefer
control()).- 参数:
*args (int) -- Control qubit indices.
- 返回类型:
None
- set_dagger()[源代码]
Manually open a DAGGER block (low-level API; prefer
dagger()).- 返回类型:
None
- swap(q1, q2)[源代码]
Apply SWAP gate to two qubits.
- 参数:
q1 (int)
q2 (int)
- 返回类型:
None
- sx(qn)[源代码]
Apply square-root-of-X (SX) gate to qubit.
- 参数:
qn (int)
- 返回类型:
None
- sxdg(qn)[源代码]
Apply conjugate-transpose of SX gate to qubit.
- 参数:
qn (int)
- 返回类型:
None
- t(qn)[源代码]
Apply T gate to qubit.
- 参数:
qn (int)
- 返回类型:
None
- tdg(qn)[源代码]
Apply T-dagger (inverse T) gate to qubit.
- 参数:
qn (int)
- 返回类型:
None
- toffoli(q1, q2, q3)[源代码]
Apply Toffoli (CCNOT) gate to three qubits.
- 参数:
q1 (int)
q2 (int)
q3 (int)
- 返回类型:
None
- u1(qn, lam)[源代码]
Apply U1 single-parameter unitary gate.
- 参数:
qn (int) -- Target qubit index.
lam (float) -- Phase angle lambda in radians.
- 返回类型:
None
- u2(qn, phi, lam)[源代码]
Apply U2 two-parameter unitary gate.
- 参数:
qn (int) -- Target qubit index.
phi (float) -- Phi angle in radians.
lam (float) -- Lambda angle in radians.
- 返回类型:
None
- u3(qn, theta, phi, lam)[源代码]
Apply U3 three-parameter unitary gate.
- 参数:
qn (int) -- Target qubit index.
theta (float) -- Theta angle in radians.
phi (float) -- Phi angle in radians.
lam (float) -- Lambda angle in radians.
- 返回类型:
None
- unset_control()[源代码]
Manually close a CONTROL block (low-level API; prefer
control()).- 返回类型:
None
- unset_dagger()[源代码]
Manually close a DAGGER block (low-level API; prefer
dagger()).- 返回类型:
None
- used_qubit_list: list[int]
- uu15(q1, q2, params)[源代码]
Apply general two-qubit UU15 gate with 15 parameters.
- 参数:
q1 (int) -- First qubit index.
q2 (int) -- Second qubit index.
params (list[float]) -- List of 15 rotation parameters in radians.
- 返回类型:
None
- x(qn)[源代码]
Apply Pauli-X (NOT) gate to qubit.
- 参数:
qn (int)
- 返回类型:
None
- xx(q1, q2, theta)[源代码]
Apply XX Ising interaction gate.
- 参数:
q1 (int) -- First qubit index.
q2 (int) -- Second qubit index.
theta (float) -- Interaction angle in radians.
- 返回类型:
None
- y(qn)[源代码]
Apply Pauli-Y gate to qubit.
- 参数:
qn (int)
- 返回类型:
None
- yy(q1, q2, theta)[源代码]
Apply YY Ising interaction gate.
- 参数:
q1 (int) -- First qubit index.
q2 (int) -- Second qubit index.
theta (float) -- Interaction angle in radians.
- 返回类型:
None
- z(qn)[源代码]
Apply Pauli-Z gate to qubit.
- 参数:
qn (int)
- 返回类型:
None
- zz(q1, q2, theta)[源代码]
Apply ZZ Ising interaction gate.
- 参数:
q1 (int) -- First qubit index.
q2 (int) -- Second qubit index.
theta (float) -- Interaction angle in radians.
- 返回类型:
None