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 Z/2\mathbb{Z}/2^{\ell} and its circuit, the geometric-sum estimate showing the measured value cc concentrates near multiples of q/rq/r, recovery of rr from cc 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 nn be odd, composite and not a prime power, and let xx be an integer with gcd(x,n)=1\gcd(x,n)=1. Write r=ordn(x)r=\operatorname{ord}_n(x) for the least positive integer with xr1(modn)x^r\equiv 1 \pmod n. Say that xx succeeds if rr is even and gcd(xr/21,n)\gcd(x^{r/2}-1,\,n) is a divisor of nn other than 11 and nn; otherwise xx fails.

Throughout, n=91=713n=91=7\cdot 13.

  1. (a) Compute ord91(3)\operatorname{ord}_{91}(3), ord91(9)\operatorname{ord}_{91}(9) and ord91(90)\operatorname{ord}_{91}(90). For each of the three, say whether it succeeds, and if it does, exhibit the factor of 9191 obtained.
    Hint 1 of 2

    36=729=891+13^6=729=8\cdot 91+1.

    Hint 2 of 2

    Two of the three fail, and they fail for different reasons.

  2. (b) Prove that xx fails if and only if rr is odd or xr/21(modn)x^{r/2}\equiv -1\pmod n. (Both directions; you will need that nn is odd for one of them.)
    Hint 1 of 2

    Factor xr1=(xr/21)(xr/2+1)x^r-1=(x^{r/2}-1)(x^{r/2}+1) and ask what gcd(xr/21,n){1,n}\gcd(x^{r/2}-1,n)\in\{1,n\} would force.

    Hint 2 of 2

    For the converse, if nxr/2+1n\mid x^{r/2}+1, what can a common divisor of nn and xr/21x^{r/2}-1 divide?

  3. (c) Determine exactly how many of the 7272 units xx modulo 9191 fail. (Your answer should be a specific integer; you may use that (Z/p)×(\mathbb{Z}/p)^{\times} is cyclic for pp prime.)
    Hint 1 of 3

    Use the Chinese remainder theorem to write x(u,v)(Z/7)××(Z/13)×x\leftrightarrow(u,v)\in(\mathbb{Z}/7)^\times\times(\mathbb{Z}/13)^\times, and let r1,r2r_1,r_2 be the orders of u,vu,v. How is rr built from r1,r2r_1,r_2?

    Hint 2 of 3

    Let eie_i be the exponent of 22 in rir_i. Show that xx fails exactly when e1=e2e_1=e_2: if e1<e2e_1<e_2 show xr/21(mod7)x^{r/2}\equiv 1 \pmod 7 while xr/21(mod13)x^{r/2}\equiv-1\pmod{13}.

    Hint 3 of 3

    In a cyclic group of order 1212, count the elements whose order has 22-adic valuation 0,1,20,1,2 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) 36=729=891+13^6=729=8\cdot 91+1, and 31,32,33=27,34=81,35=613^1,3^2,3^3=27,3^4=81,3^5=61 are all 1\ne 1, so ord91(3)=6\operatorname{ord}_{91}(3)=6. It is even, 33=27≢1903^3=27\not\equiv -1\equiv 90, and gcd(271,91)=gcd(26,91)=13\gcd(27-1,91)=\gcd(26,91)=13. So x=3x=3 succeeds and yields the factor 1313 (and 91/13=791/13=7).

9=329=3^2 has 93=72919^3=729\equiv 1, and 9,8119,81\ne 1, so ord91(9)=3\operatorname{ord}_{91}(9)=3, which is odd: x=9x=9 fails.

90190\equiv -1 has order 22, and 9011(mod91)90^{1}\equiv -1 \pmod{91}, so gcd(901,91)=gcd(89,91)=1\gcd(90-1,91)=\gcd(89,91)=1: x=90x=90 fails.

(b) Suppose rr is even and xr/2≢1x^{r/2}\not\equiv -1. Put y=xr/2y=x^{r/2}, so n(y1)(y+1)n\mid (y-1)(y+1) and let g=gcd(y1,n)g=\gcd(y-1,n). If g=ng=n then xr/21x^{r/2}\equiv 1, contradicting the minimality of rr. If g=1g=1 then ny+1n\mid y+1 (since nn divides (y1)(y+1)(y-1)(y+1) and is coprime to y1y-1), i.e. xr/21x^{r/2}\equiv -1, contradiction. So 1<g<n1<g<n and xx succeeds.

Conversely, if rr is odd then r/2r/2 does not exist and xx fails by definition; and if xr/21x^{r/2}\equiv -1, then any common divisor tt of nn and y1y-1 also divides y+1y+1 (because ny+1n\mid y+1), hence divides (y+1)(y1)=2(y+1)-(y-1)=2; as nn is odd, t=1t=1, so gcd(y1,n)=1\gcd(y-1,n)=1 and xx fails.

(c) By CRT, choosing a unit xx mod 9191 is the same as choosing u(Z/7)×Z6u\in(\mathbb{Z}/7)^\times\cong\mathbb{Z}_6 and v(Z/13)×Z12v\in(\mathbb{Z}/13)^\times\cong\mathbb{Z}_{12} independently. If r1,r2r_1,r_2 are their orders then r=lcm(r1,r2)r=\operatorname{lcm}(r_1,r_2). Write ei=v2(ri)e_i=v_2(r_i), so v2(r)=max(e1,e2)v_2(r)=\max(e_1,e_2).

If e1=e2=ee_1=e_2=e. If e=0e=0 then rr is odd and xx fails. If e1e\ge 1, then v2(r/2)=e1<e=v2(ri)v_2(r/2)=e-1<e=v_2(r_i), so rir/2r_i\nmid r/2 while rirr_i\mid r; hence xr/2x^{r/2} has order exactly 22 modulo each prime, and in a cyclic group the unique element of order 22 is 1-1. So xr/21x^{r/2}\equiv-1 mod 77 and mod 1313, hence mod 9191: xx fails.

