bloch-sphere-0

Order Finding

In order to understand the application of the fourier transform to more applications, we'll first look at an algorithm called order finding.

This algorithm takes a periodic function as input and produces, as output, the period of that function. We'll talk a bit more about exactly what that means in a bit. But first let's go back to an old friend: modular arithmetic.

We first met modular (or clock) arithmetic in our article on fields. Doing arithmetic modulo NN essentially means only being concerned with the number's remainder when divided by NN. So we get statements like the following:

133 (mod 10)13 \equiv 3\ (\text{mod }10)

This is saying that 1313 and 33 are equal (or, more formally, congruent) mod 1010 as they both have the same remainder (33) when divided by 1010.

Order finding, is concerned with this equation:

xr1 (mod N)x^r \equiv 1\ (\text{mod }N)

Specifically, we will choose some value of xx and NN and ask: what is rr? Put more plainly, this is: how many times do I have to multiply xx by itself before it gets back to 11 modulo NN?

This is a very important question in number theory. We won't dwell too much on all the interesting details, but it is important to note that for some xx and NN there is no such rr. For example, if x=2x=2 and N=8N = 8, no matter how many times we multiply 22 by itself, it will never get to 11. This is fairly easy to see since multiplying by 22 and taking a remainder when divided by 88 will always give us an even number, whereas 11 is odd.

It turns out, that rr only exists if xx and NN are co-prime. That is, they share no common factors.

One example would be when x=5x=5 and N=21N = 21, r=6r = 6. This is because:

5×5=254 (mod 21)5×4=2020 (mod 21)5×20=10016 (mod 21)5×16=8017 (mod 21)5×17=851 (mod 21)5 \times 5 = 25 \equiv 4\ (\text{mod } 21) \\[3ex] 5 \times 4 = 20 \equiv 20\ (\text{mod } 21) \\[3ex] 5 \times 20 = 100 \equiv 16\ (\text{mod } 21) \\[3ex] 5 \times 16 = 80 \equiv 17\ (\text{mod } 21) \\[3ex] 5 \times 17 = 85 \equiv 1\ (\text{mod } 21)

Exercise

Find rr when x=13x=13 and N=17N=17

Show solution

Exercise

Does rr exist if x=8x=8 and N=12N=12

Show solution

We call this value rr the order. Generally, computing the order is really difficult. At least for classical computers. We'll now apply the quantum fourier transform in order to solve it more easily using a quantum computer.

Like a lot of quantum algorithms, lets start by introducing an oracle. Our oracle will be the following:

Ux0=xax mod NU\ket{x}\ket{0} = \ket{x}\ket{a^x\ \text{mod}\ N}

It's important to note here that the 2 kets represent multiple quantum bits as just having single bit numbers wouldn't be very useful. We'll deliberately leave these two keys separate during our calculations and refer to them as the first/second register.

If we start in a state where all our qubits are 0\ket{0} and then we apply a Hadamard gate to all the qubits in the first register we get:

12n/2j=02n1j0\frac{1}{2^{n/2}}\sum_{j=0}^{2^n-1}\ket{j}\ket{0}

Where nn is the number of qubits in the first register.

Applying our oracle now gives us:

12n/2j=02n1jaj mod N\frac{1}{2^{n/2}}\sum_{j=0}^{2^n-1}\ket{j}\ket{a^j\ \text{mod}\ N}

We can then measure the second register. This will produce some value f0f_0 such that f0ax0 (mod N)f_0 \equiv a^{x_0}\ (\text{mod}\ N) for some x0x_0.

This now means the first register has dropped any states j\ket{j} such that aj≢f0 (mod N)a^j \not\equiv f_0\ (\text{mod}\ N)

So, where's rr going to come from in all this? For that, we'll jump back into a little bit of number theory.

Since we know ar1 (mod N)a^r \equiv 1\ (\text{mod}\ N) we have that for any numbers xx and kk:

