ProductBlogAlgorithmsAboutSign inSign up

Quick Example

Phase Estimation: Quantum Circuit Example

Estimate eigenvalues of a unitary

What this example teaches

Estimate the T-gate eigenphase 1/8 with three counting wires.

Expected result

The browser's full-wire result is 1001. Counting wires q0q1q2 read 100 in least-significant-first order, representing the integer 1 and phase 1/8; q3 is the eigenstate wire.

Browser bit strings list physical wires q0 first, including auxiliary wires. Native classical-register strings can use another order; inspect the declared measurements.

These are ideal probabilities. Sampled frequencies fluctuate when more than one outcome is possible; they need not match percentages exactly. An ideal outcome with probability 100% has no finite-shot variation. Hardware noise is a separate effect.

Try the example

Open Run in Marqov →

The link opens the editor. Save existing work, then choose Examples → Phase Estimation → framework. Click Run, choose an available browser simulator, then click Run in browser. The link does not preselect or execute the example.

Scope and limitations

  • The input is a known eigenstate with exactly representable phase 1/8; this is not arbitrary-matrix diagonalization.

These expected answers describe the fixed circuit. This page does not establish a completed hosted or hardware run. Read the user guide for this example.

Inspect and download the circuit

Inspect the same fixed circuit in five frameworks. Browser execution translates supported circuit definitions into OpenQASM; it does not run general Python. Outside Marqov, Qiskit, Cirq, and Marqov snippets need a separate simulator invocation. PennyLane invokes its QNode and stores sampled counts in result.

OpenQASM
// Phase Estimation — estimate phase of T gate (pi/4)
OPENQASM 2.0;
include "qelib1.inc";
qreg q[4];
creg c[3];

// Eigenstate: |1>
x q[3];

// Counting qubits in superposition
h q[0];
h q[1];
h q[2];

// Controlled-U^(2^k) applications (cu1 is qelib1's controlled phase)
// T^1 = T on q[0], T^2 = S on q[1], T^4 = Z on q[2]
cu1(pi/4) q[0],q[3];
cu1(pi/2) q[1],q[3];
cu1(pi) q[2],q[3];

// Inverse QFT on counting qubits (a swap, as three cx: swap is not in qelib1.inc)
cx q[0],q[2];
cx q[2],q[0];
cx q[0],q[2];
h q[0];
cu1(-pi/2) q[1],q[0];
h q[1];
cu1(-pi/4) q[2],q[0];
cu1(-pi/2) q[2],q[1];
h q[2];

measure q[0] -> c[0];
measure q[1] -> c[1];
measure q[2] -> c[2];
Download OpenQASM circuit
Qiskit
# Phase Estimation — estimate phase of T gate (pi/4)
from qiskit import QuantumCircuit
import math

qc = QuantumCircuit(4, 3)

# Eigenstate: |1>
qc.x(3)

# Counting qubits in superposition
qc.h([0, 1, 2])

# Controlled-U^(2^k)
qc.cp(math.pi / 4, 0, 3)  # T^1
qc.cp(math.pi / 2, 1, 3)  # T^2
qc.cp(math.pi, 2, 3)      # T^4

# Inverse QFT
qc.swap(0, 2)
qc.h(0)
qc.cp(-math.pi / 2, 1, 0)
qc.h(1)
qc.cp(-math.pi / 4, 2, 0)
qc.cp(-math.pi / 2, 2, 1)
qc.h(2)

qc.measure([0, 1, 2], [0, 1, 2])
Download Qiskit circuit
Cirq
# Phase Estimation — estimate phase of T gate (pi/4)
import cirq
import numpy as np

