Generating Random Numbers

Welcome! This blog is the first entry in the series An Introduction to Quantum Cryptography. In this post, and throughout the series, we are going to investigate how quantum computers can be used to enable secure communication and to understand the quantum algorithms that allow us to do so. To start with, we are going to look at the task of generating randomness, an essential part of creating a secure cryptographic system.

In this blog we will be creating a simple quantum circuit for generating randomness and looking at the supporting topics.

Prerequisites:

  • Complex numbers
  • Dirac notation
  • Vector spaces

Topics covered:

  • Qubits
  • Bases of quantum states
  • The Hadamard Gate
  • Quantum measurement

Classical methods for generating random numbers

If you were tasked with generating a random number, there are a lot of ways you might go about trying to do so. Perhaps you roll some dice or draw a card. Or perhaps you measure something in the environment, such as Brownian motion and report your findings.

Okay, so far, so good. The next task, however, is to create a computer program that outputs a random number each time it is run. The problem here is that computers are inherently deterministic, meaning if I run the same program (with the same parameters) twice I should expect the same result. So how do we bend the rules of these deterministic machines to perform our task of generating random numbers? The answer comes in the form of a type of algorithm called a pseudorandom number generator (PRNGs).

A PRNG is a deterministic algorithm that spits out numbers seemingly at «random». In fact, there are incredibly good PRNGs called Cryptographically Secure PRNGs that are used today in cryptography. However, there are two things which make a PRNG only pseudorandom, not truly random.

  1. A PRNG requires a seed from which the random string is generated. Using the same seed twice will result in the same string of random numbers (as the PRNG is a deterministic algorithm).
  2. It is very hard to prove that when the PRNG is run for a very long time, the numbers remain random. Therefore, when using a PRNG (for cryptographic purposes) the seeds should be periodically refreshed.

 

This may seem circular, as it now seems we need to periodically generate random numbers as seeds for the PRNG. The key here is to make sure an adversary (someone trying to break your cryptographic system) cannot learn the seed that you choose as they would then be able to generate the same string of random numbers and break your system. There are many different methods for generating seeds, your laptop might measure the temperature of it’s CPU to a fine precision and use that as a seed. Alternatively, the company Cloudflare takes photos of a wall of lava lamps and uses the binary representation as seeds in their cybersecurity services.

Rows of lava lamps used as entropy sources

 

A Quantum Random Number Generator

As far as we know, quantum processes can exhibit truly random behavior. This is because when a quantum particle particle is measured its wavefunction, which encodes all its possible states, probabilistically collapses to a single state. So, if we had a quantum computer, is it possible to generate truly random numbers from a deterministic algorithm? The answer to this question is yes! Let’s take a look at how.

A qubit is a unit of information used in quantum computers that can be in a superposition of both the 0 and 1 state simultaneously. More formally, a qubit is a 2-dimensional complex vector that can be represented as:

$$\displaystyle \lvert \psi \rangle = \alpha \lvert 0 \rangle + \beta \lvert 1 \rangle, \quad \alpha, \beta \in \mathbb{C},\; \lvert \alpha \rvert^2 + \lvert \beta \rvert^2 = 1 $$

Recall that a vector space is generated by the linear combination of a set of basis vectors. For example, a 2-dimensional real vector space has the basis vectors (1,0),(0,1). Any point in this 2-dimensional plane can be reached using some combination of these two vectors. E.g.

((10,5)=10(1,0)+5(0,1))

Equally, the same plane \( \mathbb{R} \) also has the basis {(1, 1), (1, –1)}. E.g.

(\( (10,\,5) = \tfrac{15}{2} \cdot (1,1) + \tfrac{5}{2} \cdot (1,-1) \))

In fact, there are infinitely many bases for a vector space. In all of these bases, any point can be described by the linear combination of the basis vectors with some real coefficients.

For the state space of a qubit, which is \( \mathbb{C}^2 \), there is some nuance to consider. This is a complex vector space. This means that the coefficients in the linear combination are now complex numbers. The standard basis, also called the computational basis, for \( \mathbb{C}^2 \) is \( \{\lvert 0 \rangle, \lvert 1 \rangle\} \) where

