|q⟩ Bad Qubits

intermediate · Programming · Multi-Qubit Registers & State Simulation

Preparing Uniform Superpositions

A quantum register of nn qubits has 2n2^n computational-basis states. Feeding every algorithm with a uniform superposition over all of them is the starting point of Deutsch–Jozsa, Grover, the quantum Fourier transform, and most other quantum algorithms. Building that superposition costs exactly nn gates.

One Hadamard per qubit

Recall what the Hadamard gate does to a single-qubit basis state:

H0=0+12,H1=012.H|0\rangle = \frac{|0\rangle + |1\rangle}{\sqrt{2}}, \qquad H|1\rangle = \frac{|0\rangle - |1\rangle}{\sqrt{2}}.

When every qubit starts in 0|0\rangle and we apply one HH to each, the nn-qubit state factors into a tensor product:

Hn0n=k=0n10+12=12nx=02n1x.H^{\otimes n}|0\rangle^{\otimes n} = \bigotimes_{k=0}^{n-1} \frac{|0\rangle + |1\rangle}{\sqrt{2}} = \frac{1}{\sqrt{2^n}} \sum_{x=0}^{2^n - 1} |x\rangle.

Each amplitude equals 1/2n1/\sqrt{2^n}, so by the Born rule every basis state is equally likely with probability

(12n)2=12n.\left(\frac{1}{\sqrt{2^n}}\right)^2 = \frac{1}{2^n}.

For n=3n = 3 that is 1/80.1251/8 \approx 0.125 for each of the eight strings 000|000\rangle through 111|111\rangle.

Counting amplitudes

It is worth building intuition with small nn:

The preparation cost grows only as O(n)O(n) gates, while the number of states encoded grows as O(2n)O(2^n) — this exponential encoding is the resource that quantum algorithms exploit.

Try it

Prepare the uniform superposition on 3 qubits and measure. The grader checks that all 8 outcomes appear with equal probability 1/81/8.

Run your code to see the quantum state.

After running, open the Probabilities tab: every bar should reach exactly 0.1250.125.

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