How a quantum algorithm works, in plain English
On this page
- An algorithm is a recipe
- The three ingredients
- Circuits: sheet music for qubits
- The shape most algorithms share
- Worked example: guessing a hidden code
- A second example: searching four boxes
- The same thing as real code
- Writing one yourself, in plain English
- What makes it hard in practice
- Common questions
- Sources and further reading
The five-part series explained why quantum computers can help with some problems: amplitudes can cancel, so a well-designed calculation makes wrong answers fade and right ones build up. But it never showed what a quantum algorithm actually looks like. This piece does, 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 Part 1. 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 Part 2, 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 around 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 (Part 3) |
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.
The shape most algorithms share
Part 2 sketched the broad shape. Here it is again, with the gates that do each job:
- Spread out. H gates put every qubit into an even blend, so every possible input has the same small amplitude.
- Mark. A problem-specific step flips the sign of the amplitudes connected to the answer. The odds don't change yet; only the signs do.
- 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 Part 2, 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.
A second example: searching four boxes
Part 4 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. That's the modest speed-up described in Part 4: helpful in principle, but not a shortcut through everything.
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.
- A quantum algorithm is a recipe of gates, written so that the answer is waiting when you finally measure.
- Most share one shape: spread out, mark, mix, measure.
- It only helps when the problem has structure that marking and mixing can exploit.
Sources and further reading
Tags show what kind of source each one is. A standard or government guidance is an official document; a peer-reviewed paper has been checked by other experts; a preprint has not been peer-reviewed yet; an experiment reports a real-world demonstration; a company announcement is the company's own account. Dates and figures were checked against these sources on 14 October 2026. Spotted an error? Email hello@plainquantum.com and it will be corrected, with a note.
- 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