If e1e2e_1\ne e_2, say e1<e2e_1<e_2. Then v2(r/2)=e21e1v_2(r/2)=e_2-1\ge e_1 and the odd part of rr is divisible by the odd part of r1r_1, so r1r/2r_1\mid r/2 and xr/21(mod7)x^{r/2}\equiv 1 \pmod 7; while as above xr/21(mod13)x^{r/2}\equiv -1 \pmod{13}. Thus xr/2≢±1(mod91)x^{r/2}\not\equiv\pm1 \pmod{91} and rr is even, so xx succeeds by (b).

So failures are exactly the pairs with e1=e2e_1=e_2. In Z6\mathbb{Z}_6 the number of elements of order dd is φ(d)\varphi(d): orders 1,2,3,61,2,3,6 occur 1,1,2,21,1,2,2 times, so e1=0e_1=0 for 1+2=31+2=3 elements and e1=1e_1=1 for 1+2=31+2=3 elements. In Z12\mathbb{Z}_{12}: orders 1,2,3,4,6,121,2,3,4,6,12 occur 1,1,2,2,2,41,1,2,2,2,4 times, so e2=0e_2=0 for 33 elements, e2=1e_2=1 for 1+2=31+2=3, e2=2e_2=2 for 2+4=62+4=6.

Hence the number of failures is 33+33+06=183\cdot3+3\cdot3+0\cdot 6=18.

So exactly 1818 of the 7272 units fail: a random xx succeeds with probability 3/43/4, comfortably better than the general guarantee 121k=1/21-2^{1-k}=1/2 for k=2k=2 prime factors. (Check: x=3x=3 has (e1,e2)=(1,0)(e_1,e_2)=(1,0), success; x=9x=9 has (0,0)(0,0) and x=90x=90 has (1,1)(1,1), failures — matching (a).)

Problem 2 standard

quantum Fourier transform character orthogonality circuit construction periodic states

Fix q=2q=2^{\ell} and let ω=e2πi/q\omega=e^{2\pi i/q}. The quantum Fourier transform on qq points is the linear map FF on Cq\mathbb{C}^q (with basis 0,,q1|0\rangle,\dots,|q-1\rangle) defined by Fa=1qc=0q1ωacc.F|a\rangle=\frac{1}{\sqrt q}\sum_{c=0}^{q-1}\omega^{ac}\,|c\rangle . We write a=j=01aj2ja=\sum_{j=0}^{\ell-1}a_j2^{j} for the binary expansion, and identify a|a\rangle with a1a0|a_{\ell-1}\rangle\otimes\cdots\otimes|a_0\rangle.

  1. (a) Show that FF is unitary.
    Hint 1 of 2

    Compute cFFc\langle c'|F^{\dagger}F|c\rangle and reduce to evaluating a=0q1ωa(cc)\sum_{a=0}^{q-1}\omega^{a(c-c')}.

    Hint 2 of 2

    Sum a geometric series; the ratio is 11 exactly when qccq\mid c-c'.

  2. (b) Prove the identity Fa=m=10+e2πia/2m12,F|a\rangle=\bigotimes_{m=1}^{\ell}\frac{|0\rangle+e^{2\pi i a/2^{m}}|1\rangle}{\sqrt2}, where the mm-th tensor factor carries the output bit cmc_{\ell-m}. Deduce that FF (up to reversing the order of the output bits) can be implemented by \ell one-qubit gates and (1)/2\ell(\ell-1)/2 two-qubit gates, and say exactly which gates.
    Hint 1 of 3

    Write c=kck2kc=\sum_k c_k2^k and split ωac=ke2πiack/2k\omega^{ac}=\prod_k e^{2\pi i a c_k/2^{\ell-k}}; then the sum over all cc factorises.

    Hint 2 of 3

    Note that e2πia/2me^{2\pi i a/2^{m}} depends only on amod2ma \bmod 2^{m}, i.e. only on the bits a0,,am1a_0,\dots,a_{m-1}.

    Hint 3 of 3

    The factor e2πiam12m1/2m=(1)am1e^{2\pi i a_{m-1}2^{m-1}/2^{m}}=(-1)^{a_{m-1}} is produced by a Hadamard gate on qubit m1m-1; each remaining factor e2πiaj2j/2me^{2\pi i a_j 2^{j}/2^{m}} with j<m1j<m-1 is a phase e2πi/2mje^{2\pi i/2^{m-j}} applied conditionally on aj=1a_j=1.

  3. (c) Take =4\ell=4, so q=16q=16, and let χ=12(3+7+11+15)|\chi\rangle=\tfrac12\big(|3\rangle+|7\rangle+|11\rangle+|15\rangle\big). Compute FχF|\chi\rangle and the resulting probability distribution of the measured value cc. Then do the general case: if rqr\mid q, 0k<r0\le k<r, and χr,k=r/qb=0q/r1br+k|\chi_{r,k}\rangle=\sqrt{r/q}\sum_{b=0}^{q/r-1}|br+k\rangle, determine the distribution of cc after applying FF and measuring. What does the offset kk do?
    Hint 1 of 2

    Pull the phase ωkc\omega^{kc} out of the sum; what remains is a geometric series in bb with ratio ωrc\omega^{rc}.

    Hint 2 of 2

    The series vanishes unless rc0(modq)rc\equiv 0 \pmod q, i.e. unless cc is a multiple of q/rq/r.

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 ωac\omega^{ac} into your fingers now.

Solution

