PLAIN QUANTUM
Search

How a quantum algorithm works, in plain English

What a quantum algorithm looks like: gates arranged into a circuit, one useful pattern (spread, mark, mix, measure), two worked examples, and the same steps as real code.

What you’ll learn

  • Read a small circuit diagram step by step.
  • Explain the spread, mark, mix, measure pattern.
  • Optional: follow the same example as Qiskit code.

Includes an optional section, the same steps as real code, which you can skip.

The Foundations lessons explain why quantum computers can help with some problems: amplitudes, the numbers whose squared sizes give the chance of each result, can cancel, so a well-designed calculation makes wrong answers fade and right ones build up. How quantum gates work looks at the individual steps. This lesson puts them together to show what a whole quantum algorithm looks like, using plain English first and a picture second. At the end, the same steps appear as a dozen or so lines of real code, so you can see that nothing was lost in translation. Both examples are deliberately small teaching puzzles, chosen because every step can be followed; real algorithms use the same moves on a far larger scale.

An algorithm is a recipe

An algorithm is just a precise list of steps that solves a problem. You already know plenty. Here's one in plain English for finding the largest number in a list:

  1. Remember the first number.
  2. Look at each remaining number in turn. If it's bigger than the one you're remembering, remember it instead.
  3. When you reach the end, the number you're remembering is the largest.

Every step is unambiguous, and the steps run in order. A quantum algorithm is the same kind of thing. What changes is the ingredients you're allowed to use, and one rule that ordinary recipes never have to worry about: you can't look at your work until the very end.

The three ingredients

Qubits hold the state, as in What is a qubit, really? They usually start as plain 0s.

Gates are the steps. Each one changes the amplitudes of one or two qubits in a precise, reversible way. On the qubit globe from the superposition lesson, a single-qubit gate is a rotation. A handful of gates does most of the work in this article:

GatePlain-English jobOn the globe
X (flip)Turns 0 into 1 and 1 into 0, like an ordinary NOTSwaps the poles
H (blend)Turns a plain 0 or 1 into an even blend, and turns those blends back againMoves a pole to the equator, or back
Z (sign flip)Leaves the odds alone but makes the amplitude on 1 negativeHalf a turn about the north–south axis, moving points half way round the equator: a change of phase
Controlled flipFlips a second qubit only where the first qubit is 1Works on two qubits at once; it's how entanglement is made (see Entanglement)
The H gate is named after the mathematician Jacques Hadamard. Real machines offer more gates than these, but these four are enough to follow the examples here.

For more on what each gate does, and what physically happens when one runs, see How quantum gates work.

Measurement comes last. It gives one 0 or 1 per qubit and ends the blend. Because looking earlier would destroy the calculation, a quantum algorithm has to be designed so that the answer is likely to be sitting there, ready to read, when the steps are finished.

Circuits: sheet music for qubits

Quantum algorithms are usually drawn as circuits. Each qubit gets a horizontal line, like a line of sheet music. Time runs left to right. Each gate is a box on the line of the qubit it acts on, and a two-qubit gate is drawn as a vertical link between two lines. A small meter symbol at the end means "measure here". Read a circuit the way you'd read a recipe: left to right, one step at a time.

qubit 1 qubit 2 H 1 2 3 4 5 time runs left to right qubit 1 qubit 2 H 1 2 3 4 5 time runs left to right
  1. A line is one qubit. It starts as 0 unless shown otherwise.
  2. A box is a one-qubit gate, named by its letter (here H, the blend).
  3. A filled dot marks the control qubit of a two-qubit gate.
  4. A ring with a plus is the target: it flips only when the control is 1. Dot, link and ring together are one controlled flip.
  5. A meter means “measure here”: the result is a plain 0 or 1. In this circuit the two results always match: 00 or 11, each half the time.
ControlTarget beforeTarget after
000
011
101
110
How to read a circuit. The same symbols appear in every circuit on this site, including the entangling circuit in the entanglement lesson and the hidden-code example below. The table shows what a controlled flip does to its target.

One useful pattern: spread, mark and mix

Some quantum algorithms, especially search algorithms such as Grover's, use a pattern of spreading amplitudes, marking possible answers and mixing amplitudes so interference changes the measurement probabilities.

  1. Spread out. H gates put every qubit into an even blend, so every possible input has the same small amplitude.
  2. Mark. A problem-specific operation recognises matching candidates without first measuring or revealing the answer. It changes the sign of their amplitudes, without immediately changing their measurement probabilities. The Superposition lesson explains how this recognition step works.
  3. Mix. More gates, often H gates again, make the amplitudes interfere, so the sign pattern turns into a difference in odds.
  4. Measure. If the design is right, the answer now carries most of the amplitude.

