skip to content
luminary.blog
by Oz Akan
A maze

How Classical and Quantum Computers Search a Maze

A classical computer walks the maze one path at a time. A quantum computer makes the wrong paths cancel out. What Grover's algorithm actually does, and why it is quadratically, not exponentially, faster.

/ 10 min read

Table of Contents

Imagine a maze. There’s a start, an exit, and a tangle of corridors and dead ends in between. Finding the way through is one of the oldest problems in computer science, and it turns out to be a surprisingly clean way to see what actually separates a quantum computer from the laptop on your desk.

The short version: a classical computer walks the maze, and a quantum computer makes the wrong paths cancel each other out. Let’s unpack what that really means — and where the popular “tries all paths at once” story is misleading.

How a classical computer searches

A classical computer explores a maze by trying candidate paths. The standard algorithms — depth-first search, breadth-first search, A* — all share the same basic character: they walk down a branch, and if it dead-ends, they back up and try the next one.

  • Depth-first search plunges as far down one corridor as it can, backtracking only when it hits a wall.
  • Breadth-first search explores all paths of length 1, then all of length 2, and so on, guaranteeing the shortest route.
  • A* adds a heuristic — a guess about which direction points toward the exit — to explore promising branches first.

These get smarter about which paths to check and in what order, and some — breadth-first search, for one — fan out across a whole frontier of paths at once rather than truly one at a time. But none of them has a way to make wrong answers cancel. They can prune, reorder, and even parallelize the checking, yet every wrong candidate still has to be ruled out on its own; there is no global mechanism that lets the dead ends suppress each other. That absence — not sequential-versus-parallel — is the real line between classical and quantum search. When a maze has many branching points the number of possible paths grows exponentially, and in the worst case there’s no escaping that the work scales with the number of candidates you have to eliminate.

How a quantum computer searches

A quantum computer does not literally try every path simultaneously, even though that’s the line you’ll read everywhere. The reality is more subtle and more interesting.

A quantum computer puts the system into a superposition over many possible paths at once, then uses interference to make the wrong paths cancel out while the right path’s contribution builds up. Quantum states are described by amplitudes — numbers that can be positive or negative — and the probability of measuring any given state is its amplitude squared. Because amplitudes carry a sign, they can add together or cancel, exactly like waves.

The catch is that you only ever get one measurement. You can’t peek at the superposition and read off all the paths. So the entire game is arranging the interference so that, when you finally measure, you’re overwhelmingly likely to read out the correct answer. The parallelism is real, but it’s fragile — it has to be carefully engineered to survive collapse into a single result.

Recast as unstructured search — find the one valid path among many candidates, with no clue which is which beyond what a checker tells you — the relevant tool is Grover’s algorithm. (A real maze has structure that classical graph search exploits; deliberately throwing that structure away is what turns it into a clean illustration of Grover, not how you’d actually solve a small maze.)

Encoding a maze as a search problem

Before Grover can help, the maze has to be expressed as a search problem.

Think of the maze as a graph: each cell or junction is a node, and each open passage between two cells is an edge. A path is a sequence of moves from the start to the exit.

To make this something a quantum algorithm can chew on, you encode a candidate path as a bit string. Suppose at every junction you can move in one of four directions, and a complete path is at most kk moves long. Each move needs 2 bits (4 directions = 2 bits), so a full candidate path is a string of 2k2k bits. The total space of candidate paths is:

N=22kN = 2^{2k}

Most of these strings are nonsense — they walk into walls, wander off the grid, loop forever, or never reach the exit.

Now you need an oracle: a function f(x)f(x) that takes a candidate path xx and returns 1 if it’s a valid solution and 0 otherwise. The key insight is that checking a given path is easy — you just trace it and see whether it goes start-to-exit without crossing a wall. The hard part is that there are NN candidates and you have no idea in advance which one the oracle will accept.