|0=(10),|1=(01)

This brings us back to our definition of a qubit,

$$\lvert \psi \rangle = \alpha\,\lvert 0 \rangle + \beta\,\lvert 1 \rangle, \quad \alpha,\beta \in \mathbb{C}, \;\lvert \alpha \rvert^2 + \lvert \beta \rvert^2 = 1$$

We can see that \( \lvert \psi \rangle \) is just a vector in the complex vector space generated by the standard basis, subject to the condition that \( \lvert \alpha \rvert^2 + \lvert \beta \rvert^2 = 1 \). As before, there are infinitely many bases for the state space of a qubit, and we will see one more shortly.


Hadamard Gate

For our randomness-generating algorithm, we are going to need the faithful Hadamard gate. If you’re just meeting the Hadamard gate for the first time, this guy will soon become a fast friend on your journey into quantum computing. The Hadamard gate is defined as:

H=12[1111]

Recall that gates are unitary matrices that can be applied to quantum states to change the state. Let’s see an important pair of examples:

H|0=12[1111][10]=12[11]=|+

$$ H \lvert 1 \rangle = \tfrac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \begin{bmatrix} 0 \\ 1 \end{bmatrix} = \tfrac{1}{\sqrt{2}} \begin{bmatrix} 1 \\ -1 \end{bmatrix} = \lvert – \rangle $$

There we go, with just a single qubit and the application of one quantum gate, we have created a deterministic program that generates a truly random number. What is especially interesting about this program is that, as far as we know, no matter how powerful our adversary is, there is absolutely no way that they could improve on a 50:50 guess—even if they could perfectly calculate how a die would roll or predict the movements of Cloudflare’s lava lamps with infinite precision.

This is an example of quantum superposition! The states |0 and |1 are each partially in state {|+,|} and C2: at the same time. We can tell by looking at the vector representation of the states and seeing that both elements are non-zero. Furthermore, |ψ form another basis of |ψ, the qubit state space.

Start and Apply H-gate Bloch sphere diagrams

For the final component of our method, we are going to need to be able to measure qubits and be able to calculate the probability of obtaining a certain measurement.

Say I have a qubit \(\lvert\psi\rangle\) that I want to measure. The first thing I need to do is specify which basis I want my measurement in. We are familiar with the standard basis, and that seems like a good choice, so let’s use that for now.

To calculate the probability of a quantum state collapsing to a given state |φ when measured, we use the following equation:

Pφ=|ψφ|2

where ψφ is the inner product of |ψ and |φ.

Okay, we are ready to create our randomness-generating quantum algorithm. We start with a qubit in the \(\lvert0\rangle\) state, apply a single Hadamard gate, and then measure the qubit in the standard basis. That’s it!

Let’s see why this works: We know from before that \(H\lvert0\rangle = \lvert+\rangle\). So what is the probability of measuring each of \(\lvert0\rangle\) and \(\lvert1\rangle\) when \(\lvert+\rangle\) is measured in the standard basis? We can use the formula from the previous section.

P0=|+0|2=|12(11)(10)|2=12 P1=|+1|2=|12(11)(01)|2=12


There we go, with just a single qubit and the application of one quantum gate, we have created a deterministic program that generates a truly random number. What is especially interesting about this program is that, as far as we know, no matter how powerful our adversary is, there is absolutely no way that they could improve on a 50:50 guess—even if they could perfectly calculate how a die would roll or predict the movements of Cloudflare’s lava lamps with infinite precision.


Explore the code yourself

You can find the full implementation of the quantum random number generator discussed in this post in the following Jupyter notebook, built using Qiskit and IBM Quantum:

🔗 GitHub Repository – Quantum Random Number Generator

Feel free to experiment, modify, and run the code to deepen your understanding.

This blog post, and the others in this series are adapted from Introduction to Quantum Cryptography by Thomas Vidick and Stephanie Wehner, if you are interested in exploring these topics further I would highly recommend this textbook.

For further information on QRNGs:
https://www.quantum-dice.com/blog/
https://www.nature.com/articles/s41586-025-08737-1

 

Next Post

What are bits and qubits?