Quantum Circuits: Build, Simulate & Debug
Read, construct, simulate, and debug real circuits: multi-qubit gates, circuit identities, depth, and the debugging habits that catch wrong circuits early.
A circuit diagram is a schedule: each horizontal wire is one qubit's timeline, boxes are operations applied left to right, and vertically linked symbols act on several qubits at once. Left-to-right fixes the order only where operations share a qubit — gates on different wires in the same column can run together.
Track the full list of amplitudes after every gate: each gate consumes the current list and produces the next one. Most circuit bugs become obvious the moment you compare the amplitudes you expected at a step with the ones you actually get.
Five small patterns — prepare, rotate, shift phase, undo, and change basis before measuring — cover most single-qubit work. Because gate order is not interchangeable, it pays to learn them as patterns rather than as isolated gate facts.
CX copies a definite 0/1 into a fresh qubit and computes parity, but applied to a superposition it entangles rather than clones — no circuit can copy an unknown quantum state. SWAP moves a state between wires and is usually built from three CX gates.
A parameterised gate is a gate with a number dial: RY(θ) turns a definite 0 into a state that reads 1 with probability sin²(θ/2). The circuit structure stays fixed while the numbers change — that separation underlies parameter sweeps and variational methods.
Standard measurement asks each qubit "are you 0 or 1?" — the computational (Z) basis question. Placing an H immediately before measurement asks the X-basis question instead, and which question you choose decides which property of the state your counts can reveal.
Gate count measures how much work a circuit does; depth counts the layers of gates that must run one after another. Two circuits with identical gates can have different depth, and on real hardware more layers means more time exposed to error.
Every quantum gate is unitary, meaning it is a perfect one-to-one mapping that can be run backwards, so a circuit can never merge two different inputs into the same output. Classical operations such as AND destroy information, so before a quantum circuit can compute them they must be embedded in a reversible form — typically a Toffoli gate writing the answer onto an extra qubit while the inputs survive.
Uncomputation runs the inverse of the gates that produced a temporary value, in reverse order, so helper qubits return to |0⟩ and stop being entangled with the data you care about. Resetting them instead is not equivalent: reset is non-unitary and destroys the phase relationships that later interference depends on.
Most quantum circuit bugs are representation bugs: swapped control and target operands, phase errors that Z-basis histograms cannot show, measurement placed too early, or a misread bit order. Debug methodically — test the smallest deterministic input first, inspect amplitudes before measurement, and remember that more shots never fix a logic error.
A GHZ state puts n qubits into an equal superposition of all-zeros and all-ones: one Hadamard followed by a chain of CX gates builds it. The fan-out order matters — on five qubits a linear chain needs four sequential CX layers while a balanced tree needs only three — and a two-peak histogram alone does not prove the state is genuinely entangled.
A dense state-vector simulator stores one complex amplitude for each of the 2^n basis states — 16 bytes each in QPU137 — so memory doubles with every qubit added: 20 qubits need 16 MiB, 30 need 16 GiB, and 45 needed about half a petabyte on a supercomputer. That exponential curve, not a lack of engineering effort, is why browser simulation stops in the mid-twenties.
The capstone runs the whole course loop on one five-qubit circuit: build it, compute width, gate counts and depth by hand, write down a quantitative prediction, run it with a fixed seed, then break exactly one operation and locate the first wrong state. A circuit is finished when that full record is reproducible — not when the histogram merely looks plausible.