Quantum Computation & Cryptography · Lecture 14

Lecture 14
Phase Estimation

Week 7  |  reading the eigenvalue of a unitary to t bits with t control qubits and an inverse QFT  |  reading: Watrous Ch. 7 (phase estimation); Nielsen–Chuang §5.2

Cartoon: timing a turntable by doublingA stick figure holding a stopwatch watches a turntable with a mark on its rim. Four small snapshots show the mark after 1, 2, 4 and 8 turns of a fixed time step. A speech bubble says: each doubling of the waiting time gives me one more binary digit of the speed. each doubling of the waiting time gives meone more binary digit of the speed x1x2x4x8
phase estimation looks at U, U², U⁴, U⁸, ... and reads the eigenphase one bit per doubling
In this lecture
  1. The problem
  2. Kickback, with powers
  3. The circuit, and exact phases
  4. Phases that are not t-bit fractions
  5. How many control qubits?
  6. Inputs that are not eigenvectors
  7. The cost is in the powers
  8. Try it in code
  9. Midterm review checklist
  10. Summary and exercises

Lecture 13 showed that the QFT turns a pattern of phases \(\omega^{xy}\) into a single basis state. This lecture produces such a pattern from an unknown unitary and reads off its eigenvalue. The resulting algorithm, phase estimation, is the engine inside Shor's algorithm (Lecture 15), quantum chemistry's energy estimation, and many other algorithms. By the end you should be able to state the problem, build the circuit, explain why it is exact when the phase has \(t\) bits, compute its output distribution when it does not, choose the number of control qubits for a target precision and confidence, and say what happens when the input is not an eigenvector.

1 — The problem

Every eigenvalue of a unitary has modulus 1 (Lecture 2), so it is a point on the unit circle and can be written \(e^{2\pi i\theta}\) for a unique \(\theta\in[0,1)\): the fraction of a full turn.

definition 14.1 — the phase-estimation problem

Given a unitary \(U\) on \(m\) qubits, which we can apply in controlled form, and a state \(|\psi\rangle\) with

$$U|\psi\rangle=e^{2\pi i\theta}|\psi\rangle,\qquad 0\le\theta\lt1,$$

estimate the eigenphase \(\theta\). We call an estimate \(\tilde\theta\) accurate to \(n\) bits if \(|\tilde\theta-\theta|\lt2^{-n}\), with the distance measured around the circle (so 0.98 and 0.01 are 0.03 apart).

A unitary has the eigenvalue \(-i\). What is its eigenphase \(\theta\in[0,1)\)? And how far apart, around the circle, are the estimates 0.95 and 0.02?

Write \(-i=e^{2\pi i\theta}\): \(-i\) is three quarters of a turn counterclockwise from 1. For the distance, going the short way round passes through 0 (that is, through 1).
\(e^{2\pi i\cdot3/4}=e^{3\pi i/2}=-i\), so \(\theta=\tfrac34\) (not \(-\tfrac14\), which is outside \([0,1)\)). From 0.95 up to 1 and on to 0.02 is \(0.05+0.02=0.07\), not \(0.93\).

Two features make this a quantum problem. First, \(U\) is an operation, not a matrix written down: it may act on hundreds of qubits, so we cannot diagonalise it classically. Second, a global phase is invisible (Lecture 3), so \(U|\psi\rangle\) and \(|\psi\rangle\) give identical measurement statistics. The phase \(e^{2\pi i\theta}\) only becomes visible as a relative phase, which is what control qubits provide.

We hold the eigenvector \(|\psi\rangle\) and apply \(U\) once, with no control qubit. Measured in any basis we like, the state \(U|\psi\rangle\) gives statistics that are .

Is \(e^{2\pi i\theta}\) multiplying one amplitude, or the whole vector?
\(U|\psi\rangle=e^{2\pi i\theta}|\psi\rangle\) differs from \(|\psi\rangle\) by a global phase, which no measurement can see. A control qubit turns it into a relative phase.

2 — Kickback, with powers

Lecture 6's phase kickback (Theorem 6.5) says that a controlled-\(U\) acting on \(|1\rangle\otimes|\psi\rangle\) multiplies the state by the eigenvalue, and that this phase lands on the control. If \(U|\psi\rangle=e^{2\pi i\theta}|\psi\rangle\), then \(U^k|\psi\rangle=e^{2\pi ik\theta}|\psi\rangle\) as well, so a controlled power of \(U\) kicks back a multiple of the phase.

proposition 14.1 — kickback of a power

Let \(U|\psi\rangle=e^{2\pi i\theta}|\psi\rangle\) and let \(C\text{-}U^{k}\) be \(U^{k}\) controlled by one qubit. Then

