|q⟩ Bad Qubits

intermediate · Programming · Shor's Algorithm & Period Finding

Continued Fractions

After the quantum period-finding circuit runs and you measure the ancilla register, you do not read off the period rr directly. What you get is an integer mm close to a multiple of Q/rQ/r, where QQ is the size of the ancilla register. Recovering rr from mm is a classical step that relies on the continued-fraction algorithm.

What the measurement tells you

Suppose the ancilla has nn qubits, so Q=2nQ = 2^n. The QFT concentrates probability near the multiples kQ/rk\,Q/r for k=0,1,,r1k = 0, 1, \ldots, r-1. A single measurement gives some integer mm that satisfies

mQkr12Q\left|\frac{m}{Q} - \frac{k}{r}\right| \le \frac{1}{2Q}

for some unknown integer kk with 0k<r0 \le k < r. Dividing both sides shows that m/Qm/Q is within 1/(2Q)1/(2Q) of the rational number k/rk/r. Because rNr \le N and we choose QN2Q \ge N^2, this bound is tight enough to uniquely identify k/rk/r from m/Qm/Q via continued fractions.

Continued fractions in one paragraph

Every real number xx has a continued-fraction expansion

x=a0+1a1+1a2+1x = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cfrac{1}{\ddots}}}

where a0=xa_0 = \lfloor x \rfloor and the process repeats on the fractional part. The truncated expansions pj/qjp_j/q_j (called convergents) are the best rational approximations to xx with denominator at most qjq_j. The classical algorithm computes them in O(logQ)O(\log Q) steps using the Euclidean algorithm.

The algorithm step by step

Given integer mm and register size QQ, proceed as follows:

  1. Set the working fraction to m/Qm / Q (track numerator and denominator as integers).
  2. Compute partial quotients ai=num/dena_i = \lfloor \text{num}/\text{den} \rfloor and update via (num,den)(den,  numaiden)(\text{num}, \text{den}) \leftarrow (\text{den},\; \text{num} - a_i \cdot \text{den}).
  3. Build convergent denominators with the recurrence

qi=aiqi1+qi2,q1=0,q0=1.q_i = a_i \, q_{i-1} + q_{i-2}, \quad q_{-1} = 0,\quad q_0 = 1.

  1. Stop at the first qiq_i that exceeds NN; the previous qi1q_{i-1} is the candidate period.
  2. Verify classically that aqi11(modN)a^{q_{i-1}} \equiv 1 \pmod{N}; if not, re-run the circuit.

A concrete example

Take N=15N = 15, a=7a = 7, and n=8n = 8 qubits so Q=256Q = 256. The true period is r=4r = 4 (since 74=24011(mod15)7^4 = 2401 \equiv 1 \pmod{15}). The QFT peaks near multiples of Q/r=64Q/r = 64.

For the measurement m=63m = 63: the fraction is 63/25663/256. Step by step:

We return q1=4q_1 = 4. Indeed 74mod15=17^4 \bmod 15 = 1, so r=4r = 4 is confirmed.

Try it

Implement the continued-fraction convergent finder: given mm, QQ, and bound NN, return the denominator of the best convergent of m/Qm/Q whose denominator does not exceed NN.

Run your code to see the quantum state.

The grader checks that your function returns 44 for the example above (m=63m=63, Q=256Q=256, N=15N=15), which is the period r=4r = 4 of 7xmod157^x \bmod 15.

Sign in on the full site to ask questions and join the discussion.