Some algorithms run marking and mixing once. Others repeat them many times. The art is in designing the marking step so that it captures something about the answer, and the mixing step so that interference brings it out.

Worked example: guessing a hidden code

Here's the smallest example that shows a real advantage. It's known as the Bernstein–Vazirani algorithm, after the two computer scientists who described it in 1993.

The puzzle. A box hides a secret 4-bit code, say 1011. You can give the box any 4-bit question, and it answers with a single bit: 1 if your question and the secret share an odd number of 1s in matching positions, and 0 if they share an even number.

The ordinary way. Ask 1000. The answer is the secret's first bit. Ask 0100 for the second, 0010 for the third, 0001 for the fourth. Four questions for four bits, and no cleverer strategy does better, because each answer is a single bit. A 100-bit secret takes 100 questions.

The quantum way, in plain English:

  1. Start four qubits at 0, plus one helper qubit set to 1.
  2. Put every qubit into an even blend. The four qubits now hold every possible question at once, each with the same amplitude.
  3. Ask the box once. For every question whose answer would be 1, the box makes that question's amplitude negative. The helper qubit is what turns the box's answer into a sign.
  4. Undo the blend on the four qubits. The negative signs now interfere, and everything cancels except one pattern: the secret itself.
  5. Measure. You read 1011, with certainty, after asking one question.
12345 bit 1bit 2bit 3bit 4helper the box X H H H H H H H H H 1011 12345 bit 1bit 2bit 3bit 4helper the box X HHHHH HHHH 1011
The same five steps as a circuit. Inside the box, each link from a code bit to the helper is a controlled flip: there's one wherever the secret has a 1. The box is drawn open here so you can see how it works; in the puzzle you only get to use it.

Why it isn't magic. You didn't read every answer. The box's answers were written into signs, and interference turned the pattern of signs into the code. It's the same trick as the tiny worked example in the superposition lesson, scaled up to four qubits.

Why nobody uses it for real work. This is a teaching example. The advantage is real (one question instead of four, or one instead of a hundred), but the puzzle was designed to show the effect, not to solve anything a business needs. It's valued because it makes the structure of quantum algorithms easy to see.

Go deeper: the hidden code, in symbols (optional maths)

The hidden code, in symbols. After step 2, the helper qubit is in (|0⟩ − |1⟩)/√2. When the box flips the helper, that state becomes its own negative, so the box's answer appears as a sign on the question: (−1)s·x, where s·x is the number of positions in which both the secret s and the question x have a 1. After step 2 the four code qubits hold every question x with the same amplitude. Undoing the blend (H on each) makes all those signs interfere, and only |s⟩ survives. This trick is called phase kickback.

Now you try: a different hidden code optional

1. Finish the working. This time the box hides the code 0110. Use the ordinary way first. Ask 1000: the question and the secret share no 1s in matching positions, and zero is even, so the box answers 0. Ask 0100: they share one 1, which is odd, so the box answers 1. Your turn: what does the box answer to 0010 and to 0001? Then one more: what does it answer to 1111?

Show answer 1

0010 shares one 1 with 0110, so the answer is 1. 0001 shares none, so the answer is 0. The four answers, 0, 1, 1, 0, spell out the secret. 1111 shares two 1s with 0110, and two is even, so the answer is 0.

2. On your own. Now the quantum way for the same secret, 0110. If the box were drawn open as in the circuit above, which code-bit lines would be linked to the helper by a controlled flip? What do the four code qubits read at the end, and after how many questions?

Show answer 2

There's one controlled flip wherever the secret has a 1: on code bits 2 and 3. After the final H gates everything cancels except the secret, so the four code qubits read 0110 with certainty, after asking the box once.

A second example: searching four boxes

What quantum computers are good at described Grover's search algorithm. With two qubits, it can be followed step by step. Four boxes are labelled 00, 01, 10 and 11, and one holds a prize.

  1. Spread out. Blend both qubits. Each label has a 25% chance.
  2. Mark. A step that recognises the prize box flips the sign of its amplitude. The odds are still 25% each.
  3. Mix. A step called the diffuser reflects every amplitude around their average. The marked amplitude, the only negative one, jumps up; the others fall to zero.
  4. Measure. With four boxes, one round of marking and mixing gives the prize label every time.