$$C\text{-}U^{k}\Big(\tfrac{1}{\sqrt2}\big(|0\rangle+|1\rangle\big)\otimes|\psi\rangle\Big)=\tfrac{1}{\sqrt2}\big(|0\rangle+e^{2\pi i\,k\theta}|1\rangle\big)\otimes|\psi\rangle .$$

The target register is unchanged and stays in a product state with the control.

By linearity, \(C\text{-}U^k\) leaves \(|0\rangle|\psi\rangle\) alone and maps \(|1\rangle|\psi\rangle\) to \(|1\rangle U^k|\psi\rangle=e^{2\pi ik\theta}|1\rangle|\psi\rangle\). Adding the two halves and factoring out \(|\psi\rangle\) gives the result.

The phase \(k\theta\) only matters mod 1, and that is where the information is. With \(k=2^j\), multiplying \(\theta=0.b_1b_2b_3\ldots\) (in binary) by \(2^j\) shifts the binary point \(j\) places: \(2^j\theta\equiv0.b_{j+1}b_{j+2}\ldots\pmod1\). So the control of \(C\text{-}U^{2^j}\) carries the digits of \(\theta\) from position \(j+1\) on. Using \(t\) control qubits with \(k=1,2,4,\dots,2^{t-1}\) records \(\theta\) at \(t\) different "zoom levels", just as the turntable in the cartoon is checked after 1, 2, 4 and 8 time steps.

The eigenphase is \(\theta=0.1101\) in binary. The control qubit of \(C\text{-}U^{4}\) ends in \(\tfrac1{\sqrt2}\big(|0\rangle+e^{2\pi i\varphi}|1\rangle\big)\). What is \(\varphi\in[0,1)\)?

Multiplying by \(4=2^2\) shifts the binary point two places; then drop the whole turns (the part before the point).
\(4\theta=11.01_2\equiv0.01_2=\tfrac14\pmod 1\). In decimal: \(\theta=\tfrac{13}{16}\) and \(4\cdot\tfrac{13}{16}=\tfrac{13}{4}=3+\tfrac14\).

3 — The circuit, and exact phases

definition 14.2 — the phase-estimation circuit

Use \(t\) control qubits \(0,\dots,t-1\), each starting in \(|0\rangle\), and a target register holding \(|\psi\rangle\). Write \(T=2^t\).

  1. Apply \(H\) to every control qubit.
  2. For \(j=0,\dots,t-1\), apply \(U^{2^j}\) to the target, controlled by control qubit \(j\).
  3. Apply \(\mathrm{QFT}_T^{-1}\) to the control register and measure it, obtaining an integer \(y\in\{0,\dots,T-1\}\).
  4. Output \(\tilde\theta=y/T\).
The phase-estimation circuit with three control qubitsThree control wires q0, q1, q2 start in |0> and get a Hadamard each. A target register at the bottom holds the eigenvector |psi>. Control q0 controls U, q1 controls U squared, q2 controls U to the fourth. After these gates control qubit j holds |0> + e^(2 pi i 2^j theta)|1>, up to 1/sqrt2. Then an inverse QFT acts on the three controls, and they are measured, giving an integer y with y/8 close to theta. The target register is unchanged.q0|0>Hq1|0>Hq2|0>Htarget|psi>UU^2U^4|0>+e^(2 pi i 1 theta)|1>|0>+e^(2 pi i 2 theta)|1>|0>+e^(2 pi i 4 theta)|1>QFTinversey, with y/8 ~ thetakickback puts the phase 2^j theta on control j; the inverse QFT reads it out as y
Phase estimation with \(t=3\). By Proposition 14.1, after the controlled powers, control \(j\) holds \(\tfrac{1}{\sqrt2}(|0\rangle+e^{2\pi i2^j\theta}|1\rangle)\). The inverse QFT converts these three phases into a 3-bit number \(y\) with \(y/8\approx\theta\). The target register ends exactly where it started.
theorem 14.2 — the state before the inverse QFT, and exact phases

After step 2, the control register is in the state

$$|\phi\rangle=\frac{1}{\sqrt T}\sum_{x=0}^{T-1}e^{2\pi i\,\theta x}\,|x\rangle ,$$

unentangled from the target. If \(\theta=y_0/T\) for an integer \(y_0\) (that is, \(\theta\) has an exact \(t\)-bit binary expansion), then \(|\phi\rangle=\mathrm{QFT}_T|y_0\rangle\), and the algorithm outputs \(y_0\), hence \(\tilde\theta=\theta\), with probability 1.

