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 flat line is one qubit's timeline. Boxes on the line are steps, done from left to right. Symbols joined by an up-and-down line act on several qubits at once. Left-to-right only sets the order when two steps share a qubit. Steps on different lines in the same column can happen together.
Keep track of the full list of amplitudes after every gate. Each gate takes the current list and makes the next one. Most circuit bugs jump out as soon as you compare the list you expected at a step with the list you actually got.
Five small patterns cover most work on one qubit: prepare, rotate, shift phase, undo, and change basis before measuring. Gate order matters, so it pays to learn them as patterns. Single gate facts on their own are easy to misuse.
CX copies a sure 0 or 1 into a fresh qubit, and it can work out parity. But when its input is a superposition, it entangles the qubits instead of copying. No circuit can copy an unknown quantum state. SWAP moves a state from one wire to another. It is usually built from three CX gates.
A parameterised gate is a gate with a number dial. RY(θ) takes a qubit that is surely 0. It turns it into a state that reads 1 with chance sin²(θ/2). The shape of the circuit stays fixed while the numbers change. That split is the basis of parameter sweeps and variational methods.
A normal measurement asks each qubit, "Are you 0 or 1?" This is called the Z basis question. Put an H right before measuring, and you ask the X basis question instead. The question you choose decides what your counts can show about the state.
Gate count tells you how much work a circuit does. Depth counts the layers of gates that must run one after another. Two circuits with the same gates can have different depths. On real machines, more layers means more time for errors to creep in.
Every quantum gate is unitary. That means it pairs each input with exactly one output, and it can always be run backwards. So a circuit can never merge two different inputs into the same output. Normal logic, such as AND, throws information away. Before a quantum circuit can do AND, it must be rebuilt in a reversible form. Usually a Toffoli gate writes the answer onto an extra qubit, and the inputs survive.
Uncomputation runs the reverse of the gates that made a temporary value, in reverse order. This puts helper qubits back to |0⟩. Then they are no longer entangled with the data you care about. Resetting them is not the same thing. Reset can't be undone, and it destroys the phase links that later interference needs.
Most quantum circuit bugs come from how the circuit is written, not from the physics. Common ones are a swapped control and target, sign errors a normal histogram can't show, a measurement placed too early, or a misread bit order. Debug step by step. Test the simplest input with a sure answer first. Look at the amplitudes before measurement. And remember that more shots never fix a logic error.
A GHZ state puts n qubits into an even superposition of all-zeros and all-ones. One Hadamard gate followed by a chain of CX gates builds it. The order of the CX gates matters. On five qubits, a straight chain needs four CX layers in a row, while a balanced tree needs only three. And a histogram with two peaks does not, by itself, prove the state is truly entangled.
A full state-vector simulator stores one amplitude for each of the 2^n possible readouts. In QPU137 each amplitude takes 16 bytes. So memory doubles with every qubit you add. 20 qubits need 16 MiB, and 30 need 16 GiB. 45 qubits needed about half a petabyte on a supercomputer. That doubling curve, not a lack of effort, is why browser simulation stops in the mid-twenties.
The capstone runs the whole course loop on one five-qubit circuit. Build it. Work out its width, gate counts and depth by hand. Write down a prediction with numbers. Run it with a fixed seed. Then break exactly one step and find the first wrong state. A circuit is finished when someone else can repeat that full record, not when the histogram just looks right.