Q&A-1How does Shor's algorithm reduce integer factorization to order finding, and what are the exact classical and quantum steps involved?
Answer
The 90-second version. Shor's algorithm never factors $N$ directly. It reduces factoring to order finding (given $a$ coprime to $N$, find the least $r > 0$ with $a^r \equiv 1 \pmod N$) and order finding is the only step that needs a quantum computer. The reduction: if $r$ is even and $a^{r/2} \not\equiv -1 \pmod N$, then $N$ divides $(a^{r/2}-1)(a^{r/2}+1)$ without dividing either factor, so $\gcd(a^{r/2}\pm 1, N)$ splits $N$. For random $a$ and $N$ with $k \ge 2$ distinct odd prime factors, the probability that $r$ is even and $a^{r/2} \not\equiv -1$ is at least $1 - 1/2^{k-1}$, so a couple of retries suffice. Everything except the order-finding subroutine is classical and cheap.
Classical steps.
- Reject even $N$, perfect powers $N = p^m$, and primes: Miller–Rabin plus integer $k$-th roots for $k \le \log_2 N$.
- Draw random $a \in [2, N-1]$; compute $\gcd(a, N)$. If $> 1$, you already have a factor; stop.
- After the quantum measurement returns $s$, run the continued-fraction expansion of $s/2^t$ and take convergents $p/q$ with $q < N$. Test $a^q \equiv 1 \pmod N$; the smallest such $q$ is $r$.
- If $r$ is odd or $a^{r/2} \equiv -1 \pmod N$, discard $a$ and go to 2.
- Otherwise output $\gcd(a^{r/2}-1, N)$ and $\gcd(a^{r/2}+1, N)$.
Quantum steps. Two registers: $t = 2n$ qubits ($n = \lceil \log_2 N \rceil$) in the first, $n$ qubits in the second, initialized $|0\ldots0\rangle|1\rangle$.
- Hadamard the first register: $\frac{1}{\sqrt{2^t}} \sum_{x=0}^{2^t-1} |x\rangle|1\rangle$.
- Controlled modular exponentiation: $|x\rangle|y\rangle \to |x\rangle|y \cdot a^x \bmod N\rangle$. This is the expensive part: $O(n)$ controlled modular multiplications, each $O(n^2)$ gates with schoolbook arithmetic, $O(n^2 \log n \log\log n)$ with fast multiplication.
- Inverse QFT on the first register, then measure it. The outcome $s$ concentrates near $2^t k/r$ for integers $k$ coprime to $r$, with $|s/2^t - k/r| \le 1/2^{t+1} < 1/(2r^2)$: exactly Legendre's condition for $k/r$ to be a convergent of $s/2^t$.
Worked trace, $N = 15$, $a = 7$. $n = 4$, $t = 8$, $2^t = 256$. Order of 7 mod 15: $7, 4, 13, 1$, so $r = 4$. Phase estimation peaks at $s \approx 256k/4$ for $k \in {1,3}$, i.e. $s = 64$ or $192$. Continued fractions on $64/256 = 1/4$ gives $q = 4$; check $7^4 = 2401 \equiv 1 \pmod{15}$. $r$ is even and $7^2 = 49 \equiv 4 \not\equiv -1$. $\gcd(4-1, 15) = 3$, $\gcd(4+1, 15) = 5$. Factors 3 and 5.
Where you vary the design.
- Semi-classical QFT (Griffiths–Niu, PRL 76, 3228, 1996). Replace the $t$-qubit inverse QFT with one ancilla qubit, measured and recycled, with the controlled-phase rotations applied classically. RSA-2048 drops from ~6145 logical qubits ($t = 2n+1 = 4097$ plus $n = 2048$) to ~2049. The cost is depth: modular exponentiation is repeated once per bit of the first register, and you need real-time mid-circuit measurement and classical feed-forward, which not every device supports.
- Full QFT. Keeps the exponentiation parallel and needs no feed-forward, at the price of the extra $t-1$ qubits.
- Regev's 2023 factoring algorithm (arXiv:2308.06572) is a different reduction with a different gate/qubit trade-off; it is not a drop-in replacement for the circuit above.
- When classical wins. If $N$ has a small factor, Pollard rho or ECM finds it in negligible time; Shor's deep, $2n+1$-qubit circuit only pays off for balanced semiprimes. And Shor is one instance of the abelian hidden-subgroup framework: the same phase-estimation circuit solves discrete log, so the real design question is "is my problem order finding in a group where I can implement controlled multiplication?"
Failure modes and detection. All three: $r$ odd, $a^{r/2} \equiv -1$, or a measured $s$ whose convergents never satisfy $a^q \equiv 1$: are detected classically in $O(n^2)$ bit operations and cost one retry with a fresh $a$. Expected retries $\le 2$.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.