(a) cFFc\langle c'|F^{\dagger}F|c\rangle: the aa-th coordinate of FF^\dagger applied to basis vectors gives a=0q1(1qωac)1qωac=1qa=0q1ωa(cc).\sum_{a=0}^{q-1}\overline{\left(\tfrac{1}{\sqrt q}\omega^{ac'}\right)}\tfrac{1}{\sqrt q}\omega^{ac}=\frac1q\sum_{a=0}^{q-1}\omega^{a(c-c')}. If c=cc=c' every term is 11 and the sum is qq, giving 11. Otherwise ωcc1\omega^{c-c'}\ne1 (as 0<cc<q0<|c-c'|<q) and the geometric series equals ωq(cc)1ωcc1=0\frac{\omega^{q(c-c')}-1}{\omega^{c-c'}-1}=0. Hence the columns of FF are orthonormal and FF is unitary.

(b) Write c=k=01ck2kc=\sum_{k=0}^{\ell-1}c_k2^{k}. Then ωac=e2πiac/2=k=01e2πiack2k/2=k=01(e2πia/2k)ck.\omega^{ac}=e^{2\pi i a c/2^{\ell}}=\prod_{k=0}^{\ell-1}e^{2\pi i a c_k 2^{k}/2^{\ell}}=\prod_{k=0}^{\ell-1}\left(e^{2\pi i a/2^{\ell-k}}\right)^{c_k}. Summing over all cc means summing independently over c1,,c0{0,1}c_{\ell-1},\dots,c_0\in\{0,1\}, so Fa=1qc1,,c0k(e2πia/2k)ckc1c0=k=100+e2πia/2k12,F|a\rangle=\frac{1}{\sqrt q}\sum_{c_{\ell-1},\dots,c_0}\prod_k\left(e^{2\pi i a/2^{\ell-k}}\right)^{c_k}|c_{\ell-1}\cdots c_0\rangle=\bigotimes_{k=\ell-1}^{0}\frac{|0\rangle+e^{2\pi i a/2^{\ell-k}}|1\rangle}{\sqrt2}, which is the claim with m=km=\ell-k.

The output is unentangled: the qubit carrying cmc_{\ell-m} ends in the state (0+e2πia/2m1)/2(|0\rangle+e^{2\pi i a/2^{m}}|1\rangle)/\sqrt2. Since e2πia/2m=e2πi(amod2m)/2m=j=0m1e2πiaj/2mje^{2\pi i a/2^m}=e^{2\pi i (a\bmod 2^m)/2^m}=\prod_{j=0}^{m-1}e^{2\pi i a_j/2^{m-j}}, this phase is built from the input bits a0,,am1a_0,\dots,a_{m-1} only. The term j=m1j=m-1 contributes eiπam1=(1)am1e^{i\pi a_{m-1}}=(-1)^{a_{m-1}}, which is exactly what a Hadamard H=12(1111)H=\frac1{\sqrt2}\begin{pmatrix}1&1\\1&-1\end{pmatrix} applied to qubit am1a_{m-1} produces; the terms j<m1j<m-1 contribute a phase e2πi/2mje^{2\pi i/2^{m-j}} applied precisely when aj=am1=1a_j=a_{m-1}=1, i.e. a two-qubit controlled phase gate diag(1,1,1,eiπ/2mj1)\mathrm{diag}(1,1,1,e^{i\pi/2^{m-j-1}}) on qubits jj and m1m-1. Doing this for each m=1,,m=1,\dots,\ell (in decreasing order of the target qubit, so that a qubit is used as a control only after it has been Hadamarded) costs \ell Hadamards and m=1(m1)=(1)/2\sum_{m=1}^{\ell}(m-1)=\ell(\ell-1)/2 controlled phases: O(2)O(\ell^2) gates. The bit carrying cmc_{\ell-m} sits in position m1m-1, so the output bits come out reversed; reversing them is a classical relabelling (or /2\lfloor \ell/2\rfloor swaps).

(c) For χ|\chi\rangle (so r=4r=4, k=3k=3, q=16q=16) the amplitude of c|c\rangle is 1214b=03e2πi(4b+3)c/16=18e2πi3c/16b=03e2πibc/4.\frac12\cdot\frac14\sum_{b=0}^{3}e^{2\pi i(4b+3)c/16}=\frac18e^{2\pi i\cdot 3c/16}\sum_{b=0}^{3}e^{2\pi i bc/4}. The last sum is 44 if 4c4\mid c and 00 otherwise. So Fχ=12j=03e2πi3(4j)/164j=12(0+i34+i68+i912)F|\chi\rangle=\frac12\sum_{j=0}^{3}e^{2\pi i\cdot 3(4j)/16}|4j\rangle=\frac12\big(|0\rangle+i^{3}|4\rangle+i^{6}|8\rangle+i^{9}|12\rangle\big), and the measured cc is uniform on {0,4,8,12}\{0,4,8,12\}, i.e. on the multiples of q/r=4q/r=4. (Check: four outcomes of probability 1/41/4.)

In general the amplitude of c|c\rangle in Fχr,kF|\chi_{r,k}\rangle is rqωkcb=0q/r1ωbrc,\frac{\sqrt r}{q}\,\omega^{kc}\sum_{b=0}^{q/r-1}\omega^{brc}, and bωbrc\sum_{b}\omega^{brc} equals q/rq/r if rc0(modq)rc\equiv0\pmod q, i.e. if (q/r)c(q/r)\mid c, and vanishes otherwise. Hence the amplitude has modulus 1/r1/\sqrt r on each of the rr multiples c=jq/rc=j\,q/r and 00 elsewhere: cc is uniform on the multiples of q/rq/r. The offset kk only multiplies the amplitudes by the phase ωkc\omega^{kc} and is invisible in the measurement statistics. This is the whole idea of the algorithm in its ideal case: the measured cc satisfies c/q=j/rc/q=j/r 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 n11n\ge 11 be odd and composite, let xx be coprime to nn with multiplicative order r<nr<n, and let q=2q=2^{\ell} be chosen with n2q<2n2n^{2}\le q<2n^{2}. Set ω=e2πi/q\omega=e^{2\pi i/q}. The machine is prepared in Ψ=1qa=0q1axamodn.|\Psi\rangle=\frac{1}{\sqrt q}\sum_{a=0}^{q-1}|a\rangle\,|x^{a}\bmod n\rangle . For zZz\in\mathbb{Z} write {z}q\{z\}_q for the representative of zz modulo qq lying in (q/2,q/2](-q/2,\,q/2].

The second register is measured, then the Fourier transform F:aq1/2cωaccF:|a\rangle\mapsto q^{-1/2}\sum_{c}\omega^{ac}|c\rangle is applied to the first register, which is then measured, giving c{0,,q1}c\in\{0,\dots,q-1\}.

  1. (a) Show that measuring the second register returns xkx^{k} for some kk with 0k<r0\le k<r, and that conditional on this outcome the first register is the uniform superposition over Ak={a:0a<q, ak (mod r)}A_k=\{a: 0\le a<q,\ a\equiv k \ (\mathrm{mod}\ r)\}. Writing m=Akm=|A_k| and δ={rc}q\delta=\{rc\}_q, show that Pr[ck]=1qmb=0m1ωbrc2=1qmsin2(πmδ/q)sin2(πδ/q)(δ0),\Pr[c\mid k]=\frac{1}{qm}\left|\sum_{b=0}^{m-1}\omega^{br c}\right|^{2}=\frac{1}{qm}\cdot\frac{\sin^{2}(\pi m\delta/q)}{\sin^{2}(\pi\delta/q)}\qquad(\delta\neq0), and Pr[ck]=m/q\Pr[c\mid k]=m/q when δ=0\delta=0.
    Hint 1 of 3

    xax^a depends on aa only through amodra \bmod r, so the basis states of the second register occurring in Ψ|\Psi\rangle are x0,,xr1|x^0\rangle,\dots,|x^{r-1}\rangle, and they are distinct.

    Hint 2 of 3

    Substitute a=br+ka=br+k; the factor ωkc\omega^{kc} has modulus 11 and can be discarded, and ωbrc\omega^{brc} depends on rcrc only mod qq.

    Hint 3 of 3

    eiθ1=2sin(θ/2)|e^{i\theta}-1|=2|\sin(\theta/2)|.

  2. (b) Call cc good if {rc}qr/2|\{rc\}_q|\le r/2. Prove that every good cc satisfies Pr[ck]14r\Pr[c\mid k]\ge \frac{1}{4r}, for every kk.
    Hint 1 of 3

    Bound the denominator by sin(πδ/q)πδ/q|\sin(\pi\delta/q)|\le\pi|\delta|/q, and bound mm from below by q/r1q/r-1; note r/q<1/n1/11r/q<1/n\le 1/11.

    Hint 2 of 3

    Put φ=mδ/q\varphi=m|\delta|/q. You would like sin(πφ)2φ\sin(\pi\varphi)\ge2\varphi, but check the range of φ\varphi first: mm can exceed q/rq/r by 11, so φ\varphi can be slightly larger than 1/21/2.

    Hint 3 of 3

    sin(πφ)/φ\sin(\pi\varphi)/\varphi is decreasing on (0,1)(0,1), so it suffices to evaluate it at the largest possible φ\varphi, namely 12(1+1n)611\tfrac12(1+\tfrac1n)\le\tfrac{6}{11}.

  3. (c) Show that there are at least rr good values of cc, one for each d{0,1,,r1}d\in\{0,1,\dots,r-1\}, and that a good cc with associated dd satisfies cqdr12q.\left|\frac{c}{q}-\frac{d}{r}\right|\le\frac{1}{2q}. Deduce that with probability at least 1/41/4 the measured cc satisfies this inequality for some integer dd with 0d<r0\le d<r.
    Hint 1 of 2

    Given dd, take cdc_d to be the nearest integer to dq/rdq/r, and check that {rcd}q=rcddq\{rc_d\}_q=rc_d-dq.

    Hint 2 of 2

    Distinct dd give distinct cdc_d because q/r>n>1q/r>n>1.

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 qn2q\ge n^{2} and r<nr<n are used in different places: one controls the error in mq/rm\approx q/r, the other will be needed in Question 4.

Solution

(a) Since xax^{a} depends only on amodra\bmod r and x0,,xr1x^{0},\dots,x^{r-1} are distinct residues, we may group the sum: Ψ=1qk=0r1(aAka)xk.|\Psi\rangle=\frac1{\sqrt q}\sum_{k=0}^{r-1}\Big(\sum_{a\in A_k}|a\rangle\Big)|x^{k}\rangle . Measuring the second register therefore returns xkx^{k} with probability Ak/q=m/q|A_k|/q=m/q and projects the first register onto m1/2aAkam^{-1/2}\sum_{a\in A_k}|a\rangle, the uniform superposition over Ak={k,k+r,}A_k=\{k,k+r,\dots\}.

Applying FF, the amplitude of c|c\rangle is 1mqb=0m1ω(br+k)c\frac{1}{\sqrt{mq}}\sum_{b=0}^{m-1}\omega^{(br+k)c}. The factor ωkc\omega^{kc} has modulus 11, so Pr[ck]=1qmb=0m1ωbrc2.\Pr[c\mid k]=\frac{1}{qm}\Big|\sum_{b=0}^{m-1}\omega^{brc}\Big|^{2}. Since ωrc=ωδ\omega^{rc}=\omega^{\delta} with δ={rc}q\delta=\{rc\}_q, the sum is mm if δ=0\delta=0; otherwise it is the geometric series e2πimδ/q1e2πiδ/q1,of modulus  sin(πmδ/q)sin(πδ/q),\frac{e^{2\pi i m\delta/q}-1}{e^{2\pi i\delta/q}-1},\qquad\text{of modulus }\ \frac{|\sin(\pi m\delta/q)|}{|\sin(\pi\delta/q)|}, using eiθ1=2sin(θ/2)|e^{i\theta}-1|=2|\sin(\theta/2)|. This gives the stated formulas.

(b) First, m=#{b0:br+k<q}=(qk)/rm=\#\{b\ge0: br+k<q\}=\lceil (q-k)/r\rceil, so qr1qkrmqr+1.\frac{q}{r}-1\le\frac{q-k}{r}\le m\le\frac{q}{r}+1 . Also r<nr<n and qn2q\ge n^{2} give r/q<1/n1/11r/q<1/n\le1/11.

If δ=0\delta=0: Pr[ck]=m/q1r(1rq)1011r>14r\Pr[c\mid k]=m/q\ge \frac1r\big(1-\frac rq\big)\ge\frac{10}{11r}>\frac1{4r}.

If 0<δr/20<|\delta|\le r/2, set φ=mδ/q\varphi=m|\delta|/q. Then φ(qr+1)r2q=12(1+rq)12(1+111)=611.\varphi\le\Big(\frac qr+1\Big)\frac{r}{2q}=\frac12\Big(1+\frac rq\Big)\le\frac12\Big(1+\frac1{11}\Big)=\frac{6}{11}. Using sin(πδ/q)πδ/q=πφ/m|\sin(\pi\delta/q)|\le\pi|\delta|/q=\pi\varphi/m, bωbrc  sin(πφ)πφ/m=mπsin(πφ)φ.\Big|\sum_{b}\omega^{brc}\Big|\ \ge\ \frac{|\sin(\pi\varphi)|}{\pi\varphi/m}=\frac{m}{\pi}\cdot\frac{\sin(\pi\varphi)}{\varphi}. The function φsin(πφ)/φ\varphi\mapsto\sin(\pi\varphi)/\varphi is decreasing on (0,1)(0,1) (its numerator's derivative test: ddφ[sinπφ/φ]=g(φ)/φ2\frac{d}{d\varphi}\big[\sin\pi\varphi/\varphi\big]=g(\varphi)/\varphi^{2} with g(φ)=πφcosπφsinπφg(\varphi)=\pi\varphi\cos\pi\varphi-\sin\pi\varphi, g(0)=0g(0)=0, g(φ)=π2φsinπφ0g'(\varphi)=-\pi^{2}\varphi\sin\pi\varphi\le0). Hence sin(πφ)φ  sin(6π/11)6/11=116sin5π11=1.814,\frac{\sin(\pi\varphi)}{\varphi}\ \ge\ \frac{\sin(6\pi/11)}{6/11}=\frac{11}{6}\sin\frac{5\pi}{11}=1.814\ldots, so bωbrc0.577m\big|\sum_b\omega^{brc}\big|\ge 0.577\,m and Pr[ck]  (0.577)2m2qm=0.333mq  0.3331r(1111)=0.303r>14r.\Pr[c\mid k]\ \ge\ \frac{(0.577)^{2}m^{2}}{qm}=0.333\,\frac mq\ \ge\ 0.333\cdot\frac1r\Big(1-\frac1{11}\Big)=\frac{0.303}{r}>\frac{1}{4r}. (The true asymptotic constant is 4/π2=0.4054/\pi^{2}=0.405\ldots; we have thrown away a little to keep the estimate uniform in kk.)

(c) Fix d{0,,r1}d\in\{0,\dots,r-1\} and let cdc_d be the nearest integer to dq/rdq/r, so cddq/r1/2|c_d-dq/r|\le1/2, i.e. rcddqr/2|rc_d-dq|\le r/2. Since r<n<q/2r<n<q/2, the integer rcddqrc_d-dq lies in (q/2,q/2](-q/2,q/2] and is congruent to rcdrc_d mod qq, so {rcd}q=rcddq\{rc_d\}_q=rc_d-dq and {rcd}qr/2|\{rc_d\}_q|\le r/2: each cdc_d is good. Distinct d,dd,d' give distinct cdc_d, since dq/rdq/rq/r>n>1|dq/r-d'q/r|\ge q/r>n>1 while each cc is within 1/21/2 of its dq/rdq/r. Also 0cdqq/r+1/2<q0\le c_d\le q-q/r+1/2<q, so all rr values are legitimate outcomes.

Dividing rcddqr/2|rc_d-dq|\le r/2 by rqrq gives cd/qd/r1/(2q)|c_d/q-d/r|\le1/(2q).

By (b) each of these rr outcomes has conditional probability at least 1/(4r)1/(4r), whatever kk was observed; hence conditional on any kk, and therefore also unconditionally, Pr[d: c/qd/r1/(2q)]  r14r=14.\Pr\big[\exists d:\ |c/q-d/r|\le 1/(2q)\big]\ \ge\ r\cdot\frac1{4r}=\frac14 .

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 cc with 0c<q0\le c<q such that cqdr12qfor some integer d, 0d<r,\left|\frac{c}{q}-\frac{d}{r}\right|\le\frac{1}{2q}\qquad\text{for some integer } d,\ 0\le d<r, where r<nr<n is the unknown order of xx modulo nn and qq is a power of 22 with n2q<2n2n^{2}\le q<2n^{2}. The only data available to the classical post-processing are cc, qq, nn and xx.

You may use Legendre's theorem: if αR\alpha\in\mathbb{R} and gcd(d,r)=1\gcd(d',r')=1, r1r'\ge1, satisfy αd/r<1/(2r2)|\alpha-d'/r'|<1/(2r'^{2}), then d/rd'/r' is one of the convergents of the continued fraction expansion of α\alpha.

  1. (a) Show that there is at most one rational number with denominator less than nn satisfying the displayed inequality, and that it is a convergent of the continued fraction expansion of c/qc/q. Point out exactly where each of the two hypotheses qn2q\ge n^{2} and r<nr<n is used.
    Hint 1 of 2

    If d/rd/rd/r\ne d'/r' then d/rd/r1/(rr)|d/r-d'/r'|\ge 1/(rr'); compare with what the triangle inequality allows.

    Hint 2 of 2

    For Legendre's theorem you need 1/(2q)<1/(2r2)1/(2q)<1/(2r^2) after reducing d/rd/r to lowest terms — and reducing only decreases the denominator.

  2. (b) Take n=33n=33, x=5x=5 (whose order you should not assume known), q=2048q=2048 and suppose the machine returns c=1229c=1229. Carry out the post-processing: compute the continued fraction expansion of c/qc/q, list its convergents, extract the candidate order, test it, and repair it if it is wrong. Finish the job: produce a nontrivial factor of 3333.
    Hint 1 of 3

    2048=11229+8192048=1\cdot1229+819; 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 <33<33. Test the candidate rr' by computing 5rmod335^{r'}\bmod 33.

    Hint 3 of 3

    If gcd(d,r)>1\gcd(d,r)>1 the convergent gives you a proper divisor of rr, not rr: try 2r,3r,2r',3r',\dots

  3. (c) Combining with Question 3, show that a single run yields the exact order rr with probability at least φ(r)/(4r)\varphi(r)/(4r), where φ\varphi is Euler's totient. Assuming the classical estimate φ(r)/r>δ/loglogr\varphi(r)/r>\delta/\log\log r for an absolute constant δ>0\delta>0 and r3r\ge3, deduce that O(loglogn)O(\log\log n) runs suffice to find rr with probability bounded away from 00.
    Hint 1 of 2

    Which of the rr good values of cc from Question 3(c) let you read off rr itself rather than a proper divisor?

    Hint 2 of 2

    The number of d{0,,r1}d\in\{0,\dots,r-1\} with gcd(d,r)=1\gcd(d,r)=1 is φ(r)\varphi(r); each contributes a distinct good cc.

Hint 1 of 1

The continued fraction step is entirely classical: the quantum computer's only job was to hand you one integer cc.

Solution

(a) Uniqueness. Suppose d/rd/r and d/rd'/r' both satisfy the inequality with 1r,r<n1\le r,r'<n. If they are unequal then 1rrdrdrdrcq+cqdr1q,\frac{1}{rr'}\le\left|\frac dr-\frac{d'}{r'}\right|\le\left|\frac dr-\frac cq\right|+\left|\frac cq-\frac{d'}{r'}\right|\le\frac1q, so rrqn2rr'\ge q\ge n^{2}, contradicting r,rn1r,r'\le n-1. (Here r<nr<n and qn2q\ge n^{2} are both used.)

Recoverability. Write d/r=d/rd/r=d'/r' in lowest terms, so rr<nr'\le r<n and r2<n2qr'^{2}<n^{2}\le q. Then cqdr12q<12r2,\left|\frac cq-\frac{d'}{r'}\right|\le\frac1{2q}<\frac{1}{2r'^{2}}, so by Legendre's theorem d/rd'/r' is a convergent of c/qc/q. The convergents of c/qc/q have strictly increasing denominators and are computed by the Euclidean algorithm in O(logq)O(\log q) steps, so we can list all of them with denominator <n<n and, by uniqueness, the correct one is the last such convergent. This is exactly why qq was chosen with qn2q\ge n^{2}: it makes the window 1/(2q)1/(2q) narrow enough both to pin down d/rd/r uniquely and to trigger Legendre's theorem.

(b) Euclid on 1229/20481229/2048: 2048=11229+819,1229=1819+410,819=1410+409,410=1409+1,409=4091,2048=1\cdot1229+819,\quad1229=1\cdot819+410,\quad819=1\cdot410+409,\quad410=1\cdot409+1,\quad409=409\cdot1, so c/q=[0;1,1,1,1,409]c/q=[0;1,1,1,1,409]. The convergents are 01, 11, 12, 23, 35, 12292048.\frac01,\ \frac11,\ \frac12,\ \frac23,\ \frac35,\ \frac{1229}{2048}. The last one with denominator <33<33 is 3/53/5. So the candidate order is r=5r'=5. Test it: 55=3125=9433+235^{5}=3125=94\cdot33+23, so 5523≢1(mod33)5^{5}\equiv23\not\equiv1\pmod{33} — the candidate is wrong, because the true dd shares a factor with rr. Try small multiples: 510232=529=1633+11(mod33)5^{10}\equiv23^{2}=529=16\cdot33+1\equiv1\pmod{33}, and no proper divisor of 1010 works (52=255^{2}=25, 55=235^{5}=23), so r=10r=10. (Indeed c/q=0.60009c/q=0.60009\ldots and d/r=6/10=0.6d/r=6/10=0.6, with c/q6/10=1/102401/4096=1/(2q)|c/q-6/10|=1/10240\le1/4096=1/(2q).)

Finally r=10r=10 is even and 5523≢132(mod33)5^{5}\equiv23\not\equiv-1\equiv32\pmod{33}, so by Question 1(b) gcd(551,33)=gcd(22,33)=11\gcd(5^{5}-1,33)=\gcd(22,33)=11 is a nontrivial factor: 33=31133=3\cdot11.

(c) By Question 3(c) the good cc's are in bijection with d{0,,r1}d\in\{0,\dots,r-1\}, and by (a) the post-processing recovers d/rd/r in lowest terms, i.e. d/gcd(d,r)d/\gcd(d,r) over r/gcd(d,r)r/\gcd(d,r). The denominator is rr itself exactly when gcd(d,r)=1\gcd(d,r)=1, which happens for φ(r)\varphi(r) values of dd. Each such cc has probability at least 1/(4r)1/(4r) by Question 3(b) (uniformly in the second-register outcome, hence also unconditionally), so Pr[run returns r]  φ(r)14r=φ(r)4r.\Pr[\text{run returns }r]\ \ge\ \varphi(r)\cdot\frac1{4r}=\frac{\varphi(r)}{4r}. With φ(r)/r>δ/loglogr\varphi(r)/r>\delta/\log\log r this is at least δ/(4loglogn)\delta/(4\log\log n), so after t=4δ1loglognt=\lceil 4\delta^{-1}\log\log n\rceil independent runs the probability of never seeing rr is at most (1δ/(4loglogn))te1(1-\delta/(4\log\log n))^{t}\le e^{-1}. Hence O(loglogn)O(\log\log n) runs suffice. (In practice one does much better: testing the small multiples 2r,3r,2r',3r',\dots of each candidate, as in (b), converts most of the failures into successes and reduces the expected number of runs to O(1)O(1).)

Problem 5 challenging

eigenvalue/phase estimation viewpoint reversible computation and garbage entanglement and interference algorithm assembly

Keep the notation of Question 3: nn odd composite, xx coprime to nn of order rr, q=2q=2^{\ell} with n2q<2n2n^{2}\le q<2n^{2}, ω=e2πi/q\omega=e^{2\pi i/q}, and Ψ=1qa=0q1axamodn.|\Psi\rangle=\frac{1}{\sqrt q}\sum_{a=0}^{q-1}|a\rangle\,|x^{a}\bmod n\rangle . Let UU be the linear map on the rr-dimensional space spanned by {xkmodn:0k<r}\{|x^{k}\bmod n\rangle: 0\le k<r\} determined by Uy=xymodnU|y\rangle=|xy\bmod n\rangle.

  1. (a) A careless implementation of modular exponentiation leaves its scratch bits behind, so that instead of Ψ|\Psi\rangle the machine holds Ψ=1qa=0q1axamodng(a),|\Psi'\rangle=\frac1{\sqrt q}\sum_{a=0}^{q-1}|a\rangle\,|x^{a}\bmod n\rangle\,|g(a)\rangle, where gg is injective on {0,,q1}\{0,\dots,q-1\}. Show that applying FF to the first register and measuring it gives a value cc that is exactly uniform on {0,,q1}\{0,\dots,q-1\}. What does this say about the necessity of uncomputing scratch registers?
    Hint 1 of 2

    Write Ψ=q1/2aaϕa|\Psi'\rangle=q^{-1/2}\sum_a|a\rangle|\phi_a\rangle and note what injectivity of gg says about ϕaϕa\langle\phi_a|\phi_{a'}\rangle.

    Hint 2 of 2

    The probability of cc is the squared norm of a vector q1aωacϕaq^{-1}\sum_a\omega^{ac}|\phi_a\rangle; orthonormal summands make cross terms vanish.

  2. (b) For 0s<r0\le s<r put us=r1/2k=0r1e2πisk/rxkmodn|u_{s}\rangle=r^{-1/2}\sum_{k=0}^{r-1}e^{-2\pi isk/r}|x^{k}\bmod n\rangle. Show that Uus=e2πis/rusU|u_s\rangle=e^{2\pi is/r}|u_s\rangle, that {us}s=0r1\{|u_s\rangle\}_{s=0}^{r-1} is orthonormal, and that r1/2sus=1r^{-1/2}\sum_{s}|u_{s}\rangle=|1\rangle.
    Hint 1 of 2

    UU permutes the basis cyclically with period rr.

    Hint 2 of 2

    For the last identity, sum the geometric series over ss for each fixed kk.

  3. (c) Show that Ψ=1rs=0r1ψsus,ψs=1qa=0q1e2πisa/ra,|\Psi\rangle=\frac{1}{\sqrt r}\sum_{s=0}^{r-1}|\psi_{s}\rangle\otimes|u_{s}\rangle,\qquad |\psi_{s}\rangle=\frac1{\sqrt q}\sum_{a=0}^{q-1}e^{2\pi isa/r}|a\rangle, and deduce that the distribution of the measured cc is Pr[c]=1rscFψs2\Pr[c]=\frac1r\sum_{s}\big|\langle c|F|\psi_{s}\rangle\big|^{2}, which is the same whether or not the second register is measured (and the same as the average over kk of the conditional distributions of Question 3). Identify, for each ss, the value of dd near which c/qc/q concentrates.
    Hint 1 of 3

    Invert the definition in (b): express xk|x^{k}\rangle in terms of the us|u_s\rangle, 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 Pr[c]\Pr[c] directly as a squared norm, without conditioning on anything.

    Hint 3 of 3

    cFψs=q1ae2πia(s/r+c/q)\langle c|F|\psi_s\rangle=q^{-1}\sum_a e^{2\pi ia(s/r+c/q)}, which is large when s/r+c/qs/r+c/q is close to an integer.

  4. (d) Assemble the whole algorithm for factoring an odd composite nn 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 xx into one integer cc; everything else — choosing xx, gcds, continued fractions, testing candidate orders — is classical.

    Hint 2 of 2

    Multiply the probability that a random xx is a useful witness (Question 1) by the probability that a run returns rr (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 ϕa=xamodng(a)|\phi_a\rangle=|x^{a}\bmod n\rangle|g(a)\rangle. Since gg is injective, ϕaϕa=δaa\langle\phi_a|\phi_{a'}\rangle=\delta_{aa'}: the second and third registers record aa exactly. After applying FF to the first register, (FI)Ψ=cc(1qaωacϕa),(F\otimes I)|\Psi'\rangle=\sum_{c}|c\rangle\otimes\Big(\frac1q\sum_{a}\omega^{ac}|\phi_a\rangle\Big), so Pr[c]=1qaωacϕa2=1q2a,aω(aa)cϕaϕa=1q2a1=1q.\Pr[c]=\Big\|\frac1q\sum_a\omega^{ac}|\phi_a\rangle\Big\|^{2}=\frac1{q^{2}}\sum_{a,a'}\omega^{(a-a')c}\langle\phi_{a'}|\phi_a\rangle=\frac1{q^{2}}\sum_{a}1=\frac1q . The distribution is uniform: no information about rr whatsoever. Interference is what produces the peaks, and interference between aa and a=a+ra'=a+r 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 aa only through amodra\bmod r.

(b) Uus=r1/2ke2πisk/rxk+1=r1/2ke2πis(k1)/rxk=e2πis/rusU|u_s\rangle=r^{-1/2}\sum_k e^{-2\pi isk/r}|x^{k+1}\rangle=r^{-1/2}\sum_{k'}e^{-2\pi is(k'-1)/r}|x^{k'}\rangle=e^{2\pi is/r}|u_s\rangle (indices mod rr, using xr1x^{r}\equiv1). Orthonormality: usus=r1ke2πi(ss)k/r=δss\langle u_{s'}|u_s\rangle=r^{-1}\sum_k e^{2\pi i(s'-s)k/r}=\delta_{ss'} by the geometric sum. Finally 1rsus=1rk(se2πisk/r)xk=x0=1,\frac1{\sqrt r}\sum_{s}|u_s\rangle=\frac1r\sum_{k}\Big(\sum_{s}e^{-2\pi isk/r}\Big)|x^{k}\rangle=|x^{0}\rangle=|1\rangle, since the inner sum is rr for k=0k=0 and 00 otherwise. (So the easily-prepared state 1|1\rangle is the uniform superposition of all rr eigenvectors — the reason we never need to prepare a single us|u_s\rangle, which would require knowing rr.)

(c) Inverting (b), xk=r1/2se2πisk/rus|x^{k}\rangle=r^{-1/2}\sum_{s}e^{2\pi isk/r}|u_s\rangle. Since xa=xamodrx^{a}=x^{a\bmod r}, Ψ=1qrase2πisa/raus=1rsψsus.|\Psi\rangle=\frac1{\sqrt{qr}}\sum_{a}\sum_{s}e^{2\pi isa/r}|a\rangle|u_s\rangle=\frac1{\sqrt r}\sum_{s}|\psi_s\rangle\otimes|u_s\rangle . Because the us|u_s\rangle are orthonormal, applying FF to the first register and measuring gives Pr[c]=1rscFψsus2=1rscFψs2.\Pr[c]=\Big\|\frac1{\sqrt r}\sum_s\langle c|F|\psi_s\rangle|u_s\rangle\Big\|^{2}=\frac1r\sum_{s}\big|\langle c|F|\psi_s\rangle\big|^{2}. No measurement of the second register was used in this computation. The identical computation with the computational basis {xk}\{|x^{k}\rangle\} in place of {us}\{|u_s\rangle\} gives Pr[c]=kmkqPr[ck]\Pr[c]=\sum_k\frac{m_k}{q}\Pr[c\mid k], 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 cc is the same. (Structurally: the reduced state of the first register is unchanged by a measurement of a disjoint register.)

For each ss, cFψs=q1ae2πia(s/r+c/q)\langle c|F|\psi_s\rangle=q^{-1}\sum_{a}e^{2\pi ia(s/r+c/q)}, whose modulus is large precisely when s/r+c/qs/r+c/q is near an integer, i.e. when c/q(rs)/rc/q\approx (r-s)/r. Thus the algorithm is phase estimation applied to UU on a uniformly random eigenvector us|u_s\rangle, and the dd of Questions 3–4 is ds(modr)d\equiv-s\pmod r; note gcd(d,r)=gcd(s,r)\gcd(d,r)=\gcd(s,r), so the φ(r)/r\varphi(r)/r fraction of Question 4(c) is the chance that the random eigenvector has a primitive phase.

(d) To factor nn (odd, composite, not a prime power):

  1. (classical) pick x{2,,n1}x\in\{2,\dots,n-1\} at random; compute gcd(x,n)\gcd(x,n) — if it exceeds 11 we are done by luck; compute q=2q=2^{\ell} with n2q<2n2n^{2}\le q<2n^{2}.
  2. (quantum) prepare q1/2aa0q^{-1/2}\sum_a|a\rangle|0\rangle by one Hadamard per qubit; compute axamodn|a\rangle|x^{a}\bmod n\rangle by reversible modular exponentiation with all scratch uncomputed (Part (a)); apply FF to the first register (Question 2(b), O(2)O(\ell^{2}) gates); measure, obtaining cc.
  3. (classical) expand c/qc/q in continued fractions, take the last convergent d/rd'/r' with denominator <n<n, and test r,2r,3r,r',2r',3r',\dots as candidate orders by modular exponentiation; if none satisfies xR1x^{R}\equiv1, go back to step 2.
  4. (classical) with rr in hand: if rr is odd or xr/21(modn)x^{r/2}\equiv-1\pmod n, go back to step 1 with a fresh xx; otherwise output gcd(xr/21,n)\gcd(x^{r/2}-1,n), which is a nontrivial factor by Question 1(b).

Probabilities: a random xx is useful with probability at least 121k1/21-2^{1-k}\ge1/2 (k2k\ge2 distinct odd primes dividing nn; Question 1), and each quantum run returns rr with probability at least φ(r)/(4r)=Ω(1/loglogn)\varphi(r)/(4r)=\Omega(1/\log\log n) (Question 4(c)). So the expected number of quantum runs is O(loglogn)O(\log\log n) — and O(1)O(1) in practice with the multiple-testing refinement. Only step 2 is quantum; it consists of O()O(\ell) modular multiplications on \ell-bit numbers plus an O(2)O(\ell^{2})-gate Fourier transform, and it emits a single classical integer cc.