q = cirq.LineQubit.range(4)
circuit = cirq.Circuit([
    # Eigenstate: |1>
    cirq.X(q[3]),
    # Counting qubits in superposition
    cirq.H.on_each(q[0], q[1], q[2]),
    # Controlled-U^(2^k)
    cirq.CZPowGate(exponent=1/4)(q[0], q[3]),
    cirq.CZPowGate(exponent=1/2)(q[1], q[3]),
    cirq.CZPowGate(exponent=1)(q[2], q[3]),
    # Inverse QFT
    cirq.SWAP(q[0], q[2]),
    cirq.H(q[0]),
    cirq.CZPowGate(exponent=-1/2)(q[1], q[0]),
    cirq.H(q[1]),
    cirq.CZPowGate(exponent=-1/4)(q[2], q[0]),
    cirq.CZPowGate(exponent=-1/2)(q[2], q[1]),
    cirq.H(q[2]),
    cirq.measure(q[0], q[1], q[2], key="result"),
])
Download Cirq circuit
PennyLane
# Phase Estimation — estimate phase of T gate (pi/4)
import pennylane as qml
import numpy as np

dev = qml.device("default.qubit", wires=4, shots=1000)

@qml.qnode(dev)
def phase_estimation():
    # Eigenstate: |1>
    qml.PauliX(wires=3)
    # Counting qubits in superposition
    for i in range(3):
        qml.Hadamard(wires=i)
    # Controlled-U^(2^k)
    qml.ControlledPhaseShift(np.pi / 4, wires=[0, 3])
    qml.ControlledPhaseShift(np.pi / 2, wires=[1, 3])
    qml.ControlledPhaseShift(np.pi, wires=[2, 3])
    # Inverse QFT
    qml.SWAP(wires=[0, 2])
    qml.Hadamard(wires=0)
    qml.ControlledPhaseShift(-np.pi / 2, wires=[1, 0])
    qml.Hadamard(wires=1)
    qml.ControlledPhaseShift(-np.pi / 4, wires=[2, 0])
    qml.ControlledPhaseShift(-np.pi / 2, wires=[2, 1])
    qml.Hadamard(wires=2)
    return qml.counts(wires=[0, 1, 2])

result = phase_estimation()
Download PennyLane circuit
Marqov
# Phase Estimation — estimate phase of T gate (pi/4)
from marqov import Circuit
import math

# Circuit has no controlled-phase method, so each cp(theta) on control c,
# target t is written exactly (up to a global phase) with the SDK's gates:
# rz(theta/2) c, rz(theta/2) t, cnot(c, t), rz(-theta/2) t, cnot(c, t).
circuit = (
    Circuit()
    .x(3)                       # Eigenstate |1>
    .h(0).h(1).h(2)             # Superposition
    .rz(math.pi / 8, 0).rz(math.pi / 8, 3).cnot(0, 3).rz(-math.pi / 8, 3).cnot(0, 3)  # controlled T^1
    .rz(math.pi / 4, 1).rz(math.pi / 4, 3).cnot(1, 3).rz(-math.pi / 4, 3).cnot(1, 3)  # controlled T^2
    .rz(math.pi / 2, 2).rz(math.pi / 2, 3).cnot(2, 3).rz(-math.pi / 2, 3).cnot(2, 3)  # controlled T^4
    .swap(0, 2)                 # Inverse QFT
    .h(0)
    .rz(-math.pi / 4, 1).rz(-math.pi / 4, 0).cnot(1, 0).rz(math.pi / 4, 0).cnot(1, 0)  # cp(-pi/2) 1,0
    .h(1)
    .rz(-math.pi / 8, 2).rz(-math.pi / 8, 0).cnot(2, 0).rz(math.pi / 8, 0).cnot(2, 0)  # cp(-pi/4) 2,0
    .rz(-math.pi / 4, 2).rz(-math.pi / 4, 1).cnot(2, 1).rz(math.pi / 4, 1).cnot(2, 1)  # cp(-pi/2) 2,1
    .h(2)
)
Download Marqov circuit

Sources and provenance

These references explain the concept and gate conventions. The snippets originate in Marqov's internal example collection; an upstream code attribution has not been established.

Snapshot identity
Source revision
df5a80cd474144684bf69dbda237b28ce40cc48c
Example version
bc285dd50341a7d7da08e280757e146ddf18b5f0e853d9c9f8540d934ca5f268

The source revision identifies the editor collection used for this snapshot. The example version identifies its five exact code variants; neither is evidence of a hosted run.

Go deeper

For a paper-based experiment with scientific comparisons and execution evidence, read Research Replications. Its published scope and availability are separate from this small teaching circuit.

Explore all twelve Quick Examples →