Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

QCQI – Chapter 7 Detailed Summary: Shor’s Algorithm & Phase Estimation

Learning Objectives

  • Understand Quantum Phase Estimation (QPE): circuit, accuracy, and semiclassical variants.

  • Reduce integer factoring to order finding and perform recovery via continued fractions.

  • Implement modular exponentiation xaxmodNx\mapsto a^x \bmod N with repeated squaring; reason about resources.

  • Analyze success probabilities and error sources (aQFT, imperfect order conditions).

Quantum Phase Estimation (QPE)

Given unitary UU with eigenpair Uu=e2πiϕuU|u\rangle=e^{2\pi i \phi}|u\rangle and ϕ[0,1)\phi\in[0,1), QPE estimates the binary expansion ϕ=0.ϕ1ϕ2ϕt\phi=0.\phi_1\phi_2\dots\phi_t. The tt-qubit phase register is prepared in +t|+\rangle^{\otimes t} and controls powers U2kU^{2^k}:

+tuctrl-U2k12t/2y=02t1e2πiϕyyu.\begin{aligned} |+\rangle^{\otimes t}|u\rangle\xrightarrow{\text{ctrl-}U^{2^k}} \frac{1}{2^{t/2}}\sum_{y=0}^{2^t-1} e^{2\pi i \phi y}|y\rangle|u\rangle. \end{aligned}

Applying QFT1\mathrm{QFT}^{-1} on the phase register yields (with high prob.) 2tϕ\lfloor 2^t\phi \rceil.

Accuracy: if ϕ\phi has an exact tt-bit expansion, success is 1. In general, Pr[nearest integer]4/π20.405\Pr[\text{nearest integer}] \ge 4/\pi^2\approx 0.405.

Eureka! Control on U2kU^{2^k} turns global eigenphases into computational bits via QFT1\mathrm{QFT}^{-1}.

Iterative (Semiclassical) Phase Estimation (IPEA)

Measure least significant bits first, feeding forward classically controlled ZZ-rotations to cancel known phases. Requires one control qubit reused tt times plus the eigenstate register.

Eureka! Measurements + classical feed-forward can replace QFT1\mathrm{QFT}^{-1}'s entangling gates.

Order Finding & Shor’s Reduction

For NN odd composite and aa coprime to NN, the order rr of aa modulo NN is the least r>0r>0 such that ar1 (mod N)a^r\equiv 1\ (\mathrm{mod}\ N). Define U:xaxmodNU:|x\rangle\mapsto|ax \bmod N\rangle; then UU has eigenphases ϕ=s/r\phi=s/r. QPE on UU returns ys2t/ry\approx s2^t/r.

Recover rr from y/2ty/2^t via continued fractions; if y2tsr<12r2\left|\frac{y}{2^t}-\frac{s}{r}\right|<\frac{1}{2r^2} the convergent is unique. With t2nt\ge 2n (N<2nN<2^n), this holds with good probability.

From order to factors. If rr is even and ar/2≢1 (mod N)a^{r/2}\not\equiv -1\ (\mathrm{mod}\ N), then gcd(ar/2±1,N) \gcd(a^{r/2}\pm 1, N) are nontrivial factors.

Eureka! Number theory + QPE: the quantum part finds rr; classical post-processing extracts factors.

Modular Exponentiation Circuit (Sketch)

Implement xaxmodNx\mapsto a^x \bmod N by repeated squaring:

axmodN=k=0n1axk2kmodN.\begin{aligned} a^x \bmod N = \prod_{k=0}^{n-1} a^{x_k 2^k}\bmod N. \end{aligned}

For each bit xkx_k, conditionally multiply the work register by the precomputed constant ck=a2kmodNc_k=a^{2^k}\bmod N using reversible modular multiplication (adders + controlled adds, with uncomputation). Controlled-U2U^{2^\ell} are realized by shifting which constants are applied.

Eureka! Exponentiation reduces to a sequence of controlled modular multiplies by fixed constants ckc_k.

Resource Sketch (Factoring NN with n=log2Nn=\lceil\log_2 N\rceil bits)

  • Qubits: phase register t2n+O(1)t\approx 2n+O(1); work register nn; ancillas O~(n)\tilde O(n) depending on adder design.

  • Gates: modular exponentiation dominates: O~(n3)\tilde O(n^3) Toffoli/CNOT-level with textbook adders (improvable with advanced arithmetic).

Success Considerations

Random a{2,,N2}a\in\{2,\dots,N-2\} coprime to NN yields even rr and ar/2≢1a^{r/2}\not\equiv-1 with probability 1/2\ge 1/2. Repeat with fresh aa if needed. aQFT and finite tt only slightly reduce success; classical continued fractions are robust.

Worked Nuggets

Small-NN example: For N=15N=15, a=2a=2 has order r=4r=4; 2r/2=4≢12^{r/2}=4\not\equiv-1; gcd(4±1,15)={3,5}\gcd(4\pm1,15)=\{3,5\}.

Continued fractions criterion: If ϕsr<12r2\left|\phi-\frac{s}{r}\right|<\frac{1}{2r^2} then sr\frac{s}{r} is a convergent of ϕ\phi’s CF expansion.

IPEA angle update: when estimating bit kk (LSB first), subtract the phase implied by bits already measured.

Pseudo-code Aids

Pseudocode — Quantum Phase Estimation (standard)

Input: unitary UU, eigenstate u|u\rangle, tt phase qubits Prepare +tu|+\rangle^{\otimes t}|u\rangle for k=0k=0 to t1t-1: apply controlled-U2kU^{2^k} with control qubit kk apply QFT1\mathrm{QFT}^{-1} to the tt controls; measure to get y~\tilde y return ϕ~=y~/2t\tilde \phi = \tilde y / 2^t

Pseudocode — Iterative Phase Estimation (IPEA)

Input: U,uU,|u\rangle, tt bits for j=tj=t down to 1: prepare control in +|+\rangle apply controlled-U2j1U^{2^{j-1}} apply RzR_z to cancel phases from previously obtained bits apply HH; measure to obtain bit bjb_j

Pseudocode — Shor (Factoring NN)

Repeat: pick random a{2,,N2}a\in\{2,\dots,N-2\}; if gcd(a,N)1\gcd(a,N)\neq 1, return factor run order-finding via QPE on U:xaxmodNU:|x\rangle\mapsto|ax\bmod N\rangle to get rr if rr odd or ar/21 (mod N)a^{r/2}\equiv -1 \ (\mathrm{mod}\ N): continue else return gcd(ar/2±1,N)\gcd(a^{r/2}\pm1,N)

Schematics

QPE Circuit (concept)

Order-Finding Post-Processing

Hands-on Notebook (Multi-Backend)

Run: L09_QCQI_Ch07_Shor_Algorithm_and_Phase_Estimation

In the first cell set backend to one of: cirq, pennylane, braket, pyquil, or qiskit.

Mini-Lab Ideas

  1. QPE on U=diag(1,e2πiϕ)U=\mathrm{diag}(1,e^{2\pi i\phi}) and compare to true ϕ\phi.

  2. IPEA vs standard QPE: shots vs. accuracy trade-off.

  3. Order-finding for N=15,21N=15,21 with small aa; recover rr from CF and verify gcd(ar/2±1,N)\gcd(a^{r/2}\pm1,N).