Code exampleBeginnerQiskit labLesson 3

Qubits and Registers

QuantumRegister and ClassicalRegister give meaningful names to groups of qubits and bits; each qubit still has a single index in the circuit.

For small circuits it is enough to give the number of qubits, but algorithms usually divide qubits into roles: a data register, ancilla qubits, a counting register and so on. Qiskit lets you name these groups with QuantumRegister(size, name) and store results in a ClassicalRegister.

When building QuantumCircuit(data, anc, out), the qubits are ordered by register order: data[0] and data[1] have indices 0 and 1, and anc[0] has index 2. The find_bit method returns this index. Methods accept a qubit by its index or by its register reference; qc.measure(data, out) measures the whole register at once.

Qiskit's Qubit Ordering convention is Little-endian: qubit 0 is the least significant bit, and the printed string reads $q_{n-1}\dots q_0$. When there are several classical registers, their results are printed separated by spaces, and the last register appears first, on the left.

In the example: X on data[1], then a CNOT from it to anc[0], so data[1] and anc[0] are both in state 1 while data[0] stays at 0; measuring the data register will give “10” (bit 1 = 1, bit 0 = 0). Note in the drawing that the measurement of data_0 appears early because it does not depend on any gate; the drawer places each operation in the earliest possible column.

Requirements: pip install qiskit qiskit-aer. The code runs on your machine or in Colab, not inside WordPress.

Code

Qiskit
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

data = QuantumRegister(2, "data")
anc = QuantumRegister(1, "anc")
out = ClassicalRegister(2, "out")
qc = QuantumCircuit(data, anc, out)

qc.x(data[1])
qc.cx(data[1], anc[0])
qc.measure(data, out)

print("registers:", [r.name for r in qc.qregs], [r.name for r in qc.cregs])
print("total qubits:", qc.num_qubits)
print("index of anc[0]:", qc.find_bit(anc[0]).index)
print(qc.draw("text"))

Line-by-line code explanation

LineExplanation
1Import the circuit and the two register classes.
3A two-qubit quantum register named data.
4A quantum register with one ancilla qubit named anc.
5A two-bit classical register named out.
6Build the circuit from the registers; their order determines qubit indexing.
8X gate on the second qubit of the data register.
9CNOT from data[1] to the ancilla qubit.
10Measure the whole data register into the classical register.
12Print the names of the quantum and classical registers.
13Total number of qubits (3).
14The unified index of qubit anc[0] within the circuit.
15A text drawing that shows the register names.

Expected output (from an actual run)

registers: ['data', 'anc'] ['out']
total qubits: 3
index of anc[0]: 2
             ┌─┐        
data_0: ─────┤M├────────
        ┌───┐└╥┘     ┌─┐
data_1: ┤ X ├─╫───■──┤M├
        └───┘ ║ ┌─┴─┐└╥┘
   anc: ──────╫─┤ X ├─╫─
              ║ └───┘ ║ 
 out: 2/══════╩═══════╩═
              0       1 

The output is deterministic.

Related topics

References

  1. IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com
  2. IBM Quantum (2025). IBM Quantum Documentation – Guides. IBM Quantum Documentation quantum.cloud.ibm.com