Skip to the content
Kets & Gates homeOpen the app

Lesson 15: Simon’s algorithm

A hidden XOR pattern, an exponential gap, and equations over bits

about 45 min15 partsbest after lesson 14

Start the interactive lesson

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 nn: a gap of nn against 1. This lesson hides a string ss 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 ss, the size of the gap, and the algorithm in Qiskit.

By the end of this lesson you can:

  • state Simon’s problem and find ss from any collision f(x)=f(x′)f(x) = f(x');
  • count pairs of queries to estimate how many random classical queries it takes to find ss;
  • follow one run of Simon’s circuit and say what its result tells you about ss;
  • solve the equations y⋅s=0y\cdot s = 0 for ss 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 n=3n = 3 hides a string, say s=110s = 110. The circuit is run once and its 3 input qubits are measured. What does that one result give you?

  1. AThe hidden string ss itself
  2. BThe value f(x)f(x) at one random input xx
  3. CA random string yy with y⋅s=0y\cdot s = 0: one equation that ss must satisfy
  4. Dss with probability 12\tfrac12, otherwise a random string
Show the answer

Answer: C. A random string yy with y⋅s=0y\cdot s = 0: one equation that ss must satisfy

After the query and the last Hadamards, the inputs hold a string yy chosen uniformly from the 2n−1=42^{n-1} = 4 strings with y⋅s=0y\cdot s = 0. One such yy is one equation for the bits of ss; about nn runs give enough equations to solve for ss.

Why not the others

  • A. One run gives a random string yy with y⋅s=0y\cdot s = 0: 000000, 001001, 110110 or 111111, each with probability 14\tfrac14. The result can be 000000, which says nothing at all. It takes several runs, and some algebra, to pin ss 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 yy with y⋅s=0y\cdot s = 0, not an input of ff.
  • D. No allowed result is more likely than another: each of the 44 strings with y⋅s=0y\cdot s = 0 has probability 1/41/4. Even when the result happens to equal ss, 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 n=20n = 20 by querying random inputs until two of them give the same value. Roughly how many queries does that take?

  1. AAbout 20, one per bit
  2. BAbout 210=1,0242^{10} = 1{,}024
  3. CAbout 219≈520,0002^{19} \approx 520{,}000
  4. DAbout 220≈1.0×1062^{20} \approx 1.0\times10^{6}
Show the answer

Answer: B. About 210=1,0242^{10} = 1{,}024

A good chance of a collision needs about as many pairs as there are candidates: k2/2≈220k^2/2 \approx 2^{20}, so kk is of order 2102^{10}. On average random queries take about 1.25⋅2101.25\cdot2^{10}, roughly 1,3001{,}300.

Why not the others

  • A. A query returns a value f(x)f(x), not a bit of ss. 20 random queries make only 190 pairs, and each pair is a matching pair with chance 1/(220−1)1/(2^{20} - 1).
  • C. 219+12^{19} + 1 queries make a collision certain, since there are only 2192^{19} different values. Random queries collide much sooner, because kk queries make about k2/2k^2/2 pairs: about 2102^{10} queries already make about 2192^{19} pairs.
  • D. You never need every input: 219+12^{19} + 1 queries already force a collision. Random queries usually find one after about 2102^{10}, since the number of pairs grows like k2/2k^2/2.

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 ff, which takes nn bits to nn bits. You are promised that there is a string ss, not all zeros, such that

f(x)=f(x′)  ⟺  x′=x or x′=x⊕s.f(x) = f(x') \iff x' = x \text{ or } x' = x\oplus s.

So the inputs come in pairs {x, x⊕s}\{x,\ x\oplus s\}. The two members of a pair share a value, and different pairs have different values: ff is two-to-one. The values themselves carry no pattern. All the structure is in which inputs collide. Your task is to find ss. The figure shows one such ff for n=3n = 3 and s=110s = 110.

A function with the promise, for n=3n = 3 and s=110s = 110. Each input’s partner is the input XOR 110110: 000⊕110=110000\oplus110 = 110, 001⊕110=111001\oplus110 = 111, and so on. The four values are arbitrary; only the pairing matters.

Check yourself

Here n=3n = 3 and s=101s = 101. You know f(011)=001f(011) = 001. Which other input has the value 001001?

  1. ANone: ff gives each value only once
  2. B001001
  3. C100100
  4. D110110
  5. E101101
Hints
  1. Which input is paired with this one?
  2. Its partner is x⊕sx\oplus s: XOR the two strings bit by bit.
Show the answer

Answer: D. 110110

f(x)=f(x⊕s)f(x) = f(x\oplus s), so the partner of 011011 is 011⊕101=110011\oplus101 = 110, and f(110)=001f(110) = 001 as well.

Why not the others

  • A. That would make ff one-to-one. The promise makes it two-to-one: xx and x⊕sx\oplus s always share a value, so f(110)=001f(110) = 001 too.
  • B. That is the value f(011)f(011), not an input. The other input with this value is the partner 011⊕s=110011\oplus s = 110.
  • C. That flips every bit of 011011, which is 011⊕111011\oplus111. It would be the partner only if s=111s = 111. XOR with s=101s = 101 flips only the bits where ss has a 1: 110110.
  • E. That is ss itself, the difference between partners. The partner of 011011 is 011⊕101011\oplus101: XOR bit by bit, which gives 110110.

In a circuit, ff becomes the oracle of Lesson 9 with a wider output: nn input qubits, nn output qubits, and

Uf∣x⟩∣b⟩=∣x⟩∣b⊕f(x)⟩,U_f\ket{x}\ket{b} = \ket{x}\ket{b\oplus f(x)},

where b⊕f(x)b\oplus f(x) is XOR bit by bit. (The output’s contents are called bb here, because yy will name something else.) With the outputs starting at 0…00\ldots0, one query writes f(x)f(x) 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. f(x)=f(x′)f(x) = f(x') with x≠x′x \ne x' forces x′=x⊕sx' = x\oplus s, so s=x⊕x′s = x\oplus x'.

Check yourself

You query ff for n=4n = 4 and find f(0110)=f(1011)f(0110) = f(1011). What is ss?

  1. AYou can’t tell yet: more queries are needed
  2. B01100110
  3. C11011101
  4. D00100010
Hints
  1. When can two different inputs share a value?
  2. Only when they differ by ss, so XOR them.
Show the answer

Answer: C. 11011101

Two different inputs with the same value must be partners, x′=x⊕sx' = x\oplus s, so s=x⊕x′=0110⊕1011=1101s = x\oplus x' = 0110\oplus1011 = 1101.

Why not the others

  • A. One collision is enough. Two different inputs with the same value must be partners, x′=x⊕sx' = x\oplus s, so s=0110⊕1011s = 0110\oplus1011.
  • B. That is one of the inputs. The promise says two different inputs share a value only when they differ by ss, so ss is the XOR of the two: 11011101.
  • D. That is the bitwise AND of the two inputs. Partners differ by ss, so ss is their XOR: 0110⊕1011=11010110\oplus1011 = 1101.

3 / 15 · Learn

Part A · The classical cost: birthdays

Without a collision, a classical query teaches little. If f(x)≠f(x′)f(x) \ne f(x'), you learn only that s≠x⊕x′s \ne x\oplus x': one candidate out of 2n−12^n - 1 is ruled out. Each pair of queried inputs is one such test, and kk queries make

k(k−1)2\frac{k(k-1)}{2}

pairs. That count grows like k2/2k^2/2, 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
  1. Each new input can be compared with every input before it.
  2. The 40th input adds 39 pairs; add them all up, or use k(k−1)/2k(k-1)/2.
Show the answer

Answer: 780780

k(k−1)/2=40⋅39/2=780k(k-1)/2 = 40\cdot39/2 = 780 pairs: the second input makes 1 pair, the third 2 more, and the 40th 39 more.

Common slips

  • 16001600: That is 40240^2, which counts every pair twice and also pairs each input with itself. The count is 40⋅39/2=78040\cdot39/2 = 780.
  • 15601560: That counts each pair twice, once in each order. Divide by 2: 780780.
  • 4040: That is the number of queries. The tests are pairs: each new input is compared with every earlier one, 0+1+⋯+39=7800 + 1 + \cdots + 39 = 780.

Now put in numbers for n=20n = 20. There are 220−1=1,048,5752^{20} - 1 = 1{,}048{,}575 candidates for ss, and each pair of queried inputs has a chance of about one in a million of being partners.

  • 100 queries make 4,9504{,}950 pairs: a collision chance of about 0.5%.
  • 1,024=2101{,}024 = 2^{10} queries make about 524,000524{,}000 pairs, as many as half the candidates: a chance of about 39%.
  • 2,0482{,}048 queries make about 2.1 million pairs: a chance of about 87%.

So a random search needs about 2n/22^{n/2} queries, and on average about 1.25⋅2n/21.25\cdot2^{n/2}, which is about 1,3001{,}300 for n=20n = 20. Simon proved that no classical algorithm does better than order 2n/22^{n/2} queries, not even a randomized one that is allowed a small chance of error.

The chance of at least one collision after kk random queries for n=20n = 20, computed exactly. It is 39% at k=210k = 2^{10}, one half at about k=1,206k = 1{,}206, and 87% at k=2⋅210k = 2\cdot2^{10}: doubling the queries quadruples the pairs.

Check yourself

A classical program attacks Simon’s problem for n=40n = 40 by querying random inputs until two of them give the same value. Roughly how many queries does that take?

  1. AAbout 40, one per bit
  2. BAbout 220≈1.0×1062^{20} \approx 1.0\times10^{6}
  3. CAbout 239≈5.5×10112^{39} \approx 5.5\times10^{11}
  4. DAbout 240≈1.1×10122^{40} \approx 1.1\times10^{12}
Show the answer

Answer: B. About 220≈1.0×1062^{20} \approx 1.0\times10^{6}

A good chance of a collision needs about as many pairs as there are candidates: k2/2≈240k^2/2 \approx 2^{40}, so kk is of order 2202^{20}. On average random queries take about 1.25⋅2201.25\cdot2^{20}, roughly 1.3 million1.3\text{ million}.

Why not the others

  • A. A query returns a value f(x)f(x), not a bit of ss. 40 random queries make only 780 pairs, and each pair is a matching pair with chance 1/(240−1)1/(2^{40} - 1).
  • C. 239+12^{39} + 1 queries make a collision certain, since there are only 2392^{39} different values. Random queries collide much sooner, because kk queries make about k2/2k^2/2 pairs: about 2202^{20} queries already make about 2392^{39} pairs.
  • D. You never need every input: 239+12^{39} + 1 queries already force a collision. Random queries usually find one after about 2202^{20}, since the number of pairs grows like k2/2k^2/2.

4 / 15 · Learn

Part A · One run of the circuit

Simon’s circuit has 2n2n qubits, all starting in ∣0⟩\ket0: nn inputs and nn outputs.

  1. H⊗nH^{\otimes n} on the inputs gives every xx the same amplitude.
  2. One query: UfU_f writes f(x)f(x) into the outputs, in every branch at once.
  3. H⊗nH^{\otimes n} on the inputs again.
  4. Measure the inputs. Call the result yy.

After step 2 the state is

∣ψ2⟩=12n∑x∣x⟩∣f(x)⟩.\ket{\psi_2} = \frac{1}{\sqrt{2^n}}\sum_x \ket{x}\ket{f(x)}.

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, ∣x⟩\ket{x} and ∣x⊕s⟩\ket{x\oplus s}.

Simon’s circuit drawn with registers: each line stands for nn qubits (the slash and nn). The output register may be measured after UfU_f (dashed meter) or left alone. The inputs are measured at the end.

Check yourself

Take n=3n = 3. In ∣ψ2⟩\ket{\psi_2}, how many terms ∣x⟩∣f(x)⟩\ket{x}\ket{f(x)} have the value f(x)=f(000)f(x) = f(000) in the output register?

  1. AEight: the query writes the same value in every branch
  2. BTwo: x=000x = 000 and x=sx = s
  3. COne: the term with x=000x = 000
  4. DFour: half of the eight inputs
Hint
  1. Which inputs share a value with 000000?
Show the answer

Answer: B. Two: x=000x = 000 and x=sx = s

The inputs sharing a value with 000000 are 000000 and its partner 000⊕s=s000\oplus s = s. So f(000)f(000) 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 f(x)f(x). The eight inputs make four pairs and four different values, two terms per value.
  • C. Under the promise ff is two-to-one. The partner 000⊕s=s000\oplus s = s has the same value, so its term carries f(000)f(000) too.
  • D. That would make ff four-to-one. Only xx and x⊕sx\oplus s share a value, and 000⊕s=s000\oplus s = s, so two terms carry f(000)f(000).

Suppose you measure the output register now. You get one value, each of the 2n−12^{n-1} values with probability 2/2n2/2^n. Only the two inputs with that value survive, so the inputs are left in

12(∣x0⟩+∣x0⊕s⟩),\frac{1}{\sqrt2}\big(\ket{x_0} + \ket{x_0\oplus s}\big),

where x0x_0 is either member of that pair. You can’t read ss from this state directly. Measuring it would give x0x_0 or x0⊕sx_0\oplus s, 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 ss out of a single copy.

Check yourself

Take n=3n = 3, s=110s = 110 and the function in the figure of pairs above, so f(001)=010f(001) = 010. After the query you measure the output register and get 010010. Enter the state of the three input qubits, ∣000⟩\ket{000} to ∣111⟩\ket{111}.

Hints
  1. Which inputs have the same value as 001001?
  2. Those two branches keep equal amplitudes; renormalize so that the squares add up to 1.
Show the answer

Answer: 12∣001⟩+12∣111⟩\frac{1}{\sqrt{2}}|001\rangle + \frac{1}{\sqrt{2}}|111\rangle

The value f(001)f(001) belongs to the pair {001,111}\{001, 111\}, since 001⊕110=111001\oplus110 = 111. Those two branches survive with equal amplitudes, so the inputs are (∣001⟩+∣111⟩)/2(\ket{001} + \ket{111})/\sqrt2.

Do you have to measure the outputs? No. Nothing acts on them after UfU_f, 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 f(x)f(x), and that is the same for xx and x⊕sx\oplus s. 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 yy?

  1. AThe circuit fails: the inputs must collapse to one pair before the Hadamards
  2. BNothing changes: every yy has the same probability as before
  3. CThey become uniform over all 2n2^n strings: the inputs are entangled with the outputs, so nothing interferes
  4. DEvery run now gives ss, because all the pairs interfere together
Hint
  1. Does anything act on the output register after the query?
Show the answer

Answer: B. Nothing changes: every yy has the same probability as before

Nothing touches the outputs after UfU_f, 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 yy 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 f(x)f(x), so they interfere as before, and the last Hadamards still cancel every yy with y⋅s=1y\cdot s = 1.
  • D. Different pairs never interfere: their outputs hold different values. With or without the output measurement, a run gives a random yy with y⋅s=0y\cdot s = 0, not ss.

5 / 15 · Learn

Part A · The last Hadamards

Predict, then see

Take n=2n = 2 and s=10s = 10, with f(x)=x2f(x) = x_2, the second bit: f(00)=f(10)=0f(00) = f(10) = 0 and f(01)=f(11)=1f(01) = f(11) = 1. Since ff takes only two values, one output qubit is enough. The circuit applies HH to both inputs, then UfU_f (a CNOT from x2x_2 to the output), then HH to both inputs again. What will the two input qubits read?

  1. A1010 every time: the circuit reads out ss
  2. B0000 or 1010: the two inputs of one pair
  3. CAny of the four strings, a quarter of the time each
  4. D0000 or 0101, half the time each

Why do 1010 and 1111 never appear on the inputs? A sentence or two is enough.

Show what happens

What happens: D. 0000 or 0101, half the time each

After UfU_f the state is 12(∣00⟩+∣10⟩)∣0⟩+12(∣01⟩+∣11⟩)∣1⟩\tfrac12(\ket{00} + \ket{10})\ket0 + \tfrac12(\ket{01} + \ket{11})\ket1: each output value sits beside one pair, {00,10}\{00, 10\} or {01,11}\{01, 11\}. The last Hadamards act on each pair. In ∣00⟩+∣10⟩\ket{00} + \ket{10} the first qubit is ∣0⟩+∣1⟩\ket0 + \ket1, and HH turns it into ∣0⟩\ket0; the second qubit is ∣0⟩\ket0 and becomes ∣+⟩\ket+. The other pair gives ∣0⟩∣−⟩\ket0\ket-. Either way the first bit of yy is 0 and the second is random: yy is 0000 or 0101, exactly the strings with y⋅s=y1=0y\cdot s = y_1 = 0. The string s=10s = 10 never appears.

Why not the others

  • A. The widget never shows 1010 on the inputs. It shows 0000 and 0101, about half the time each. A run gives a string yy with y⋅s=0y\cdot s = 0, and 10⋅10=110\cdot10 = 1.
  • B. Those are the two members of the pair {00,10}\{00, 10\}, which is what you would read just before the last Hadamards. The Hadamards change that: the widget shows 0000 and 0101.
  • C. The Hadamards don’t just spread amplitude around. Two of the four strings get contributions that cancel exactly: in the widget, 1010 and 1111 never appear on the inputs.

Here is the general calculation. Lesson 9 gave H⊗n∣x⟩=12n∑y(−1)x⋅y∣y⟩H^{\otimes n}\ket{x} = \frac{1}{\sqrt{2^n}}\sum_y(-1)^{x\cdot y}\ket{y}. Apply it to both branches of a pair:

H⊗n 12(∣x⟩+∣x⊕s⟩)=12n+1∑y[(−1)x⋅y+(−1)(x⊕s)⋅y]∣y⟩.\begin{aligned}&H^{\otimes n}\,\tfrac{1}{\sqrt2}\big(\ket{x} + \ket{x\oplus s}\big) \\ &\quad= \tfrac{1}{\sqrt{2^{n+1}}}\sum_y\big[(-1)^{x\cdot y} + (-1)^{(x\oplus s)\cdot y}\big]\ket{y}.\end{aligned}

The dot product splits over XOR, (x⊕s)⋅y=x⋅y⊕s⋅y(x\oplus s)\cdot y = x\cdot y\oplus s\cdot y, so the bracket is

(−1)x⋅y(1+(−1)s⋅y).(-1)^{x\cdot y}\big(1 + (-1)^{s\cdot y}\big).

If s⋅y=1s\cdot y = 1, the two terms cancel and ∣y⟩\ket{y} gets amplitude 0. If s⋅y=0s\cdot y = 0, they add, giving amplitude ±2/2n+1=±1/2n−1\pm2/\sqrt{2^{n+1}} = \pm1/\sqrt{2^{n-1}}. So the measurement gives each of the 2n−12^{n-1} strings with s⋅y=0s\cdot y = 0 with probability 1/2n−11/2^{n-1}, whichever pair you had: xx only sets the signs.

The last Hadamards on (∣001⟩+∣111⟩)/2(\ket{001} + \ket{111})/\sqrt2, for s=110s = 110. Each ∣y⟩\ket{y} gets one sign from each branch. Where s⋅y=1s\cdot y = 1 the signs are opposite and cancel (shaded rows); where s⋅y=0s\cdot y = 0 they agree, and ∣y⟩\ket{y} gets amplitude ±2/16=±12\pm2/\sqrt{16} = \pm\tfrac12.

Check yourself

Simon’s circuit with n=3n = 3 and s=110s = 110. After the query the inputs are in (∣010⟩+∣100⟩)/2(\ket{010} + \ket{100})/\sqrt2. What is the amplitude of ∣111⟩\ket{111} after the last Hadamards?

Hints
  1. Each branch ∣x⟩\ket x gives ∣111⟩\ket{111} the term (−1)x⋅y/24(-1)^{x\cdot y}/\sqrt{2^{4}}.
  2. Compute 010⋅111010\cdot111 and 100⋅111100\cdot111; equal dot products add, different ones cancel.
Show the answer

Answer: −12-\frac{1}{2}

010⋅111=1010\cdot111 = 1 and 100⋅111=1100\cdot111 = 1, so the two terms are −1/24-1/\sqrt{2^{4}} and −1/24-1/\sqrt{2^{4}}. They add, since s⋅y=0s\cdot y = 0: the amplitude is −12-\frac{1}{2}.

Common slips

  • 12\frac{1}{2}: Right size, wrong sign. Both branches give ∣111⟩\ket{111} the sign −1-1, because 010⋅111=1010\cdot111 = 1 and 100⋅111=1100\cdot111 = 1.
  • 14\frac{1}{4}: That is the probability, the squared amplitude. The amplitude itself is −12-\frac{1}{2}.
  • −14-\frac{1}{4}: That counts only the branch ∣010⟩\ket{010}. Its partner ∣100⟩\ket{100} adds an equal term, since s⋅y=110⋅111=0s\cdot y = 110\cdot111 = 0: together −12-\frac{1}{2}.
  • 00: The two branches cancel only when s⋅y=1s\cdot y = 1. Here 110⋅111=0110\cdot111 = 0, so they add.

Check yourself

Simon’s circuit runs with n=3n = 3 and s=111s = 111. Which results yy can the input measurement give? Choose all that are possible.

Select all that apply.

  1. A101101
  2. B110110
  3. C011011
  4. D001001
  5. E111111
  6. F000000
Hint
  1. yy appears only if y⋅s=0y\cdot s = 0. What is y⋅111y\cdot111 in words?
Show the answer

Answers: A. 101101; B. 110110; C. 011011; F. 000000

y⋅111=y1⊕y2⊕y3y\cdot111 = y_1\oplus y_2\oplus y_3, so the possible results are the strings with an even number of 1s: 000000, 011011, 101101 and 110110, each with probability 14\tfrac14.

Why not the others

  • D. 001⋅111=1001\cdot111 = 1, so the two branches of every pair cancel on ∣001⟩\ket{001}.
  • E. That is ss itself. Here s⋅s=1⊕1⊕1=1s\cdot s = 1\oplus1\oplus1 = 1, so the two branches of every pair cancel on ∣111⟩\ket{111}: with an odd number of 1s, ss is never a result.

6 / 15 · Learn

Part A · Build one run

Now build a whole run yourself, for n=2n = 2 and s=11s = 11. A function with this promise is f(x)=x1⊕x2f(x) = x_1\oplus x_2: f(00)=f(11)=0f(00) = f(11) = 0 and f(01)=f(10)=1f(01) = f(10) = 1. 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 n=2n = 2 and s=11s = 11 on three wires (the inputs x1x_1, x2x_2 and the output), with the oracle for f(x)=x1⊕x2f(x) = x_1\oplus x_2. Leave out the measurement.

Build it in the circuit builder (gates: H, CNOT, control, X).

Hints
  1. Three stages: Hadamards on the inputs, the oracle, Hadamards on the inputs again.
  2. The oracle adds x1x_1 and then x2x_2 to the output: a CNOT from each input to the output.
  3. Column 1: HH on x1x_1 and x2x_2. Columns 2 and 3: a CNOT from x1x_1 to the output, then one from x2x_2. Column 4: HH 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 12(∣00⟩∣0⟩+∣11⟩∣0⟩+∣00⟩∣1⟩−∣11⟩∣1⟩)\tfrac12\big(\ket{00}\ket0 + \ket{11}\ket0 + \ket{00}\ket1 - \ket{11}\ket1\big), so the inputs read 0000 or 1111: the strings with y1⊕y2=0y_1\oplus y_2 = 0. That 1111 equals ss is a coincidence of this ss: 11⋅11=1⊕1=011\cdot11 = 1\oplus1 = 0. With s=10s = 10, earlier, ss never appeared.

7 / 15 · Learn

Part B · From equations to s

Part B turns runs into ss, measures the size of the gap, and writes the algorithm in Qiskit.

Each run gives a string yy, and with it one equation for the bits of ss:

y⋅s=y1s1⊕y2s2⊕⋯⊕ynsn=0.y\cdot s = y_1s_1\oplus y_2s_2\oplus\cdots\oplus y_ns_n = 0.

For n=3n = 3, y=111y = 111 says s1⊕s2⊕s3=0s_1\oplus s_2\oplus s_3 = 0: ss has an even number of 1s. That rules out four of the seven candidates and leaves 011011, 101101 and 110110. A second run with y=001y = 001 says s3=0s_3 = 0, which leaves only 110110.

Some runs add nothing. y=0…0y = 0\ldots0 gives 0=00 = 0, and a yy that is the XOR of earlier results gives an equation you could already get by adding theirs. What you need are n−1n - 1 independent equations. Each one halves the solutions, from 2n2^n down to exactly two: 0…00\ldots0 and ss. Since s≠0…0s \ne 0\ldots0, it is the other one.

The candidates for ss with n=3n = 3, as two runs come in. y=111y = 111 keeps the strings with an even number of 1s; y=001y = 001 keeps those that end in 0. Two independent equations, n−1=2n - 1 = 2, 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 n=3n = 3 gives y=011y = 011 and then y=110y = 110. Which string is ss?

  1. A101101
  2. BNot enough runs yet: three bits need three equations
  3. C000000
  4. D011011
  5. E111111
Hint
  1. Write the two equations y⋅s=0y\cdot s = 0 in terms of s1s_1, s2s_2, s3s_3.
Show the answer

Answer: E. 111111

y=011y = 011 gives s2⊕s3=0s_2\oplus s_3 = 0 and y=110y = 110 gives s1⊕s2=0s_1\oplus s_2 = 0, so s1=s2=s3s_1 = s_2 = s_3. The solutions are 000000 and 111111, and s≠000s \ne 000, so s=111s = 111.

Why not the others

  • A. 101=011⊕110101 = 011\oplus110 gives a third equation you can derive, s1⊕s3=0s_1\oplus s_3 = 0; it is not a solution. Test it: 011⋅101=1011\cdot101 = 1, so it breaks the first equation.
  • B. Two independent equations are enough for n=3n = 3. They leave two solutions, 000000 and ss, and ss is the nonzero one.
  • C. 000000 satisfies every equation, which is exactly why it doesn’t count: the promise says s≠000s \ne 000. The other solution is 111111.
  • D. 011011 is a result, not ss. Test it against both equations: 011⋅011=1⊕1=0011\cdot011 = 1\oplus1 = 0 is fine, but 110⋅011=1110\cdot011 = 1, so it breaks the second.

How many runs does that take? Suppose you already have kk independent results, with k<n−1k < n - 1. The strings you can make by XOR-ing some of them, 2k2^k in all (counting 0…00\ldots0), are exactly the ones that add nothing new. A fresh run gives one of the 2n−12^{n-1} allowed strings at random, so it brings a new independent equation with probability

1−2k2n−1 ≥ 12.1 - \frac{2^k}{2^{n-1}} \ \ge\ \frac12.

So each missing equation takes at most two runs on average, and the average total is under n+1n + 1. Solving the equations is ordinary elimination, about n3n^3 bit operations. In all: about nn queries.

Check yourself

Simon’s circuit with n=4n = 4. So far you have 2 independent results. What is the probability that the next run gives a new independent equation?

Hints
  1. How many strings can a run give at all, and how many of them add nothing new?
  2. The useless ones are the XORs of your results, including 00000000: 2k2^k of them.
Show the answer

Answer: 12\frac{1}{2}

A run gives one of the 23=82^{3} = 8 strings with y⋅s=0y\cdot s = 0, all equally likely. The 22=42^{2} = 4 XORs of your results (including 00000000) add nothing. So the chance is 1−4/8=121 - 4/8 = \frac{1}{2}.

Common slips

  • 34\frac{3}{4}: That treats only your 2 results as useless. 00000000 and every XOR of your results add nothing either: 22=42^{2} = 4 of the 8 allowed strings.
  • 58\frac{5}{8}: That counts your 2 results and 00000000 as useless, but every XOR of your results adds nothing too: 22=42^{2} = 4 useless strings in all.

8 / 15 · Worked example

Part B · Worked example: solving for s

Every string here has its first bit, y1y_1 or s1s_1, on the left.

Worked example

Four bits

Simon’s circuit with n=4n = 4 gives the results 11001100, 00100010, 11101110 and 01010101, in that order. Find ss.

  1. Equations

    One equation per result: 11001100 gives s1⊕s2=0s_1\oplus s_2 = 0; 00100010 gives s3=0s_3 = 0; 11101110 gives s1⊕s2⊕s3=0s_1\oplus s_2\oplus s_3 = 0; 01010101 gives s2⊕s4=0s_2\oplus s_4 = 0.

  2. Drop what is not new

    1110=1100⊕00101110 = 1100\oplus0010, and its equation is the sum of the first two: (s1⊕s2)⊕s3=0⊕0(s_1\oplus s_2)\oplus s_3 = 0\oplus0. It adds nothing. That leaves three independent equations, which is n−1n - 1 for n=4n = 4: enough.

  3. Eliminate

    s3=0s_3 = 0 directly. The other two tie the remaining bits together: s1=s2s_1 = s_2 and s4=s2s_4 = s_2. So everything follows from s2s_2: s2=0s_2 = 0 gives 00000000, and s2=1s_2 = 1 gives 11011101.

  4. Answer and check

    s≠0000s \ne 0000, so s=1101s = 1101. Check every result: 1100⋅1101=1⊕1=01100\cdot1101 = 1\oplus1 = 0, 0010⋅1101=00010\cdot1101 = 0, 1110⋅1101=1⊕1=01110\cdot1101 = 1\oplus1 = 0, 0101⋅1101=1⊕1=00101\cdot1101 = 1\oplus1 = 0.

  5. Your turn: enough?

    New runs, again with n=4n = 4, give 10011001, 01100110 and 11111111.

    Your turn

    Do the results 10011001, 01100110 and 11111111 pin ss down?

    1. AYes: three results is n−1n - 1 for n=4n = 4
    2. BNo: one of the three adds nothing new
    3. CNo: four bits need four equations
    4. DYes: none of them is 00000000, so each one is new
    Hint
    1. Is any of the three the XOR of the other two?
    Show the answer

    Answer: B. No: one of the three adds nothing new

    1001⊕0110=11111001\oplus0110 = 1111, so there are two independent equations, s1⊕s4=0s_1\oplus s_4 = 0 and s2⊕s3=0s_2\oplus s_3 = 0. They leave four solutions: 00000000 and the candidates 01100110, 10011001 and 11111111. One more independent run is needed.

    Why not the others

    • A. Three results are enough only if they are independent. Here 1001⊕0110=11111001\oplus0110 = 1111, so the third equation is the sum of the first two. Two equations leave 24/22=42^4/2^2 = 4 solutions: 00000000 and three candidates.
    • C. Three independent equations would be enough for four bits: they leave 00000000 and ss. The trouble here is that only two are independent, because 1001⊕0110=11111001\oplus0110 = 1111.
    • D. Being nonzero isn’t enough: a result can also be the XOR of earlier ones, and 1001⊕0110=11111001\oplus0110 = 1111. Only two of these equations are independent.
  6. Your turn: solve

    One more run gives 00110011.

    Your turn

    With 10011001, 01100110, 11111111 and now 00110011, which string is ss?

    1. A11111111
    2. B10011001
    3. C00110011
    4. D01100110
    Hint
    1. Write the new equation, then test the three candidates from the last step.
    Show the answer

    Answer: A. 11111111

    The equations now say s1=s4s_1 = s_4, s2=s3s_2 = s_3 and s3=s4s_3 = s_4: all four bits are equal. So s=1111s = 1111, and indeed 10011001, 01100110, 11111111 and 00110011 each share an even number of 1s with it.

    Why not the others

    • B. 10011001 was a candidate, but 0011⋅1001=10011\cdot1001 = 1: it breaks the new equation s3⊕s4=0s_3\oplus s_4 = 0.
    • C. 00110011 is the newest result. Use it as an equation, s3⊕s4=0s_3\oplus s_4 = 0, and test the three candidates left: only 11111111 passes.
    • D. 01100110 was a candidate, but 0011⋅0110=10011\cdot0110 = 1: it breaks the new equation s3⊕s4=0s_3\oplus s_4 = 0.

One more, fully worked, with fewer bits and a few wasted runs.

Another worked example

Another example: three bits

  1. Equations

    With n=3n = 3 the runs give 101101, 000000, 101101 and 011011. The result 000000 gives 0=00 = 0, and the second 101101 repeats the first. The useful equations are s1⊕s3=0s_1\oplus s_3 = 0 (from 101101) and s2⊕s3=0s_2\oplus s_3 = 0 (from 011011).

  2. Solve

    So s1=s3s_1 = s_3 and s2=s3s_2 = s_3: all three bits are equal. The solutions are 000000 and 111111, and s≠000s \ne 000, so s=111s = 111.

  3. Check

    101⋅111=1⊕1=0101\cdot111 = 1\oplus1 = 0 and 011⋅111=1⊕1=0011\cdot111 = 1\oplus1 = 0. Four runs, two of them useful: about what to expect, since each run is new with probability at least 12\tfrac12.

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 n=3n = 3, what is the probability that a run gives y=000y = 000, an equation that says nothing?

Hint
  1. How many results are possible for n=3n = 3, and are they equally likely?
Show the answer

Answer: 14\frac{1}{4}

A run gives one of the 2n−1=42^{n-1} = 4 strings with y⋅s=0y\cdot s = 0, each with probability 14\tfrac14, and 000000 is always one of them. So a quarter of the runs are wasted this way.

Common slips

  • 18\frac{1}{8}: That treats all eight strings as possible results. Only the four with y⋅s=0y\cdot s = 0 ever appear, each with probability 14\tfrac14.
  • 00: 000⋅s=0000\cdot s = 0 for every ss, so 000000 is always one of the allowed results. It just teaches you nothing.
  • 17\frac{1}{7}: There are seven nonzero strings, but that is the count of candidates for ss, not of results. A run gives one of the four strings with y⋅s=0y\cdot s = 0, each with probability 14\tfrac14.

10 / 15 · Learn

Part B · An exponential gap

Put the two costs side by side. The quantum algorithm uses about nn queries, plus about n3n^3 steps of classical algebra afterwards. Every classical algorithm, even a randomized one that may err with small probability, needs on the order of 2n/22^{n/2} queries. For n=20n = 20 that is about 21 runs against about 1,3001{,}300 queries; for n=40n = 40, 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 n−1n - 1 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?

  1. AIt needs only one query, like the circuit of Lesson 14
  2. BLike Deutsch–Jozsa, its gain is exponential only against classical methods that must never err
  3. CIt uses exponentially fewer queries than any classical algorithm, including randomized ones that may err with small probability
  4. DIt proves that quantum computers are exponentially faster at practical problems
Hint
  1. 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 nn queries. Classical: order 2n/22^{n/2}, 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 y⋅s=0y\cdot s = 0. You need n−1n - 1 independent ones, so about nn runs: still exponentially fewer than 2n/22^{n/2}.
  • B. That was Deutsch–Jozsa’s weakness: kk random queries erred with probability at most 21−k2^{1-k}, whatever nn was. For Simon’s problem, random queries need about 2n/22^{n/2} tries to find a collision, and no classical method of any kind does better than order 2n/22^{n/2}.
  • 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 ff. 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 ss, and there is a simple recipe. First copy xx into the outputs with nn CNOTs. Then pick a position jj where sj=1s_j = 1 and XOR the bit xjx_j into every output where ss has a 1. The outputs now hold f(x)=x⊕xj sf(x) = x\oplus x_j\,s: that is xx, with ss XOR-ed in when xj=1x_j = 1. The partner x⊕sx\oplus s has the other value of bit jj, so

f(x⊕s)=x⊕s⊕(xj⊕1) s=x⊕xj s,f(x\oplus s) = x\oplus s\oplus(x_j\oplus1)\,s = x\oplus x_j\,s,

the same value, as the promise requires. Python counts from 0, so s[0] is the first bit, s1s_1, and it sits on qubit 0.

Reading the counts takes care. Qiskit writes classical bit 0 on the right, so the key '100' means y3y2y1=100y_3y_2y_1 = 100, which is y=001y = 001 in the lessons’ order. For s=110s = 110 the four keys 000, 100, 011 and 111 are, reversed, 000000, 001001, 110110 and 111111: exactly the strings with y1⊕y2=0y_1\oplus y_2 = 0. Reverse each key before you write its equation.

Check yourself

A colleague runs Simon’s algorithm in Qiskit for n=3n = 3, 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 ss, written as s1s2s3s_1s_2s_3?

  1. AYou can’t tell: the counts never show ss
  2. B010010
  3. C001001
  4. D100100
Hints
  1. Qiskit writes classical bit 0, here y1y_1, on the right. Reverse each key first.
  2. Then write y⋅s=0y\cdot s = 0 for two nonzero results and solve.
Show the answer

Answer: D. 100100

Reversed into the lessons’ order, the results are 000000, 010010, 001001, 011011. Two nonzero ones, 010010 and 001001, give s2=0s_2 = 0 and s3=0s_3 = 0. The nonzero solution is s=100s = 100. Check: 010⋅100=0010\cdot100 = 0, 001⋅100=0001\cdot100 = 0, 011⋅100=0011\cdot100 = 0.

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 y⋅s=0y\cdot s = 0 leave exactly one nonzero solution.
  • B. 010 is one of the keys: a result yy, written in Qiskit’s order. A result only tells you that y⋅s=0y\cdot s = 0; it isn’t ss. Reverse the keys, write one equation per result, and solve.
  • C. That comes from reading the keys as if classical bit 0, y1y_1, were on the left. Qiskit puts bit 0 on the right, so the key 100 means y=001y = 001. Reverse every key first: the results are 000000, 010010, 001001, 011011, and they give s=100s = 100.

Check yourself

Complete the program so that it runs Simon’s algorithm for n=3n = 3 with s=011s = 011: add CNOTs that put into the outputs a value f(x)f(x) with f(x)=f(x⊕011)f(x) = f(x\oplus011), 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
  1. Start by copying each input into its output: qc.cx(i, n + i) for i in range(n).
  2. Then pick a position j with s[j] == '1' and add qc.cx(j, n + i) for every i where s[i] == '1'.
  3. With s=011s = 011, j=1j = 1 works: after the three copies, add qc.cx(1, 4) and qc.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 f(x)=x⊕x2 011f(x) = x\oplus x_2\,011, and f(x⊕011)=f(x)f(x\oplus011) = f(x). The measured inputs are then the strings with y2⊕y3=0y_2\oplus y_3 = 0: 000000, 011011, 100100 and 111111, 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 n=3n = 3 and s=011s = 011. After the query the inputs are in (∣000⟩+∣011⟩)/2(\ket{000} + \ket{011})/\sqrt2. What is the amplitude of ∣100⟩\ket{100} after the last Hadamards?

Hints
  1. Each branch ∣x⟩\ket x gives ∣100⟩\ket{100} the term (−1)x⋅y/24(-1)^{x\cdot y}/\sqrt{2^{4}}.
  2. Compute 000⋅100000\cdot100 and 011⋅100011\cdot100; equal dot products add, different ones cancel.
Show the answer

Answer: 12\frac{1}{2}

000⋅100=0000\cdot100 = 0 and 011⋅100=0011\cdot100 = 0, so the two terms are +1/24+1/\sqrt{2^{4}} and +1/24+1/\sqrt{2^{4}}. They add, since s⋅y=0s\cdot y = 0: the amplitude is 12\frac{1}{2}.

Common slips

  • −12-\frac{1}{2}: Right size, wrong sign. Both branches give ∣100⟩\ket{100} the sign +1+1, because 000⋅100=0000\cdot100 = 0 and 011⋅100=0011\cdot100 = 0.
  • 14\frac{1}{4}: That is the probability, the squared amplitude. The amplitude itself is 12\frac{1}{2}.
  • 00: The two branches cancel only when s⋅y=1s\cdot y = 1. Here 011⋅100=0011\cdot100 = 0, so they add.

Check yourself

Simon’s circuit with n=4n = 4 gives the results 11011101, 10011001 and 01110111, which are independent. Which string is ss?

  1. A00100010
  2. B11011101
  3. C10111011
  4. D01000100
  5. E00000000
Hints
  1. Write one equation y⋅s=0y\cdot s = 0 per result.
  2. 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. 10111011

The equations are s1⊕s2⊕s4=0s_1 \oplus s_2 \oplus s_4 = 0, s1⊕s4=0s_1 \oplus s_4 = 0, s2⊕s3⊕s4=0s_2 \oplus s_3 \oplus s_4 = 0. Exactly two strings satisfy all of them, 00000000 and 10111011, and s≠0000s \ne 0000, so s=1011s = 1011.

One equation per result: s1⊕s2⊕s4=0s_1 \oplus s_2 \oplus s_4 = 0, s1⊕s4=0s_1 \oplus s_4 = 0, s2⊕s3⊕s4=0s_2 \oplus s_3 \oplus s_4 = 0. With n−1=3n - 1 = 3 independent equations there are exactly two solutions. Check 10111011 against each result: 1101⋅1011=01101\cdot1011 = 0, 1001⋅1011=01001\cdot1011 = 0, 0111⋅1011=00111\cdot1011 = 0. The only other solution is 00000000, which the promise rules out.

Why not the others

  • A. 00100010 passes the first equations but not the last: 0111⋅0010=10111\cdot0010 = 1.
  • B. 11011101 is a result, not ss. Use it as an equation, and test it against the others: 1101⋅1101=11101\cdot1101 = 1.
  • D. 0100=1101⊕10010100 = 1101\oplus1001 gives an equation you can derive from the first two, not a solution. Test it: 1101⋅0100=11101\cdot0100 = 1, 0111⋅0100=10111\cdot0100 = 1.
  • E. 00000000 satisfies every equation, which is exactly why it doesn’t count: the promise says s≠0000s \ne 0000. The other solution is 10111011.

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
  1. Each new input can be compared with every input before it.
  2. The 25th input adds 24 pairs; add them all up, or use k(k−1)/2k(k-1)/2.
Show the answer

Answer: 300300

k(k−1)/2=25⋅24/2=300k(k-1)/2 = 25\cdot24/2 = 300 pairs: the second input makes 1 pair, the third 2 more, and the 25th 24 more.

Common slips

  • 625625: That is 25225^2, which counts every pair twice and also pairs each input with itself. The count is 25⋅24/2=30025\cdot24/2 = 300.
  • 600600: That counts each pair twice, once in each order. Divide by 2: 300300.
  • 2525: That is the number of queries. The tests are pairs: each new input is compared with every earlier one, 0+1+⋯+24=3000 + 1 + \cdots + 24 = 300.

13 / 15 · Explain

Explain it

Explain it

Explain why a run of Simon’s circuit never gives a string yy with y⋅s=1y\cdot s = 1, and why you still need about nn runs to find ss.

Show a model answer

After the query, the inputs are (for whichever value the outputs hold) in an equal superposition of a pair, ∣x⟩\ket{x} and ∣x⊕s⟩\ket{x\oplus s}. The last Hadamards give each ∣y⟩\ket{y} one contribution from each branch, with signs (−1)x⋅y(-1)^{x\cdot y} and (−1)(x⊕s)⋅y=(−1)x⋅y(−1)s⋅y(-1)^{(x\oplus s)\cdot y} = (-1)^{x\cdot y}(-1)^{s\cdot y}. When s⋅y=1s\cdot y = 1 these are opposite and cancel, so such a yy never appears; when s⋅y=0s\cdot y = 0 they add. So a run gives a uniformly random yy with y⋅s=0y\cdot s = 0: one linear equation for the bits of ss, not ss itself. It takes n−1n - 1 independent equations to leave only 0…00\ldots0 and ss, and each run gives a new one with probability at least one half, so about nn runs.

A good answer covers

  • After the query the inputs hold a pair, ∣x⟩+∣x⊕s⟩\ket{x} + \ket{x\oplus s} (for each value in the outputs)
  • The last Hadamards give ∣y⟩\ket{y} two contributions whose signs differ by (−1)s⋅y(-1)^{s\cdot y}, so they cancel when s⋅y=1s\cdot y = 1
  • A run gives one random equation y⋅s=0y\cdot s = 0, not ss
  • About nn runs: n−1n - 1 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 n=3n = 3 hides a string, say s=101s = 101. The circuit is run once and its 3 input qubits are measured. What does that one result give you?

  1. Ass with probability 12\tfrac12, otherwise a random string
  2. BA random string yy with y⋅s=0y\cdot s = 0: one equation that ss must satisfy
  3. CThe value f(x)f(x) at one random input xx
  4. DThe hidden string ss itself
Show the answer

Answer: B. A random string yy with y⋅s=0y\cdot s = 0: one equation that ss must satisfy

After the query and the last Hadamards, the inputs hold a string yy chosen uniformly from the 2n−1=42^{n-1} = 4 strings with y⋅s=0y\cdot s = 0. One such yy is one equation for the bits of ss; about nn runs give enough equations to solve for ss.

Why not the others

  • A. No allowed result is more likely than another: each of the 44 strings with y⋅s=0y\cdot s = 0 has probability 1/41/4. Even when the result happens to equal ss, 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 yy with y⋅s=0y\cdot s = 0, not an input of ff.
  • D. One run gives a random string yy with y⋅s=0y\cdot s = 0: 000000, 010010, 101101 or 111111, each with probability 14\tfrac14. The result can be 000000, which says nothing at all. It takes several runs, and some algebra, to pin ss down.

Check yourself

You query ff for n=5n = 5 and find f(10011)=f(01101)f(10011) = f(01101). What is ss?

  1. A1111011110
  2. BYou can’t tell yet: more queries are needed
  3. C0000100001
  4. D1001110011
Hints
  1. When can two different inputs share a value?
  2. Only when they differ by ss, so XOR them.
Show the answer

Answer: A. 1111011110

Two different inputs with the same value must be partners, x′=x⊕sx' = x\oplus s, so s=x⊕x′=10011⊕01101=11110s = x\oplus x' = 10011\oplus01101 = 11110.

Why not the others

  • B. One collision is enough. Two different inputs with the same value must be partners, x′=x⊕sx' = x\oplus s, so s=10011⊕01101s = 10011\oplus01101.
  • C. That is the bitwise AND of the two inputs. Partners differ by ss, so ss is their XOR: 10011⊕01101=1111010011\oplus01101 = 11110.
  • D. That is one of the inputs. The promise says two different inputs share a value only when they differ by ss, so ss is the XOR of the two: 1111011110.

Check yourself

Simon’s circuit with n=2n = 2 and s=01s = 01. After the query the inputs are in (∣11⟩+∣10⟩)/2(\ket{11} + \ket{10})/\sqrt2. What is the amplitude of ∣10⟩\ket{10} after the last Hadamards?

Hints
  1. Each branch ∣x⟩\ket x gives ∣10⟩\ket{10} the term (−1)x⋅y/23(-1)^{x\cdot y}/\sqrt{2^{3}}.
  2. Compute 11⋅1011\cdot10 and 10⋅1010\cdot10; equal dot products add, different ones cancel.
Show the answer

Answer: −12-\frac{1}{\sqrt{2}}

11⋅10=111\cdot10 = 1 and 10⋅10=110\cdot10 = 1, so the two terms are −1/23-1/\sqrt{2^{3}} and −1/23-1/\sqrt{2^{3}}. They add, since s⋅y=0s\cdot y = 0: the amplitude is −12-\frac{1}{\sqrt{2}}.

Common slips

  • 12\frac{1}{\sqrt{2}}: Right size, wrong sign. Both branches give ∣10⟩\ket{10} the sign −1-1, because 11⋅10=111\cdot10 = 1 and 10⋅10=110\cdot10 = 1.
  • 12\frac{1}{2}: That is the probability, the squared amplitude. The amplitude itself is −12-\frac{1}{\sqrt{2}}.
  • −24-\frac{\sqrt{2}}{4}: That counts only the branch ∣11⟩\ket{11}. Its partner ∣10⟩\ket{10} adds an equal term, since s⋅y=01⋅10=0s\cdot y = 01\cdot10 = 0: together −12-\frac{1}{\sqrt{2}}.
  • 00: The two branches cancel only when s⋅y=1s\cdot y = 1. Here 01⋅10=001\cdot10 = 0, so they add.

Check yourself

A classical program attacks Simon’s problem for n=24n = 24 by querying random inputs until two of them give the same value. Roughly how many queries does that take?

  1. AAbout 24, one per bit
  2. BAbout 212=4,0962^{12} = 4{,}096
  3. CAbout 223≈8.4×1062^{23} \approx 8.4\times10^{6}
  4. DAbout 224≈1.7×1072^{24} \approx 1.7\times10^{7}
Show the answer

Answer: B. About 212=4,0962^{12} = 4{,}096

A good chance of a collision needs about as many pairs as there are candidates: k2/2≈224k^2/2 \approx 2^{24}, so kk is of order 2122^{12}. On average random queries take about 1.25⋅2121.25\cdot2^{12}, roughly 5,1005{,}100.

Why not the others

  • A. A query returns a value f(x)f(x), not a bit of ss. 24 random queries make only 276 pairs, and each pair is a matching pair with chance 1/(224−1)1/(2^{24} - 1).
  • C. 223+12^{23} + 1 queries make a collision certain, since there are only 2232^{23} different values. Random queries collide much sooner, because kk queries make about k2/2k^2/2 pairs: about 2122^{12} queries already make about 2232^{23} pairs.
  • D. You never need every input: 223+12^{23} + 1 queries already force a collision. Random queries usually find one after about 2122^{12}, since the number of pairs grows like k2/2k^2/2.

Check yourself

Simon’s circuit with n=4n = 4 gives the results 11011101, 01110111 and 00100010, which are independent. Which string is ss?

  1. A01010101
  2. B10101010
  3. C10111011
  4. D11011101
  5. E00000000
Hints
  1. Write one equation y⋅s=0y\cdot s = 0 per result.
  2. 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. 01010101

The equations are s1⊕s2⊕s4=0s_1 \oplus s_2 \oplus s_4 = 0, s2⊕s3⊕s4=0s_2 \oplus s_3 \oplus s_4 = 0, s3=0s_3 = 0. Exactly two strings satisfy all of them, 00000000 and 01010101, and s≠0000s \ne 0000, so s=0101s = 0101.

One equation per result: s1⊕s2⊕s4=0s_1 \oplus s_2 \oplus s_4 = 0, s2⊕s3⊕s4=0s_2 \oplus s_3 \oplus s_4 = 0, s3=0s_3 = 0. With n−1=3n - 1 = 3 independent equations there are exactly two solutions. Check 01010101 against each result: 1101⋅0101=01101\cdot0101 = 0, 0111⋅0101=00111\cdot0101 = 0, 0010⋅0101=00010\cdot0101 = 0. The only other solution is 00000000, which the promise rules out.

Why not the others

  • B. 1010=1101⊕01111010 = 1101\oplus0111 gives an equation you can derive from the first two, not a solution. Test it: 1101⋅1010=11101\cdot1010 = 1, 0111⋅1010=10111\cdot1010 = 1, 0010⋅1010=10010\cdot1010 = 1.
  • C. 10111011 passes the first equations but not the last: 0010⋅1011=10010\cdot1011 = 1.
  • D. 11011101 is a result, not ss. Use it as an equation, and test it against the others: 1101⋅1101=11101\cdot1101 = 1.
  • E. 00000000 satisfies every equation, which is exactly why it doesn’t count: the promise says s≠0000s \ne 0000. The other solution is 01010101.

Check yourself

A colleague runs Simon’s algorithm in Qiskit for n=3n = 3, 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 ss, written as s1s2s3s_1s_2s_3?

  1. AYou can’t tell: the counts never show ss
  2. B011011
  3. C001001
  4. D110110
Hints
  1. Qiskit writes classical bit 0, here y1y_1, on the right. Reverse each key first.
  2. Then write y⋅s=0y\cdot s = 0 for two nonzero results and solve.
Show the answer

Answer: B. 011011

Reversed into the lessons’ order, the results are 000000, 100100, 011011, 111111. Two nonzero ones, 100100 and 011011, give s1=0s_1 = 0 and s2⊕s3=0s_2 \oplus s_3 = 0. The nonzero solution is s=011s = 011. Check: 100⋅011=0100\cdot011 = 0, 011⋅011=0011\cdot011 = 0, 111⋅011=0111\cdot011 = 0.

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 y⋅s=0y\cdot s = 0 leave exactly one nonzero solution.
  • C. 001 is one of the keys: a result yy, written in Qiskit’s order. A result only tells you that y⋅s=0y\cdot s = 0; it isn’t ss. Reverse the keys, write one equation per result, and solve.
  • D. That comes from reading the keys as if classical bit 0, y1y_1, were on the left. Qiskit puts bit 0 on the right, so the key 001 means y=100y = 100. Reverse every key first: the results are 000000, 100100, 011011, 111111, and they give s=011s = 011.

Check yourself

Simon’s problem with n=24n = 24. 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?

  1. AAbout 2525 runs, and about 5,1005{,}100 queries
  2. B11 run, and about 2424 queries
  3. CAbout 2525 runs, and 223+12^{23} + 1 queries
  4. DAbout 2424 of each
Hint
  1. 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 2525 runs, and about 5,1005{,}100 queries

Quantum: n−1=23n - 1 = 23 independent equations, each run new with probability at least 12\tfrac12, about 2525 runs on average. Classical: a collision takes about 1.25⋅212≈5,1001.25\cdot2^{12} \approx 5{,}100 random queries.

Why not the others

  • B. That is the Bernstein–Vazirani pattern. Here one run gives one equation y⋅s=0y\cdot s = 0; you need n−1=23n - 1 = 23 independent ones, so about 2525 runs. A classical search needs a collision, which takes about 1.25⋅2121.25\cdot2^{12} queries.
  • C. 223+12^{23} + 1 queries make a collision certain, but random ones find one much sooner: kk queries make about k2/2k^2/2 pairs, so about 1.25⋅212≈5,1001.25\cdot2^{12} \approx 5{,}100 on average.
  • D. A classical query gives one value f(x)f(x). Without a collision, each pair of queries rules out a single candidate, and there are 224−12^{24} - 1 candidates. That takes about 2122^{12} queries, not 2424.

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.

  1. State Simon’s problem, and say what a classical algorithm needs to solve it.

    Show the key points
    • ff maps nn bits to nn bits, and f(x)=f(x′)f(x) = f(x') exactly when x′=xx' = x or x′=x⊕sx' = x\oplus s, with s≠0…0s \ne 0\ldots0
    • A collision f(x)=f(x′)f(x) = f(x') with x≠x′x \ne x' gives s=x⊕x′s = x\oplus x'
    • Random queries need about 2n/22^{n/2} tries, since kk queries make k(k−1)/2k(k-1)/2 pairs; no classical algorithm beats order 2n/22^{n/2}
  2. Describe one run of Simon’s circuit and what it returns.

    Show the key points
    • H⊗nH^{\otimes n} on the inputs, UfU_f, H⊗nH^{\otimes n} on the inputs, then measure the inputs
    • After the query the inputs are in a pair, (∣x⟩+∣x⊕s⟩)/2(\ket{x} + \ket{x\oplus s})/\sqrt2; measuring the outputs is optional
    • ∣y⟩\ket{y} gets amplitude proportional to (−1)x⋅y(1+(−1)s⋅y)(-1)^{x\cdot y}(1 + (-1)^{s\cdot y}): a uniformly random yy with y⋅s=0y\cdot s = 0
  3. How do you get from the results to ss, and how many queries does that take?

    Show the key points
    • Each yy gives an equation y⋅s=0y\cdot s = 0; collect n−1n - 1 independent ones and solve by elimination
    • The solutions are 0…00\ldots0 and ss; take the nonzero one
    • Each run is new with probability at least 12\tfrac12, so about nn queries, against order 2n/22^{n/2} classically

Key ideas

  1. Simon’s promise: f(x)=f(x′)f(x) = f(x') exactly when x′∈{x,x⊕s}x' \in \{x, x\oplus s\}. Any collision gives s=x⊕x′s = x\oplus x'.

  2. Classically, kk queries test k(k−1)/2k(k-1)/2 pairs, so a collision takes about 2n/22^{n/2} queries, and no classical algorithm, even a randomized one, does better than order 2n/22^{n/2}.

  3. One run: H⊗nH^{\otimes n}, UfU_f, H⊗nH^{\otimes n}. The pair (∣x⟩+∣x⊕s⟩)/2(\ket{x} + \ket{x\oplus s})/\sqrt2 turns into a sum where ∣y⟩\ket{y} has amplitude proportional to (−1)x⋅y(1+(−1)s⋅y)(-1)^{x\cdot y}(1 + (-1)^{s\cdot y}), so yy is uniform over the strings with y⋅s=0y\cdot s = 0.

  4. One run gives one equation, never ss itself. n−1n - 1 independent equations, solved by elimination over bits, leave 0…00\ldots0 and ss; about nn runs are enough.

  5. About nn quantum queries against order 2n/22^{n/2} classical ones: an exponential separation in the query model, and the idea behind Shor’s period finding.

  6. 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.

Math refreshers

The toolkit topics this lesson leans on.

Start the interactive lesson