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.
Terms in this lesson
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:
- Remember the first number.
- Look at each remaining number in turn. If it's bigger than the one you're remembering, remember it instead.
- 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:
| Gate | Plain-English job | On the globe |
|---|---|---|
| X (flip) | Turns 0 into 1 and 1 into 0, like an ordinary NOT | Swaps the poles |
| H (blend) | Turns a plain 0 or 1 into an even blend, and turns those blends back again | Moves a pole to the equator, or back |
| Z (sign flip) | Leaves the odds alone but makes the amplitude on 1 negative | Half a turn about the north–south axis, moving points half way round the equator: a change of phase |
| Controlled flip | Flips a second qubit only where the first qubit is 1 | Works on two qubits at once; it's how entanglement is made (see Entanglement) |
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.
- A line is one qubit. It starts as 0 unless shown otherwise.
- A box is a one-qubit gate, named by its letter (here H, the blend).
- A filled dot marks the control qubit of a two-qubit gate.
- 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.
- 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.
| Control | Target before | Target after |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
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.
- Spread out. H gates put every qubit into an even blend, so every possible input has the same small amplitude.
- 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.
- Mix. More gates, often H gates again, make the amplitudes interfere, so the sign pattern turns into a difference in odds.
- 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:
- Start four qubits at 0, plus one helper qubit set to 1.
- Put every qubit into an even blend. The four qubits now hold every possible question at once, each with the same amplitude.
- 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.
- Undo the blend on the four qubits. The negative signs now interfere, and everything cancels except one pattern: the secret itself.
- Measure. You read 1011, with certainty, after asking one question.
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.
- Spread out. Blend both qubits. Each label has a 25% chance.
- Mark. A step that recognises the prize box flips the sign of its amplitude. The odds are still 25% each.
- 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.
- Measure. With four boxes, one round of marking and mixing gives the prize label every time.
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.
| Rounds | Chance of the prize |
|---|---|
| 0 | 1.6% |
| 1 | 13.5% |
| 2 | 34.4% |
| 3 | 59.1% |
| 4 | 81.6% |
| 5 | 96.4% |
| 6 | 99.7% |
| 7 | 90.7% |
| 8 | 71.8% |
| 9 | 47.5% |
| 10 | 23.8% |
| 11 | 6.6% |
| 12 | 0.0% |
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.
- 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.
- Mark. A controlled sign flip on 11 gives ½(|00⟩ + |01⟩ + |10⟩ − |11⟩). The chances are still 25% each, because (−0.5)² = 0.25.
- 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.
- 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:
- What's the problem, and what does a correct answer look like?
- 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.
- How will the answer be marked? Usually by flipping signs, which means describing a step that recognises the answer without already knowing it.
- How will the amplitudes be mixed so that the marks turn into odds?
- How many rounds, and how likely is the right answer at the end?
- 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.
- Peer-reviewed paperQuantum complexity theory (the Bernstein–Vazirani algorithm)Ethan Bernstein and Umesh Vazirani, SIAM Journal on Computing, 1997
- Peer-reviewed paperA fast quantum mechanical algorithm for database searchLov Grover, STOC, 1996
- BookQuantum Computation and Quantum Information (10th anniversary edition)Michael Nielsen and Isaac Chuang, Cambridge University Press, 2010
- ReferenceQiskit: open-source toolkit for quantum computingQiskit contributors (IBM), 2026
Written by Muhammad Atif · Reviewed 26 November 2026 · Report a correction