Given its importance in the field of (post-)quantum cryptography (and thus in the real world), as well as its fame, it would be remiss of us to not dedicate at least one section in this book to Shor’s algorithm.
However, as you will see, the actual quantum part of the algorithm is exactly the order-finding circuit from Section 10.10; the rest is an application of classical number theory to reduce the problem of prime factorisation to the problem of hidden-order determination.
Because of this, we will not spend too much time on the details — this is not a book on classical number theory! — but we hope that this section at least highlights the important fact which is that other areas of mathematics and physics have much to offer for the aspiring quantum information scientist.
Of course, it is infeasible to try to learn everything in mathematics, but this is also exactly the point: it’s good to be aware that there is a lot of it out there, and that we all have a lot to learn from each others’ specialities.
The purpose of Shor’s algorithm232 is to find the prime factorisation of a composite integer.
This is a classically difficult problem, and hence forms the basis of some very well-known public-key cryptography schemes, such as RSA (see Exercise 10.12.1), but Shor’s algorithm offers a distinct speed-up.
More precisely, to factor an integer R classically with the general number field sieve method is a sub-exponential problem (i.e. somewhere between polynomial in R and exponential in R), whereas Shor’s algorithm is polylogarithmic (i.e. polynomial in logR).
This means that Shor’s algorithm witnesses integer factorisation as a bounded-error quantum polynomial time problem: it lives in BQP (recall Section 1.9).
Since any integer has a unique (up to ordering) prime factorisation233 consisting of finitely many prime numbers, it suffices to find a single prime factor, since then we can simply divide our original number by this and run the algorithm over and over until it terminates, recording the prime factors that we find along the way.
But still, turning the problem of “find a prime factor” into a problem that we have already solved (namely hidden-order determination) requires some work, so let’s get started.
Recall that the greatest common divisorgcd(m,n) (or highest common factorhcf(m,n)) of a pair of integers (m,n) is the largest integer that divides both of them.
This can be efficiently computed by a (very old) classical algorithm known as Euclid’s algorithm, so we can make use of it in freely.
In fact, let us now list the things that we can do efficiently with classical algorithms:
compute the hcf of two integers (Euclid’s algorithm)
determine if an integer is even (check if the last digit is a 0 in its binary expansion)
determine if an integer R can be written as R=ab for some integers a⩾1 and b⩾2 (Exercise 10.12.6).
One more small note of terminology: throughout this section, we say factor to mean non-trivial factor, i.e. the factors of R are the natural numbers that divide Rapart from1 and R.
Lemma.
Let x and R be natural numbers such that
R is not prime;
1<x<R−1;
x2≡1modR.
Then both hcf(x−1,R) and hcf(x+1,R) are factors of R.
Note that 1<x<R−1 is equivalent to saying that x≡±1modR.
In other words, we assume x to be a non-trivial square root of 1 modulo R.
Proof. The hypothesis tells us that
x2−1=kR
for some integer k, and so factoring x2−1 tells us that
R∣(x+1)(x−1)
(where we write a∣b to mean that a divides b).
Now, since x<R−1, we know that x+1<R and so R cannot divide x+1 because it is simply too big.
This means, in particular, that hcf(x+1,R)=R.
But now note that if hcf(x−1,R)=1 then, since R∣(x+1)(x−1), it must be the case that R∣x+1, and we have just said that this cannot happen.
The same argument applies if we swap x+1 and x−1, so we have proven that both hcf(x−1,R) and hcf(x+1,R) are factors of R.
The essence of the above proof is much simpler than the length might suggest: we are sort of just saying that R cannot divide either x+1 or x−1 alone, and so must be “split up” into two parts, with one inside x+1 and the other inside x−1.
Or we could appeal directly to the fundamental theorem of arithmetic.
Write R=p1a1…pnan, and now consider each prime factor individually.
Since R∣(x+1)(x−1), we know that p1∣(x+1)(x−1).
A very useful fact234 about prime numbers is that, if p divides a product ab, then either p divides a or p divides b.
So here we see that p1 must divide either x+1 or x−1.
We can continue like this for all the prime factors pi of R, but note that it cannot be the case that all the pi divide onlyx+1 and not x−1, since this would then force p1a1…pnan=R to divide x+1, which we have already said cannot happen (and similarly for all the pi dividing only x−1).
This means that both hcf(x−1,R) and hcf(x+1,R) are non-trivial.
Now let’s state another useful lemma (which we will not prove).
Lemma.
Let R be an odd natural number with m⩾2 distinct prime factors.
If y is chosen uniformly at random from the set of natural numbers that are coprime to and smaller than R, then the probability that the order r of y modulo R is even and satisfies
yr/2≡−1modR
is at least
1−2m−11.
How is this lemma useful to us?
Well, it says that if we randomly pick some 1<y<R satisfying the hypotheses, then with non-zero probability (that increases as R has more and more prime factors) it will be such that we can apply the previous lemma to x=yr/2, almost.
The one problem is that x>y, and so it might also be the case that x>R, and the previous lemma needed the assumption that x<R−1.
But we can fix this!
Lemma.
Let y and R be natural numbers such that
R is not prime;
the order r of y modulo R is even;
yr/2<R;
Then eitheryr/2+1 is divisible by R, or both hcf(yr/2−1,R) and hcf(yr/2+1,R) are factors of R.
Proof. First of all, recall that the order r is the smallest natural number such that
yr≡1modR.
Since r is even, yr/2 is well defined and we can factor
kR=yr−1=(yr/2+1)(yr/2−1)
as before.
Now we have to do something different, because it is no longer necessarily the case that yr/2+1 is smaller than R.
However, we can still show that R does not divide the yr/2−1 term, since if it did then we would have that
yr/2≡1modR
which contradicts the fact that r is the smallest natural number such that this holds.
So either R divides the yr/2+1 term, or it doesn’t; in the latter case, we can then apply exactly the same argument as before to show that both hcf(yr/2−1,R) and hcf(yr/2+1,R) are factors of R.
The fact that this lemma requires R to be odd and also have at least two distinct prime factors means that we can only apply it if we have checked that both of these things are true.
In other words, we need to know that R is not even, and also that R is not of the form pb for some prime p.
But as we’ve already mentioned, both of these properties can be checked by an efficient algorithm by a classical computer.
Now we’re ready to state Shor’s algorithm.
Shor’s algorithm.(Factoring the natural number R.)
Check that R is not prime.
If so, stop and return the factor R.
Check if R is even.
If so, stop and return the factor 2.
Check if R=ab for some integers a⩾1 and b⩾2.
If so, stop and return the factor a.
Uniformly at random pick an integer 1<y<R and evaluate hcf(y,R); check if hcf(y,R)>1.
If so, stop and return the factor hcf(y,R).
Compute the order r of y modulo R via hidden-order determination (from Section 10.10).
Check if r is odd.
If so, go back to step 4.
Check if yr/2≡−1modR.
If so, go back to step 4.
Compute hcf(yr/2−1,R) and hcf(yr/2+1,R).
Stop and return these as factors.
Picking it apart, we see that steps 1 to 3 are simply checking easy cases; steps 4, 6, and 7 are checking that the necessary hypotheses are satisfied in order for step 8 to calculate factors of R.
The only place that we use anything quantum in in step 5, where we have to do hidden-order determination.235
The fact that steps 6 and 7 won’t cause us to get stuck in an endless loop is justified by the fact that they will both be passed over with probability 1−1/(2m−1), as we mentioned above.
There are a few algorithms known by the name of “Shor’s algorithm”, but they are all somewhat related.↩︎
The fact that prime factorisation is essentially unique is so is important that it earns the name of the fundamental theorem of arithmetic.↩︎
This “very useful fact” is secretly just the fact that prime numbers are prime elements in the ring of integers. The usual definition of “prime number” that we are used to (“only divisible by 1 and itself”) actually says that they are irreducible elements. The link between these two concepts requires some knowledge of ring theory.↩︎
Even better, thanks to a result by Ekerå, one can be rather sure that this quantum subroutine will only need to be run a single time.↩︎