1. Spread out: all equal 00: +0.5 01: +0.5 10: +0.5 11: +0.5 2. Mark: prize box flipped 00: +0.5 01: +0.5 10: +0.5 11: −0.5 3. Mix: prize takes it all 00: 0 01: 0 10: 0 11: +1 1. Spread out: all equal 00:+0.5 01:+0.5 10:+0.5 11:+0.5 2. Mark: prize box flipped 00:+0.5 01:+0.5 10:+0.5 11:−0.5 3. Mix: prize takes it all 00:0 01:0 10:0 11:+1
Grover's search on four boxes, with the prize in box 11. Bars show amplitudes. Marking only flips a sign, so the odds don't change; mixing reflects every amplitude around their average, which moves everything onto the marked box.

An ordinary search of four boxes needs up to three looks. With more boxes, Grover's algorithm repeats the mark-and-mix round a number of times that grows with the square root of the number of boxes, and the right answer becomes very likely rather than certain. More rounds aren't always better: the chance of the right answer rises to a peak and then falls again if you keep going, so the number of rounds is chosen for the number of boxes. That's the modest speed-up described in What quantum computers are good at: helpful in principle, but not a shortcut through everything.

64 boxes, one prize 0% 50% 100% 0 2 4 6 8 10 12 rounds of mark and mix 6 rounds: 99.7% more rounds is not better 16 boxes: best at 3 rounds 64 boxes: best at 6 rounds 256 boxes: best at 12 rounds 4× the boxes, 2× the rounds 64 boxes, one prize 0% 50% 100% 0 2 4 6 8 10 12 rounds of mark and mix 6 rounds: 99.7% more rounds is not better 16 boxes: best at 3 rounds 64 boxes: best at 6 rounds 256 boxes: best at 12 rounds 4× the boxes, 2× the rounds
Chance that Grover's search on 64 boxes finds the prize, by rounds of mark and mix
RoundsChance of the prize
01.6%
113.5%
234.4%
359.1%
481.6%
596.4%
699.7%
790.7%
871.8%
947.5%
1023.8%
116.6%
120.0%
Grover's search on 64 boxes. Each round of marking and mixing moves more of the chance onto the prize, up to 99.7% after 6 rounds; keep going and it falls again. The best number of rounds is about π/4 × √64, rounded down: 6, so four times as many boxes needs only about twice as many rounds: that's the square-root speed-up, before any of the overheads real machines add.

Try it yourself: in the Mark and mix experiment, hide the prize in one of four boxes, predict what each step does, then measure. See why marking alone leaves the odds unchanged, and why running a second round reduces the chance of finding the prize.

Go deeper: the circuit maths, step by step (optional maths)

Searching four boxes, with the prize in 11. The steps match the bars in the figure above.

  1. Spread out. H on both qubits turns |00⟩ into ½(|00⟩ + |01⟩ + |10⟩ + |11⟩). Each amplitude is +0.5, and 0.5² = 0.25, a 25% chance each.
  2. Mark. A controlled sign flip on 11 gives ½(|00⟩ + |01⟩ + |10⟩ − |11⟩). The chances are still 25% each, because (−0.5)² = 0.25.
  3. Mix. The diffuser replaces each amplitude a with 2 × average − a. The average is (0.5 + 0.5 + 0.5 − 0.5)/4 = 0.25. So 0.5 becomes 0, and −0.5 becomes 1.
  4. Measure. The state is |11⟩, so the answer is 11 every time.

Run a second round: after marking again, the average is −0.25. Every amplitude returns to ±0.5, and the chance of the prize falls back to 25%. That is the "more rounds aren't always better" effect described above.

Now you try: move the prize optional

1. Finish the working. Put the prize in box 01 instead of box 11. Spread out: all four amplitudes are +0.5. Mark: the amplitude on 01 becomes −0.5, and the other three stay at +0.5. Mix: first find the average, (0.5 − 0.5 + 0.5 + 0.5) ÷ 4 = 0.25. Reflecting around the average means each new amplitude is twice the average minus the old amplitude. For box 00: 2 × 0.25 − 0.5 = 0. Boxes 10 and 11 work out the same way. Your turn: what is the new amplitude on 01, and what is the chance of reading 01?

Show answer 1

2 × 0.25 − (−0.5) = 0.5 + 0.5 = 1. Square it: 1, or 100%. Measuring gives 01 every time, and the other three boxes have amplitude 0.

2. On your own. Skip the marking step: spread out, then mix straight away. What are the four amplitudes after mixing, and what are the chances?

Show answer 2

All four amplitudes are +0.5, so the average is 0.5. Each new amplitude is 2 × 0.5 − 0.5 = 0.5: nothing changes, and every box still has a 25% chance. Mixing only helps once marking has made one amplitude different from the rest.

