|q⟩ Bad Qubits

beginner · Programming · Building & Reading Quantum Circuits

Reusing Subcircuits

Real circuits quickly grow too large to read as one flat stream of gates. The standard remedy is factoring: carve the circuit into small, self-contained subcircuits and apply each one wherever it is needed. Subcircuits make large circuits easier to write, easier to audit, and easier to reuse across different programs — the same engineering principle that turns spaghetti code into well-named functions.

What a subcircuit is

A subcircuit is any fixed sequence of gates that performs a coherent, nameable operation on a specific set of qubits. It has no special syntax at the hardware level; it is simply a group of gate calls that you identify as a unit. In higher-level frameworks you would package it as a function or a named gate; in the flat API used here you apply it by repeating the same gate-call pattern on different qubits.

The Bell-pair preparation is a canonical example: apply HH to one qubit, then a CNOT with that qubit as the control and a second qubit as the target. This two-gate unit entangles the pair into

Φ+=12(00+11).|\Phi^+\rangle = \frac{1}{\sqrt{2}}\bigl(|00\rangle + |11\rangle\bigr).

The derivation is straightforward: starting from 00|00\rangle, the Hadamard produces 12(0+1)0\tfrac{1}{\sqrt{2}}(|0\rangle + |1\rangle)|0\rangle, and the CNOT flips the second qubit whenever the first is 1|1\rangle, yielding 12(00+11)\tfrac{1}{\sqrt{2}}(|00\rangle + |11\rangle).

Reuse without copying errors

Once you identify a subcircuit, applying it a second time means repeating the same gate calls with different qubit indices. No new logic is introduced — you just shift the "address" of the pattern. On a 4-qubit register with pairs (0,1)(0,1) and (2,3)(2,3), the Bell subcircuit runs on (0,1)(0,1) first, then identically on (2,3)(2,3). The final state is the tensor product of two independent Bell pairs:

Φ+01Φ+23=12(0000+0011+1100+1111).|\Phi^+\rangle_{01} \otimes |\Phi^+\rangle_{23} = \frac{1}{2}\bigl(|0000\rangle + |0011\rangle + |1100\rangle + |1111\rangle\bigr).

Reading a factored circuit

In a circuit diagram, a subcircuit shows up as a repeated visual motif — the same shape of gates appearing at multiple horizontal positions or on multiple groups of wires. Recognising the motif is the first step to reading a large circuit quickly: instead of parsing every gate individually, you identify the building blocks and reason about their combined effect.

Try it

A 4-qubit circuit should prepare two independent Bell pairs by applying the Bell subcircuit to qubits (0,1)(0,1) and then again to qubits (2,3)(2,3). The grader checks the full unitary matrix, so an empty circuit will not pass.

Run your code to see the quantum state.

After running, inspect the statevector: you should see four equal-amplitude basis states — 0000|0000\rangle, 0011|0011\rangle, 1100|1100\rangle, and 1111|1111\rangle — each with amplitude 12\tfrac{1}{2}, confirming that both pairs are entangled but independent of each other.

Sign in on the full site to ask questions and join the discussion.