uniqc.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 uniqc.circuit_builder.qcircuit.Circuit(qregs=None)[source]#
Bases:
objectQuantum circuit builder that generates OriginIR and OpenQASM output.
- Variables:
used_qubit_list (list[int]) – Qubits referenced in the circuit.
circuit_str (str) – Raw string builder used by context managers.
max_qubit (int) – Highest qubit index used.
qubit_num (int) – Total number of qubits.
cbit_num (int) – Total number of classical bits.
measure_list (list[int]) – Qubits scheduled for measurement.
opcode_list (list[OpCode]) – Internal list of gate opcodes.
_qregs (dict[str, QReg]) – Named quantum registers (if created with qregs parameter).
- Parameters:
- add_circuit(other)[source]#
Add all gates from another circuit into this circuit.
- Parameters:
other (Circuit)
- Return type:
None
- add_gate(operation, qubits, cbits=None, params=None, dagger=False, control_qubits=None)[source]#
Add a gate to the circuit.
- Parameters:
operation (str) – Gate name (e.g., “H”, “CNOT”, “RX”)
qubits (QubitInput) – Target qubit(s) - can be int, Qubit, QRegSlice, or list
cbits (CbitSpec) – Classical bit(s) for measurement
params (ParamSpec) – Gate parameters
dagger (bool) – Whether to apply dagger (adjoint)
control_qubits (QubitInput) – Control qubit(s)
- Return type:
None
- barrier(*qubits)[source]#
Insert a barrier across the specified qubits.
- Parameters:
*qubits (QubitInput) – Qubits to include in the barrier.
- Return type:
None
- cnot(controller, target)[source]#
Apply CNOT (controlled-X) gate.
- Parameters:
controller (QubitInput) – Control qubit - can be int, Qubit, or QRegSlice
target (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- control(*args)[source]#
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>.- Parameters:
*args (QubitInput) – One or more control qubits - can be int, Qubit, or QRegSlice
- Returns:
A
CircuitControlContextcontext manager.- Raises:
ValueError – No control qubits were supplied.
- Return type:
- cswap(q1, q2, q3)[source]#
Apply CSWAP (Fredkin) gate to three qubits.
- Parameters:
q1 (QubitInput) – Control qubit - can be int, Qubit, or QRegSlice
q2 (QubitInput) – First target qubit
q3 (QubitInput) – Second target qubit
- Return type:
None
- cx(controller, target)[source]#
Apply CX gate (alias for CNOT).
- Parameters:
controller (QubitInput) – Control qubit - can be int, Qubit, or QRegSlice
target (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- cz(q1, q2)[source]#
Apply controlled-Z gate to two qubits.
- Parameters:
q1 (QubitInput) – First qubit - can be int, Qubit, or QRegSlice
q2 (QubitInput) – Second qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- dagger()[source]#
Return a context manager that wraps gates in a DAGGER block.
All gates added inside the
withblock will be conjugate-transposed (adjoint).- Returns:
A
CircuitDagContextcontext manager.- Return type:
- h(qn)[source]#
Apply single-qubit Hadamard gate to qubit.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- identity(qn)[source]#
Apply the identity (no-op) gate to qubit.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- iswap(q1, q2)[source]#
Apply iSWAP gate to two qubits.
- Parameters:
q1 (QubitInput) – First qubit - can be int, Qubit, or QRegSlice
q2 (QubitInput) – Second qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- measure(*qubits)[source]#
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.
- Parameters:
*qubits (QubitInput) – One or more qubits to measure - can be int, Qubit, or QRegSlice
- Raises:
ValueError – Called inside an active CONTROL or DAGGER context block.
- Return type:
None
- phase2q(q1, q2, theta1, theta2, thetazz)[source]#
Apply two-qubit phase gate with local and ZZ terms.
- Parameters:
- Return type:
None
- rx(qn, theta)[source]#
Apply RX rotation gate.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
theta (float) – Rotation angle in radians.
- Return type:
None
- ry(qn, theta)[source]#
Apply RY rotation gate.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
theta (float) – Rotation angle in radians.
- Return type:
None
- rz(qn, theta)[source]#
Apply RZ rotation gate.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
theta (float) – Rotation angle in radians.
- Return type:
None
- s(qn)[source]#
Apply S (phase) gate to qubit.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- sdg(qn)[source]#
Apply S-dagger (inverse phase) gate to qubit.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- set_control(*args)[source]#
Manually open a CONTROL block (low-level API; prefer
control()).- Parameters:
*args (QubitInput) – Control qubits - can be int, Qubit, or QRegSlice
- Return type:
None
- set_dagger()[source]#
Manually open a DAGGER block (low-level API; prefer
dagger()).- Return type:
None
- swap(q1, q2)[source]#
Apply SWAP gate to two qubits.
- Parameters:
q1 (QubitInput) – First qubit - can be int, Qubit, or QRegSlice
q2 (QubitInput) – Second qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- sx(qn)[source]#
Apply square-root-of-X (SX) gate to qubit.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- sxdg(qn)[source]#
Apply conjugate-transpose of SX gate to qubit.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- t(qn)[source]#
Apply T gate to qubit.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- tdg(qn)[source]#
Apply T-dagger (inverse T) gate to qubit.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- toffoli(q1, q2, q3)[source]#
Apply Toffoli (CCNOT) gate to three qubits.
- Parameters:
q1 (QubitInput) – First control qubit
q2 (QubitInput) – Second control qubit
q3 (QubitInput) – Target qubit
- Return type:
None
- u1(qn, lam)[source]#
Apply U1 single-parameter unitary gate.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
lam (float) – Phase angle lambda in radians.
- Return type:
None
- unset_control()[source]#
Manually close a CONTROL block (low-level API; prefer
control()).- Return type:
None
- unset_dagger()[source]#
Manually close a DAGGER block (low-level API; prefer
dagger()).- Return type:
None
- x(qn)[source]#
Apply Pauli-X (NOT) gate to qubit.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- xx(q1, q2, theta)[source]#
Apply XX Ising interaction gate.
- Parameters:
q1 (QubitInput) – First qubit - can be int, Qubit, or QRegSlice
q2 (QubitInput) – Second qubit - can be int, Qubit, or QRegSlice
theta (float) – Interaction angle in radians.
- Return type:
None
- y(qn)[source]#
Apply Pauli-Y gate to qubit.
- Parameters:
qn (QubitInput) – Target qubit - can be int, Qubit, or QRegSlice
- Return type:
None
- yy(q1, q2, theta)[source]#
Apply YY Ising interaction gate.
- Parameters:
q1 (QubitInput) – First qubit - can be int, Qubit, or QRegSlice
q2 (QubitInput) – Second qubit - can be int, Qubit, or QRegSlice
theta (float) – Interaction angle in radians.
- Return type:
None