Creating Circuits and Registers
Circuit stores gates, measurement mappings, and register information. It is the common entry point for constructing, executing, drawing, exporting, and compiling circuits. A simple circuit can be created by passing the qubit count; named or multiple registers can be supplied explicitly.
Recommended imports
from unitarylab import Circuit, Register, ClassicalRegisterCreating a Circuit
import numpy as np
from unitarylab import Circuit, Register, ClassicalRegister
# Specify the number of bits; default q/c registers are created together.
qc = Circuit(3)
# Start from a known state.
qc_from_state = Circuit(np.array([1, 0, 0, 0], dtype=complex))
# Choose register names and layout.
qr = Register("q", 2)
cr = ClassicalRegister("c", 2)
qc_named = Circuit(qr, cr, name="Bell Circuit")Circuit(n) creates default quantum register q and classical register c, both of size n. When constructing a circuit with explicit Register objects, also provide a ClassicalRegister when measurements are required.
| Constructor | Use |
|---|---|
Circuit(n) | One quantum and one classical register, both of size n |
Circuit(state) | Create a circuit from a state vector of length |
Circuit(qreg, creg, name=...) | Custom register names and layout |
The state vector must be one-dimensional, nonzero, and normalizable. Qubit indices start at 0; an index outside the circuit raises an exception.
Register
from unitarylab import Register
qr = Register("q", 3)
q0 = qr[0]
first_two = qr[:2]
selected = qr[[0, 2]]Quantum registers support integer, slice, and index-list access. Indices must be within the register bounds.
ClassicalRegister
from unitarylab import Circuit, Register, ClassicalRegister
cr = ClassicalRegister("c", 2)
print(cr.values) # [-1, -1]
qc = Circuit(Register("q", 2), cr)
qc.measure([0, 1], [0, 1])
result = qc.execute()
print(result.classical_registers["c"])Classical bits start at -1 and are updated when circuit measurements execute. For quantum gates and measurement instructions, see Quantum Gates and Measurement.
Usage recommendations
- Registers are index views; gate operations still act on the circuit’s global qubit indices.
- Read measurement outcomes from
ExecutionResult; a classical register created with the circuit is not a multi-shot statistics object. - Check circuit widths and target indices before composing subcircuits.