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.

Quantum Phase Estimation (QPE)

Tags: algorithm primitive resource-estimation

Quantum Phase Estimation (QPE) estimates the eigenphase ϕ\phi of a unitary matrix UU from an eigenstate ∣ψ⟩|\psi\rangle satisfying U∣ψ⟩=e2πiϕ∣ψ⟩U|\psi\rangle = e^{2\pi i \phi}|\psi\rangle. It is a central primitive in Shor’s algorithm and other algorithms that use phases encoded by the eigenvalues of unitary matrices Kitaev, 1995Cleve et al., 1998.

This notebook implements the QPE procedure as a Qamomile qkernel and compares it with the built-in qmc.qpe function. It then explores the relationship between the number of counting qubits, phase-estimation precision, and gate count.

Background: Phase Kickback and Quantum Fourier Transform

QPE combines two ideas: phase kickback from controlled unitaries Cleve et al., 1998 and the Quantum Fourier Transform Coppersmith, 2002.

Phase Kickback

Phase kickback is the mechanism by which, when a controlled-UU gate is applied to an eigenstate ∣ψ⟩|\psi\rangle of a unitary matrix UU, the eigenphase of UU appears as a relative phase on the control qubit. Suppose

U∣ψ⟩=e2πiϕ∣ψ⟩.U|\psi\rangle = e^{2\pi i\phi}|\psi\rangle.

Prepare one control qubit in a superposition and the target quantum state in ∣ψ⟩|\psi\rangle:

∣0⟩+∣1⟩2∣ψ⟩.\frac{|0\rangle + |1\rangle}{\sqrt{2}}|\psi\rangle.

A controlled-UU gate applies UU only to the component in which the control qubit is ∣1⟩|1\rangle. Because ∣ψ⟩|\psi\rangle is an eigenstate of UU, the eigenphase appears on that component:

∣0⟩∣ψ⟩+∣1⟩U∣ψ⟩2=∣0⟩+e2πiϕ∣1⟩2∣ψ⟩.\frac{|0\rangle|\psi\rangle + |1\rangle U|\psi\rangle}{\sqrt{2}} = \frac{|0\rangle + e^{2\pi i\phi}|1\rangle}{\sqrt{2}}|\psi\rangle.

This transformation leaves the target quantum state in ∣ψ⟩|\psi\rangle and encodes the eigenphase in the relative phase of the control qubit. QPE deliberately uses this mechanism with multiple control qubits. The Algorithm section explains the encoding in detail.

Quantum Fourier Transform

The Quantum Fourier Transform (QFT) maps a computational-basis state to a superposition whose relative phases encode that state’s value. On M=2mM=2^m basis states, the QFT is defined as

QFTM∣x⟩=1M∑y=0M−1e2πixy/M∣y⟩.\mathrm{QFT}_M|x\rangle = \frac{1}{\sqrt{M}} \sum_{y=0}^{M-1} e^{2\pi i xy/M}|y\rangle.

It maps an integer xx to a regular phase pattern across the computational basis. We refer to a state that stores a value in these relative phases as a Fourier-encoded state. For an application in Qamomile, see the multidimensional QFT example. The inverse QFT QFTM−1\mathrm{QFT}_M^{-1} performs the reverse operation. If ϕ=a/M\phi=a/M is exactly representable with mm bits, then the controlled powers U20,U21,…,U2m−1U^{2^0}, U^{2^1}, \ldots, U^{2^{m-1}} prepare the counting register as

1M∑k=0M−1e2πiak/M∣k⟩=QFTM∣a⟩.\frac{1}{\sqrt{M}}\sum_{k=0}^{M-1} e^{2\pi i a k/M}|k\rangle = \mathrm{QFT}_M|a\rangle.

Applying QFTM−1\mathrm{QFT}_M^{-1} then returns ∣a⟩|a\rangle. The important point is that the inverse QFT turns phase information into a computational-basis bit string.