By Proposition 14.1 control qubit \(j\) ends in \(\tfrac{1}{\sqrt2}\big(|0\rangle+e^{2\pi i\theta 2^j}|1\rangle\big)\). Multiply out the \(t\) factors: the term with control bits \(x_{t-1}\cdots x_0\) picks up the phase \(\prod_je^{2\pi i\theta x_j2^j}=e^{2\pi i\theta x}\), where \(x=\sum_jx_j2^j\). That gives \(|\phi\rangle\). If \(\theta=y_0/T\), then \(e^{2\pi i\theta x}=e^{2\pi i\,y_0x/T}=\omega^{y_0x}\) with \(\omega=e^{2\pi i/T}\), so \(|\phi\rangle=\tfrac{1}{\sqrt T}\sum_x\omega^{y_0x}|x\rangle\), which is \(\mathrm{QFT}_T|y_0\rangle\) by Definition 13.1. Applying \(\mathrm{QFT}_T^{-1}\) gives \(|y_0\rangle\).

Phase estimation with \(t=4\) control qubits is run on an eigenvector with \(\theta=\tfrac{11}{16}\). Which outcome \(y\) does it return, and with what probability?

Is \(\theta\) a multiple of \(1/T\) with \(T=16\)? If so, Theorem 14.2 applies.
\(\theta=\tfrac{11}{16}=y_0/T\) with \(y_0=11\), so the control register is exactly \(\mathrm{QFT}_{16}|11\rangle\) and the inverse QFT returns \(y=11\) with certainty.
example 14.1 — an exact phase, θ = 5/8, t = 3

Suppose \(U|\psi\rangle=e^{2\pi i\cdot5/8}|\psi\rangle\). What does each control qubit hold after the controlled powers, and what does the measurement return?

\(\theta=5/8=0.101\) in binary. Control 0 gets the phase \(e^{2\pi i\cdot5/8}=e^{5\pi i/4}\); control 1 gets \(e^{2\pi i\cdot10/8}=e^{2\pi i/4}=i\) (one whole turn discarded); control 2 gets \(e^{2\pi i\cdot20/8}=e^{\pi i}=-1\). Control 2 is therefore \(|-\rangle\), which records the last bit of \(\theta\), and control 1 is \(|{+i}\rangle\). Since \(\theta=5/8\) is a 3-bit fraction, Theorem 14.2 says the inverse QFT returns \(y=5=101_2\) with certainty, and \(\tilde\theta=5/8\) exactly.

With \(t=1\) the circuit is \(H\), a controlled-\(U\), \(H\): the inverse QFT on one qubit is just \(H\). For \(\theta\in\{0,\tfrac12\}\) it decides which one with certainty, which is Deutsch's algorithm of Lecture 9 in disguise.

4 — Phases that are not t-bit fractions

Most phases are not multiples of \(1/T\). Then \(|\phi\rangle\) is not a Fourier basis state, and the output is random, but concentrated near \(T\theta\).

theorem 14.3 — the output distribution

For every \(\theta\in[0,1)\), phase estimation with \(t\) control qubits returns \(y\) with probability

$$P(y)=\frac{1}{T^2}\Big|\sum_{x=0}^{T-1}e^{2\pi i\,x(\theta-y/T)}\Big|^2=\frac{\sin^2(\pi T\delta)}{T^2\sin^2(\pi\delta)},\qquad\delta=\theta-\frac yT ,$$

with the value 1 when \(\delta\) is an integer.

By Proposition 13.1 the amplitude of \(|y\rangle\) in \(\mathrm{QFT}_T^{-1}|\phi\rangle\) is \(\tfrac{1}{\sqrt T}\sum_x\omega^{-xy}\langle x|\phi\rangle=\tfrac1T\sum_xe^{2\pi ix(\theta-y/T)}\). This is a geometric series with ratio \(q=e^{2\pi i\delta}\). If \(q\neq1\), its sum is \(\frac{q^T-1}{q-1}\), and \(|e^{i\alpha}-1|=2|\sin(\alpha/2)|\) gives \(\big|\frac{q^T-1}{q-1}\big|=\frac{|\sin(\pi T\delta)|}{|\sin(\pi\delta)|}\).

Take \(t=1\) (so \(T=2\)) and \(\theta=0.45\). Use Theorem 14.3 to find the probability of the output \(y=1\). (Three decimals is fine.)

