Grover's Search on a Noisy Quantum Computer: How Many Iterations Actually Help?
Every Grover tutorial gives you the same formula: for a search space of N items, run about (π/4)·√N iterations and you will find the marked item with high probability. That formula is correct — on a perfect machine. I wanted to know what happens to it on real hardware, where every iteration adds gates and every gate adds error. Short answer: the best number of iterations is smaller than the textbook says, and you can measure exactly how much smaller.
Grover in three sentences
Start with every item in equal superposition. Repeat two steps: an oracle flips the sign of the marked item's amplitude, and a diffuser reflects all amplitudes about their average, which grows the marked one a little each round. Too few rounds and the marked item is not amplified enough; too many and you rotate past it and the probability falls again.
The circuit
The oracle uses X gates to map the marked string onto |11…1⟩, then a multi-controlled Z (written as H · multi-controlled X · H). The diffuser is the same trick around |00…0⟩.
from qiskit import QuantumCircuit
def oracle(qc, marked):
n = len(marked)
zeros = [i for i, b in enumerate(reversed(marked)) if b == "0"]
if zeros: qc.x(zeros)
qc.h(n - 1); qc.mcx(list(range(n - 1)), n - 1); qc.h(n - 1) # multi-controlled Z
if zeros: qc.x(zeros)
def diffuser(qc, n):
qc.h(range(n)); qc.x(range(n))
qc.h(n - 1); qc.mcx(list(range(n - 1)), n - 1); qc.h(n - 1)
qc.x(range(n)); qc.h(range(n))
def grover(marked, k):
n = len(marked)
qc = QuantumCircuit(n, n)
qc.h(range(n))
for _ in range(k):
oracle(qc, marked); diffuser(qc, n)
qc.measure(range(n), range(n))
return qc
The experiment
For each iteration count k, I computed the ideal success probability with a statevector and ran the transpiled circuit 20,000 times on FakeFez, a local simulator carrying the noise model of IBM's 156-qubit ibm_fez. I also counted the two-qubit gates after transpilation, because those are where most of the error comes from.
import numpy as np
from qiskit.quantum_info import Statevector
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import SamplerV2 as Sampler
from qiskit_ibm_runtime.fake_provider import FakeFez
backend = FakeFez()
pm = generate_preset_pass_manager(backend=backend, optimization_level=1, seed_transpiler=7)
sampler = Sampler(mode=backend)
for marked in ["101", "1011"]:
n = len(marked)
opt = int(np.floor(np.pi / 4 * np.sqrt(2 ** n)))
for k in range(0, opt + 3):
ideal = Statevector(grover(marked, k).remove_final_measurements(inplace=False)) \
.probabilities_dict().get(marked, 0)
isa = pm.run(grover(marked, k))
counts = sampler.run([isa], shots=20000).result()[0].data.c.get_counts()
twoq = sum(v for g, v in isa.count_ops().items() if g in ("cz", "ecr", "cx"))
print(marked, k, round(ideal, 3), round(counts.get(marked, 0) / 20000, 3), twoq)
Results: 3 qubits (8 items, textbook optimum k = 2)
| Iterations k | Ideal | Noisy ibm_fez model | Two-qubit gates |
|---|---|---|---|
| 0 | 12.5% | 12.3% | 0 |
| 1 | 78.1% | 70.0% | 21 |
| 2 | 94.5% | 75.1% | 45 |
| 3 | 33.0% | 26.5% | 69 |
| 4 | 1.2% | 6.7% | 93 |
With 3 qubits the textbook optimum still wins, but look at the gap: the second iteration adds 16 points of success in theory and only 5 in practice. Noise is already eating most of the benefit.
Results: 4 qubits (16 items, textbook optimum k = 3)
| Iterations k | Ideal | Noisy ibm_fez model | Two-qubit gates |
|---|---|---|---|
| 0 | 6.2% | 6.5% | 0 |
| 1 | 47.3% | 38.3% | 61 |
| 2 | 90.8% | 56.4% | 125 |
| 3 | 96.1% | 48.0% | 189 |
| 4 | 58.2% | 27.0% | 253 |
| 5 | 12.5% | 9.9% | 317 |
Here the optimum moves. On the noisy model, two iterations beat three: 56.4% against 48.0%. The third iteration would add about 5 points of success probability in theory, but it costs 64 more two-qubit gates, and on today's hardware that costs more than it gains.
Why the optimum shifts
Each Grover iteration rotates the state a fixed angle towards the marked item, while every gate shrinks the "signal" a little through decoherence and gate errors. Once the per-iteration gain in success probability is smaller than the per-iteration loss to noise, stopping early is better. The textbook formula only counts the first effect.
The multi-controlled gates make this worse than it looks on paper. A 3-control MCX does not exist on the chip; the transpiler decomposes it into many CNOT-equivalent gates and adds SWAPs for qubits that are not neighbours on the heavy-hex lattice. That is why the two-qubit count jumps from 45 to 125 between 3 and 4 qubits at the same iteration count.
Practical takeaways
- Treat k as a parameter to measure, not a formula to trust. Sweep a few values around the textbook optimum on your actual backend.
- Count two-qubit gates after transpilation. That number predicts your noisy success rate far better than the circuit you drew.
- The marked item still wins. Even at 48–56%, the correct answer is by far the most common outcome out of 16, so a majority vote recovers it. Grover on small instances "works" — it just does not reach the textbook numbers.
- Do not expect quantum advantage here. Searching 16 items classically takes at most 16 checks. Grover's √N speed-up only pays off at sizes far beyond what current noise allows.
Try it yourself
Swap FakeFez() for a real backend from QiskitRuntimeService, raise optimization_level to 3, or try a marked string with more 1s. I would be curious whether anyone gets k = 3 to win on 4 qubits on current IBM hardware — that would be a good sign of how fast error rates are falling.
References
- L. K. Grover, "A fast quantum mechanical algorithm for database search," Proceedings of STOC (1996). arXiv:quant-ph/9605043.
- M. Boyer, G. Brassard, P. Høyer, A. Tapp, "Tight bounds on quantum searching," Fortschritte der Physik 46, 493 (1998).
- Qiskit documentation: transpilation, multi-controlled gates and fake backends.
Comments
Post a Comment