ax+kraxakrax(ar)kax(1)kax (mod N)a^{x+kr} \equiv a^xa^{kr} \equiv a^x(a^r)^k \equiv a^x(1)^k \equiv a^x\ (\text{mod}\ N)

So, we know:

f0ax0ax0+rax0+2r (mod N)f_0 \equiv a^{x_0} \equiv a^{x_0 + r} \equiv a^{x_0 + 2r} \equiv \ldots\ (\text{mod}\ N)

This means any value in the first register must now be of the form:j+kr\ket{j+kr} for some kk.

So our state now becomes:

1mj=0m1x0+jr\frac{1}{\sqrt{m}}\sum_{j=0}^{m-1}\ket{x_0 + jr}

Where mm is the number of remaining kets.

And now we apply the Quantum Fourier Transform:

QFT(1mj=0m1x0+jr)=1mj=0m1QFT(x0+jr)=1mj=0m112n/2k=02n1ω2n(x0+jr)kk=1m2nj=0m1k=02n1ω2nkjrω2nkx0k=1m2nk=02n1(j=0m1ω2nkjr)ω2nkx0kQFT\Bigl(\frac{1}{\sqrt{m}}\sum_{j=0}^{m-1}\ket{x_0 + jr}\Bigr) = \frac{1}{\sqrt{m}}\sum_{j=0}^{m-1}QFT(\ket{x_0 + jr}) \\[3ex] = \frac{1}{\sqrt{m}}\sum_{j=0}^{m-1}\frac{1}{2^{n/2}}\sum_{k=0}^{2^n-1}\omega_{2^n}^{(x_0 + jr)k}\ket{k} \\[3ex] = \frac{1}{\sqrt{m2^n}}\sum_{j=0}^{m-1}\sum_{k=0}^{2^n-1}\omega_{2^n}^{kjr}\omega_{2^n}^{kx_0}\ket{k} \\[3ex] = \frac{1}{\sqrt{m2^n}}\sum_{k=0}^{2^n-1}\Bigl(\sum_{j=0}^{m-1}\omega_{2^n}^{kjr}\Bigl)\omega_{2^n}^{kx_0}\ket{k}

That part in the brackets will prove crucial to us now.

If we expand the ω\omega we get:

j=0m1e2πijkr2n\sum_{j=0}^{m-1}e^{2\pi ij\frac{kr}{2^n}}

In case the text is getting too small, that fraction in the exponent is:

kr2n\frac{kr}{2^n}

This is a geometric series and we can use the formula for the sum of a geometric series to get:

j=0m1e2πijkr2n=1e2πimkr2n1e2πikr2n\sum_{j=0}^{m-1}e^{2\pi ij\frac{kr}{2^n}} = \frac{1-e^{2\pi im\frac{kr}{2^n}}}{1-e^{2\pi i\frac{kr}{2^n}}}

Now, if kk is a value very close to an integer multiple of 2n/r2^n/r, i.e. kc2n/rk \approx c2^n/r for some cc, we can see the fraction in the exponent at the bottom is near to 11 so the whole fraction gets very large. Otherwise the term is very small.

What this whole exercise essentially tells us is that the co-efficient for terms where kk is close to c2n/rc2^n/r will be very large, and the rest will be very small.

This means, upon measuring now the first register, we have a very high probability to measure a state of the form:

c2n/r\ket{c2^n/r}

Great! That's all the messy quantum stuff out the way, now how do we get rr from this?

If we let our measurement be yy then we can simply divide by the known value 2n2^n to get:

y2ncr\frac{y}{2^n} \approx \frac{c}{r}

Notice we say approximately here. This is because we can't gaurantee we measure an exact multiple, only that our measurement is very close.

Now, if cc and rr are not co-prime (i.e. they share a common factor), we can't extract rr. e.g. if c=8c = 8 and r=4r = 4 then y/2ny/2^n is 1/21/2, so our rr value gets "destroyed" by the common factor cancelling. If we have this problem there's unfortunately no real way of knowing. We'll see later that if we fail in this way we will simply retry the quantum part of the algorithm.