Here \(\delta=0.45-\tfrac12=-0.05\). Use \(\sin(2x)=2\sin x\cos x\) to simplify \(\sin^2(2\pi\delta)/(4\sin^2(\pi\delta))\), or just evaluate it.
\(P(1)=\dfrac{\sin^2(0.1\pi)}{4\sin^2(0.05\pi)}=\cos^2(0.05\pi)=\sin^2(0.45\pi)\approx0.976\). The phase 0.45 is close to \(\tfrac12\), so the one-bit estimate \(\tfrac12\) is very likely.
example 14.2 — θ = 0.3 with three and with six control qubits

Find the most likely outputs for \(\theta=0.3\) with \(t=3\) and with \(t=6\).

With \(t=3\), \(T\theta=2.4\), between the grid points 2 and 3, closer to 2. Theorem 14.3 gives

$$P(2)=0.578,\qquad P(3)=0.259,\qquad P(1)=0.052,\qquad P(4)=0.041,$$

and the remaining four outcomes share about 0.07. The best 3-bit estimate, \(y=2\) (\(\tilde\theta=0.25\)), comes out 58% of the time, and one of the two neighbours of \(2.4\) comes out 84% of the time. With \(t=6\), \(T\theta=19.2\), and \(P(19)=0.875\), \(P(20)=0.055\), \(P(18)=0.024\): the estimate \(19/64\approx0.297\) is much more precise and also more likely, because \(19.2\) sits closer to a grid point than \(2.4\) did.

Phase estimation of theta = 0.3 with 3 and with 6 control qubitsTwo bar charts. Left, t = 3: bars over y = 0 to 7 with the largest at y = 2 (0.578) and the next at y = 3 (0.259); a dashed line marks 8 theta = 2.4. Right, t = 6: bars over y = 0 to 63 with a sharp peak at y = 19 (0.875) and small neighbours; the dashed line marks 64 theta = 19.2. More control qubits make the peak both narrower in theta and taller.t = 3: P(y), peak 0.578 at y = 200.518 theta = 2.401234567t = 6: P(y), peak 0.875 at y = 1900.5164 theta = 19.208192432404856theta = 0.3; the estimate is y / 2^t
Example 14.2. Bars: \(P(y)\) from Theorem 14.3. The dashed line marks \(T\theta\). Each extra control qubit halves the grid spacing; the probability of the best estimate depends on how far \(T\theta\) falls from the nearest grid point.

How bad can it get? The worst case for the nearest grid point is when \(T\theta\) sits exactly half-way between two of them.

theorem 14.4 — the nearest estimate is likely

Let \(y^*\) be the integer nearest to \(T\theta\) (mod \(T\)). Then phase estimation returns \(y^*\) with probability at least \(4/\pi^2\approx0.405\), for every \(\theta\) and every \(t\). It returns one of the two grid points adjacent to \(T\theta\) with probability at least \(8/\pi^2\approx0.81\).

For \(y^*\), \(|\delta|=|\theta-y^*/T|\le\tfrac1{2T}\), so \(\pi T|\delta|\le\tfrac\pi2\). On \([0,\tfrac\pi2]\) the sine lies above its chord: \(\sin u\ge\tfrac2\pi u\). So \(\sin^2(\pi T\delta)\ge4T^2\delta^2\). Also \(\sin^2(\pi\delta)\le\pi^2\delta^2\). Theorem 14.3 gives

$$P(y^*)\ge\frac{4T^2\delta^2}{T^2\pi^2\delta^2}=\frac4{\pi^2}.$$

For the two neighbours \(y_-\le T\theta\le y_+\), with \(T\theta-y_-=\Delta\in[0,1]\): here \(|T\delta|\) is \(\Delta\) for one and \(1-\Delta\) for the other, so \(\sin^2(\pi T\delta)=\sin^2(\pi\Delta)\) for both, and \(\sin^2(\pi\delta)\le\pi^2\delta^2\) gives \(P(y_-)+P(y_+)\ge\tfrac{\sin^2(\pi\Delta)}{\pi^2}\big(\tfrac1{\Delta^2}+\tfrac1{(1-\Delta)^2}\big)\), whose minimum over \(\Delta\) is at \(\Delta=\tfrac12\) and equals \(\tfrac{8}{\pi^2}\).

Tick every statement that holds for every \(\theta\) and every \(t\).

