🌑

Simon's Algorithm

  1. Problem Description
  2. Solutions
  3. Implementation
  4. References
  5. Technical Instructions
  6. Appendix

Simon’s Algorithm was first proposed by Daniel Simon in 1994. It was the first quantum algorithm that was exponentially faster than the best classical algorithm and was also an inspiration for Shor’s Algorithm.

Problem Description

Goal:

We are given a hidden function f:{0,1}n{0,1}nf:\{0,1\}^n\to\{0,1\}^n, such that f(x)=f(y)f(x)=f(y) if and only if y=xy=x or y=xsy=x \oplus s for a secret string s{0,1}ns \in \{0,1\}^n. Our goal is to find ss.

Remarks:

  • If s=0ns=0^n, then ff is one-to-one. If s0ns \ne 0^n, then ff is two-to-one. Both cases are identifiable by Simon’s Algorithm. However, for illustration, we may assume s0ns \ne 0^n in later parts.

An example with n=4,s=1001n=4, s=1001:

xx xsx \oplus s f(x)f(x) xx xsx \oplus s f(x)f(x)
0000 1001 1111 1000 0001 1101
0001 1000 1101 1001 0000 1111
0010 1011 1010 1010 0011 1000
0011 1010 1000 1011 0010 1010
0100 1101 0011 1100 0101 0001
0101 1100 0001 1101 0100 0011
0110 1111 0110 1110 0111 0100
0111 1110 0100 1111 0110 0110

It can be easily checked that this ff is a two-to-one function since exactly two different inputs are mapped to a unique output.

Solutions

Classic Solution:

If we can find xx and yy such that xyx \ne y and f(x)=f(y)f(x)=f(y), then we can obtain s=xys=x \oplus y. Therefore, the question now becomes how many different inputs do we have to try in order to find two inputs with the same output?

We have 2n2^n different inputs, and half of them (2n12^{n-1} inputs) produce unique outputs.

  • Worst case: We need to try 2n1+12^{n-1}+1 inputs \longrightarrow Θ(2n)=O(2n)\Theta(2^n)=\mathcal{O}(2^n) complexity
  • Average case: We need to try approximately 2n/22^{n/2} inputs (see birthday problem) \longrightarrow Θ(2n/2)=O(2n)\Theta(2^{n/2})=\mathcal{O}(2^n) complexity

In fact, we can show that all classic solutions have at least Ω(2n/2)\Omega(2^{n/2}) query complexity.

Quantum Solution:

