bloch-sphere-0

Shor's Algorithm

We're here! We've made it. Finally after all the build-up, we're finally ready to see how quantum computers can factor large numbers into prime factors!

And quite surprisingly, this may be one of the shortest articles. We won't even use any quantum computing here!

Being able to factor large numbers is hugely important since it forms the backbone of all modern encryption. Specifically, RSA encryption uses a system where we define some number NN such that N=p×qN = p \times q where pp and qq are large prime numbers. NN forms the "public" key that is exposed to everyone and pp and qq form the "private" that should be kept secret.

So we can see that having some machine that could factor composite numbers quickly would allow us to easily obtain the private key from the public key.

We won't go into the specifics of RSA encryption here, but just know that factoring NN would give an attacker the ability to read any encrypted message sent to the owner of the private keys.

Shor's algorithm was first proposed by Peter Shor in 1994. In fact, we've already seen half of it. In his original paper: "Algorithms for Quantum Computation: Discrete Logarithms and Factoring" he first presented the fast quantum algorithm for solving the order-finding problem and then showed that that algorithm gives us a way to do factorisation.

Let's see how we can use order-finding to factor NN!

We start by choosing some random value aa such that 1<a<N1 < a < N. We then calculate the greatest common divisor of aa and NN. If the greatest common divisor is not 1, then we have found a factor of NN (either pp or qq) already, so we can stop here. If the greatest common divisor is 1, then we move on to the next step.

Since aa and NN are co-prime we know that there must exist some rr such that:

ar1 (mod N)a^r \equiv 1\ (\text{mod}\ N)

This is our order-finding problem. We simply use our fast quantum algorithm from the previous article to find rr.

If rr turns out to be odd, we're out of luck and we must try another value of aa. Once we've got an even value of rr we know that r=2mr=2m for some mm so we have:

a2m1 (mod N)(am)21 (mod N)(am)210 (mod N)(am+1)(am1)0 (mod N)a^{2m} \equiv 1\ (\text{mod}\ N) \\[3ex] (a^m)^2 \equiv 1\ (\text{mod}\ N) \\[3ex] (a^m)^2 - 1 \equiv 0\ (\text{mod}\ N) \\[3ex] (a^m + 1)(a^m - 1) \equiv 0\ (\text{mod}\ N)

This looks really promising as we have two integers on the left hand side which multiply to a value that is congruent to 00 mod NN. This is another way of saying that NN divides (am+1)(am1)(a^m + 1)(a^m - 1). More specifically:

(am+1)(am1)N=k\frac{(a^m + 1)(a^m - 1)}{N} = k

where kk is an integer.

This must mean that pp and qq appear somewhere in the prime factorisation of the numerator since N=p×qN = p \times q.

We will use an already existing fast classical algorithm to compute the greatest common divisor (we will use the term gcd\gcd from now on) of NN and am+1a^m + 1. If gcd(N,am+1)=N\gcd(N, a^m + 1) = N (in other words, am+1a^m + 1 is a multiple of NN) then it unfortunately means we have both pp and qq in the factorisation of am+1a^m + 1 so we can't extract any factors and we have to start our algorithm again from the begining.

Taking a look at the other term, it is actually always the case that gcd(N,am1)N\gcd(N, a^m - 1) \neq N. This is because if am1a^m - 1 was a multiple of NN then we would have am1 (mod N)a^m \equiv 1\ (\text{mod}\ N) which would mean that, sincem=r/2m = r/2 and thus m<rm < r, we would have found a smaller order of aa and NN. Our order-finding algorthim is always guaranteed to give us the smallest possible order so this would be a contradiction.

Ok, so we're now at a point where neither am+1a^m + 1 or am1a^m - 1 are a multiple of NN. But since their product is divisible by NN it must be the case that one of them is a multiple of pp and the other a multiple of qq.

This means calculating gcd(N,am+1)\gcd(N, a^m + 1) and gcd(N,am1)\gcd(N, a^m - 1) will now give us our factors pp and qq.

Exercise

Apply the above algorithm to factor the number 1515.

Show solution

This is amazing! Our fast order-finding quantum algorithm has given us a tool to factor large numbers into their prime factors without needing any extra quantum algorithm!

Now if NN had more than two prime factors we might require some more work to find the other factors, but the process remains more-or-less the same. The final two gcd\gcd operations will always yield two non-trivial (not 11 or NN) factors.