1M(1e2πia/M⋮e2πia(M−1)/M)⏟vector of phase values→QFTM−1∣a⟩=∣am−1⋯a0⟩⏟computational-basis bit string.\underbrace{ \frac{1}{\sqrt{M}} \begin{pmatrix} 1 \\ e^{2\pi i a/M} \\ \vdots \\ e^{2\pi i a(M-1)/M} \end{pmatrix} }_{\text{vector of phase values}} \xrightarrow{\mathrm{QFT}_M^{-1}} \underbrace{|a\rangle = |a_{m-1}\cdots a_0\rangle}_{\text{computational-basis bit string}}.

Algorithm

QPE uses two groups of qubits: mm counting qubits that read out the phase and target qubits that hold an eigenstate ∣ψ⟩|\psi\rangle of UU. The counting qubits are auxiliary qubits used to represent the phase as an mm-bit binary fraction. Suppose U∣ψ⟩=e2πiϕ∣ψ⟩U|\psi\rangle=e^{2\pi i\phi}|\psi\rangle. The counting qubits start in ∣0⟩⊗m|0\rangle^{\otimes m}, and we write M=2mM=2^m.

Step 1: Put the counting qubits in superposition

Apply Hadamard gates to the counting qubits. This creates a uniform superposition over the MM possible values.

∣Ψ1⟩=H⊗m∣0⟩⊗m∣ψ⟩=1M∑r=0M−1∣r⟩∣ψ⟩.|\Psi_1\rangle = H^{\otimes m}|0\rangle^{\otimes m}|\psi\rangle = \frac{1}{\sqrt{M}}\sum_{r=0}^{M-1}|r\rangle|\psi\rangle.

Step 2: Apply controlled-U2kU^{2^k} gates

Use each counting qubit kk as the control qubit for a controlled-U2kU^{2^k} gate. If r=∑k=0m−1rk2kr=\sum_{k=0}^{m-1} r_k2^k, the target eigenstate picks up the phase e2πiϕre^{2\pi i\phi r}:

∣Ψ2⟩=1M∑r=0M−1e2πiϕr∣r⟩∣ψ⟩.|\Psi_2\rangle = \frac{1}{\sqrt{M}}\sum_{r=0}^{M-1} e^{2\pi i \phi r}|r\rangle|\psi\rangle.

Step 3: Convert phase information into a bit string with inverse QFT

If ϕ=a/M\phi=a/M is exactly representable, the counting register is QFTM∣a⟩\mathrm{QFT}_M|a\rangle. Applying the inverse QFT returns ∣a⟩|a\rangle.

∣Ψ2⟩=(1M∑r=0M−1e2πiar/M∣r⟩)∣ψ⟩=QFTM∣a⟩∣ψ⟩,|\Psi_2\rangle = \left( \frac{1}{\sqrt{M}}\sum_{r=0}^{M-1} e^{2\pi i ar/M}|r\rangle \right)|\psi\rangle = \mathrm{QFT}_M|a\rangle|\psi\rangle,
∣Ψ3⟩=QFTM−1QFTM∣a⟩∣ψ⟩=∣a⟩∣ψ⟩.|\Psi_3\rangle = \mathrm{QFT}_M^{-1}\mathrm{QFT}_M|a\rangle|\psi\rangle = |a\rangle|\psi\rangle.

Step 4: Measure the bit string and decode the phase

Measure the counting qubits. In the exact case, the measurement returns aa, and decoding this bit string gives the phase estimate

ϕ~=aM.\tilde{\phi} = \frac{a}{M}.

If ϕ\phi is not exactly representable with mm bits, the distribution concentrates around the closest mm-bit approximations. Increasing mm reduces the spacing between representable phases to 1/2m1/2^m, allowing the estimate to approach the true eigenphase.

Quantum phase estimation circuit with a counting register, controlled powers of U, inverse QFT, and measurement.

Schematic QPE circuit. The counting register controls the powers of UU, and the inverse QFT converts the accumulated phase pattern into measured bits.

Implementation with Qamomile

We use a diagonal 4x4 unitary matrix:

