Quantum Random Number Generator
First time trying anything quantum computing w/ my friend Shuhul from Caltech :)
Prompt


In this project for QiskitFest@UCLA, we implement a Quantum random number generator that uniformly samples in the fewest number of qubits and gates as possible. We include 4 sample circuits ranging from a naiive implementation to the most optimal design.
Preliminaries:
Quickly, this problem of sampling is trivial for the case , since we can use a series of Hadamard transforms to evenly split the amplitudes across all computational basis states. Each Hadamard gate doubles the number of possible outcomes, creating a uniform superposition where every basis state has equal probability . This only becomes interesting when isn’t just a power of , for which we explore a series of circuit constructions. Naiively, we could just simulate extra states and use rejection sampling, but we wanted to design this circuit to operate purely on a quantum state.
V0: Naiive Control

In this circuit, we manually set the probabilities of each qubit using cascaded rotations, but this approach quickly becomes impractical. Each new qubit requires multiple controlled rotations whose angles depend on all previous qubits, causing the gate count and circuit depth to grow quadratically. These controlled rotations are also error-prone and hardware-expensive, and the precise rotation values are hard to compute or calibrate.
V1: Binary Expansion
Since for powers of 2, building a uniform sampler is straightforward using Hadamard gates, we instead view as a sum of powers of two, . By constructing uniform superpositions for each block and combining them with conditional rotations, we can approximate a uniform distribution over while keeping the circuit shallow and efficient.
Example
Here is an example of the breakdown for :

And here is the corresponding circuit representation

We recursively sample over states as a binary tree of conditional probabilities. At each branching point, the circuit applies a rotation that splits the amplitude proportionally to how many valid states remain in each subtree.
For instance, the first qubit has probability of being and of being . These are then recursively refined until all of the basis states are assigned equal amplitude.
Some Math
For the first qubit, we want to split the probability mass between the left and right branches according to how many valid states lie under each.
An gate on prepares
giving probabilities and . Setting yields
V2: Consolidating Hadamards

Since the Hadamard gate is unitary, applying it twice yields the identity operation . Previously, we used a Hadamard gate with a control to emulate an anti-control, but since H is self-inverse, we can simplify the circuit by directly using an anti-control instead, reducing unnecessary gates.
V3: Complement Graph

This approach first builds a fully balanced superposition over states using Hadamard gates, then applies conditional rotations to correct the amplitudes for . The Hadamard layer creates an even base distribution, while the correction step prunes invalid branches, creating an even more compact circuit.
Example
For , where the first split is and , the rotation angle is
so relative to a Hadamard baseline (), the correction is
V4: Log-depth Hadamard Expansion

To build a uniform quantum state over , we basically follow the binary expansion of . Each power of two, , is like a “block” of evenly distributed states, and you only need Hadamards to generate it, since each Hadamard doubles your reach. So if , you use three Hadamards to cover the first eight states and then just patch in the last one. The key idea is that you don’t brute-force all outcomes — you reuse the structure of powers of two to get there in about steps, with a few small corrections at the end to make everything line up perfectly.
There might be a slightly more clever way of reducing the number of control gates, but this is the best we could do up to gates and number of qubits.
Exercise (for fun)
Let and .
Estimate when the complement approach (cost ) becomes less efficient than the Hadamard tree method (cost ).