Circuits
qsimlab.circuit
¶
Circuit construction: Circuit, the gate table and instructions.
Conventions (python/API.md §5): qubit q is bit q of a basis-state
index (little-endian, as in Qiskit); two-qubit gates take the control first;
angles are in radians; measurement k (program order) writes classical
bit k.
>>> from qsimlab import Circuit
>>> c = Circuit(3).h(0).cx(0, 1).cx(1, 2).measure_all()
>>> c.stats()["gates_2q"], c.num_measurements
(2, 3)
>>> print(Circuit(2).h(0).cx(0, 1).draw())
q0 : ── [H] ── ■ ────
q1 : ───────── X ────
Instruction
¶
Bases: NamedTuple
One operation of a circuit, as returned by Circuit.instructions.
name is a gate name from GATES, "measure", "reset" or a
noise channel; params holds angles (gates) or the probability (noise);
c_if is None or (measurement_index, value).
Circuit
¶
A quantum circuit on num_qubits qubits, all starting in |0>.
Builder methods return self so they chain. Every gate method accepts
c_if= to condition it on a measurement: c_if=k applies the gate if
measurement k read 1, c_if=(k, 0) if it read 0.
>>> c = Circuit(2)
>>> c.h(0).measure(0).x(1, c_if=0).measure(1)
Circuit(num_qubits=2, ops=4, measurements=2)
global_phase
property
writable
¶
Global phase in radians; every amplitude is multiplied by exp(1j * global_phase).
readout_error
property
writable
¶
Probability that each measurement result is reported flipped (Stim M(p)).
detectors
property
¶
Detectors (lists of absolute measurement indices), for QEC circuits.
observables
property
¶
observables[k]: measurement indices whose parity is logical observable k.
from_instructions
classmethod
¶
stats
¶
append
¶
p
¶
Phase gate diag(1, e^{iθ}) (OpenQASM u1/p).
u
¶
OpenQASM u3(θ, φ, λ) = [[cos θ/2, -e^{iλ} sin θ/2], [e^{iφ} sin θ/2, e^{i(φ+λ)} cos θ/2]].
iswap
¶
iSWAP (|01> -> i|10>, |10> -> i|01>).
cp
¶
Controlled phase diag(1, 1, 1, e^{iθ}).
ccx
¶
Toffoli (doubly controlled X).
measure
¶
Measures each qubit in the Z basis; measurement k writes classical bit k.
noise
¶
Appends a stochastic Pauli channel (one of NOISE_CHANNELS) on each qubit (pairs for depolarize2).
x_error
¶
X with probability p (Stim X_ERROR).
y_error
¶
Y with probability p (Stim Y_ERROR).
z_error
¶
Z with probability p (Stim Z_ERROR).
depolarize1
¶
X, Y or Z, each with probability p/3 (Stim DEPOLARIZE1).
depolarize2
¶
One of the 15 non-identity two-qubit Paulis, each with probability p/15 (Stim DEPOLARIZE2).
detector
¶
Declares a detector (parity of absolute measurement indices); returns its index.
observable_include
¶
Adds measurements to logical observable index.
compose
¶
Appends other (its qubit i on qubits[i]) in place; returns self.
Measurement indices in conditionals, detectors and observables of
other are shifted past this circuit's measurements.
repeat
¶
inverse
¶
The inverse of a unitary circuit (reversed, each gate inverted, global phase negated).
remove_final_measurements
¶
A copy without terminal measurements (and without detectors/observables).
to_qasm
¶
OpenQASM 2.0 source. Conditionals and noise channels have no faithful
OpenQASM 2 form and raise UnsupportedOperationError;
the global phase is not representable and is dropped.
from_qasm
classmethod
¶
Parses OpenQASM 2.0: registers (laid out in declaration order), user
gate definitions, broadcasting, barrier, reset, measure,
if on one-bit registers, and the qelib1.inc gate set plus common
Qiskit extensions (rzz, rxx, ryy, rzx, ecr, cu, ...).
to_stim
¶
Stim circuit text (Clifford gates, measurements, resets, Pauli noise,
detectors, observables; readout_error becomes M(p)).