Theorem 14.4 makes two separate claims, one about \(y^*\) alone and one about the pair of neighbours. Neither says the tails vanish.
The guarantees are \(4/\pi^2\approx0.405\) for the nearest point and \(8/\pi^2\approx0.81\) for the two neighbours; the remaining probability can land on far-away outcomes.
Probability of the nearest estimate against the position of thetaCurves of the probability that phase estimation returns the grid point nearest to theta, as a function of where 2^t theta sits between two grid points, from -1/2 to 1/2. Each curve is 1 at the centre, where theta is exactly representable, and falls to its minimum half-way between grid points: 0.5 for t = 1, 0.427 for t = 2 and 0.405 for t = 8. A dashed line marks the limit 4/pi^2 = 0.405, which every curve stays above.0.30.40.50.60.70.80.91.0-0.5-0.250+0.25+0.54/pi^2 = 0.405t = 1t = 2t = 8offset of 2^t theta from the nearest integer
Theorem 14.4: the probability of the nearest grid point, as \(T\theta\) moves from half-way below a grid point (\(-\tfrac12\)) through it (0) to half-way above (\(+\tfrac12\)). The curves for different \(t\) converge to \(\big(\tfrac{\sin\pi u}{\pi u}\big)^2\), whose minimum is \(4/\pi^2\).

Phase-estimation histogram

Choose the eigenphase \(\theta\) and the number of control qubits \(t\). The bars are the exact probabilities \(P(y)\) of Theorem 14.3; the amber line marks \(T\theta\) and the rose line \(4/\pi^2\). Watch the best estimate's probability swing as \(T\theta\) passes through grid points.

5 — How many control qubits?

Theorem 14.4 gives the nearest \(t\)-bit estimate only with probability around 40%. In practice we want \(n\) correct bits with high confidence. Adding a few control qubits beyond \(n\) does both: the extra digits are thrown away, and the probability that the estimate is off by more than \(2^{-n}\) becomes small.

theorem 14.5 — precision and confidence (Cleve, Ekert, Macchiavello and Mosca, 1998)

Let \(n\ge1\) and \(0\lt\varepsilon\lt1\). Phase estimation with

$$t=n+\Big\lceil\log_2\Big(2+\frac{1}{2\varepsilon}\Big)\Big\rceil$$

control qubits returns an estimate with \(|\tilde\theta-\theta|\lt2^{-n}\) with probability at least \(1-\varepsilon\).

Let \(b\) be the integer with \(b\le T\theta\lt b+1\) and write \(T\theta=b+\Delta\), \(0\le\Delta\lt1\). An outcome \(y=b+k\) (with \(k\) taken in \((-T/2,T/2]\)) has \(T\delta=\Delta-k\). Using \(\sin^2(\pi T\delta)\le1\) and \(|\sin(\pi\delta)|\ge2|\delta|\) for \(|\delta|\le\tfrac12\), Theorem 14.3 gives

$$P(b+k)\le\frac{1}{4(\Delta-k)^2}.$$

Fix an integer \(e\ge2\). For \(k\ge e+1\), \(|\Delta-k|\ge k-1\ge e\); for \(k\le-e\), \(|\Delta-k|\ge|k|\ge e\). So

$$P(|y-b|\gt e)\le2\sum_{j\ge e}\frac{1}{4j^2}\le\frac12\cdot\frac{1}{e-1},$$

using \(\sum_{j\ge e}j^{-2}\le\int_{e-1}^\infty u^{-2}\,du\). If \(|y-b|\le e\) then \(|y/T-\theta|\le(|y-b|+\Delta)/T\lt(e+1)/T\). Choose \(e=2^{t-n}-1\), so that \((e+1)/T=2^{-n}\). The failure probability is at most \(\frac{1}{2(2^{t-n}-2)}\), which is at most \(\varepsilon\) exactly when \(2^{t-n}\ge2+\frac1{2\varepsilon}\). (This is the argument of Nielsen–Chuang §5.2.1.)

How many control qubits does Theorem 14.5 prescribe for a 6-bit estimate with failure probability at most \(\varepsilon=0.02\)?

Compute \(2+\frac{1}{2\varepsilon}\), take \(\log_2\), round up, and add \(n\).
\(2+\frac{1}{0.04}=27\), and \(\lceil\log_2 27\rceil=5\) since \(16\lt27\le32\). So \(t=6+5=11\).
example 14.3 — ten bits with 99% confidence

How many control qubits give a 10-bit estimate with probability at least 0.99, and how many applications of \(U\) does that cost if each \(U^{2^j}\) is done as \(2^j\) copies of \(U\)?

\(2+\frac{1}{2\cdot0.01}=52\) and \(\lceil\log_252\rceil=6\), so \(t=10+6=16\). The controlled powers use \(1+2+4+\dots+2^{15}=2^{16}-1=65{,}535\) controlled applications of \(U\). The bound is conservative: for \(n=4\), \(\varepsilon=0.05\), it prescribes \(t=8\), and the exact worst case over all \(\theta\) (computed in verify_L14.py) is a success probability of 0.9875, not just 0.95.

