Lesson 15: Simon’s algorithm
A hidden XOR pattern, an exponential gap, and equations over bits
The interactive version checks every answer, gives hints and saves your progress in this browser. This page is the same lesson on one page, with the answers folded away.
1 / 15 · Learn
Before you start
Lesson 14 counted queries and found a hidden string with a single one, where a classical computer needs : a gap of against 1. This lesson hides a string in a different way, and the gap becomes exponential, even against classical methods that are allowed to guess. Part A: the problem, the classical cost and one run of the circuit. Part B: from runs to , the size of the gap, and the algorithm in Qiskit.
By the end of this lesson you can:
- state Simon’s problem and find from any collision ;
- count pairs of queries to estimate how many random classical queries it takes to find ;
- follow one run of Simon’s circuit and say what its result tells you about ;
- solve the equations for by elimination over bits, and say how many runs that takes;
- build the circuit in Qiskit and read its counts.
Check yourself
A warm-up: make a guess before reading on. The lesson comes back to it.
Simon’s circuit for hides a string, say . The circuit is run once and its 3 input qubits are measured. What does that one result give you?
- AThe hidden string itself
- BThe value at one random input
- CA random string with : one equation that must satisfy
- D with probability , otherwise a random string
Show the answer
Answer: C. A random string with : one equation that must satisfy
After the query and the last Hadamards, the inputs hold a string chosen uniformly from the strings with . One such is one equation for the bits of ; about runs give enough equations to solve for .
Why not the others
- A. One run gives a random string with : , , or , each with probability . The result can be , which says nothing at all. It takes several runs, and some algebra, to pin down.
- B. That is what you get by measuring right after the query, as in Lesson 9. Simon’s circuit adds Hadamards after the query, and the measured inputs then hold a string with , not an input of .
- D. No allowed result is more likely than another: each of the strings with has probability . Even when the result happens to equal , nothing in the run tells you so.
Check yourself
A warm-up: make a guess before reading on. The lesson comes back to it.
A classical program attacks Simon’s problem for by querying random inputs until two of them give the same value. Roughly how many queries does that take?
- AAbout 20, one per bit
- BAbout
- CAbout
- DAbout
Show the answer
Answer: B. About
A good chance of a collision needs about as many pairs as there are candidates: , so is of order . On average random queries take about , roughly .
Why not the others
- A. A query returns a value , not a bit of . 20 random queries make only 190 pairs, and each pair is a matching pair with chance .
- C. queries make a collision certain, since there are only different values. Random queries collide much sooner, because queries make about pairs: about queries already make about pairs.
- D. You never need every input: queries already force a collision. Random queries usually find one after about , since the number of pairs grows like .
2 / 15 · Learn
Part A · Simon’s problem
Part A covers the problem, what it costs a classical computer, and one run of the quantum circuit.
Simon’s problem is a promise problem, like Deutsch–Jozsa. A black box computes , which takes bits to bits. You are promised that there is a string , not all zeros, such that
So the inputs come in pairs . The two members of a pair share a value, and different pairs have different values: is two-to-one. The values themselves carry no pattern. All the structure is in which inputs collide. Your task is to find . The figure shows one such for and .
A function with the promise, for and . Each input’s partner is the input XOR : , , and so on. The four values are arbitrary; only the pairing matters.
Check yourself
Here and . You know . Which other input has the value ?
- ANone: gives each value only once
- B
- C
- D
- E
Hints
- Which input is paired with this one?
- Its partner is : XOR the two strings bit by bit.
Show the answer
Answer: D.
, so the partner of is , and as well.
Why not the others
- A. That would make one-to-one. The promise makes it two-to-one: and always share a value, so too.
- B. That is the value , not an input. The other input with this value is the partner .
- C. That flips every bit of , which is . It would be the partner only if . XOR with flips only the bits where has a 1: .
- E. That is itself, the difference between partners. The partner of is : XOR bit by bit, which gives .
In a circuit, becomes the oracle of Lesson 9 with a wider output: input qubits, output qubits, and
where is XOR bit by bit. (The output’s contents are called here, because will name something else.) With the outputs starting at , one query writes there. A classical query is the same call on a single input. Either way, the cost we count is the number of calls, as in Lesson 14.
Classically, a collision settles everything: two different inputs with the same value. with forces , so .
Check yourself
You query for and find . What is ?
- AYou can’t tell yet: more queries are needed
- B
- C
- D
Hints
- When can two different inputs share a value?
- Only when they differ by , so XOR them.
Show the answer
Answer: C.
Two different inputs with the same value must be partners, , so .
Why not the others
- A. One collision is enough. Two different inputs with the same value must be partners, , so .
- B. That is one of the inputs. The promise says two different inputs share a value only when they differ by , so is the XOR of the two: .
- D. That is the bitwise AND of the two inputs. Partners differ by , so is their XOR: .
3 / 15 · Learn
Part A · The classical cost: birthdays
Without a collision, a classical query teaches little. If , you learn only that : one candidate out of is ruled out. Each pair of queried inputs is one such test, and queries make
pairs. That count grows like , which is why collisions come sooner than you might expect. It is the birthday puzzle: a room of 23 people holds 253 pairs, and the chance that two of them share a birthday is just over 50%, although there are 365 possible days.
Check yourself
You have queried 40 different inputs and seen no collision yet. How many pairs of queried inputs is that? Each pair is one chance that two of them are partners.
Hints
- Each new input can be compared with every input before it.
- The 40th input adds 39 pairs; add them all up, or use .
Show the answer
Answer:
pairs: the second input makes 1 pair, the third 2 more, and the 40th 39 more.
Common slips
- : That is , which counts every pair twice and also pairs each input with itself. The count is .
- : That counts each pair twice, once in each order. Divide by 2: .
- : That is the number of queries. The tests are pairs: each new input is compared with every earlier one, .
Now put in numbers for . There are candidates for , and each pair of queried inputs has a chance of about one in a million of being partners.
- 100 queries make pairs: a collision chance of about 0.5%.
- queries make about pairs, as many as half the candidates: a chance of about 39%.
- queries make about 2.1 million pairs: a chance of about 87%.
So a random search needs about queries, and on average about , which is about for . Simon proved that no classical algorithm does better than order queries, not even a randomized one that is allowed a small chance of error.
The chance of at least one collision after random queries for , computed exactly. It is 39% at , one half at about , and 87% at : doubling the queries quadruples the pairs.
Check yourself
A classical program attacks Simon’s problem for by querying random inputs until two of them give the same value. Roughly how many queries does that take?
- AAbout 40, one per bit
- BAbout
- CAbout
- DAbout
Show the answer
Answer: B. About
A good chance of a collision needs about as many pairs as there are candidates: , so is of order . On average random queries take about , roughly .
Why not the others
- A. A query returns a value , not a bit of . 40 random queries make only 780 pairs, and each pair is a matching pair with chance .
- C. queries make a collision certain, since there are only different values. Random queries collide much sooner, because queries make about pairs: about queries already make about pairs.
- D. You never need every input: queries already force a collision. Random queries usually find one after about , since the number of pairs grows like .
4 / 15 · Learn
Part A · One run of the circuit
Simon’s circuit has qubits, all starting in : inputs and outputs.
- on the inputs gives every the same amplitude.
- One query: writes into the outputs, in every branch at once.
- on the inputs again.
- Measure the inputs. Call the result .
After step 2 the state is
Group its terms by value. The two inputs of a pair share their value, so each value in the outputs sits beside two branches of the inputs, and .
Simon’s circuit drawn with registers: each line stands for qubits (the slash and ). The output register may be measured after (dashed meter) or left alone. The inputs are measured at the end.
Check yourself
Take . In , how many terms have the value in the output register?
- AEight: the query writes the same value in every branch
- BTwo: and
- COne: the term with
- DFour: half of the eight inputs
Hint
- Which inputs share a value with ?
Show the answer
Answer: B. Two: and
The inputs sharing a value with are and its partner . So appears in exactly two terms, and the four values split the eight terms into four pairs.
Why not the others
- A. Each branch gets its own value . The eight inputs make four pairs and four different values, two terms per value.
- C. Under the promise is two-to-one. The partner has the same value, so its term carries too.
- D. That would make four-to-one. Only and share a value, and , so two terms carry .
Suppose you measure the output register now. You get one value, each of the values with probability . Only the two inputs with that value survive, so the inputs are left in
where is either member of that pair. You can’t read from this state directly. Measuring it would give or , one random input, and to compare the two you would need a second copy of the same pair, which you can’t make. The last Hadamards get information about out of a single copy.
Check yourself
Take , and the function in the figure of pairs above, so . After the query you measure the output register and get . Enter the state of the three input qubits, to .
Hints
- Which inputs have the same value as ?
- Those two branches keep equal amplitudes; renormalize so that the squares add up to 1.
Show the answer
Answer:
The value belongs to the pair , since . Those two branches survive with equal amplitudes, so the inputs are .
Do you have to measure the outputs? No. Nothing acts on them after , so the input measurement has the same statistics whether or not you look at the outputs first. Left alone, the outputs simply keep a record of which pair the inputs are in.
Compare Lesson 13, where leftover work qubits wrecked interference. Here the leftover register holds , and that is the same for and . So the two branches of a pair can still interfere with each other. Branches from different pairs can’t, because their outputs differ, but the algorithm never needs them to.
Check yourself
A student leaves out the measurement of the output register and measures only the inputs at the end. What happens to the results ?
- AThe circuit fails: the inputs must collapse to one pair before the Hadamards
- BNothing changes: every has the same probability as before
- CThey become uniform over all strings: the inputs are entangled with the outputs, so nothing interferes
- DEvery run now gives , because all the pairs interfere together
Hint
- Does anything act on the output register after the query?
Show the answer
Answer: B. Nothing changes: every has the same probability as before
Nothing touches the outputs after , so measuring them or not leaves the input statistics unchanged. Unmeasured, the state is a sum over pairs, each pair tagged by its value in the outputs, and within each pair the two branches interfere exactly as in the measured case.
Why not the others
- A. No collapse is needed. Each pair sits in its own branch of the output register, and the Hadamards act on every branch. Measuring the outputs only tells you which pair you had, and the statistics of are the same for every pair.
- C. Entanglement with the outputs blocks interference only between branches whose outputs differ. The two branches of a pair share , so they interfere as before, and the last Hadamards still cancel every with .
- D. Different pairs never interfere: their outputs hold different values. With or without the output measurement, a run gives a random with , not .
5 / 15 · Learn
Part A · The last Hadamards
Predict, then see
Take and , with , the second bit: and . Since takes only two values, one output qubit is enough. The circuit applies to both inputs, then (a CNOT from to the output), then to both inputs again. What will the two input qubits read?
- A every time: the circuit reads out
- B or : the two inputs of one pair
- CAny of the four strings, a quarter of the time each
- D or , half the time each
Why do and never appear on the inputs? A sentence or two is enough.
Show what happens
What happens: D. or , half the time each
After the state is : each output value sits beside one pair, or . The last Hadamards act on each pair. In the first qubit is , and turns it into ; the second qubit is and becomes . The other pair gives . Either way the first bit of is 0 and the second is random: is or , exactly the strings with . The string never appears.
Why not the others
- A. The widget never shows on the inputs. It shows and , about half the time each. A run gives a string with , and .
- B. Those are the two members of the pair , which is what you would read just before the last Hadamards. The Hadamards change that: the widget shows and .
- C. The Hadamards don’t just spread amplitude around. Two of the four strings get contributions that cancel exactly: in the widget, and never appear on the inputs.
Here is the general calculation. Lesson 9 gave . Apply it to both branches of a pair:
The dot product splits over XOR, , so the bracket is
If , the two terms cancel and gets amplitude 0. If , they add, giving amplitude . So the measurement gives each of the strings with with probability , whichever pair you had: only sets the signs.
The last Hadamards on , for . Each gets one sign from each branch. Where the signs are opposite and cancel (shaded rows); where they agree, and gets amplitude .
Check yourself
Simon’s circuit with and . After the query the inputs are in . What is the amplitude of after the last Hadamards?
Hints
- Each branch gives the term .
- Compute and ; equal dot products add, different ones cancel.
Show the answer
Answer:
and , so the two terms are and . They add, since : the amplitude is .
Common slips
- : Right size, wrong sign. Both branches give the sign , because and .
- : That is the probability, the squared amplitude. The amplitude itself is .
- : That counts only the branch . Its partner adds an equal term, since : together .
- : The two branches cancel only when . Here , so they add.
Check yourself
Simon’s circuit runs with and . Which results can the input measurement give? Choose all that are possible.
Select all that apply.
- A
- B
- C
- D
- E
- F
Hint
- appears only if . What is in words?
Show the answer
Answers: A. ; B. ; C. ; F.
, so the possible results are the strings with an even number of 1s: , , and , each with probability .
Why not the others
- D. , so the two branches of every pair cancel on .
- E. That is itself. Here , so the two branches of every pair cancel on : with an odd number of 1s, is never a result.
6 / 15 · Learn
Part A · Build one run
Now build a whole run yourself, for and . A function with this promise is : and . One output qubit can hold it, and the oracle is two CNOTs, one from each input into the output.
Check yourself
Build Simon’s circuit for and on three wires (the inputs , and the output), with the oracle for . Leave out the measurement.
Build it in the circuit builder (gates: H, CNOT, control, X).
Hints
- Three stages: Hadamards on the inputs, the oracle, Hadamards on the inputs again.
- The oracle adds and then to the output: a CNOT from each input to the output.
- Column 1: on and . Columns 2 and 3: a CNOT from to the output, then one from . Column 4: on both inputs.
Show the answer
One circuit that works: H on q0 and q1, then CNOT q0→q2, then CNOT q1→q2, then H on q0 and q1.
The state is , so the inputs read or : the strings with . That equals is a coincidence of this : . With , earlier, never appeared.
7 / 15 · Learn
Part B · From equations to s
Part B turns runs into , measures the size of the gap, and writes the algorithm in Qiskit.
Each run gives a string , and with it one equation for the bits of :
For , says : has an even number of 1s. That rules out four of the seven candidates and leaves , and . A second run with says , which leaves only .
Some runs add nothing. gives , and a that is the XOR of earlier results gives an equation you could already get by adding theirs. What you need are independent equations. Each one halves the solutions, from down to exactly two: and . Since , it is the other one.
Before any run
After run 1
After run 2
The candidates for with , as two runs come in. keeps the strings with an even number of 1s; keeps those that end in 0. Two independent equations, , leave one candidate.
Refresher: Bits, XOR and bit strings Equations over bits: XOR as addition, the dot product mod 2, and elimination, with practice.
Check yourself
Simon’s circuit with gives and then . Which string is ?
- A
- BNot enough runs yet: three bits need three equations
- C
- D
- E
Hint
- Write the two equations in terms of , , .
Show the answer
Answer: E.
gives and gives , so . The solutions are and , and , so .
Why not the others
- A. gives a third equation you can derive, ; it is not a solution. Test it: , so it breaks the first equation.
- B. Two independent equations are enough for . They leave two solutions, and , and is the nonzero one.
- C. satisfies every equation, which is exactly why it doesn’t count: the promise says . The other solution is .
- D. is a result, not . Test it against both equations: is fine, but , so it breaks the second.
How many runs does that take? Suppose you already have independent results, with . The strings you can make by XOR-ing some of them, in all (counting ), are exactly the ones that add nothing new. A fresh run gives one of the allowed strings at random, so it brings a new independent equation with probability
So each missing equation takes at most two runs on average, and the average total is under . Solving the equations is ordinary elimination, about bit operations. In all: about queries.
Check yourself
Simon’s circuit with . So far you have 2 independent results. What is the probability that the next run gives a new independent equation?
Hints
- How many strings can a run give at all, and how many of them add nothing new?
- The useless ones are the XORs of your results, including : of them.
Show the answer
Answer:
A run gives one of the strings with , all equally likely. The XORs of your results (including ) add nothing. So the chance is .
Common slips
- : That treats only your 2 results as useless. and every XOR of your results add nothing either: of the 8 allowed strings.
- : That counts your 2 results and as useless, but every XOR of your results adds nothing too: useless strings in all.
8 / 15 · Worked example
Part B · Worked example: solving for s
Every string here has its first bit, or , on the left.
Worked example
Four bits
Simon’s circuit with gives the results , , and , in that order. Find .
Equations
One equation per result: gives ; gives ; gives ; gives .
Drop what is not new
, and its equation is the sum of the first two: . It adds nothing. That leaves three independent equations, which is for : enough.
Eliminate
directly. The other two tie the remaining bits together: and . So everything follows from : gives , and gives .
Answer and check
, so . Check every result: , , , .
Your turn: enough?
New runs, again with , give , and .
Your turn
Do the results , and pin down?
- AYes: three results is for
- BNo: one of the three adds nothing new
- CNo: four bits need four equations
- DYes: none of them is , so each one is new
Hint
- Is any of the three the XOR of the other two?
Show the answer
Answer: B. No: one of the three adds nothing new
, so there are two independent equations, and . They leave four solutions: and the candidates , and . One more independent run is needed.
Why not the others
- A. Three results are enough only if they are independent. Here , so the third equation is the sum of the first two. Two equations leave solutions: and three candidates.
- C. Three independent equations would be enough for four bits: they leave and . The trouble here is that only two are independent, because .
- D. Being nonzero isn’t enough: a result can also be the XOR of earlier ones, and . Only two of these equations are independent.
Your turn: solve
One more run gives .
Your turn
With , , and now , which string is ?
- A
- B
- C
- D
Hint
- Write the new equation, then test the three candidates from the last step.
Show the answer
Answer: A.
The equations now say , and : all four bits are equal. So , and indeed , , and each share an even number of 1s with it.
Why not the others
- B. was a candidate, but : it breaks the new equation .
- C. is the newest result. Use it as an equation, , and test the three candidates left: only passes.
- D. was a candidate, but : it breaks the new equation .
One more, fully worked, with fewer bits and a few wasted runs.
Another worked example
Another example: three bits
Equations
With the runs give , , and . The result gives , and the second repeats the first. The useful equations are (from ) and (from ).
Solve
So and : all three bits are equal. The solutions are and , and , so .
Check
and . Four runs, two of them useful: about what to expect, since each run is new with probability at least .
9 / 15 · Explore
Part B · Play both sides
Here is the whole algorithm, next to the classical search from Part A. In the quantum tab, each run adds an equation and crosses out the candidates it breaks; the round ends when one candidate is left. Play a few rounds of each and compare the averages in the tally.
Check yourself
With , what is the probability that a run gives , an equation that says nothing?
Hint
- How many results are possible for , and are they equally likely?
Show the answer
Answer:
A run gives one of the strings with , each with probability , and is always one of them. So a quarter of the runs are wasted this way.
Common slips
- : That treats all eight strings as possible results. Only the four with ever appear, each with probability .
- : for every , so is always one of the allowed results. It just teaches you nothing.
- : There are seven nonzero strings, but that is the count of candidates for , not of results. A run gives one of the four strings with , each with probability .
10 / 15 · Learn
Part B · An exponential gap
Put the two costs side by side. The quantum algorithm uses about queries, plus about steps of classical algebra afterwards. Every classical algorithm, even a randomized one that may err with small probability, needs on the order of queries. For that is about 21 runs against about queries; for , about 41 against 1.3 million.
So Simon’s problem gives an exponential separation in the query model, and a stronger one than Deutsch–Jozsa’s. There, a few random classical queries were nearly always right, so the gap was exponential only against methods that must never err (Lesson 9). Here guessing doesn’t help.
Expected uses of the black box: random classical queries until a collision, and runs of Simon’s circuit until independent equations. The vertical axis is logarithmic, each grid line ten times the one below. On it, the classical count is a straight line (it doubles every two bits), while the quantum count grows by about one per bit.
Check yourself
Which statement about Simon’s algorithm is accurate?
- AIt needs only one query, like the circuit of Lesson 14
- BLike Deutsch–Jozsa, its gain is exponential only against classical methods that must never err
- CIt uses exponentially fewer queries than any classical algorithm, including randomized ones that may err with small probability
- DIt proves that quantum computers are exponentially faster at practical problems
Hint
- How does the classical cost behave when the classical method may guess?
Show the answer
Answer: C. It uses exponentially fewer queries than any classical algorithm, including randomized ones that may err with small probability
Quantum: about queries. Classical: order , even with randomness and a small error allowed. That is an exponential separation, in the query model.
Why not the others
- A. One run gives one equation . You need independent ones, so about runs: still exponentially fewer than .
- B. That was Deutsch–Jozsa’s weakness: random queries erred with probability at most , whatever was. For Simon’s problem, random queries need about tries to find a collision, and no classical method of any kind does better than order .
- D. It is a statement about calls to a black box. It shows what interference can do with a hidden XOR pattern, but on its own it says nothing about problems where you can read the circuit that computes . The step toward practical problems came with Shor’s algorithm.
11 / 15 · Learn
Part B · Simon’s algorithm in Qiskit
In Qiskit the circuit takes a few lines (Lesson 12 has the basics). It needs an oracle for a given , and there is a simple recipe. First copy into the outputs with CNOTs. Then pick a position where and XOR the bit into every output where has a 1. The outputs now hold : that is , with XOR-ed in when . The partner has the other value of bit , so
the same value, as the promise requires. Python counts from 0, so s[0] is the first bit, , and it sits on qubit 0.
Reading the counts takes care. Qiskit writes classical bit 0 on the right, so the key '100' means , which is in the lessons’ order. For the four keys 000, 100, 011 and 111 are, reversed, , , and : exactly the strings with . Reverse each key before you write its equation.
Check yourself
A colleague runs Simon’s algorithm in Qiskit for , measuring qubits 0, 1 and 2 into classical bits 0, 1 and 2, and gets {'000': 251, '010': 244, '100': 262, '110': 243}. What is , written as ?
- AYou can’t tell: the counts never show
- B
- C
- D
Hints
- Qiskit writes classical bit 0, here , on the right. Reverse each key first.
- Then write for two nonzero results and solve.
Show the answer
Answer: D.
Reversed into the lessons’ order, the results are , , , . Two nonzero ones, and , give and . The nonzero solution is . Check: , , .
Why not the others
- A. They don’t have to. Three of the four results are nonzero, and any two of those are independent. Their equations leave exactly one nonzero solution.
- B.
010is one of the keys: a result , written in Qiskit’s order. A result only tells you that ; it isn’t . Reverse the keys, write one equation per result, and solve. - C. That comes from reading the keys as if classical bit 0, , were on the left. Qiskit puts bit 0 on the right, so the key
100means . Reverse every key first: the results are , , , , and they give .
Check yourself
Complete the program so that it runs Simon’s algorithm for with : add CNOTs that put into the outputs a value with , two inputs per value. The recipe above works, and so does any other oracle with the promise: the check compares the measured distribution with the one Simon’s circuit must give.
from qiskit import QuantumCircuit
n = 3
s = '011' # s[0] is the first bit, s_1, on qubit 0
qc = QuantumCircuit(2 * n, n)
qc.h(range(n))
# The oracle goes here: CNOTs from the inputs (qubits 0 to 2)
# into the outputs (qubits 3 to 5).
qc.h(range(n))
qc.measure(range(n), range(n))Write the program in the app’s code editor (Python with Qiskit’s circuit methods), run it, and check it.
Hints
- Start by copying each input into its output:
qc.cx(i, n + i)foriinrange(n). - Then pick a position
jwiths[j] == '1'and addqc.cx(j, n + i)for everyiwheres[i] == '1'. - With , works: after the three copies, add
qc.cx(1, 4)andqc.cx(1, 5).
Show the answer
One program that works:
from qiskit import QuantumCircuit
n = 3
s = '011' # s[0] is the first bit, s_1, on qubit 0
qc = QuantumCircuit(2 * n, n)
qc.h(range(n))
# The oracle: copy x into the outputs ...
for i in range(n):
qc.cx(i, n + i)
# ... then XOR bit j of x into the outputs where s has a 1
j = s.index('1')
for i in range(n):
if s[i] == '1':
qc.cx(j, n + i)
qc.h(range(n))
qc.measure(range(n), range(n))With the recipe the outputs hold , and . The measured inputs are then the strings with : , , and , which Qiskit prints as 000, 110, 001 and 111.
12 / 15 · Practice
Practice
Three items, each with new numbers when it comes back. Hints are there if you need them.
Check yourself
Simon’s circuit with and . After the query the inputs are in . What is the amplitude of after the last Hadamards?
Hints
- Each branch gives the term .
- Compute and ; equal dot products add, different ones cancel.
Show the answer
Answer:
and , so the two terms are and . They add, since : the amplitude is .
Common slips
- : Right size, wrong sign. Both branches give the sign , because and .
- : That is the probability, the squared amplitude. The amplitude itself is .
- : The two branches cancel only when . Here , so they add.
Check yourself
Simon’s circuit with gives the results , and , which are independent. Which string is ?
- A
- B
- C
- D
- E
Hints
- Write one equation per result.
- Test each option against every equation: it survives only if it shares an even number of 1s with every result.
Show the answer
Answer: C.
The equations are , , . Exactly two strings satisfy all of them, and , and , so .
One equation per result: , , . With independent equations there are exactly two solutions. Check against each result: , , . The only other solution is , which the promise rules out.
Why not the others
- A. passes the first equations but not the last: .
- B. is a result, not . Use it as an equation, and test it against the others: .
- D. gives an equation you can derive from the first two, not a solution. Test it: , .
- E. satisfies every equation, which is exactly why it doesn’t count: the promise says . The other solution is .
Check yourself
You have queried 25 different inputs and seen no collision yet. How many pairs of queried inputs is that? Each pair is one chance that two of them are partners.
Hints
- Each new input can be compared with every input before it.
- The 25th input adds 24 pairs; add them all up, or use .
Show the answer
Answer:
pairs: the second input makes 1 pair, the third 2 more, and the 25th 24 more.
Common slips
- : That is , which counts every pair twice and also pairs each input with itself. The count is .
- : That counts each pair twice, once in each order. Divide by 2: .
- : That is the number of queries. The tests are pairs: each new input is compared with every earlier one, .
13 / 15 · Explain
Explain it
Explain it
Explain why a run of Simon’s circuit never gives a string with , and why you still need about runs to find .
Show a model answer
After the query, the inputs are (for whichever value the outputs hold) in an equal superposition of a pair, and . The last Hadamards give each one contribution from each branch, with signs and . When these are opposite and cancel, so such a never appears; when they add. So a run gives a uniformly random with : one linear equation for the bits of , not itself. It takes independent equations to leave only and , and each run gives a new one with probability at least one half, so about runs.
A good answer covers
- After the query the inputs hold a pair, (for each value in the outputs)
- The last Hadamards give two contributions whose signs differ by , so they cancel when
- A run gives one random equation , not
- About runs: independent equations are needed, and each run is new with probability at least one half
14 / 15 · Mastery check
Mastery check
Check yourself
Simon’s circuit for hides a string, say . The circuit is run once and its 3 input qubits are measured. What does that one result give you?
- A with probability , otherwise a random string
- BA random string with : one equation that must satisfy
- CThe value at one random input
- DThe hidden string itself
Show the answer
Answer: B. A random string with : one equation that must satisfy
After the query and the last Hadamards, the inputs hold a string chosen uniformly from the strings with . One such is one equation for the bits of ; about runs give enough equations to solve for .
Why not the others
- A. No allowed result is more likely than another: each of the strings with has probability . Even when the result happens to equal , nothing in the run tells you so.
- C. That is what you get by measuring right after the query, as in Lesson 9. Simon’s circuit adds Hadamards after the query, and the measured inputs then hold a string with , not an input of .
- D. One run gives a random string with : , , or , each with probability . The result can be , which says nothing at all. It takes several runs, and some algebra, to pin down.
Check yourself
You query for and find . What is ?
- A
- BYou can’t tell yet: more queries are needed
- C
- D
Hints
- When can two different inputs share a value?
- Only when they differ by , so XOR them.
Show the answer
Answer: A.
Two different inputs with the same value must be partners, , so .
Why not the others
- B. One collision is enough. Two different inputs with the same value must be partners, , so .
- C. That is the bitwise AND of the two inputs. Partners differ by , so is their XOR: .
- D. That is one of the inputs. The promise says two different inputs share a value only when they differ by , so is the XOR of the two: .
Check yourself
Simon’s circuit with and . After the query the inputs are in . What is the amplitude of after the last Hadamards?
Hints
- Each branch gives the term .
- Compute and ; equal dot products add, different ones cancel.
Show the answer
Answer:
and , so the two terms are and . They add, since : the amplitude is .
Common slips
- : Right size, wrong sign. Both branches give the sign , because and .
- : That is the probability, the squared amplitude. The amplitude itself is .
- : That counts only the branch . Its partner adds an equal term, since : together .
- : The two branches cancel only when . Here , so they add.
Check yourself
A classical program attacks Simon’s problem for by querying random inputs until two of them give the same value. Roughly how many queries does that take?
- AAbout 24, one per bit
- BAbout
- CAbout
- DAbout
Show the answer
Answer: B. About
A good chance of a collision needs about as many pairs as there are candidates: , so is of order . On average random queries take about , roughly .
Why not the others
- A. A query returns a value , not a bit of . 24 random queries make only 276 pairs, and each pair is a matching pair with chance .
- C. queries make a collision certain, since there are only different values. Random queries collide much sooner, because queries make about pairs: about queries already make about pairs.
- D. You never need every input: queries already force a collision. Random queries usually find one after about , since the number of pairs grows like .
Check yourself
Simon’s circuit with gives the results , and , which are independent. Which string is ?
- A
- B
- C
- D
- E
Hints
- Write one equation per result.
- Test each option against every equation: it survives only if it shares an even number of 1s with every result.
Show the answer
Answer: A.
The equations are , , . Exactly two strings satisfy all of them, and , and , so .
One equation per result: , , . With independent equations there are exactly two solutions. Check against each result: , , . The only other solution is , which the promise rules out.
Why not the others
- B. gives an equation you can derive from the first two, not a solution. Test it: , , .
- C. passes the first equations but not the last: .
- D. is a result, not . Use it as an equation, and test it against the others: .
- E. satisfies every equation, which is exactly why it doesn’t count: the promise says . The other solution is .
Check yourself
A colleague runs Simon’s algorithm in Qiskit for , measuring qubits 0, 1 and 2 into classical bits 0, 1 and 2, and gets {'000': 244, '001': 244, '110': 248, '111': 264}. What is , written as ?
- AYou can’t tell: the counts never show
- B
- C
- D
Hints
- Qiskit writes classical bit 0, here , on the right. Reverse each key first.
- Then write for two nonzero results and solve.
Show the answer
Answer: B.
Reversed into the lessons’ order, the results are , , , . Two nonzero ones, and , give and . The nonzero solution is . Check: , , .
Why not the others
- A. They don’t have to. Three of the four results are nonzero, and any two of those are independent. Their equations leave exactly one nonzero solution.
- C.
001is one of the keys: a result , written in Qiskit’s order. A result only tells you that ; it isn’t . Reverse the keys, write one equation per result, and solve. - D. That comes from reading the keys as if classical bit 0, , were on the left. Qiskit puts bit 0 on the right, so the key
001means . Reverse every key first: the results are , , , , and they give .
Check yourself
Simon’s problem with . On average, about how many uses of the black box does each approach need: runs of Simon’s circuit, and random classical queries until a collision?
- AAbout runs, and about queries
- B run, and about queries
- CAbout runs, and queries
- DAbout of each
Hint
- A run is new with probability at least one half; random queries need about as many pairs as candidates.
Show the answer
Answer: A. About runs, and about queries
Quantum: independent equations, each run new with probability at least , about runs on average. Classical: a collision takes about random queries.
Why not the others
- B. That is the Bernstein–Vazirani pattern. Here one run gives one equation ; you need independent ones, so about runs. A classical search needs a collision, which takes about queries.
- C. queries make a collision certain, but random ones find one much sooner: queries make about pairs, so about on average.
- D. A classical query gives one value . Without a collision, each pair of queries rules out a single candidate, and there are candidates. That takes about queries, not .
15 / 15 · Recap
Recap
From memory
Before reading the summary, write down what you remember. Pulling it back from memory is what makes it last.
State Simon’s problem, and say what a classical algorithm needs to solve it.
Show the key points
- maps bits to bits, and exactly when or , with
- A collision with gives
- Random queries need about tries, since queries make pairs; no classical algorithm beats order
Describe one run of Simon’s circuit and what it returns.
Show the key points
- on the inputs, , on the inputs, then measure the inputs
- After the query the inputs are in a pair, ; measuring the outputs is optional
- gets amplitude proportional to : a uniformly random with
How do you get from the results to , and how many queries does that take?
Show the key points
- Each gives an equation ; collect independent ones and solve by elimination
- The solutions are and ; take the nonzero one
- Each run is new with probability at least , so about queries, against order classically
Key ideas
Simon’s promise: exactly when . Any collision gives .
Classically, queries test pairs, so a collision takes about queries, and no classical algorithm, even a randomized one, does better than order .
One run: , , . The pair turns into a sum where has amplitude proportional to , so is uniform over the strings with .
One run gives one equation, never itself. independent equations, solved by elimination over bits, leave and ; about runs are enough.
About quantum queries against order classical ones: an exponential separation in the query model, and the idea behind Shor’s period finding.
In Qiskit, reverse each counts key (bit 0 is on the right) before you write its equation.
Go further
Free resources for when you want more than this lesson covers.
- Fundamentals of quantum algorithms: Simon’s algorithmJohn Watrous, IBM Quantum · Course
A careful treatment with the classical lower bound proved, and the circuit built in Qiskit.
- Quantum Computing: Lecture NotesRonald de Wolf · Lecture notes
Free lecture notes whose early chapters cover query algorithms, Simon’s algorithm among them, and then Shor’s period finding.
- Simon’s problemWikipedia · Article
The problem, the circuit and the history, including the link to Shor’s algorithm.
- Birthday problemWikipedia · Article
The counting behind the classical cost, with the square-root rule worked out in several ways.
Math refreshers
The toolkit topics this lesson leans on.
- Bits, XOR and bit stringsx ⊕ y, x · z mod 2, sums over {0,1}ⁿ, signs that depend on f(x), and equations over bits