simons-algorithm-1.png

  1. We initialize 22 quantum systems AA and BB with nn qubits each in the state 0\ket{0}.

  2. Pass each of the nn qubits in AA through a Hadamard gate.

  3. Pass AA and BB through the oracle Uf:x,yx,yf(x)U_f:\ket{x},\ket{y}\to\ket{x},\ket{y\oplus f(x)}.

  4. Measure BB on the computational basis.

  5. Pass each of the nn qubits in AA through a Hadamard gate again.

  6. Measure AA on the computational basis and record the outcome string ziz_i.

  7. Repeat procedure 1-6 until we have n1n-1 linearly independent ziz_i’s.

    Then ss can be solved by equations:

    {sz1=0mod2sz2=0mod2szn1=0mod2\left\{\begin{array}{c}s \cdot z_{1}=0 \mod 2 \\ s \cdot z_{2}=0 \mod 2 \\ \vdots \\ s \cdot z_{n-1}=0 \mod 2 \end{array}\right.

Remarks:

  • The probability that ziz_i’s are not linearly independent is at most 0.7112120.711212 (proof omitted), so we only need to repeat procedure 1-6 for O(n)\mathcal{O}(n) times to satisfy the independence condition. Each time, we only query UfU_f once. Therefore, the query complexity of Simon’s Algorithm is O(n)\mathcal{O}(n), which is exponentially faster than classic algorithms.
  • Although this is a system of n1n-1 linear equations in nn unknowns, we can still solve for a unique non-zero solution ss. (Recall our assumption that s0ns \ne 0^n)
  • A detailed proof of the correctness of Simon’s Algorithm is omitted here, as we will put emphasis on how to use code to implement this quantum algorithm. However, more details can be found in References.

Implementation

Quantum Implementation:

To illustrate how to implement the quantum circuit, we use the example ff given in the above table.

To design a UfU_f based on ff:

  • We notice that f(0000)=f(1001)f(0000)=f(1001). This implies we may use x0x_0 and x3x_3 to control the same yiy_i so that the output remains unchanged for two xx’s differing by s=1001s=1001.
  • Since only the two-to-one mapping relations are essential, while the specific numeric numbers allocated to f(x)f(x) are not. We may design the remaining part of UfU_f through trials and errors.

The final UfU_f is

simons-algorithm-2.png

The code of this oracle is in Appendix(1.1)

With UfU_f, the full quantum circuit of Simon’s Algorithm for the example ff is

simons-algorithm-3.png

The code of this algorithm is in Appendix(1.2)

Experimental Results:

To test the performance of our implementation, we run the quantum circuit on the Quantum Leaf CloudBaiduSim2Water simulator. The server simulates the code for 1024 times and provides the histogram of raw outputs.

simons-algorithm-4.png

The raw outcomes are in Appendix(1.3)

However, we are only interested in the last 44 bits measured on system AA. After performing some data cleansing, we can obtain the desired histogram.

simons-algorithm-5.png

The transformed outcomes are in Appendix(1.4)

Notice that we obtain more than needed data in this simulation. So, we may randomly choose n1=3n-1=3 linearly independent outcomes for further post-processing. Assume the obtained outcomes are {0010,0100,1001}\{0010,0100,1001\}. We can solve for ss with the equation

[0010010010010000][s1s2s3s4]=[0000] \left[\begin{array}{llll}0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 1 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0\end{array}\right]\left[\begin{array}{l}s_{1} \\ s_{2} \\ s_{3} \\ s_{4}\end{array}\right]=\left[\begin{array}{l}0 \\ 0 \\ 0 \\ 0\end{array}\right]

where s=s1s2s3s4s=\overline{s_1 s_2 s_3 s_4}. After Gaussian elimination, the augmented matrix becomes

[10010010000010000000] \left[\begin{array}{llll|l}1 & 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0\end{array}\right]

A non-trial solution (i.e. s0000s \ne 0000) to this equation is s=1001s=1001.

If we pick any other set of linearly independent outcomes, the result is the same. In this way, we have shown the correctness of our quantum implementation.

References

  1. Johansson, N.; Larsson, JÅ. (2017). “Efficient classical simulation of the Deutsch–Jozsa and Simon’s algorithms”. Quantum Inf Process (2017)16 (9): 233. arXiv:1508.05027Bibcode:2017QuIP…16…233Jdoi:10.1007/s11128-017-1679-7S2CID 28670540.
  2. Koiran, P.; Nesme, V.; Portier, N. (2005), “A quantum lower bound for the query complexity of Simon’s Problem”Proc. ICALP3580: 1287–1298, arXiv:quant-ph/0501060Bibcode:2005quant.ph…1060K, retrieved 2011-06-06
  3. Qiskit. Simon’s Algorithm. Retrieved from https://qiskit.org/textbook/ch-algorithms/simon.html.
  4. Simon, Daniel R. (1997-10-01). “On the Power of Quantum Computation”SIAM Journal on Computing26 (5): 1474–1483. doi:10.1137/S0097539796298637ISSN 0097-5397.

Technical Instructions

How to use Quantum Leaf?

  1. Register and login https://quantum-hub.baidu.com/.
  2. Click QComposer on the side panel.
  3. Copy and paste codes into the code space on the left.
  4. Select a Quantum End in the upper right corner and click Run.
  5. After execution, results will be shown at the bottom.

Appendix

1.1 Code of UfU_f for Simon’s Algorithm

OPENQASM 2.0;
include "qelib1.inc";
qreg q[8];
creg c[8];
//
x q[4];
x q[5];
x q[6];
x q[7];
//
cx q[0], q[6];
//
cx q[1], q[4];
//
cx q[2], q[7];
//
cx q[3], q[6];
//
cx q[4], q[5];
//
cx q[7], q[5];

1.2 Code of Simon’s Algorithm

OPENQASM 2.0;
include "qelib1.inc";
qreg q[8];
creg c[8];
//
h q[0];
h q[1];
h q[2];
h q[3];
//
x q[4];
x q[5];
x q[6];
x q[7];
//
cx q[0], q[6];
//
cx q[1], q[4];
//
cx q[2], q[7];
//
cx q[3], q[6];
//
cx q[4], q[5];
//
cx q[7], q[5];
//
//
h q[0];
h q[1];
h q[2];

h q[3];
//
measure q[4] -> c[4];
measure q[5] -> c[5];
measure q[6] -> c[6];
measure q[7] -> c[7];
measure q[0] -> c[0];
measure q[1] -> c[1];
measure q[2] -> c[2];
measure q[3] -> c[3];

1.3 Raw outcomes of Simon’s Algorithm

{
    "00010000": 9,
    "00010010": 21,
    "00010100": 17,
    "00010110": 11,
    "00011001": 17,
    "00011011": 18,
    "00011101": 23,
    "00011111": 20,
    "00100000": 14,
    "00100010": 10,
    "00100100": 20,
    "00100110": 16,
    "00101001": 22,
    "00101011": 13,
    "00101101": 16,
    "00101111": 12,
    "01010000": 20,
    "01010010": 19,
    "01010100": 13,
    "01010110": 15,
    "01011001": 17,
    "01011011": 23,
    "01011101": 11,
    "01011111": 16,
    "01100000": 14,
    "01100010": 20,
    "01100100": 13,
    "01100110": 18,
    "01101001": 22,
    "01101011": 17,
    "01101101": 11,
    "01101111": 20,
    "10000000": 13,
    "10000010": 19,
    "10000100": 12,
    "10000110": 14,
    "10001001": 20,
    "10001011": 16,
    "10001101": 14,
    "10001111": 14,
    "10110000": 13,
    "10110010": 16,
    "10110100": 16,
    "10110110": 15,
    "10111001": 17,
    "10111011": 18,
    "10111101": 12,
    "10111111": 12,
    "11000000": 15,
    "11000010": 17,
    "11000100": 16,
    "11000110": 20,
    "11001001": 19,
    "11001011": 15,
    "11001101": 13,
    "11001111": 13,
    "11110000": 13,
    "11110010": 16,
    "11110100": 15,
    "11110110": 11,
    "11111001": 21,
    "11111011": 19,
    "11111101": 18,
    "11111111": 14
}

1.4 Transformed outcomes of Simon’s Algorithm

{
    "0000": 111,
    "0010": 138,
    "0100": 122,
    "0110": 120,
    "1001": 155,
    "1011": 139,
    "1101": 118,
    "1111": 121,
}

— Oct 30, 2021

Related:

Creative Commons License
Simon's Algorithm by Lu Meng is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at About.