There is a second way to buy confidence: run phase estimation with fewer control qubits several times and take the median of the estimates. Each run is correct to \(n\) bits with probability above \(\tfrac12\) (say 0.8), so by a Chernoff bound the median of \(O(\log(1/\varepsilon))\) runs is correct with probability \(1-\varepsilon\). Fewer qubits per run, more runs: which is better depends on the hardware.

6 — Inputs that are not eigenvectors

Preparing an eigenvector is often as hard as the problem itself. Fortunately phase estimation is linear, so it handles superpositions of eigenvectors gracefully.

proposition 14.6 — superpositions of eigenvectors

Let \(U|\psi_k\rangle=e^{2\pi i\theta_k}|\psi_k\rangle\) for an orthonormal family \(|\psi_k\rangle\), and run phase estimation on \(|\psi\rangle=\sum_kc_k|\psi_k\rangle\). Then the output is distributed as follows: choose \(k\) with probability \(|c_k|^2\), then output \(y\) with the distribution of Theorem 14.3 for \(\theta_k\). If the output is exact, the target collapses to an eigenvector with that phase.

By linearity and Theorem 14.2, before measurement the state is \(\sum_kc_k\,\big(\mathrm{QFT}^{-1}|\phi_k\rangle\big)\otimes|\psi_k\rangle\), where \(|\phi_k\rangle\) is the control state for \(\theta_k\). The amplitude of \(|y\rangle|\psi_k\rangle\) is \(c_k\,a_k(y)\), with \(|a_k(y)|^2\) the probability of Theorem 14.3. Because the \(|\psi_k\rangle\) are orthonormal, \(P(y)=\sum_k|c_k|^2|a_k(y)|^2\), which is the two-stage description. The target registers for different \(k\) never interfere, which is why the mixture is classical.

\(U\) has eigenvectors \(|\psi_1\rangle,|\psi_2\rangle\) with eigenphases \(\tfrac14\) and \(\tfrac34\). Phase estimation with \(t=2\) is run on \(0.6\,|\psi_1\rangle+0.8i\,|\psi_2\rangle\). What is the output distribution?

Both phases are multiples of \(\tfrac14=1/T\), so each branch on its own is exact. What weights does Proposition 14.6 give the branches?
Branch \(k\) is chosen with probability \(|c_k|^2\): 0.36 and \(|0.8i|^2=0.64\). Each branch then returns \(T\theta_k\) exactly: \(y=1\) or \(y=3\).

For example, \(U=P(2\pi\theta)=\mathrm{diag}(1,e^{2\pi i\theta})\) has eigenvectors \(|0\rangle\) (phase 0) and \(|1\rangle\) (phase \(\theta\)). Run on \(|+\rangle\), phase estimation returns 0 half the time and an estimate of \(\theta\) half the time; the second code box below shows it. The crucial use is next lecture.

crypto connection — order finding is phase estimation

Let \(N\) be the RSA modulus to factor and \(a\) a number coprime to \(N\). The unitary \(U_a|y\rangle=|ay\bmod N\rangle\) permutes the residues, and its eigenvalues are \(e^{2\pi is/r}\), where \(r\) is the order of \(a\) (the least \(r\gt0\) with \(a^r\equiv1\bmod N\)) and \(s=0,\dots,r-1\). We cannot prepare any one eigenvector without knowing \(r\), but the easy state \(|1\rangle\) is an equal superposition of them. By Proposition 14.6, phase estimation on \(|1\rangle\) returns an approximation of \(s/r\) for a uniformly random \(s\), and Lecture 15 recovers \(r\) from it with continued fractions. Knowing \(r\) factors \(N\). Everything that makes Shor's algorithm work is already in this lecture except two things: the efficient powers \(U_a^{2^j}\) (Section 7) and the number theory.

7 — The cost is in the powers

The QFT costs about \(t^2/2\) gates, which is cheap. The expensive part is step 2. Implemented naively, \(U^{2^j}\) is \(2^j\) copies of \(U\), and the circuit uses \(2^t-1\) controlled applications of \(U\) in total:

control qubits \(t\)381632
applications of \(U\), naively725565,535about \(4.3\times10^9\)

Since the precision is \(2^{-t}\), naive phase estimation costs time proportional to \(1/\text{precision}\): no better than measuring the phase by brute repetition. Phase estimation is efficient only when \(U^{2^j}\) can be implemented with cost polynomial in \(j\). Two cases matter.

Order finding with \(N=55\) and \(a=2\). The controlled power \(U_2^{2^3}\) is the single multiplication \(U_c\). What is \(c\in\{0,\dots,54\}\)?

