|q⟩ Bad Qubits

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

Project: A Quantum Random Number

A single Hadamard on one qubit gives one random bit. What if you need more? The answer is straightforward: use nn independent qubits, apply a Hadamard to each, and measure all of them. The result is an nn-bit quantum random number whose bits are genuinely independent and uniformly distributed.

From one random bit to many

After a Hadamard, qubit kk sits in the equal superposition

H0k=12(0k+1k).H|0\rangle_k = \frac{1}{\sqrt{2}}\bigl(|0\rangle_k + |1\rangle_k\bigr).

Applying this to each of the nn qubits independently produces the product state

Hn0n=12nx=02n1x,H^{\otimes n}|0\rangle^{\otimes n} = \frac{1}{\sqrt{2^n}} \sum_{x=0}^{2^n - 1} |x\rangle,

where the sum runs over every nn-bit string xx. This is the uniform superposition over all 2n2^n computational basis states. Measuring it collapses the state to one of those strings, each with probability

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

For n=4n = 4 qubits, there are 24=162^4 = 16 possible outcomes and each carries probability 116=0.0625\tfrac{1}{16} = 0.0625.

Why the bits are independent

The product state above has no entanglement: qubit 0 does not know the result of qubit 1. Formally, the state factors as a tensor product, meaning the joint probabilities factorize too — P(x0x1x2x3)=P(x0)P(x1)P(x2)P(x3)P(x_0 x_1 x_2 x_3) = P(x_0)\,P(x_1)\,P(x_2)\,P(x_3) with each marginal equal to 12\tfrac{1}{2}. Each qubit contributes exactly one independent, unbiased random bit.

Try it

Build the 4-qubit circuit that generates a 4-bit quantum random number. The grader checks that every one of the 16 outcomes has equal probability 116\tfrac{1}{16}.

Run your code to see the quantum state.

After running, open the Probabilities panel. You should see 16 equal bars, each at height 0.06250.0625. Because the bits are independent, you can read the 4-bit result as an integer from 0 to 15, drawn uniformly at random by the laws of quantum mechanics.

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