Shor's Algorithm: Order Finding, Fourier Transforms and Continued Fractions
A five-problem development of Shor's factoring algorithm in the modern order: Miller's classical reduction of factoring to order-finding, the quantum Fourier transform over and its circuit, the geometric-sum estimate showing the measured value concentrates near multiples of , recovery of from by continued fractions, and the eigenvector (phase-estimation)…
Peter W. Shor — arXiv:quant-ph/9508027
0 of 5 statements machine-checked
Problem 1 warmup
order of an element Chinese remainder theorem reduction of factoring to order-finding
Let be odd, composite and not a prime power, and let be an integer with . Write for the least positive integer with . Say that succeeds if is even and is a divisor of other than and ; otherwise fails.
Throughout, .
- (a) Compute , and . For each of the three, say whether it succeeds, and if it does, exhibit the factor of obtained.
Hint 1 of 2
.
Hint 2 of 2
Two of the three fail, and they fail for different reasons.
- (b) Prove that fails if and only if is odd or . (Both directions; you will need that is odd for one of them.)
Hint 1 of 2
Factor and ask what would force.
Hint 2 of 2
For the converse, if , what can a common divisor of and divide?
- (c) Determine exactly how many of the units modulo fail. (Your answer should be a specific integer; you may use that is cyclic for prime.)
Hint 1 of 3
Use the Chinese remainder theorem to write , and let be the orders of . How is built from ?
Hint 2 of 3
Let be the exponent of in . Show that fails exactly when : if show while .
Hint 3 of 3
In a cyclic group of order , count the elements whose order has -adic valuation respectively. Then take the appropriate inner product of the two count vectors.
Hint 1 of 1
Nothing here is quantum: this is the classical wrapper that turns a period into a factor.
Solution
(a) , and are all , so . It is even, , and . So succeeds and yields the factor (and ).
has , and , so , which is odd: fails.
has order , and , so : fails.
(b) Suppose is even and . Put , so and let . If then , contradicting the minimality of . If then (since divides and is coprime to ), i.e. , contradiction. So and succeeds.
Conversely, if is odd then does not exist and fails by definition; and if , then any common divisor of and also divides (because ), hence divides ; as is odd, , so and fails.
(c) By CRT, choosing a unit mod is the same as choosing and independently. If are their orders then . Write , so .
If . If then is odd and fails. If , then , so while ; hence has order exactly modulo each prime, and in a cyclic group the unique element of order is . So mod and mod , hence mod : fails.
If , say . Then and the odd part of is divisible by the odd part of , so and ; while as above . Thus and is even, so succeeds by (b).
So failures are exactly the pairs with . In the number of elements of order is : orders occur times, so for elements and for elements. In : orders occur times, so for elements, for , for .
Hence the number of failures is .
So exactly of the units fail: a random succeeds with probability , comfortably better than the general guarantee for prime factors. (Check: has , success; has and has , failures — matching (a).)
Problem 2 standard
quantum Fourier transform character orthogonality circuit construction periodic states
Fix and let . The quantum Fourier transform on points is the linear map on (with basis ) defined by We write for the binary expansion, and identify with .
- (a) Show that is unitary.
Hint 1 of 2
Compute and reduce to evaluating .
Hint 2 of 2
Sum a geometric series; the ratio is exactly when .
- (b) Prove the identity where the -th tensor factor carries the output bit . Deduce that (up to reversing the order of the output bits) can be implemented by one-qubit gates and two-qubit gates, and say exactly which gates.
Hint 1 of 3
Write and split ; then the sum over all factorises.
Hint 2 of 3
Note that depends only on , i.e. only on the bits .
Hint 3 of 3
The factor is produced by a Hadamard gate on qubit ; each remaining factor with is a phase applied conditionally on .
- (c) Take , so , and let . Compute and the resulting probability distribution of the measured value . Then do the general case: if , , and , determine the distribution of after applying and measuring. What does the offset do?
Hint 1 of 2
Pull the phase out of the sum; what remains is a geometric series in with ratio .
Hint 2 of 2
The series vanishes unless , i.e. unless is a multiple of .
Hint 1 of 1
Every computation on this sheet reduces to summing a geometric series of roots of unity; it is worth getting the bookkeeping of into your fingers now.
Solution
(a) : the -th coordinate of applied to basis vectors gives If every term is and the sum is , giving . Otherwise (as ) and the geometric series equals . Hence the columns of are orthonormal and is unitary.
(b) Write . Then Summing over all means summing independently over , so which is the claim with .
The output is unentangled: the qubit carrying ends in the state . Since , this phase is built from the input bits only. The term contributes , which is exactly what a Hadamard applied to qubit produces; the terms contribute a phase applied precisely when , i.e. a two-qubit controlled phase gate on qubits and . Doing this for each (in decreasing order of the target qubit, so that a qubit is used as a control only after it has been Hadamarded) costs Hadamards and controlled phases: gates. The bit carrying sits in position , so the output bits come out reversed; reversing them is a classical relabelling (or swaps).
(c) For (so , , ) the amplitude of is The last sum is if and otherwise. So , and the measured is uniform on , i.e. on the multiples of . (Check: four outcomes of probability .)
In general the amplitude of in is and equals if , i.e. if , and vanishes otherwise. Hence the amplitude has modulus on each of the multiples and elsewhere: is uniform on the multiples of . The offset only multiplies the amplitudes by the phase and is invisible in the measurement statistics. This is the whole idea of the algorithm in its ideal case: the measured satisfies exactly, and it carries no information about which coset the second register collapsed to.
Problem 3 challenging
period finding Dirichlet kernel geometric sums measurement statistics
Let be odd and composite, let be coprime to with multiplicative order , and let be chosen with . Set . The machine is prepared in For write for the representative of modulo lying in .
The second register is measured, then the Fourier transform is applied to the first register, which is then measured, giving .
- (a) Show that measuring the second register returns for some with , and that conditional on this outcome the first register is the uniform superposition over . Writing and , show that and when .
Hint 1 of 3
depends on only through , so the basis states of the second register occurring in are , and they are distinct.
Hint 2 of 3
Substitute ; the factor has modulus and can be discarded, and depends on only mod .
Hint 3 of 3
.
- (b) Call good if . Prove that every good satisfies , for every .
Hint 1 of 3
Bound the denominator by , and bound from below by ; note .
Hint 2 of 3
Put . You would like , but check the range of first: can exceed by , so can be slightly larger than .
Hint 3 of 3
is decreasing on , so it suffices to evaluate it at the largest possible , namely .
- (c) Show that there are at least good values of , one for each , and that a good with associated satisfies Deduce that with probability at least the measured satisfies this inequality for some integer with .
Hint 1 of 2
Given , take to be the nearest integer to , and check that .
Hint 2 of 2
Distinct give distinct because .
Hint 1 of 2
Everything is one geometric series; the work is in bounding it honestly rather than replacing it by an integral.
Hint 2 of 2
The two hypotheses and are used in different places: one controls the error in , the other will be needed in Question 4.
Solution
(a) Since depends only on and are distinct residues, we may group the sum: Measuring the second register therefore returns with probability and projects the first register onto , the uniform superposition over .
Applying , the amplitude of is . The factor has modulus , so Since with , the sum is if ; otherwise it is the geometric series using . This gives the stated formulas.
(b) First, , so Also and give .
If : .
If , set . Then Using , The function is decreasing on (its numerator's derivative test: with , , ). Hence so and (The true asymptotic constant is ; we have thrown away a little to keep the estimate uniform in .)
(c) Fix and let be the nearest integer to , so , i.e. . Since , the integer lies in and is congruent to mod , so and : each is good. Distinct give distinct , since while each is within of its . Also , so all values are legitimate outcomes.
Dividing by gives .
By (b) each of these outcomes has conditional probability at least , whatever was observed; hence conditional on any , and therefore also unconditionally,
Problem 4 standard
continued fractions rational approximation Euler totient classical post-processing
Suppose a run of the algorithm of Question 3 has produced an integer with such that where is the unknown order of modulo and is a power of with . The only data available to the classical post-processing are , , and .
You may use Legendre's theorem: if and , , satisfy , then is one of the convergents of the continued fraction expansion of .
- (a) Show that there is at most one rational number with denominator less than satisfying the displayed inequality, and that it is a convergent of the continued fraction expansion of . Point out exactly where each of the two hypotheses and is used.
Hint 1 of 2
If then ; compare with what the triangle inequality allows.
Hint 2 of 2
For Legendre's theorem you need after reducing to lowest terms — and reducing only decreases the denominator.
- (b) Take , (whose order you should not assume known), and suppose the machine returns . Carry out the post-processing: compute the continued fraction expansion of , list its convergents, extract the candidate order, test it, and repair it if it is wrong. Finish the job: produce a nontrivial factor of .
Hint 1 of 3
; keep going with the Euclidean algorithm and read off the partial quotients.
Hint 2 of 3
The convergent you want is the last one whose denominator is . Test the candidate by computing .
Hint 3 of 3
If the convergent gives you a proper divisor of , not : try
- (c) Combining with Question 3, show that a single run yields the exact order with probability at least , where is Euler's totient. Assuming the classical estimate for an absolute constant and , deduce that runs suffice to find with probability bounded away from .
Hint 1 of 2
Which of the good values of from Question 3(c) let you read off itself rather than a proper divisor?
Hint 2 of 2
The number of with is ; each contributes a distinct good .
Hint 1 of 1
The continued fraction step is entirely classical: the quantum computer's only job was to hand you one integer .
Solution
(a) Uniqueness. Suppose and both satisfy the inequality with . If they are unequal then so , contradicting . (Here and are both used.)
Recoverability. Write in lowest terms, so and . Then so by Legendre's theorem is a convergent of . The convergents of have strictly increasing denominators and are computed by the Euclidean algorithm in steps, so we can list all of them with denominator and, by uniqueness, the correct one is the last such convergent. This is exactly why was chosen with : it makes the window narrow enough both to pin down uniquely and to trigger Legendre's theorem.
(b) Euclid on : so . The convergents are The last one with denominator is . So the candidate order is . Test it: , so — the candidate is wrong, because the true shares a factor with . Try small multiples: , and no proper divisor of works (, ), so . (Indeed and , with .)
Finally is even and , so by Question 1(b) is a nontrivial factor: .
(c) By Question 3(c) the good 's are in bijection with , and by (a) the post-processing recovers in lowest terms, i.e. over . The denominator is itself exactly when , which happens for values of . Each such has probability at least by Question 3(b) (uniformly in the second-register outcome, hence also unconditionally), so With this is at least , so after independent runs the probability of never seeing is at most . Hence runs suffice. (In practice one does much better: testing the small multiples of each candidate, as in (b), converts most of the failures into successes and reduces the expected number of runs to .)
Problem 5 challenging
eigenvalue/phase estimation viewpoint reversible computation and garbage entanglement and interference algorithm assembly
Keep the notation of Question 3: odd composite, coprime to of order , with , , and Let be the linear map on the -dimensional space spanned by determined by .
- (a) A careless implementation of modular exponentiation leaves its scratch bits behind, so that instead of the machine holds where is injective on . Show that applying to the first register and measuring it gives a value that is exactly uniform on . What does this say about the necessity of uncomputing scratch registers?
Hint 1 of 2
Write and note what injectivity of says about .
Hint 2 of 2
The probability of is the squared norm of a vector ; orthonormal summands make cross terms vanish.
- (b) For put . Show that , that is orthonormal, and that .
Hint 1 of 2
permutes the basis cyclically with period .
Hint 2 of 2
For the last identity, sum the geometric series over for each fixed .
- (c) Show that and deduce that the distribution of the measured is , which is the same whether or not the second register is measured (and the same as the average over of the conditional distributions of Question 3). Identify, for each , the value of near which concentrates.
Hint 1 of 3
Invert the definition in (b): express in terms of the , then swap the order of summation.
Hint 2 of 3
Two orthonormal decompositions of the same vector give the same reduced statistics for the first register — compute directly as a squared norm, without conditioning on anything.
Hint 3 of 3
, which is large when is close to an integer.
- (d) Assemble the whole algorithm for factoring an odd composite that is not a prime power, using Questions 1, 3 and 4. State the expected number of quantum runs, and say for each step whether it is executed on the quantum computer or on the classical one.
Hint 1 of 2
The quantum computer is used only to turn one integer into one integer ; everything else — choosing , gcds, continued fractions, testing candidate orders — is classical.
Hint 2 of 2
Multiply the probability that a random is a useful witness (Question 1) by the probability that a run returns (Question 4(c)).
Hint 1 of 1
Parts (a) and (c) are both computations of the same kind: the statistics of the first register depend on the second only through the inner products of the states it is correlated with.
Solution
(a) Write . Since is injective, : the second and third registers record exactly. After applying to the first register, so The distribution is uniform: no information about whatsoever. Interference is what produces the peaks, and interference between and requires the rest of the machine to be in the same state for both. Hence the modular exponentiation must be made reversible and its scratch bits uncomputed (Bennett's trick: compute, copy the answer, run the computation backwards), leaving the second register depending on only through .
(b) (indices mod , using ). Orthonormality: by the geometric sum. Finally since the inner sum is for and otherwise. (So the easily-prepared state is the uniform superposition of all eigenvectors — the reason we never need to prepare a single , which would require knowing .)
(c) Inverting (b), . Since , Because the are orthonormal, applying to the first register and measuring gives No measurement of the second register was used in this computation. The identical computation with the computational basis in place of gives , the average of the conditional distributions of Question 3. So the second register may be measured, or not, or measured later: the marginal law of is the same. (Structurally: the reduced state of the first register is unchanged by a measurement of a disjoint register.)
For each , , whose modulus is large precisely when is near an integer, i.e. when . Thus the algorithm is phase estimation applied to on a uniformly random eigenvector , and the of Questions 3–4 is ; note , so the fraction of Question 4(c) is the chance that the random eigenvector has a primitive phase.
(d) To factor (odd, composite, not a prime power):
- (classical) pick at random; compute — if it exceeds we are done by luck; compute with .
- (quantum) prepare by one Hadamard per qubit; compute by reversible modular exponentiation with all scratch uncomputed (Part (a)); apply to the first register (Question 2(b), gates); measure, obtaining .
- (classical) expand in continued fractions, take the last convergent with denominator , and test as candidate orders by modular exponentiation; if none satisfies , go back to step 2.
- (classical) with in hand: if is odd or , go back to step 1 with a fresh ; otherwise output , which is a nontrivial factor by Question 1(b).
Probabilities: a random is useful with probability at least ( distinct odd primes dividing ; Question 1), and each quantum run returns with probability at least (Question 4(c)). So the expected number of quantum runs is — and in practice with the multiple-testing refinement. Only step 2 is quantum; it consists of modular multiplications on -bit numbers plus an -gate Fourier transform, and it emits a single classical integer .