\(U_2^{8}\) multiplies by 2 eight times, which is multiplying once by \(2^8\bmod 55\). Square three times: \(2\to4\to16\to\,?\)
\(16^2=256=4\cdot55+36\), so \(2^8\equiv36\pmod{55}\) and \(U_2^{8}=U_{36}\): one modular multiplication instead of eight.

For a generic \(U\), such as the time evolution of a molecule, no shortcut exists, and phase estimation pays the full \(2^t\). This is why phase estimation for quantum chemistry is so expensive: every extra bit of the energy doubles the number of applications of \(U\).

8 — Try it in code

tool — phase estimation of P(2π·0.3) with 3 to 8 control qubits (tested with qiskit 2.5)
import numpy as np
from qiskit import QuantumCircuit
from qiskit.circuit.library import QFTGate
from qiskit.quantum_info import Statevector

def qpe(theta, t):
    """phase estimation of U = P(2 pi theta) on its eigenvector |1>; controls are qubits 0..t-1"""
    qc = QuantumCircuit(t + 1)
    qc.x(t)                                       # target = |1>, eigenvalue e^{2 pi i theta}
    qc.h(range(t))
    for j in range(t):
        qc.cp(2 * np.pi * theta * 2 ** j, j, t)   # controlled-U^(2^j) is one controlled phase here
    qc.append(QFTGate(t).inverse(), range(t))
    return qc

def closed_form(theta, t):
    T = 2 ** t; x = np.arange(T)
    return np.array([abs(np.exp(2j * np.pi * x * (theta - y / T)).sum() / T) ** 2 for y in range(T)])

theta = 0.3
for t in range(3, 9):
    p = Statevector.from_instruction(qpe(theta, t)).probabilities(qargs=list(range(t)))
    y = int(np.argmax(p))
    print(f"t = {t}: best y = {y:3d}, estimate {y / 2**t:.5f}, P(best) = {p[y]:.3f}, "
          f"matches closed form: {np.allclose(p, closed_form(theta, t))}")
t = 3: best y =   2, estimate 0.25000, P(best) = 0.578, matches closed form: True
t = 4: best y =   5, estimate 0.31250, P(best) = 0.876, matches closed form: True
t = 5: best y =  10, estimate 0.31250, P(best) = 0.573, matches closed form: True
t = 6: best y =  19, estimate 0.29688, P(best) = 0.875, matches closed form: True
t = 7: best y =  38, estimate 0.29688, P(best) = 0.573, matches closed form: True
t = 8: best y =  77, estimate 0.30078, P(best) = 0.875, matches closed form: True

The circuit agrees with Theorem 14.3 for every \(t\). The probability of the best estimate alternates between 0.58 and 0.88 because \(2^t\cdot0.3\) alternates between fractional parts 0.4 (as in \(2.4\), \(9.6\), \(38.4\)) and 0.8 (as in \(4.8\), \(19.2\), \(76.8\)), that is, between 0.4 and 0.2 away from the nearest grid point. QFTGate(t).inverse() is the inverse QFT with the same qubit ordering as Lecture 13.

tool — an input that is not an eigenvector
import numpy as np
from qiskit import QuantumCircuit
from qiskit.circuit.library import QFTGate
from qiskit.quantum_info import Statevector

t, theta = 4, 0.3
qc = QuantumCircuit(t + 1)
qc.h(t)                                           # target = |+> = (|0> + |1>)/sqrt2: NOT an eigenvector
qc.h(range(t))
for j in range(t):
    qc.cp(2 * np.pi * theta * 2 ** j, j, t)
qc.append(QFTGate(t).inverse(), range(t))
p = Statevector.from_instruction(qc).probabilities(qargs=list(range(t)))
print({y: round(float(p[y]), 3) for y in range(2 ** t) if p[y] > 0.02})
{0: 0.501, 4: 0.028, 5: 0.438}

Proposition 14.6 in action: \(y=0\) (the eigenphase 0 of \(|0\rangle\)) with probability one half, plus a little from the other branch, and otherwise the distribution for \(\theta=0.3\) at \(t=4\), with \(y=5\) (\(\tilde\theta=0.3125\)) most likely.

9 — Midterm review checklist

The midterm (next meeting, 75 minutes) covers Lectures 1–14. A formula sheet with the gate matrices, the Hadamard-transform formula, Grover's angle formulas and the QFT definition is provided. You should be able to do the following without notes.