One practical caveat the tidy picture hides: on real hardware this oracle has to be built as a reversible circuit — every step undoable, no information discarded along the way — which adds engineering overhead, though it doesn’t change the asymptotic speedup.

That structure — “find the one needle the oracle marks, among NN haystack items” — is precisely what Grover’s algorithm is built to solve.

  • Classically: you feed candidates to the oracle one by one, expecting to test about N/2N/2 before you stumble on the answer.
  • With Grover: you find it in roughly N\sqrt{N} steps.

Two things that “roughly N\sqrt{N} steps” quietly hides. Each step is one oracle call plus one diffusion operator, and an oracle call isn’t free — tracing a length-kk path costs about O(k)O(k) — so the honest runtime is O(Ncost of one oracle call)O(\sqrt{N} \cdot \text{cost of one oracle call}). Grover cuts the number of oracle calls from about NN to about N\sqrt{N}; it doesn’t make each call cheaper. And this assumes a single valid path. If there are MM of them, Grover finds one in about N/M\sqrt{N/M} steps — more needles, fewer iterations.

How Grover’s interference builds up the answer

Here’s the mechanism, step by step.

Step 1 — Equal superposition

Start with all NN candidate paths in equal superposition. Every path has the same amplitude:

amplitude of every state=1N\text{amplitude of every state} = \frac{1}{\sqrt{N}}

If you measured now, you’d get a random path. Useless — the right answer is just one in NN.

Step 2 — The oracle flips the sign

The oracle doesn’t boost the winning path’s probability directly. Instead it flips the sign of the winner’s amplitude, leaving every other amplitude untouched. The winner now sits at 1N-\frac{1}{\sqrt{N}} while everyone else stays at +1N+\frac{1}{\sqrt{N}}.

Its probability (amplitude squared) hasn’t changed at all — you’ve marked it invisibly, purely by sign.

Step 3 — Inversion about the mean

This is the clever move, often called the diffusion operator. You compute the average of all the amplitudes, then reflect every amplitude across that average. Each value aa becomes:

a    2aˉaa \;\rightarrow\; 2\bar{a} - a

Watch what happens. Because the winner is negative and everyone else is positive, the average sits slightly below the positive cluster. Reflecting the positive paths across that average nudges them down a little. But reflecting the negative winner across the average flings it way up to a large positive value. In a single step, the winner’s amplitude has grown while everyone else’s has shrunk.

Step 4 — Repeat (but not too much)

Each oracle-plus-diffusion cycle rotates probability toward the winner. Geometrically, it’s literally a rotation: the state vector turns by a fixed small angle toward the answer on each iteration. After about

π4N\frac{\pi}{4}\sqrt{N}

iterations, the winner’s amplitude is close to 1, and a measurement returns the correct path with high probability.

The subtlety people get wrong: you have to stop at the right time. Because it’s a rotation, running past the optimal point rotates you past the answer, and the success probability starts falling again. More iterations is not better — Grover overshoots. That’s a genuine departure from classical search, where extra effort never hurts.

Watching it happen: a worked example with N = 16

Let’s make this concrete with a small maze of N=16N = 16 candidate paths, where path #0 is the correct one. The optimal number of iterations is π4163\frac{\pi}{4}\sqrt{16} \approx 3.

These aren’t hand-waved figures — each row is the exact Grover rotation. With one correct path out of NN, define θ=arcsin ⁣(1/N)\theta = \arcsin\!\big(1/\sqrt{N}\big). After kk iterations the winner’s amplitude is sin ⁣((2k+1)θ)\sin\!\big((2k+1)\theta\big), each of the N1N-1 wrong paths sits at cos ⁣((2k+1)θ)/N1\cos\!\big((2k+1)\theta\big)/\sqrt{N-1}, and the success probability is sin2 ⁣((2k+1)θ)\sin^2\!\big((2k+1)\theta\big). For N=16N = 16, θ=arcsin(1/4)0.2527\theta = \arcsin(1/4) \approx 0.2527, and the rotation lands on clean values — sin3θ=1116=0.6875\sin 3\theta = \tfrac{11}{16} = 0.6875 and sin5θ=6164=0.953125\sin 5\theta = \tfrac{61}{64} = 0.953125 — which is where the table comes from.

