Project: Build a Mini Circuit Library
Every quantum algorithm begins by preparing qubits in a carefully chosen state before the main computation runs. Rather than scattering gates randomly across a program, good quantum software organises these state-preparation routines as named, reusable building blocks — the same way a classical programmer might factor repeated logic into a helper function.
What a library routine looks like
A state-prep routine is a small, self-contained sequence of gates that transforms into a target state. Once defined, it can be embedded into larger circuits without re-deriving the gate sequence each time.
The simplest and most widely used example is the uniform superposition layer: apply one Hadamard to each qubit. For qubits starting in , the tensor-product structure of gives
Every one of the computational basis states appears with equal amplitude , so each is equally likely to be measured. For the amplitude is and the probability of any single outcome is .
This routine is not just a curiosity — it appears verbatim at the start of Deutsch–Jozsa, the Bernstein–Vazirani algorithm, and Grover's search. It is also structurally related to the Quantum Fourier Transform (QFT), which is the engine behind Shor's factoring algorithm: QFT applied to produces a uniform superposition with state-dependent phases, but the QFT circuit itself uses Hadamards interleaved with controlled phase rotations rather than a plain layer. Recognising the uniform superposition as a named operation makes circuits much easier to read and reason about.
What makes a routine reusable
Three properties let a state-prep routine earn a place in a library:
- Well-defined input — the routine assumes as input. Passing any other state in would produce an incorrect result.
- Guaranteed output — the output state is fixed by the gate sequence alone, not by any measurement outcome. Determinism is what allows the routine to be composed with other routines.
- Correct gate count — an efficient routine uses the fewest gates needed. The uniform superposition layer is optimal: one per qubit is both necessary and sufficient.
Beyond uniform superposition
Other common entries in a beginner circuit library include:
- Bell-pair factory — on qubit 0, then CNOT from qubit 0 to qubit 1 → .
- preparation — then on a single qubit → .
- Computational-basis state encoder — a sequence of gates that flips specific qubits to encode a classical bit string.
Each of these is just a short, named sequence of gates. A mini circuit library is simply the habit of giving those sequences names and storing them somewhere you can find them again.
Try it
Implement the 3-qubit uniform superposition routine. The grader checks the full state vector, so every amplitude must be . Leaving any qubit un-acted on (so its amplitude is 0 for ) will cause the check to fail.
After running, inspect the state vector. All eight entries should show magnitude and zero phase — the hallmark of the uniform superposition over three qubits.
Sign in on the full site to ask questions and join the discussion.