lecturesyou can
1–3compute measurement probabilities in the Z, X and Y bases; tell global from relative phase; place a state on the Bloch sphere; give a 95% interval for a probability from counts
4multiply gate matrices; describe gates as rotations; choose the basis change for an X or Y measurement
5–6compute tensor products in Qiskit's ordering; test entanglement with the determinant; carry out a partial measurement; build the Bell states
7–8build classical functions reversibly with Toffolis; trace a teleportation branch; explain no-cloning; compute a CHSH correlator
9–10use \(H^{\otimes n}|x\rangle=2^{-n/2}\sum_y(-1)^{x\cdot y}|y\rangle\); run Deutsch–Jozsa, Bernstein–Vazirani and one step of Simon; solve a small system over \(\mathbb F_2\)
11–12compute Grover's success probability \(\sin^2((2k+1)\theta)\) and the iteration count; explain why Grover halves symmetric key length
13–14apply the QFT definition and product formula; compute a phase-estimation output distribution; choose \(t\) for a given precision and confidence
allread a short Qiskit snippet and predict its counts, including the bit order

10 — Summary and exercises

summary

Next time: the midterm, then order finding and Shor's algorithm, and the launch of Lab 4. HW7 (Lectures 13–14) is due at the start of Week 8.

exercises 14
  1. Let \(\theta=3/16\). What does phase estimation return with \(t=4\)? With \(t=3\), show that the outputs 1 and 2 are equally likely, and find their common probability.
  2. Take \(U=Z\) and \(|\psi\rangle=|1\rangle\). Write out phase estimation with \(t=1\) gate by gate and show that it returns 1 with certainty. Which earlier algorithm is this?
  3. For \(\theta=0.7\) and \(t=2\), which output is most likely, and with what probability? Check with the widget.
  4. Explain in two sentences why \(t=4\) estimates \(\theta=0.3\) with a higher best-estimate probability than \(t=5\) does.
  5. Run phase estimation of \(U=P(2\pi\theta)\) on \(\cos\alpha|0\rangle+\sin\alpha|1\rangle\). What is the output distribution? What is the state of the target after the outcome \(y\neq0\) is observed, if \(\theta\) is a \(t\)-bit fraction?
  6. A controlled-\(U\) costs 1,000 gates and has no shortcut for its powers. What does a 20-bit estimate with 90% confidence cost, in gates, using Theorem 14.5?
  1. With \(t=4\), \(3/16\) is a 4-bit fraction, so \(y=3\) with certainty (Theorem 14.2). With \(t=3\), \(T\theta=1.5\) is half-way between 1 and 2, so \(\delta=\pm\tfrac1{16}\) and both get \(\frac{\sin^2(\pi/2)}{64\sin^2(\pi/16)}=\frac{1}{64\sin^2(\pi/16)}\approx0.4105\), just above \(4/\pi^2\).
  2. \(H\) on the control gives \(|+\rangle|1\rangle\); the CZ kicks back \(-1\), giving \(|-\rangle|1\rangle\); the final \(H\) (the one-qubit inverse QFT) gives \(|1\rangle|1\rangle\). So \(y=1\), \(\tilde\theta=\tfrac12\), the phase of \(Z\) on \(|1\rangle\). This is Deutsch's algorithm, with the oracle replaced by a controlled-\(Z\).
  3. \(T\theta=2.8\), so the nearest grid point is \(y=3\), with \(P(3)=0.882\); the others are 0.063 (\(y=2\)), 0.033 (\(y=0\)) and 0.022 (\(y=1\)).
  4. \(16\cdot0.3=4.8\) is 0.2 from the grid point 5, while \(32\cdot0.3=9.6\) is 0.4 from 10. The best-estimate probability depends on that distance (the figure for Theorem 14.4), not only on \(t\); the guarantee in Theorem 14.5 concerns accuracy to \(n\) bits, which more qubits always improve.
  5. \(|0\rangle\) has phase 0 and \(|1\rangle\) phase \(\theta\), so by Proposition 14.6 the output is \(y=0\) with probability \(\cos^2\alpha\) (exactly, since 0 is a \(t\)-bit fraction) and otherwise follows the distribution for \(\theta\), with weight \(\sin^2\alpha\). If \(\theta\) is a \(t\)-bit fraction and \(y\neq0\) is seen, then \(y=T\theta\) and the target has collapsed to \(|1\rangle\).
  6. \(\varepsilon=0.1\) gives \(\lceil\log_2(2+5)\rceil=3\), so \(t=23\), and \(2^{23}-1\approx8.4\times10^6\) controlled applications cost about \(8.4\times10^9\) gates. Precision is expensive when the powers have no shortcut.

Reading and sources

all numbers and figures in these notes are computed in python (verify_L14.py) and follow the page theme