Factoring using quantum algorithms

Shor’s algorithm

This post is part of COSIC’s guide to crypto. Today’s topic is Shor’s algorithm.

Modern cryptosystems rely on the hardness of certain computational problems, like for instance given the product \(N=pq\) of two large primes, find \(p\) and \(q\). It turns out however that this problem and others could be solved efficiently by a new kind of computer called a quantum computer. Hence a quantum computer would break some widely used cryptosystems in use today.

What is a quantum computer? A normal computer is based on binary digits: zeroes and ones. A quantum computer is based on qubits, where instead of either 0 or 1 you can have a quantum superposition of 0 and 1. Qubits can be operated on by quantum logic gates, and one can perform quantum measurement. This new kind of computation is called quantum computing and allows a quantum computer to solve problems like the aforementioned integer factorization problem that cannot be efficiently solved on a classical (non-quantum) computer, at least when the numbers are really large.

While big companies are already building primitive quantum computers, it is not yet certain that powerful enough quantum computers will ever be built that could outperform classical computers on integer factorization and break our current cryptosystems. Nevertheless it is a possibility that has to be taken into account, and hence research is being done into post quantum cryptography: coming up with cryptosystems for classical computers that cannot be broken by classical or quantum computers.

Shor’s algorithm factors positive integers into their prime factors using a combination of classical and quantum computation, and it does so in polynomial time. Hence it could break the widely used RSA system. We now describe the basic idea. Suppose \(N\) is not a prime or a prime power, so it decomposes \(N=N_1 N_2\) with \(N_1\) and \(N_2\) coprime. Pick a random integer \(a\) and consider the function

\(f:x\longmapsto a^x\text{ (mod } N).\)

The quantum part of the algorithm computes the period \(r\) of this function, which by Euler’s theorem divides the (unknown) Euler totient function \(\phi(N)\). What we hope is that this period \(r\) is even and that \(a^{r/2}\) is congruent to 1 modulo some factor of \(N\) and congruent to \(-1\) modulo some other factor of N. Computing the gcd of \(a^{r/2}-1\) with \(N\) then gives a non-trivial factor of \(N\).

For the quantum part of the algorithm we will need a large power of two: \(Q=2^q\) with \(N^2<Q<2N^2\).

Example: \(N=15\), \(a=2\), \(Q=256\).

We see that we have a repetitive pattern with period \(r\). In this example \(r=4\).

Now what a quantum computer can do is to simultaneously compute

\(f(x)=a^x\) (mod \(N\))

for all \(x\) ranging from \(0\) to \(Q-1\). Now there is no way an ordinary computer could do this as \(Q\) can be much bigger than the number of atoms in the observable universe. But we can take a quantum superposition of all possible sequences of zeroes and ones of length \(q\). Interpreting such sequences as binary expansions of integers these are exactly the numbers from \(0\) to \(2^q-1=Q-1\). Our formal notation for this superposition will be

\(\frac{1}{\sqrt{Q}}\sum_{x=0}^{Q-1}|x\rangle.\)

These binary digits are called qubits and there are \(q\) of them in our register. But we will need additional qubits to compute our \(f(x)\) for all these \(x\). This involves taking a classical circuit of logic gates that computes \(f(x)\) for any \(x\) expressed as a binary sequence of length \(q\), and then turning this classical circuit into a quantum circuit. The result is the superposition

\(\frac{1}{\sqrt{Q}}\sum_{x=0}^{Q-1}|x,f(x)\rangle.\)

This is the heaviest part of the calculation. The next step is applying the quantum Fourier transform. Its output is

\(\frac{1}{Q}\sum_{x=0}^{Q-1}\sum_{y=0}^{Q-1}e^\frac{2\pi ixy}{Q}|y,f(x)\rangle.\)

What’s the idea here? Our function \(f\) repeats with period \(r\), and it repeats more than \(N\) times as \(Q\) is bigger than \(Nr\). Because the period \(r\) repeats so often, the frequency \(Q/r\) will show up in this quantum Fourier transform. In fact the terms \(|y,f(x)\rangle\) with the largest coefficients will have a \(y\) close to an integer multiple of \(Q/r\).

Our next step is to perform quantum measurement. Without measurement we can’t retrieve any (classical) information from our quantum state. The outcome of a quantum measurement is probabilistic. In our case this turns our whole superposition into a single term

\(|y,z\rangle,\)

for some specific \(y\) and \(z\) which we now know. The probability of measuring any particular value of \(y\) and \(z\) is given by the Born rule: it is the square of the modulus of the coefficient of \(|y,z\rangle\). So the bigger the coefficient of a term, the more likely it is that we will measure this particular term.

But as we said earlier the terms \(|y,f(x)\rangle\) with the largest coefficients will have a \(y\) close to an integer multiple of \(Q/r\). So our measurement gives us a value for \(y\), and we know that \(\frac{yr}{Q}\) is probably close to an integer \(c\). We then have that \(\frac{y}{Q}\) (which we know), is close to the fraction \(\frac{c}{r}\) whose denominator we wish to know. Using continued fraction expansion of \(\frac{y}{Q}\) we can calculate different possibilities for \(\frac{c}{r}\), and for each we can check whether the denominator \(r\) really is the period of our function \(f\).

Once we found the period \(r\) of \(f\) we then do the classical part of the algorithm. If \(r\) is odd, or if it is even but \(\gcd(a^{r/2}-1,N)\) does not give a nontrivial factor of \(N\) then we pick another \(a\) and we do the whole quantum part of the algorithm again until some \(a\) works.

As of present the record of factoring numbers with Shor’s algorithm is only factoring 21 as 3 times 7. So at least for now implementing Shor’s algorithm is very far from being practical.

Note that there is also a similar algorithm for solving the discrete logarithm problem, which in its original form asks to retrieve \(g\text{ mod }\phi(N)\) from \(a\text{ mod }N\) and \(a^g\text{ mod }N\). This problem is also the basis of certain widely used cryptosystems like Diffie Hellman key exchange.