U=(10000eiθ010000eiθ100000eiθ11).U = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & e^{i\theta_{01}} & 0 & 0 \\ 0 & 0 & e^{i\theta_{10}} & 0 \\ 0 & 0 & 0 & e^{i\theta_{11}} \end{pmatrix}.

Every computational-basis state is an eigenstate of this matrix. We prepare the target state ∣01⟩|01\rangle and set the phase to estimate, θ01/2π\theta_{01} / 2\pi, to 0.6. Because 0.6 is not exactly representable on a finite binary fraction, increasing the number of counting qubits visibly improves the approximation.

phase fractions: [0.   0.6  0.23 0.81]
target phase fraction: 0.60000000
U =
[[ 1.   +0.j     0.   +0.j     0.   +0.j     0.   +0.j   ]
 [ 0.   +0.j    -0.809-0.588j  0.   +0.j     0.   +0.j   ]
 [ 0.   +0.j     0.   +0.j     0.125+0.992j  0.   +0.j   ]
 [ 0.   +0.j     0.   +0.j     0.   +0.j     0.368-0.93j ]]

From-Scratch Implementation

First, we define the 4x4 unitary matrix whose phase we want to estimate. The diagonal_4x4 qkernel implements this matrix directly. The phase gate P(θ)P(\theta) multiplies the ∣1⟩|1\rangle component of a qubit by eiθe^{i\theta}; Qamomile expresses this gate as qmc.p(q, theta). Applying qmc.p(q[0], phi10) therefore gives every basis state whose first target bit is 1 the phase eiθ10e^{i\theta_{10}}, and qmc.p(q[1], phi01) gives every basis state whose second target bit is 1 the phase eiθ01e^{i\theta_{01}}. At that point ∣11⟩|11\rangle has accumulated ei(θ10+θ01)e^{i(\theta_{10}+\theta_{01})}, so the controlled phase gate adds only the correction

θ11−θ10−θ01\theta_{11} - \theta_{10} - \theta_{01}

on ∣11⟩|11\rangle. The resulting diagonal is exactly diag⁡(1,eiθ01,eiθ10,eiθ11)\operatorname{diag}(1, e^{i\theta_{01}}, e^{i\theta_{10}}, e^{i\theta_{11}}) in the ordered basis ∣00⟩,∣01⟩,∣10⟩,∣11⟩|00\rangle, |01\rangle, |10\rangle, |11\rangle.

<Figure size 678x200 with 1 Axes>

We now implement the four QPE steps using Qamomile’s basic operations. qmc.control(diagonal_4x4) constructs controlled-UU, and power=2**k applies controlled-U2kU^{2^k}. After the inverse QFT, the qubits are cast to a QFixed with no integer bits. qmc.measure measures those qubits and decodes the resulting bit string into a floating-point phase estimate.

<Figure size 3236x416 with 1 Axes>

Built-in Function: qpe

The qmc.qpe function combines the Hadamard gates, controlled-U2kU^{2^k} gates, inverse QFT, and cast to QFixed used in the from-scratch implementation. Passing the returned QFixed to qmc.measure measures the qubits and decodes the fixed-point bit string into a floating-point phase estimate.

<Figure size 3236x416 with 1 Axes>

Execution Result

The target phase 0.6 cannot be represented exactly with only a few bits, so QPE returns a distribution over nearby mm-bit approximations. We vary the number of counting qubits from 3 to 9 and compare the estimate produced by the built-in qpe function with the exact phase.

(0.625, 3610)
(0.625, 2321)
(0.59375, 3605)
(0.59375, 2399)
(0.6015625, 3596)
(0.6015625, 2332)
(0.599609375, 3571)
(0.625, 3610)
<Figure size 700x350 with 1 Axes>

Resource Estimation

The previous section showed that more counting qubits improve precision. We can apply estimate_resources() directly to the same QPE kernels used by run_qpe_experiment() above. This estimate includes the Hadamards, controlled powers from qmc.qpe, inverse QFT, and final fixed-point measurement. The plot uses gates.total, which counts logical gates under the default CLEAN_ANCILLA_TOFFOLI control-decomposition model. Measurement counts are reported separately in measurements.total. Engine compilation and optimization can change the emitted gate count. See the resource-estimation tutorial for details of the counting model.

