|q⟩ Bad Qubits

intermediate · Programming · Shor's Algorithm & Period Finding

Checkpoint: Period Finding

This checkpoint consolidates the entire Shor module. You will simulate the decisive quantum step of period finding: starting from the periodic superposition that emerges after measuring the output register, you apply the inverse Quantum Fourier Transform and observe that the measurement probabilities concentrate at integer multiples of Q/rQ/r, directly revealing the period rr.

The setup

In a full execution of the period-finding subroutine (§4 of the prior lesson) the circuit produces, after measuring the ancilla register, an equally-spaced superposition in the input register. For a function with period r=2r = 2 on an n=3n = 3-qubit (Q=23=8Q = 2^3 = 8) register the surviving states are those xx with axv(modN)a^x \equiv v \pmod{N}, spaced by r=2r = 2:

14(0+2+4+6).\frac{1}{\sqrt{4}}\bigl(|0\rangle + |2\rangle + |4\rangle + |6\rangle\bigr).

In binary (qubit 0 most significant): states 000,010,100,110|000\rangle, |010\rangle, |100\rangle, |110\rangle. Qubit 2 is always 00; qubits 0 and 1 range over all combinations. This is exactly the state produced by HHIH \otimes H \otimes I applied to 000|000\rangle.

Why the inverse QFT reveals the period

The inverse QFT maps the equally-spaced superposition to a new superposition concentrated at integer multiples of Q/rQ/r. For a uniform superposition over M=Q/rM = Q/r equally-spaced inputs {0,r,2r,,(M1)r}\{0, r, 2r, \ldots, (M-1)r\}:

QFT(1Mk=0M1kr)=1rj=0r1jQr.\mathrm{QFT}^\dagger \left(\frac{1}{\sqrt{M}}\sum_{k=0}^{M-1}|kr\rangle\right) = \frac{1}{\sqrt{r}}\sum_{j=0}^{r-1} \left|\frac{j \cdot Q}{r}\right\rangle.

For r=2r = 2 and Q=8Q = 8 the two peaks are at j8/2=0j \cdot 8 / 2 = 0 and 44. Each has probability 1/r=1/21/r = 1/2, so measuring the input register returns 00 or 44 with equal probability. Dividing by Q=8Q = 8 gives the fraction j/r{0,1/2}j/r \in \{0, 1/2\}; continued-fraction expansion of 1/21/2 immediately yields the denominator r=2r = 2.

The 3-qubit inverse QFT circuit

The forward 3-qubit QFT (as built in the module 2 checkpoint) is:

QFT3=SWAP02H2CP ⁣(π2)21H1CP ⁣(π4)20CP ⁣(π2)10H0.\text{QFT}_3 = \mathrm{SWAP}_{02} \cdot H_2 \cdot CP\!\left(\tfrac{\pi}{2}\right)_{2 \to 1} \cdot H_1 \cdot CP\!\left(\tfrac{\pi}{4}\right)_{2 \to 0} \cdot CP\!\left(\tfrac{\pi}{2}\right)_{1 \to 0} \cdot H_0.

Wait — the forward QFT is built left to right as: H0,CP(π/2)10,CP(π/4)20,H1,CP(π/2)21,H2,SWAP02H_0, CP(\pi/2)_{1\to 0}, CP(\pi/4)_{2\to 0}, H_1, CP(\pi/2)_{2\to 1}, H_2, \mathrm{SWAP}_{02}.

Its inverse QFT3\mathrm{QFT}_3^\dagger reverses the gate order and takes the adjoint of each gate (H=HH^\dagger = H, CP(λ)=CP(λ)CP(\lambda)^\dagger = CP(-\lambda), SWAP=SWAP\mathrm{SWAP}^\dagger = \mathrm{SWAP}):

QFT3=H0CP ⁣(π2)10CP ⁣(π4)20H1CP ⁣(π2)21H2SWAP02.\mathrm{QFT}_3^\dagger = H_0 \cdot CP\!\left(-\tfrac{\pi}{2}\right)_{1 \to 0} \cdot CP\!\left(-\tfrac{\pi}{4}\right)_{2 \to 0} \cdot H_1 \cdot CP\!\left(-\tfrac{\pi}{2}\right)_{2 \to 1} \cdot H_2 \cdot \mathrm{SWAP}_{02}.

Reading the circuit in application order (first to last):

  1. SWAP(0,2)\mathrm{SWAP}(0, 2)
  2. H(2)H(2)
  3. CP(π/2, 2, 1)CP(-\pi/2,\ 2,\ 1)
  4. H(1)H(1)
  5. CP(π/4, 2, 0)CP(-\pi/4,\ 2,\ 0)
  6. CP(π/2, 1, 0)CP(-\pi/2,\ 1,\ 0)
  7. H(0)H(0)

Try it

The starter code already prepares the periodic superposition. Add the seven inverse-QFT gates. The grader checks the output distribution — it must place probability 1/21/2 on state 000|000\rangle (index 0) and 1/21/2 on state 100|100\rangle (index 4), exactly as the period-finding theory predicts.

Run your code to see the quantum state.

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