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 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
The derivation is straightforward: starting from , the Hadamard produces , and the CNOT flips the second qubit whenever the first is , yielding .
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 and , the Bell subcircuit runs on first, then identically on . The final state is the tensor product of two independent Bell pairs:
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 and then again to qubits . The grader checks the full unitary matrix, so an empty circuit will not pass.
After running, inspect the statevector: you should see four equal-amplitude basis states — , , , and — each with amplitude , confirming that both pairs are entangled but independent of each other.
Sign in on the full site to ask questions and join the discussion.