Circuits, Gates, and Measurements in Code
Track: Quantum Programming · Difficulty: Beginner · Est: 14 min
Circuits, Gates, and Measurements in Code
Overview
This page introduces the programming concept that a circuit is an ordered list of operations with explicit wiring:
- which qubit an operation acts on
- where measurement results are stored
It answers: How do circuit diagrams translate into code objects precisely?
Conceptual Mapping
A circuit diagram has three kinds of “wires”:
- qubit wires (quantum information)
- classical wires (measurement outcomes)
- time flowing left to right (operation order)
In code:
- qubits are indexed:
0, 1, 2, ... - classical bits are indexed:
0, 1, 2, ... - order is the order you call methods like
h,cx,measure
Two key mappings:
- Gate application order matters (non-commuting operations).
- Measurement mapping matters (which classical bit gets which qubit’s measurement).
Code Walkthrough
A minimal two-qubit example to show ordering and measurement mapping:
from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure(0, 1)
qc.measure(1, 0)
print(qc)Line by line:
QuantumCircuit(2, 2)creates 2 qubits and 2 classical bits.h(0)acts only on qubit 0.cx(0, 1)uses qubit 0 as control and qubit 1 as target.measure(0, 1)stores the measurement of qubit 0 into classical bit 1.measure(1, 0)stores the measurement of qubit 1 into classical bit 0.
The last two lines intentionally “swap” the mapping. This is a reminder that classical bits are separate storage.
What Happens Under the Hood
Conceptually, Qiskit keeps a data structure for the circuit:
- a list of operations
- each operation has a type (gate/measurement) and targets (qubits/bits)
When you run the circuit on a backend:
- the backend reads this list
- it executes operations in order
- measurement operations write classical bits
- the final classical bitstring is what you see in results
So results like "01" are not “qubits.”
They are classical bits after measurement, arranged in a specific order.
Turtle Tip
Always be explicit about two things: operation order and measurement mapping. If your results look “flipped,” it’s often a wiring/mapping issue, not a quantum mystery.
Common Pitfalls
- Assuming classical bit 0 automatically corresponds to qubit 0.
- Forgetting that
cx(a, b)has a direction (control vs target). - Treating the printed bitstring as a direct snapshot of the quantum state.
- Ignoring operation order; reordering gates can change the computation.
Quick Check
- What does it mean that a circuit is an “ordered list of operations”?
- In the example, which classical bit stores the measurement of qubit 0?
- Why is a measured bitstring not the same thing as a quantum state?
What’s Next
Next we’ll compare simulators and real hardware. You’ll learn which parts of this workflow are idealized in simulation, and why hardware adds extra constraints and noise.
