Path: blob/main/notebooks/ch-algorithms/simon.ipynb
3855 views
Simon's Algorithm
In this section, we first introduce the Simon problem, and classical and quantum algorithms to solve it. We then implement the quantum algorithm using Qiskit, and run on a simulator and device.
1. Introduction
Simon's algorithm, first introduced in Reference [1], was the first quantum algorithm to show an exponential speed-up versus the best classical algorithm in solving a specific problem. This inspired the quantum algorithms based on the quantum Fourier transform, which is used in the most famous quantum algorithm: Shor's factoring algorithm.
1a. Simon's Problem
We are given an unknown blackbox function , which is guaranteed to be either one-to-one () or two-to-one (), where one-to-one and two-to-one functions have the following properties:
one-to-one: maps exactly one unique output for every input. An example with a function that takes 4 inputs is:
two-to-one: maps exactly two inputs to every unique output. An example with a function that takes 4 inputs is:
This two-to-one mapping is according to a hidden bitstring, , where:
Given this blackbox , how quickly can we determine if is one-to-one or two-to-one? Then, if turns out to be two-to-one, how quickly can we determine ? As it turns out, both cases boil down to the same problem of finding , where a bitstring of represents the one-to-one .
1b. Simon's Algorithm
Classical Solution
Classically, if we want to know what is with 100% certainty for a given , we have to check up to inputs, where n is the number of bits in the input. This means checking just over half of all the possible inputs until we find two cases of the same output. Much like the Deutsch-Jozsa problem, if we get lucky, we could solve the problem with our first two tries. But if we happen to get an that is one-to-one, or get really unlucky with an that’s two-to-one, then we’re stuck with the full . There are known algorithms that have a lower bound of (see Reference 2 below), but generally speaking the complexity grows exponentially with n.
Quantum Solution
The quantum circuit that implements Simon's algorithm is shown below.

Where the query function, acts on two quantum registers as:
In the specific case that the second register is in the state we have:
The algorithm involves the following steps.
Two -qubit input registers are initialized to the zero state:
Apply a Hadamard transform to the first register:
Apply the query function :
Measure the second register. A certain value of will be observed. Because of the setting of the problem, the observed value could correspond to two possible inputs: and . Therefore the first register becomes:
where we omitted the second register since it has been measured.
Apply Hadamard on the first register:
Measuring the first register will give an output only if:
which means:
A string will be measured, whose inner product with . Thus, repeating the algorithm times, we will be able to obtain different values of and the following system of equation can be written:
From which can be determined, for example by Gaussian elimination.
So, in this particular problem the quantum algorithm performs exponentially fewer steps than the classical one. Once again, it might be difficult to envision an application of this algorithm (although it inspired the most famous algorithm created by Shor) but it represents the first proof that there can be an exponential speed-up in solving a specific problem by using a quantum computer rather than a classical one.
2. Example
Let's see the example of Simon's algorithm for 2 qubits with the secret string , so that if . The quantum circuit to solve the problem is:

Two -qubit input registers are initialized to the zero state:
Apply Hadamard gates to the qubits in the first register:
For the string , the query function can be implemented as (as seen in the circuit diagram above):
Thus:
We measure the second register. With probability we will see either or . For the sake of the example, let us assume that we see . The state of the system is then where we omitted the second register since it has been measured.
Apply Hadamard on the first register
Measuring the first register will give either or with equal probability.
If we see , then:
which tells us that or , and the two remaining potential solutions are or . Note that will always be a trivial solution to our simultaneous equations. If we repeat steps 1-6 many times, we would only measure or as
are the only equations that satisfy . We can verify by picking a random input () and checking . For example:
The function simon_oracle (imported above) creates a Simon oracle for the bitstring b. This is given without explanation, but we will discuss the method in section 4.
In Qiskit, measurements are only allowed at the end of the quantum circuit. In the case of Simon's algorithm, we actually do not care about the output of the second register, and will only measure the first register.
Since we know already, we can verify these results do satisfy :
Using these results, we can recover the value of by solving this set of simultaneous equations. For example, say we first measured 001, this tells us:
If we next measured 111, we have:
Which tells us either:
or
Of which is the non-trivial solution to our simultaneous equations. We can solve these problems in general using Gaussian elimination, which has a run time of .
3b. Experiment with Real Devices
The circuit in section 3a uses qubits, while at the time of writing many IBM Quantum devices only have 5 qubits. We will run the same code, but instead using as in the example in section 2, requiring only 4 qubits.
This circuit is slightly different to the circuit shown in section 2. The outputs are different, but the input collisions are the same, i.e. both have the property that .
As we can see, the most significant results are those for which (mod 2). The other results are erroneous, but have a lower probability of occurring. Assuming we are unlikely to measure the erroneous results, we can then use a classical computer to recover the value of by solving the linear system of equations. For this case, .
4. Oracle
The above example and implementation of Simon's algorithm are specifically for specific values of . To extend the problem to other secret bit strings, we need to discuss the Simon query function or oracle in more detail.
The Simon algorithm deals with finding a hidden bitstring from an oracle that satisfies if and only if for all . Here, the is the bitwise XOR operation. Thus, if , i.e., the all-zero bitstring, then is a 1-to-1 (or, permutation) function. Otherwise, if , then is a 2-to-1 function.
In the algorithm, the oracle receives as input. With regards to a predetermined , the oracle writes its output to the second register so that it transforms the input to such that for all .
Such a blackbox function can be realized by the following procedures.
Copy the content of the first register to the second register.
(Creating 1-to-1 or 2-to-1 mapping) If is not all-zero, then there is the least index so that . If , then XOR the second register with . Otherwise, do not change the second register.
(Creating random permutation) Randomly permute and flip the qubits of the second register.
6. References
Daniel R. Simon (1997) "On the Power of Quantum Computation" SIAM Journal on Computing, 26(5), 1474–1483, doi:10.1137/S0097539796298637
Guangya Cai and Daowen Qiu. Optimal separation in exact query complexities for Simon's problem. Journal of Computer and System Sciences 97: 83-93, 2018, https://doi.org/10.1016/j.jcss.2018.05.001