Quantum Computing Master Course — 079. Order Finding and Shor Algorithm
Lesson 079: Order Finding and Shor Algorithm
Smartyfy Quantum Computing Master Course
Prerequisites and objectives
Shor’s factoring algorithm uses quantum order finding inside a larger classical-and-quantum procedure. The Fourier step does not directly output prime factors. You will connect modular multiplication to eigenphases, work through a small factorization, and identify the classical checks and resource assumptions that complete the algorithm.
Let N be the integer to factor. We discuss the usual nontrivial case of an odd composite that is not a prime power, after appropriate classical preprocessing. This avoids presenting the quantum subroutine as necessary for cases already handled directly.
From a chosen base to its order
Choose an integer a with 1<a<N and compute gcd(a,N). If the gcd is a proper factor, the task has already made progress classically. Otherwise a is invertible modulo N.
The order r of a modulo N is the smallest positive integer satisfying a^r≡1 modulo N. The residues 1,a,a²,… repeat with that period. Finding r is the quantum subproblem.
If r is even, set x=a^(r/2) modulo N. Then x²≡1 modulo N, so N divides (x−1)(x+1). If x is neither +1 nor −1 modulo N, gcd(x−1,N) and gcd(x+1,N) provide nontrivial factors.
Minimality of an even order rules out x=+1. The x=−1 case can occur and causes this attempt to fail. Odd orders also do not support the stated factor-extraction step, so the overall method must allow new choices and retries.
Worked factorization of fifteen
Take N=15 and a=2. The gcd is one. Successive powers modulo fifteen are 1,2,4,8,1, so the order is r=4.
The halfway power is x=2²=4. It is neither one nor fourteen, the residues corresponding to +1 and −1. The two gcd calculations are gcd(4−1,15)=3 and gcd(4+1,15)=5.
| Quantity | Value | Role |
|---|---|---|
| gcd(2,15) | 1 | Base is invertible |
| Order of 2 modulo 15 | 4 | Even period |
| 2^(4/2) modulo 15 | 4 | Nontrivial square root of one |
| gcd(3,15), gcd(5,15) | 3,5 | Recovered factors |
In contrast, choosing a=14 gives order two but halfway value fourteen, or −1 modulo fifteen. The gcds become one and fifteen, providing no proper factor. An even order alone is not sufficient.
The modular-multiplication unitary
Define Ma|z⟩=|az mod N⟩ for residues 0≤z<N. Because gcd(a,N)=1, multiplication permutes those residues and is reversible. On an n-qubit register with 2ⁿ≥N, define its action consistently on unused labels z≥N, for example as identity there.
This extension makes a full unitary on the entire register, rather than an incomplete rule on only part of its basis. The computation starts in |1⟩ and stays within the cyclic orbit generated by a.
Controlled powers Ma^(2^j) correspond to multiplication by a^(2^j) modulo N. These classical constants can be obtained by repeated squaring, and reversible modular-arithmetic circuits implement their action. A power label is not permission to omit those arithmetic costs.
Why phase estimation reveals order information
On the r-element orbit, define |u_s⟩=(1/√r)Σ from k=0 to r−1 of e^(−2πisk/r)|a^k mod N⟩, for s=0,…,r−1. Shifting k by one shows Ma|u_s⟩=e^(2πis/r)|u_s⟩.
The easy initial state |1⟩ is an equal superposition of these orthonormal eigenstates. Phase estimation therefore samples an approximation to s/r, with s uniformly distributed in the ideal eigenbasis decomposition. It is unnecessary to prepare a particular unknown |u_s⟩ separately.
For the fifteen example, r=4. With four control bits, the exactly representable phases are 0,1/4,1/2,3/4, giving labels 0,4,8,12 with equal probabilities. The phase result is information about r, not r itself.
Classical reconstruction and verification
A continued fraction expresses a number through successive integer parts and reciprocals; truncating it gives rational approximations called convergents. A sufficiently accurate phase estimate can be processed this way to find candidate denominators. The general analysis chooses enough precision relative to the size of N and verifies candidates with modular arithmetic.
The fraction s/r may already be reduced. If s and r share a factor, its denominator is smaller than r. In the example, s=2 gives phase 1/2 and denominator two, although the actual order is four. Testing 2² modulo fifteen gives four, not one, so two is rejected as the order.
The s=0 outcome supplies no useful denominator. Additional samples, suitable candidate combinations, or further verified processing may be needed. It would be incorrect to declare the first continued-fraction denominator automatically equal to r.
Even after a valid order is found, the parity and halfway-power checks remain necessary. Finally multiply the returned factors and verify they divide N. Classical verification is part of the algorithm’s correctness, not an optional confidence ritual.
What the efficiency statement includes
Shor’s result gives a quantum algorithm polynomial in the binary length of N under the standard circuit model, including modular arithmetic and classical processing. It does not follow merely from the inverse QFT’s compact gate count.
Practical resources include logical qubits, reversible modular multiplication, controlled-power circuits, repeated attempts, precision, and the overhead needed for reliable physical operations. A tiny compiled demonstration that exploits known factors or a specially simplified instance is not a general large-number factoring implementation.
The result also does not prove every NP problem has an efficient quantum algorithm. Factoring is a specific computational task with exploitable arithmetic structure, and no universal classical lower bound for factoring follows from comparison with elementary trial division.
Four exercises with worked solutions
-
For N=15 and a=6, what happens before order finding?
Solution. gcd(6,15)=3, a proper factor. This choice already yields a factor through a classical calculation, so no quantum order subroutine is needed for that attempt.
-
Why does a=14 fail after finding order two?
Solution. Its halfway power is −1 modulo fifteen. The gcds of thirteen and fifteen with fifteen are one and fifteen, so neither is a proper factor.
-
For a=2 modulo fifteen, what does phase sample 1/2 tell us?
Solution. It is compatible with s/r=2/4 reduced to 1/2. Denominator two is only a candidate; modular verification rejects it as the order because 2²≢1 modulo fifteen.
-
Why define the modular unitary on unused n-qubit labels?
Solution. A unitary must act on the entire Hilbert space. Extending the residue permutation consistently, such as with identity on unused labels, supplies a complete reversible operation.
Five explained multiple-choice questions
-
The quantum subroutine primarily estimates: A. Eigenphases carrying modular-order information. B. Every divisor simultaneously. C. A random classical gcd. D. Prime factors directly.
Answer: A. Classical reconstruction and factor extraction follow the phase measurement.
-
The order r is: A. The number of bits in a. B. Always N. C. The smallest positive exponent with a^r≡1 modulo N. D. Always even.
Answer: C. Minimality defines the repeating modular orbit.
-
An even order guarantees factors without further checks: A. Always. B. No; the halfway power may equal −1. C. Only if N is odd, with no other condition. D. Whenever a exceeds two.
Answer: B. The nontrivial-square-root condition is essential.
-
A reduced phase denominator may be smaller than r because: A. A finite control register always rounds the order downward. B. s and r share a common factor. C. Every denominator must equal the control-register dimension. D. Reversing the measured bit order always preserves the same fraction.
Answer: B. Rational reduction can remove part of the denominator.
-
The general efficiency claim includes: A. Free modular exponentiation. B. Only the QFT gate count. C. No classical processing. D. Reversible arithmetic, phase estimation, and classical processing.
Answer: D. The complete algorithm, not a single subroutine, supports the polynomial-time result.
Pitfalls and summary
Track the chain from modular permutation to eigenphase, rational reconstruction, verified order, and nontrivial factors. Handle unsuccessful samples and base choices explicitly. The small example illustrates the mathematics without hiding the arithmetic and reliability costs of a general implementation.
Primary sources and further study
These sources support the definitions and context. The numerical examples, exercises, and explanations here were prepared for this lesson. Research papers are optional advanced reading.
- Peter Shor: Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer
- IBM Quantum Learning: The phase-estimation procedure and quantum Fourier transform
- Alexei Kitaev: Quantum measurements and the Abelian Stabilizer Problem
- IBM Quantum Learning: Classical computations on quantum computers