|q⟩ Bad Qubits

intermediate · Programming · The Hidden Subgroup Problem (Intro)

Simon’s Algorithm

Simon's algorithm solves a structured search problem exponentially faster on a quantum computer than on any classical one. It was the first clear evidence of an exponential quantum speedup (Simon, 1994) and directly inspired Shor's factoring algorithm.

The problem

You are given a black-box function f:{0,1}n→{0,1}nf : \{0,1\}^n \to \{0,1\}^n that is either one-to-one or two-to-one. In the two-to-one case there is a hidden nonzero string s∈{0,1}ns \in \{0,1\}^n such that

f(x)=f(y)  ⟺  y=x⊕s,f(x) = f(y) \iff y = x \oplus s,

where ⊕\oplus denotes bitwise XOR. Your task is to find ss.

Classically you need Ω(2n/2)\Omega(2^{n/2}) queries on average. Simon's quantum algorithm uses only O(n)O(n) queries.

The circuit

The circuit uses 2n2n qubits: nn input qubits (register A) and nn ancilla qubits (register B).

  1. Uniform superposition. Apply H⊗nH^{\otimes n} to register A.
  2. Query the oracle. Apply UfU_f so that ∣x⟩∣0⟩↦∣x⟩∣f(x)⟩|x\rangle|0\rangle \mapsto |x\rangle|f(x)\rangle.
  3. Hadamard again. Apply H⊗nH^{\otimes n} to register A.
  4. Measure register A.

After step 1 the state is

12n/2∑x∈{0,1}n∣x⟩∣0⟩.\frac{1}{2^{n/2}} \sum_{x \in \{0,1\}^n} |x\rangle|0\rangle.

After step 2:

12n/2∑x∣x⟩∣f(x)⟩.\frac{1}{2^{n/2}} \sum_{x} |x\rangle|f(x)\rangle.

Because ff is two-to-one with period ss, each output value vv appears for exactly the pair x0x_0 and x0⊕sx_0 \oplus s. After the second H⊗nH^{\otimes n} on register A, interference ensures that every measurement outcome yy satisfies

y⋅s=0(mod2).y \cdot s = 0 \pmod{2}.

Running the circuit O(n)O(n) times yields n−1n-1 independent linear equations over F2\mathbb{F}_2; Gaussian elimination then recovers ss.

A concrete 2-qubit example

Take n=2n = 2 and s=112s = 11_2. We need f(x)=f(x⊕11)f(x) = f(x \oplus 11) for all xx:

| xx | x⊕11x \oplus 11 | f(x)f(x) | |-----|---------------|--------| | 00 | 11 | 0 | | 01 | 10 | 1 | | 10 | 01 | 1 | | 11 | 00 | 0 |

A simple oracle realizing this is f(x1x0)=x1⊕x0f(x_1 x_0) = x_1 \oplus x_0. In circuit language: CNOT(0, 2) copies qubit 0 into qubit 2, then CNOT(1, 2) XORs qubit 1 into qubit 2, leaving x1⊕x0x_1 \oplus x_0 in qubit 2.

After the full circuit the allowed measurement outcomes for qubits 0 and 1 must satisfy y1⊕y0=0(mod2)y_1 \oplus y_0 = 0 \pmod{2}, i.e. only y=00y = 00 and y=11y = 11 can appear. Working through the algebra: the four-qubit state just before measurement is

12(∣0000⟩+∣0010⟩+∣1100⟩−∣1110⟩),\frac{1}{2}\bigl(|0000\rangle + |0010\rangle + |1100\rangle - |1110\rangle\bigr),

so each of these four basis states has probability 14\tfrac{1}{4}.

Try it

Complete the oracle in the starter code. The grader checks the full probability distribution over all 4 qubits; the correct circuit places equal probability on ∣0000⟩|0000\rangle, ∣0010⟩|0010\rangle, ∣1100⟩|1100\rangle, and ∣1110⟩|1110\rangle (and zero everywhere else).

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.