IterationWinner amplitudeEach wrong amplitudeP(winner)P(each wrong)
00.25000.25006.25%6.25%
10.68750.187547.3%3.52%
20.95310.078190.8%0.61%
30.9805−0.050896.1%0.26%
40.7627−0.167058.2%2.79%
50.3542−0.241512.6%5.83%
6−0.1428−0.25562.04%6.53%

Reading the table:

  • Iteration 0 — everything is flat. All 16 paths sit at amplitude 0.25, each with a 6.25% chance (1/161/16). Measuring now is a coin-toss across all paths.
  • Iteration 1 — after one cycle, the winner jumps to 0.6875 and each wrong path drops to 0.1875. The winner alone is already at 47%, nearly even odds against all 15 competitors combined.
  • Iteration 2 — winner at 0.95; the 15 wrong paths barely register. Already 90.8%.
  • Iteration 3 — the peak. The winner hits 96.1%, exactly where the formula predicted. This is where you measure.
  • Iterations 4–6 — the overshoot. Past the peak, the rotation keeps turning past the answer. The winner’s amplitude falls, goes negative by iteration 6, and its probability collapses to 2% — worse than where you started.

You can see the rotation directly in the signs: the winner climbs to its maximum, then the amplitudes start flipping negative as the state vector swings past the target. That’s why “just run it longer” actively hurts. A quantum search has a correct stopping time; a classical search never does.

So how much faster is it, really?

The honest answer is quadratically faster — not exponentially.

If a classical search through NN possibilities takes roughly NN steps, Grover finds the marked item in about N\sqrt{N} steps. A maze that takes a million classical checks might take around a thousand Grover iterations.

A quick way to feel the speedup: each Grover iteration rotates the state by 2θ=2arcsin ⁣(1/N)2\theta = 2\arcsin\!\big(1/\sqrt{N}\big), which for large NN is close to 2/N2/\sqrt{N}. You start near angle θ\theta and need to turn the state to about π/2\pi/2 radians, so it takes about π4N\frac{\pi}{4}\sqrt{N} iterations rather than NN checks.

Search space NNClassical (≈ N/2N/2)Grover (≈ π4N\frac{\pi}{4}\sqrt{N})
1683
1,000,000500,000~785

The gap at N=16N = 16 looks modest. At a million, it’s the difference between half a million checks and a few hundred iterations. But notice it’s N\sqrt{N} versus NN, not exponential-versus-polynomial. For this kind of unstructured search, quantum computing helps a great deal — but it doesn’t make genuinely hard problems trivially easy.

Two honest caveats

It’s worth being clear-eyed about what this does and doesn’t deliver.

First, most real mazes have structure. Good classical algorithms — A* with a sensible heuristic, for instance — exploit that structure aggressively. Grover’s quadratic speedup assumes unstructured search, where you have no clue which candidate is the answer beyond what the oracle tells you. A quantum computer is not automatically better than a smart classical algorithm on a structured maze.

Second, today’s hardware can’t do this for real. Current quantum machines are too noisy to beat a laptop on a problem like this. The advantage here is theoretical and asymptotic — a statement about how the work scales as the problem grows, not something you’d observe on present-day devices.

The takeaway

A classical computer searches a maze by trying paths, one after another, backtracking from dead ends. A quantum computer never “tries” paths in that sense at all. It places every candidate into superposition and then, using nothing more than the oracle’s sign-flip and a reflection about the mean, sloshes probability amplitude around until it concentrates on the correct path — in about N\sqrt{N} steps instead of NN.

It never knows the answer along the way. The geometry of those two reflections simply happens to funnel near-certainty onto the marked state. And if you let it run too long, that same geometry funnels the certainty right back out again.