<Figure size 700x350 with 1 Axes>

The plot compares the number of counting qubits mm and the resulting total gate count with a reference line proportional to 2m2^m. If the target additive error is ϵ\epsilon, the spacing between representable phases should satisfy roughly

2−m≲ϵ,2^{-m} \lesssim \epsilon,

so the required number of counting qubits is

m=O ⁣(log⁡1ϵ).m = O\!\left(\log\frac{1}{\epsilon}\right).

In this implementation, each controlled-U2kU^{2^k} gate is constructed by repeating UU. Therefore the number of applications of UU is

∑k=0m−12k=2m−1=O ⁣(1ϵ).\sum_{k=0}^{m-1} 2^k = 2^m - 1 = O\!\left(\frac{1}{\epsilon}\right).

Thus, the number of counting qubits grows logarithmically in 1/ϵ1/\epsilon, while the number of applications of UU grows as O(1/ϵ)O(1/\epsilon) Nielsen & Chuang, 2012.

In this case, the gate count is O(1/ϵ)O(1/\epsilon) as shown above, making this approach inefficient for high-precision QPE. More generally, let G(V)G(V) be the number of gates needed to implement a unitary matrix VV. The gate count for the QPE body can then be written as

GQPE(m)=∑k=0m−1G ⁣(controlled-U2k)+O(m2),G_{\mathrm{QPE}}(m) = \sum_{k=0}^{m-1} G\!\left(\mathrm{controlled}\text{-}U^{2^k}\right) + O(m^2),

In practice, however, this estimate can change significantly. The expression above counts the gates in the QPE body and does not include initial-state preparation. The gate count of each controlled-U2kU^{2^k} also depends on its implementation. We consider these two factors separately below.

  1. Controlled-UU implementation cost.

    QPE is used for order finding in Shor’s algorithm and for estimating energies from eigenphases of time-evolution operators in quantum-system simulation. In Shor’s algorithm, modular-multiplication powers can be constructed from the problem structure, so they do not require exponentially many repetitions of UU. In Hamiltonian simulation, controlled time evolution may also be implementable with polynomial resources, depending on the Hamiltonian and simulation method. A useful QPE resource estimate should therefore state whether controlled-U2kU^{2^k} gates are repeated applications of UU, directly synthesized circuits, or problem-specific arithmetic or simulation circuits.

  2. Initial-state preparation cost.

    The probability of measuring a particular eigenphase is the squared overlap between the prepared state and the corresponding eigenstate. Preparing an approximate state with sufficient overlap can require a nontrivial quantum circuit, so an end-to-end resource estimate must count those gates separately.

Summary

In this notebook, we learned:

  • QPE uses phase kickback from controlled-U2kU^{2^k} gates and the inverse QFT to read an eigenphase of a unitary matrix as a binary value.

  • Qamomile can construct QPE from qmc.control, qmc.iqft, and qmc.cast, while the built-in qmc.qpe expresses the same procedure concisely.

  • The phase resolution from mm counting qubits is O(2−m)O(2^{-m}), while the required gate count depends on the implementation of controlled-U2kU^{2^k} gates and the initial-state preparation method.

References
  1. Kitaev, A. Yu. (1995). Quantum measurements and the Abelian Stabilizer Problem. arXiv. 10.48550/ARXIV.QUANT-PH/9511026
  2. Cleve, R., Ekert, A., Macchiavello, C., & Mosca, M. (1998). Quantum algorithms revisited. Proceedings of the Royal Society of London. Series A: Mathematical, Physical and Engineering Sciences, 454(1969), 339–354. 10.1098/rspa.1998.0164
  3. Coppersmith, D. (2002). An approximate Fourier transform useful in quantum factoring. arXiv. 10.48550/ARXIV.QUANT-PH/0201067
  4. Nielsen, M. A., & Chuang, I. L. (2012). Quantum Computation and Quantum Information: 10th Anniversary Edition. Cambridge University Press. 10.1017/cbo9780511976667