|q⟩ Bad Qubits

intermediate · Programming · The Hidden Subgroup Problem (Intro)

Solving the Linear System

Simon's algorithm uses quantum parallelism to collect n1n - 1 linearly independent bitstring samples, each orthogonal (in F2\mathbb{F}_2) to a hidden string s{0,1}n{0}s \in \{0,1\}^n \setminus \{0\}. The quantum part is finished once those samples are in hand; the final step is purely classical: solve a small linear system over the two-element field F2\mathbb{F}_2.

The constraint each sample imposes

After one query, Simon's circuit produces a uniformly random y{0,1}ny \in \{0,1\}^n with the guarantee

ys=i=0n1yisi=0,y \cdot s = \bigoplus_{i=0}^{n-1} y_i s_i = 0,

where \oplus is addition mod 2 and the bit indices run from 0 (least significant) to n1n-1. Equivalently, yy is orthogonal to ss over F2\mathbb{F}_2. Repeat the quantum circuit n1n - 1 times (re-running until the samples are linearly independent) and you have a full set of equations.

Gaussian elimination over F2\mathbb{F}_2

The system Ys=0Y s = 0 with YF2(n1)×nY \in \mathbb{F}_2^{(n-1) \times n} has a one-dimensional solution space when the oracle is balanced (i.e. s0s \ne 0). Standard Gaussian elimination works unchanged except all arithmetic is mod 2: no fractions, just XOR.

For nn qubits the algorithm runs in O(n2)O(n^2) time on a classical computer, so this post-processing adds no asymptotic cost to the overall exponential speedup over classical period finding.

A concrete 3-qubit example

Suppose n=3n = 3 and the hidden string is s=101s = 101 (decimal 5). The orthogonality condition becomes

y01y10y21=y0y2=0,y_0 \cdot 1 \oplus y_1 \cdot 0 \oplus y_2 \cdot 1 = y_0 \oplus y_2 = 0,

so a measurement yy is valid only when bits 0 and 2 agree. After two independent quantum measurements we obtain, for instance:

| Sample (binary) | Decimal | Check: y101y \cdot 101 | |-----------------|---------|-------------------------------| | 010010 | 2 | 00=00 \oplus 0 = 0 ✓ | | 111111 | 7 | 11=01 \oplus 1 = 0 ✓ |

Substituting y(1)=010y^{(1)} = 010 into the constraint gives s1=0s_1 = 0. Substituting y(2)=111y^{(2)} = 111 gives s0s2=0s_0 \oplus s_2 = 0, i.e. s0=s2s_0 = s_2. With s0s \ne 0 the only solution is s0=s2=1s_0 = s_2 = 1, confirming s=101=5s = 101 = 5.

Try it

The starter code provides the two samples as integer bitvectors. Iterate over the 2n12^n - 1 non-zero candidates and return the unique ss that is orthogonal to every sample.

Run your code to see the quantum state.

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