Optional section

The same thing as real code

Here's the hidden-code example written for Qiskit, IBM's free, open-source toolkit for programming quantum computers. Each line is labelled with the plain-English step it carries out.

from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler

secret = "1011"
n = len(secret)
qc = QuantumCircuit(n + 1, n)

qc.x(n)                         # 1. helper qubit starts at 1
qc.h(range(n + 1))              # 2. put every qubit into an even blend
for i, bit in enumerate(reversed(secret)):
    if bit == "1":              # 3. ask the box once: a controlled flip
        qc.cx(i, n)             #    for each position where the secret has a 1
qc.h(range(n))                  # 4. undo the blend on the four code qubits
qc.measure(range(n), range(n))  # 5. measure

result = StatevectorSampler().run([qc], shots=100).result()
print(result[0].data.c.get_counts())   # {'1011': 100}

Qiskit numbers bits from the right, which is why the code reads the secret in reverse. The last two lines run the circuit 100 times on a simulator in your own computer; every run returns 1011. The same circuit can be sent to real quantum hardware, where a small share of runs may come back wrong because of the noise described in Why qubits are so fragile.

Writing one yourself, in plain English

You don't need code to think through a quantum algorithm. Before anyone writes a line, it helps to answer six questions in plain sentences:

  1. What's the problem, and what does a correct answer look like?
  2. What structure does it have? A hidden pattern, a repeating cycle, a way to recognise the answer when you see it. Without structure, interference has nothing to work with.
  3. How will the answer be marked? Usually by flipping signs, which means describing a step that recognises the answer without already knowing it.
  4. How will the amplitudes be mixed so that the marks turn into odds?
  5. How many rounds, and how likely is the right answer at the end?
  6. How will you check the result? For many problems, checking an answer is easy even when finding it is hard, so run it, check it, and repeat if needed.

If question 2 has no good answer, a quantum computer probably won't help. That's the same lesson as What quantum computers are good at, seen from the designer's side.

What makes it hard in practice

  • No peeking. You can't print a value halfway through to see what's going on. Designers reason about amplitudes on paper or in simulators instead.
  • Every step must be reversible. Gates can't simply throw information away, so algorithms often need helper qubits to hold intermediate results, and steps to clean them up afterwards.
  • Errors pile up. On today's hardware, every gate adds a little noise. Long algorithms like Shor's need error-corrected machines that don't exist yet.
  • Simulators run out of road. An ordinary computer can simulate small circuits exactly, which is great for learning, but the memory needed doubles with every qubit.

Common questions

Do I need to know physics to write quantum programs? Not much. Most programmers work with gates and circuits, as above, and never touch the hardware physics. Some linear algebra helps once you go beyond the basics.

Can I run this myself? Yes. Qiskit and similar toolkits are free, and their simulators run on an ordinary laptop. Several providers also offer limited free time on real quantum hardware.

Is Shor's algorithm built the same way? Broadly, yes. It spreads out, uses arithmetic to imprint a repeating pattern hidden in the number, mixes with a step called the quantum Fourier transform, and measures. It just needs far more qubits and far more steps than these examples, which is why it can't yet be run at a useful size.

What you’ve learned

  • A quantum algorithm is a recipe of gates, written so that the answer is waiting when you finally measure.
  • One useful pattern: spread out, mark, mix, measure.
  • It only helps when the problem has structure that marking and mixing can exploit.

Check your understanding optional

The marking step flips the sign of the answer's amplitude. Why doesn't that change the odds straight away?

Show the answer

Because the chance of a result is the square of its amplitude's size, and squaring hides the sign. Only the mixing step, where amplitudes interfere, turns the sign difference into a difference in odds.

Sources and further reading

Tags show the kind of source (how sources are chosen and checked). Dates and figures were checked against these sources on 19 October 2026.

  1. Peer-reviewed paperQuantum complexity theory (the Bernstein–Vazirani algorithm)Ethan Bernstein and Umesh Vazirani, SIAM Journal on Computing, 1997
  2. Peer-reviewed paperA fast quantum mechanical algorithm for database searchLov Grover, STOC, 1996
  3. BookQuantum Computation and Quantum Information (10th anniversary edition)Michael Nielsen and Isaac Chuang, Cambridge University Press, 2010
  4. ReferenceQiskit: open-source toolkit for quantum computingQiskit contributors (IBM), 2026

Written by Muhammad Atif · Reviewed 26 November 2026 · Report a correction