|q⟩ Bad Qubits

beginner · Programming · Mini-Projects: Coin Flips, RNG & Bell Tests

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 00|0\cdots0\rangle 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 nn qubits starting in 00|0\cdots0\rangle, the tensor-product structure of HH gives

Hn0n=(0+12)qubit 0(0+12)qubit n1=12nk=02n1k.H^{\otimes n}|0\rangle^{\otimes n} = \underbrace{\left(\frac{|0\rangle + |1\rangle}{\sqrt{2}}\right)}_{\text{qubit }0} \otimes \cdots \otimes \underbrace{\left(\frac{|0\rangle + |1\rangle}{\sqrt{2}}\right)}_{\text{qubit }n-1} = \frac{1}{\sqrt{2^n}} \sum_{k=0}^{2^n - 1} |k\rangle.

Every one of the 2n2^n computational basis states appears with equal amplitude 1/2n1/\sqrt{2^n}, so each is equally likely to be measured. For n=3n = 3 the amplitude is 1/80.3541/\sqrt{8} \approx 0.354 and the probability of any single outcome is 18\tfrac{1}{8}.

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 00|0\cdots0\rangle produces a uniform superposition with state-dependent phases, but the QFT circuit itself uses Hadamards interleaved with controlled phase rotations rather than a plain HnH^{\otimes n} 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:

  1. Well-defined input — the routine assumes 00|0\cdots0\rangle as input. Passing any other state in would produce an incorrect result.
  2. 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.
  3. Correct gate count — an efficient routine uses the fewest gates needed. The uniform superposition layer is optimal: one HH per qubit is both necessary and sufficient.

Beyond uniform superposition

Other common entries in a beginner circuit library include:

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 18\tfrac{1}{\sqrt{8}}. Leaving any qubit un-acted on (so its amplitude is 0 for 1|1\rangle) will cause the check to fail.

Run your code to see the quantum state.

After running, inspect the state vector. All eight entries should show magnitude 0.354\approx 0.354 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.