Category: simulation | Difficulty: advanced | Qubits: 6 | Gates: 18 | Depth: 12
Quantum counting combines Grover's algorithm with QPE to count M solutions in a search space of N items. The Grover operator has eigenphases ±2arcsin(√(M/N)), which QPE reads off. For 1 solution in 4 items: phase ≈ π/3, count ≈ 1. This circuit uses 3 counting qubits and applies the oracle for |11⟩ as a simplified controlled-Grover demonstration.
Counting register encodes phase ~1/6 ≈ |001⟩ (1 solution in 4-item space)
The OpenQASM 2.0 circuit is in circuit.qasm.
OPENQASM 2.0;
include "qelib1.inc";
// Quantum counting: count solutions to oracle marking |11> in 2-qubit space
// cnt[0..2]: counting register, srch[0..1]: search space, anc: oracle ancilla
qreg cnt[3];
qreg srch[2];
qreg anc[1];
creg c[3];
// Superpose counting and search qubits
h cnt[0]; h cnt[1]; h cnt[2];
h srch[0]; h srch[1];
// Prepare oracle ancilla in |->
x anc[0]; h anc[0];
// Oracle: marks |11> with phase kickback (one application)
ccx srch[0],srch[1],anc[0];
// Diffusion on search register
h srch[0]; h srch[1];
x srch[0]; x srch[1];
h srch[1]; cx srch[0],srch[1]; h srch[1];
x srch[0]; x srch[1];
h srch[0]; h srch[1];
// Inverse QFT on counting register
swap cnt[0],cnt[2];
h cnt[0];
cu1(-1.5707963267948966) cnt[1],cnt[0];
h cnt[1];
cu1(-0.7853981633974483) cnt[2],cnt[0];
cu1(-1.5707963267948966) cnt[2],cnt[1];
h cnt[2];
measure cnt[0] -> c[0];
measure cnt[1] -> c[1];
measure cnt[2] -> c[2];
counting grover qpe amplitude-estimation
MIT — part of the OpenQC Algorithm Catalog.