Trying to extract rr from oury/2ny/2^n value comes with another difficulty: We only used a finite number of bits. If rr is odd, then 1/r1/r has an infinite decimal representation. So it's likely we may end up with an approximate value that is cutoff at some point.

Luckily, we know that cc and rr are integers, so our number must be rational. This means we can approximate rr with the process of continued fractions.

Continued fractions provides a way to develop a sequence of fractions providing better and better approximations to a given number. This also helps because we know we didn't necessarily measure an exact multiple of 2n/r2^n/r.

The continued fraction for a number qq is:

q=a0+1a1+1a2+1q = a_0 + \frac{1}{a_1 + \frac{1}{a_2 + \frac{1}{\ldots}}}

Where the sequence of ana_n provides better and better approximations for qq.

For example, if q=1.6q = 1.6 we can find it's continued fraction with the following method:

q=1+0.6=1+35=1+15353=1+23=1+13232=1+12q = 1 + 0.6 = 1 + \frac{3}{5} = 1 + \frac{1}{\frac{5}{3}} \\[3ex] \frac{5}{3} = 1 + \frac{2}{3} = 1 + \frac{1}{\frac{3}{2}} \\[3ex] \frac{3}{2} = 1 + \frac{1}{2}

Subbing each of those statements into the one above we have:

q=1+11+11+12q = 1 + \frac{1}{1 + \frac{1}{1 + \frac{1}{2}}}

So our sequence is a0=1, a1=1, a2=1, a3=2a_0 = 1,\ a_1 = 1,\ a_2 = 1,\ a_3 = 2.

Our approximations pnp_n are then:

p0=1p1=1+11=2p2=1+11+11=32p3=1+11+11+12=85p_0 = 1 \\[3ex] p_1 = 1 + \frac{1}{1} = 2 \\[3ex] p_2 = 1 + \frac{1}{1 + \frac{1}{1}} = \frac{3}{2} \\[3ex] p_3 = 1 + \frac{1}{1 + \frac{1}{ 1 + \frac{1}{2} }} = \frac{8}{5}

So, continued fractions gives us a list of continuing better approximations for our rational fraction. We can simply read off the denominator of any of them to get a candidate value for rr. So our three candidate rr values in this example are: 11, 22 and 55.

How do we check a candidate value? Just sub it into our original equation! xr mod Nx^r\ \text{mod}\ N can be easily computed and checked by a classical computer. If it equals 11 we are done. If it doesn't, we simply move on to our next candidate value.

If all of our candidate values fail, it means either cc and rr were not co-prime or our continued fractions approximation was unnsuccesful. In this scenario we just try again.

Now at this point, you're probably confused about this "trying again". Trying again, is not something we do in classical algorithms, but it can still be shown that doing this produces the correct answer quicker than any classical algorithm on average.

In fact, this algorithm actually performs better with bigger numbers! More qubits, produces more "sharp" spikes in the co-efficient of the c2n/r\ket{c2^n/r} states and also gives us a closer approximation for the continued fraction process. So whereas a classical algorithm would have to work harder for larger values of xx and NN a quantum computer actually gets more accurate making the speedup even more noticeable!

Let's quickly review the steps we went through:

  1. Prepare two registers: 00\ket{0}\ket{0}
  2. Hadamard gate all the qubits in the first register
  3. Apply the oracle Ux0=xax mod NU\ket{x}\ket{0} = \ket{x}\ket{a^x\ \text{mod}\ N}
  4. Measure the second register
  5. Apply the Quantum Fourier Transform to the first register
  6. Measure the first register
  7. Use continued fractions on the measured value to approximate some candidate values for rr
  8. If no candidate values work, start again from step 1

We've skipped over a few details, such as how many qubits we should use and what the probability of success actually is but it's enough for now.

In the next article we'll use what we've learned to factor prime numbers!

> Next Article (Shor's Algorithm)