Documentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/qasm-playground/llms.txt
Use this file to discover all available pages before exploring further.
Quantum Phase Estimation (QPE) determines the phase φ in the eigenvalue equation U|ψ⟩ = e^(i·2π·φ)|ψ⟩ for a given unitary U and its eigenstate |ψ⟩. It is a core subroutine in Shor’s algorithm, quantum chemistry simulations, and many other quantum algorithms. The circuit uses a register of counting qubits to encode φ in binary, then extracts it via an inverse Quantum Fourier Transform (QFT). Precision scales with the number of counting qubits: n bits yield a resolution of 1/2ⁿ.
Code
T gate (phase = 1/8)
Rz(pi/3) (phase = 1/6)
// Quantum Phase Estimation (T)
//
// Estimates the eigenphase of the T gate using the |1> eigenstate.
// U(0,0,theta) = diag{1, exp(i*theta)}.
// U(0,0,pi/4) = diag{1, exp(i*pi/4)} = T.
//
// U(0,0,pi/4)|1> = exp(i*pi/4)|1> = exp(i*2pi*1/8)|1>.
// The expected phase is phi = 1/8 = 0.001 in binary.
OPENQASM 3.0;
gate h q { U(pi/2, 0, pi) q; }
gate x q { U(pi, 0, pi) q; }
gate cr(theta) c, t { ctrl @ U(0, 0, theta) c, t; }
def inv_qft(qubit[3] q) {
h q[2];
cr(-pi/2) q[2], q[1];
h q[1];
cr(-pi/4) q[2], q[0];
cr(-pi/2) q[1], q[0];
h q[0];
}
const int n = 3;
qubit[n] c;
qubit a;
reset c;
reset a;
h c;
x a;
// controlled-U^(2^i)
cr(1*pi/4) c[0], a;
cr(2*pi/4) c[1], a;
cr(4*pi/4) c[2], a;
inv_qft(c);
// Quantum Phase Estimation (Rz(pi/3))
//
// Estimates the eigenphase of Rz(pi/3) using the |1> eigenstate.
// U(0,0,theta) = diag{1, exp(i*theta)}
// = exp(i*theta/2)*diag{exp(-i*theta/2), exp(i*theta/2)}
// = exp(i*theta/2)Rz(theta).
//
// U(0,0,pi/3)|1> = exp(i*pi/3)|1> = exp(i*2pi*1/6)|1>.
// The expected phase is phi = 1/6 = 0.001010101... in binary.
OPENQASM 3.0;
gate h q { U(pi/2, 0, pi) q; }
gate x q { U(pi, 0, pi) q; }
gate cr(theta) c, t { ctrl @ U(0, 0, theta) c, t; }
def inv_qft(qubit[7] q) {
h q[6];
cr(-pi/2) q[6], q[5];
h q[5];
cr(-pi/4) q[6], q[4];
cr(-pi/2) q[5], q[4];
h q[4];
cr(-pi/8) q[6], q[3];
cr(-pi/4) q[5], q[3];
cr(-pi/2) q[4], q[3];
h q[3];
cr(-pi/16) q[6], q[2];
cr(-pi/ 8) q[5], q[2];
cr(-pi/ 4) q[4], q[2];
cr(-pi/ 2) q[3], q[2];
h q[2];
cr(-pi/32) q[6], q[1];
cr(-pi/16) q[5], q[1];
cr(-pi/ 8) q[4], q[1];
cr(-pi/ 4) q[3], q[1];
cr(-pi/ 2) q[2], q[1];
h q[1];
cr(-pi/64) q[6], q[0];
cr(-pi/32) q[5], q[0];
cr(-pi/16) q[4], q[0];
cr(-pi/ 8) q[3], q[0];
cr(-pi/ 4) q[2], q[0];
cr(-pi/ 2) q[1], q[0];
h q[0];
}
const int n = 7;
qubit[n] c;
qubit a;
reset c;
reset a;
h c;
x a;
// controlled-U^(2^i)
cr( 1*pi/3) c[0], a;
cr( 2*pi/3) c[1], a;
cr( 4*pi/3) c[2], a;
cr( 8*pi/3) c[3], a;
cr(16*pi/3) c[4], a;
cr(32*pi/3) c[5], a;
cr(64*pi/3) c[6], a;
inv_qft(c);
Step-by-step explanation
Gate definitions
h (Hadamard): Creates superposition over the counting register.
x (Pauli-X): Flips the ancilla a to |1⟩, the eigenstate of both target unitaries.
cr(theta): Controlled phase rotation — controlled U(0, 0, theta) applies a phase of e^(i·theta) to |1⟩ on the target when the control is |1⟩.
Structure of QPE
All QPE circuits follow the same three-stage structure:
- Hadamard stage — Place all counting qubits in uniform superposition with
h c.
- Controlled-U powers — Apply controlled-U^(2^i) for each counting qubit c[i].
- Inverse QFT — Extract the phase from the counting register.
Eigenstate preparation
x a sets the ancilla to |1⟩ — the +1 eigenstate of U(0, 0, theta). Because U(0, 0, theta)|1⟩ = e^(i·theta)|1⟩, each controlled application introduces a phase factor onto the corresponding counting qubit without disturbing the ancilla.
Controlled-U powers
For the T gate example with theta = pi/4:
c[0] controls U^1 = cr(1·pi/4) → contributes phase for the 2^0 bit
c[1] controls U^2 = cr(2·pi/4) → contributes phase for the 2^1 bit
c[2] controls U^4 = cr(4·pi/4) → contributes phase for the 2^2 bit
The phase φ = 1/8 in binary is 0.001, so only c[2] (the highest bit of the 3-bit fraction) accumulates a non-trivial phase — the inverse QFT will concentrate probability on the binary string 001.
Inverse QFT
The inv_qft subroutine is the standard n-qubit inverse QFT built from Hadamard gates and controlled phase rotations with negative angles. It transforms the phase-encoded state in the counting register into a computational basis state whose binary value approximates φ.
Expected output
T gate (3 counting qubits, φ = 1/8)
The phase 1/8 = 0.001 in binary is exactly representable in 3 bits. The measurement is deterministic:
| Measurement (q[2]q[1]q[0]) | Binary fraction | Phase | Probability |
|---|
001 | 0.001 | 1/8 | 100% |
Rz(pi/3) (7 counting qubits, φ = 1/6)
The phase 1/6 = 0.001010101… in binary is irrational and cannot be represented exactly in 7 bits. The QPE output spreads across nearby representable values:
| Measurement | Binary fraction | Decimal approx. | Phase target |
|---|
0010101 | 0.0010101 | ≈ 0.1641 | 1/6 ≈ 0.1667 |
0010110 | 0.0010110 | ≈ 0.1719 | 1/6 ≈ 0.1667 |
Increasing the number of counting qubits improves phase precision. With n qubits you can resolve phases differing by as little as 1/2ⁿ. The T gate example uses 3 qubits because 1/8 is exactly a 3-bit fraction. Rz(pi/3) uses 7 qubits to get closer to the irrational value 1/6.
Quantum mechanics concepts
Eigenphase — A unitary operator U has complex eigenvalues of the form e^(i·2π·φ). QPE estimates this phase φ ∈ [0, 1) using quantum parallelism: the counting register holds a superposition of all possible phase values simultaneously, and interference causes the correct one to have the highest probability amplitude.
Quantum Fourier Transform — The QFT is the quantum analogue of the Discrete Fourier Transform. Applied to a state encoding phase φ in the amplitudes, it transforms that state into a computational basis state close to the binary encoding of φ. The inverse QFT (used here) undoes the encoding introduced by the controlled-U applications.
Phase kickback — When a controlled-U acts on a control qubit and the target is already in an eigenstate |ψ⟩ of U with eigenvalue e^(i·φ), the eigenvalue phase is “kicked back” onto the control qubit’s amplitude. This is what encodes φ into the counting register without disturbing the ancilla.
Precision and resources — QPE with n counting qubits succeeds with probability at least (8/π²) ≈ 81% when measuring the closest n-bit approximation to φ. Increasing n by one qubit doubles the resolution and adds one layer of controlled-U and one controlled rotation to the inverse QFT.