Skip main navigation

Quantum Period Finding

In the article, we see how the pieces come together to complete the process of finding factors of large numbers using Shor's algorithm.
ยฉ Keio University

Now that we understand what the quantum Fourier transform (QFT) does, and we know what the period of a function is, we can talk about how the QFT is used to execute the period finding function.

You probably have a pretty good idea how this works already. In the last phase of the discussion of the QFT, we saw that the QFT operating on a superposition state behaves in striking ways. If the input superposition is something like (|0rangle+|4rangle) then some of the terms cancel, and the output is (|0rangle+|2rangle+|4rangle+|6rangle).

Note that all of the values in the output superposition are multiples of two. Two happens to be the number of terms in our input superposition! Is that significant?

Turns out that it is. We can set up our algorithm so that the number of terms in that output superposition is the period of the function.

Letโ€™s walk through an example. Can we factor the number (N = 21) using Shorโ€™s algorithm? Consider the sequence (2^i), for some long series of numbers:

(2^0 = 1 bmod 21 = 1)
(2^1 = 2 bmod 21 = 2)
(2^2 = 4 bmod 21 = 4)
(2^3 = 8 bmod 21 = 8)
(2^4 = 16 bmod 21 = 16)
(2^5 = 32 bmod 21 = 11)
(2^6 = 64 bmod 21 = 1)
(2^7 = 128 bmod 21 = 2)
(vdots)

(Instead of 2, we could have picked almost any number greater than one and less than the number we are trying to factor, but 2 makes a nice, simple case. Sometimes by accident we choose a number that doesnโ€™t work well, but that should be rare.)

We can see that every six steps we come back to 1. The period of this function is six. If we write out the sequence, we get (1, 2, 4, 8, 16, 11, 1, 2, 4, 8, 16, 11, 1, 2, 4, 8, 16, 11, 1, โ€ฆ).

Letโ€™s pair each input number with its output number:

input โ†’ output ย  input โ†’ output
0 โ†’ 1 ย  13 โ†’ 2
1 โ†’ 2 ย  14 โ†’ 4
2 โ†’ 4 ย  15 โ†’ 8
3 โ†’ 8 ย  16 โ†’ 16
4 โ†’ 16 ย  17 โ†’ 11
5 โ†’ 11 ย  18 โ†’ 1
6 โ†’ 1 ย  19 โ†’ 2
7 โ†’ 2 ย  20 โ†’ 4
8 โ†’ 4 ย  21 โ†’ 8
9 โ†’ 8 ย  22 โ†’ 16
10 โ†’ 16 ย  23 โ†’ 11
11 โ†’ 11 ย  24 โ†’ 1
12 โ†’ 1 ย  โ€ฆ ย  ย 

and on and on. Now, letโ€™s sort those lines by the output number:

input โ†’ output ย  input โ†’ output
0 โ†’ 1 ย  3 โ†’ 8
6 โ†’ 1 ย  9 โ†’ 8
12 โ†’ 1 ย  15 โ†’ 8
18 โ†’ 1 ย  21 โ†’ 8
24 โ†’ 1 ย  4 โ†’ 16
1 โ†’ 2 ย  10 โ†’ 16
7 โ†’ 2 ย  16 โ†’ 16
13 โ†’ 2 ย  22 โ†’ 16
19 โ†’ 2 ย  5 โ†’ 11
2 โ†’ 4 ย  11 โ†’ 11
8 โ†’ 4 ย  17 โ†’ 11
14 โ†’ 4 ย  23 โ†’ 11
20 โ†’ 4 ย  โ€ฆ ย  ย 

In Shorโ€™s algorithm, the modular exponentiation step does exactly this. It groups input values that all have the same output value: give me a list of all of the values for (x) for which (2^x bmod 21) equals the same value.

Before we run the QFT, we measure some of our qubits (just the ones holding the output value), which causes the quantum state to partially collapse. This measurement causes one of the output values to be picked at random. If the output value we find is 1, for example, we would have left a superposition like this:

[|0rangle+|6rangle+|12rangle+|18rangle+|24rangle โ€ฆ]

If instead we found the output value 11, we would have the list

[|5rangle+|11rangle+|17rangle+|23rangle+|29rangle โ€ฆ]

In any case, we have a superposition with the period six.

If we have nine qubits, there are (2^9 = 512) basis states, 0 through 511, and we are creating interference across them. Since we have divided them up into 6 roughly equal sets, there are about (512/6 approx 85) members of each set. This number shows up in the QFT of our superposition

9-qubit QFT with period 6

where the peaks (corresponding to high probability of being measured) appear at 0, 85, 171, 256, 341, and 427. As you can see in the picture, the six peaks arenโ€™t all the same size, but they are all between 11% and 17%, so very, very roughly we can say that there is a similar probability of getting each of those results when we measure our register at the end of the quantum part of the computation.

Once we have the result, we then take that and turn it into the period. If we measure and find, for example, the value 85, then we use the value (512 / 85 approx 6). The procedure for finding the period is actually a little more complicated than this, but we donโ€™t need to go into the details. That complexity is needed when we get one of the numbers larger than 85, which will happen most of the time. Occassionally, we find the number 0 when we measure, which we canโ€™t use, so we rerun the quantum part of the algorithm and hope for a different result the next time, which probability suggests we are likely to get.

The Final Step: Euclid

Earlier, we saw how Euclidโ€™s algorithm can be used to find the greatest common denominator (gcd) of two integers. Now, weโ€™re going to need it.

Take the period we just found (call it (r); in this case, (r = 6)), and use the number we picked to exponentiate (call it (a); in this case, (a = 2)). Calculate the numbers (a^{(r/2)}+1 = 2^{(6/2)}+1=2^3+1 = 9) and (a^{(r/2)}-1 = 2^3-1 = 7).

Finally, take each of those two numbers and our number weโ€™re trying to factor ((N = 21)), and

use Euclidโ€™s algorithm to find the gcd:

gcd(9,21) = 3

gcd(7,21) = 7

and of course (21 = 7 times 3), so we have found the two factors of our number!

้‡ๅญๅ‘จๆœŸใฎ็™บ่ฆ‹

้‡ๅญใƒ•ใƒผใƒชใ‚จๅค‰ๆ›๏ผˆQFT๏ผ‰ใฎๅƒใใ‚’็†่งฃใ—ใ€้–ขๆ•ฐใฎๅ‘จๆœŸใ‚’็ŸฅใฃใŸใฎใงใ€QFTใ‚’ไฝฟ็”จใ—ใฆๅ‘จๆœŸๆŽข็Ÿฅ้–ขๆ•ฐใ‚’ๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆ่ฉฑใ—ใพใ™ใ€‚

ใฉใฎใ‚ˆใ†ใซๅ‘จๆœŸ้–ขๆ•ฐใŒๆฉŸ่ƒฝใ—ใฆใ„ใ‚‹ใฎใ‹ใ€ๆ—ขใซไบˆๆƒณใŒไป˜ใ„ใฆใ„ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ QFTใฎ่ญฐ่ซ–ใฎๆœ€ๅพŒใซใ€้‡ใญๅˆใ‚ใ›็Šถๆ…‹ใงQFTใŒๅ‹•ไฝœใ™ใ‚‹ใฎใ‚’่ฆ‹ใฆๆฅใพใ—ใŸใ€‚ใ‚‚ใ—ๅ…ฅๅŠ›ใ•ใ‚Œใ‚‹้‡ใญๅˆใ‚ใ›็Šถๆ…‹ใŒ (vert0rangle+vert4rangle)ใฎใ‚ˆใ†ใชใ‚‚ใฎใ ใจใ€ใ„ใใคใ‹ใฎ้ …ใŒใ‚ญใƒฃใƒณใ‚ปใƒซใ•ใ‚Œใฆๅ‡บๅŠ›ใฏ(vert0rangle+vert2rangle+vert4rangle+vert6rangle)ใซใชใ‚Šใพใ™ใ€‚

ๅ‡บๅŠ›ใ•ใ‚Œใ‚‹้‡ใญๅˆใ‚ใ›ใฎๅ€คใฏๅ…จใฆ2ใฎๅ€ๆ•ฐใงใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ 2ใคใฏๅ…ฅๅŠ›ใ•ใ‚ŒใŸ้‡ใญๅˆใ‚ใ›ใฎ้ …ใฎๆ•ฐใซใชใ‚Šใพใ™๏ผ ใ“ใฎไบ‹ๅฎŸใฏ้‡่ฆใงใ™ใ€‚

ใ“ใ‚Œใซใ‚ˆใฃใฆๅ‡บๅŠ›ใ•ใ‚Œใ‚‹้‡ใญๅˆใ‚ใ›็Šถๆ…‹ใฎ้ …ใฎๆ•ฐใŒ้–ขๆ•ฐใฎๅ‘จๆœŸใซใชใ‚‹ใ‚ˆใ†ใซใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใ‚’่จญๅฎšใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚

ไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚็งใŸใกใฏใ‚ทใƒงใ‚ขใฎใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใ‚’ไฝฟใฃใฆ(N=21)ใ‚’ๅ› ๆ•ฐๅˆ†่งฃใงใใ‚‹ใงใ—ใ‚‡ใ†ใ‹ใ€‚2ใฎ(i)ไน— ใซๅฏพใ—ใฆๅคงใใ„ๆ•ฐใง่€ƒใˆใฆใฟใพใ—ใ‚‡ใ†ใ€‚

(2^0 = 1 bmod 21 = 1)
(2^1 = 2 bmod 21 = 2)
(2^2 = 4 bmod 21 = 4)
(2^3 = 8 bmod 21 = 8)
(2^4 = 16 bmod 21 = 16)
(2^5 = 32 bmod 21 = 11)
(2^6 = 64 bmod 21 = 1)
(2^7 = 128 bmod 21 = 2)
(vdots)

ๅ› ๆ•ฐๅˆ†่งฃใ™ใ‚‹ๆ•ฐใ‚ˆใ‚Šๅฐใ•ใ1ใ‚ˆใ‚Šๅคงใใ„ๆ•ฐๅญ—ใฎใ„ใšใ‚Œใ‹ใ‚’ใ€2ใฎไปฃใ‚ใ‚Šใซ้ธใถใ“ใจใŒใงใใพใ™ใ€‚๏ผ’ใฏๅ˜็ด”ใงใ‚ใ‹ใ‚Šใ‚„ใ™ใ„ใ‚ฑใƒผใ‚นใงใ™ใ€‚่ชคใฃใฆใ†ใพใใ„ใ‹ใชใ„ๆ•ฐใ‚’้ธๆŠžใ™ใ‚‹ใ“ใจใ‚‚ใ‚ใ‚Šใพใ™ใŒใ€ใใ‚Œใฏ็จ€ใงใ™ใ€‚)

6ใ‚นใƒ†ใƒƒใƒ—ใ”ใจใซ๏ผ‘ใซๆˆปใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ใใฎใŸใ‚ใ€ใ“ใฎ้–ขๆ•ฐใฎๅ‘จๆœŸใฏ๏ผ–ใงใ™ใ€‚้ †็•ชใซๆ›ธใใ ใ™ใจ(1, 2, 4, 8, 16, 11, 1, 2, 4, 8, 16, 11, 1, 2, 4, 8, 16, 11, 1, โ€ฆ)ใจใชใ‚Šใพใ™ใ€‚

ๅ„ๅ…ฅๅŠ›็•ชๅทใจใใฎๅ‡บๅŠ›็•ชๅทใ‚’ใƒšใ‚ขใซใ—ใพใ—ใ‚‡ใ†:

input โ†’ output ย  input โ†’ output
0 โ†’ 1 ย  13 โ†’ 2
1 โ†’ 2 ย  14 โ†’ 4
2 โ†’ 4 ย  15 โ†’ 8
3 โ†’ 8 ย  16 โ†’ 16
4 โ†’ 16 ย  17 โ†’ 11
5 โ†’ 11 ย  18 โ†’ 1
6 โ†’ 1 ย  19 โ†’ 2
7 โ†’ 2 ย  20 โ†’ 4
8 โ†’ 4 ย  21 โ†’ 8
9 โ†’ 8 ย  22 โ†’ 16
10 โ†’ 16 ย  23 โ†’ 11
11 โ†’ 11 ย  24 โ†’ 1
12 โ†’ 1 ย  โ€ฆ ย  ย 

็ถšใ„ใฆใ€ใ“ใ‚Œใ‚‰ใฎ่กŒใ‚’ๅ‡บๅŠ›็•ชๅทใงใ‚ฝใƒผใƒˆใ—ใพใ—ใ‚‡ใ†:

input โ†’ output ย  input โ†’ output
0 โ†’ 1 ย  3 โ†’ 8
6 โ†’ 1 ย  9 โ†’ 8
12 โ†’ 1 ย  15 โ†’ 8
18 โ†’ 1 ย  21 โ†’ 8
24 โ†’ 1 ย  4 โ†’ 16
1 โ†’ 2 ย  10 โ†’ 16
7 โ†’ 2 ย  16 โ†’ 16
13 โ†’ 2 ย  22 โ†’ 16
19 โ†’ 2 ย  5 โ†’ 11
2 โ†’ 4 ย  11 โ†’ 11
8 โ†’ 4 ย  17 โ†’ 11
14 โ†’ 4 ย  23 โ†’ 11
20 โ†’ 4 ย  โ€ฆ ย  ย 

Shorใฎใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใงใฏใ€ๅ†ชไน—ไฝ™ใฎใ‚นใƒ†ใƒƒใƒ—ใงใ“ใ‚Œใ‚’่กŒใ„ใพใ™ใ€‚ๅŒใ˜ๅ‡บๅŠ›ๅ€คใ‚’ๆŒใคๅ…จใฆใฎๅ…ฅๅŠ›ๅ€คใ‚’ใ‚ฐใƒซใƒผใƒ—ๅŒ–ใ—ใพใ™ใ€‚ใคใพใ‚Š(2^x bmod 21)ใŒ็ญ‰ใ—ใ„ใ™ในใฆใฎ(x)ใฎๅ€คใฎใƒชใ‚นใƒˆใ‚’ๆฑ‚ใ‚ใพใ™ใ€‚

QFTใ‚’ๅฎŸ่กŒใ™ใ‚‹ๅ‰ใซใ€ใ„ใใคใ‹ใฎ้‡ๅญใƒ“ใƒƒใƒˆ๏ผˆๅ‡บๅŠ›ๅ€คใ‚’ไฟๆŒใ™ใ‚‹้‡ๅญใƒ“ใƒƒใƒˆ๏ผ‰ใ‚’ๆธฌๅฎšใ—ใ€้‡ๅญ็Šถๆ…‹ใ‚’้ƒจๅˆ†็š„ใซๅดฉๅฃŠใ•ใ›ใพใ™ใ€‚ ใ“ใฎๆธฌๅฎšใซใ‚ˆใ‚Šใ€ๅ‡บๅŠ›ๅ€คใฎ1ใคใŒใƒฉใƒณใƒ€ใƒ ใซ้ธๆŠžใ•ใ‚Œใพใ™ใ€‚ ใŸใจใˆใฐใ€่ฆ‹ใคใ‹ใฃใŸๅ‡บๅŠ›ๅ€คใŒ1ใฎๅ ดๅˆใ€ๆฌกใฎใ‚ˆใ†ใซ้‡ใญใฆ่กจ็คบใ•ใ‚Œใพใ™ใ€‚

[|0rangle+|6rangle+|12rangle+|18rangle+|24rangle โ€ฆ]

ๅ‡บๅŠ›ๅ€ค11ใŒ่ฆ‹ใคใ‹ใฃใŸๅ ดๅˆใฏใ€ไปฅไธ‹ใฎใƒชใ‚นใƒˆใ‚’ๅพ—ใพใ™ใ€‚

[|5rangle+|11rangle+|17rangle+|23rangle+|29rangle โ€ฆ]

ใ„ใšใ‚Œใซใ›ใ‚ˆใ€็ง้”ใฏ6ๅ‘จๆœŸใฎ้‡ใญๅˆใ‚ใ›ใ‚’ๅพ—ใพใ™ใ€‚

ใ‚‚ใ—9้‡ๅญใƒ“ใƒƒใƒˆใŒใ‚ใ‚‹ๅ ดๅˆใ€(2^9 = 512)ใฎ็Šถๆ…‹ใ€ใคใพใ‚Š0ใ‹ใ‚‰511ใŒใ‚ใ‚Šใ€ใใ‚Œใ‚‰ใฎ้–“ใซๅนฒๆธ‰ใŒ็”Ÿใ˜ใฆใ„ใพใ™ใ€‚ใใ‚Œใ‚‰ใ‚’6ใคใฎใปใผๆ•ฐใฎ็ญ‰ใ—ใ„ใ‚ฐใƒซใƒผใƒ—ใซๅˆ†ใ‘ใŸใŸใ‚ใ€ๅ„ใ‚ฐใƒซใƒผใƒ—ใฎไธญใซใฏ(512/6 approx 85)ๅ€‹ใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎๆ•ฐใฏใ€้‡ใญๅˆใ‚ใ›ใฎQFTใซ็พใ‚Œใพใ™ใ€‚

9-qubit QFT with period 6

(ๆธฌๅฎšใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„็ขบ็Ž‡ใซๅฏพๅฟœใ™ใ‚‹)ใƒ”ใƒผใ‚ฏใฏใ€0, 85, 171, 256, 341,427ใซ็พใ‚Œใพใ™ใ€‚็”ปๅƒใฎใ‚ˆใ†ใซใ€6ใคใฎใƒ”ใƒผใ‚ฏใฏใ™ในใฆๅŒใ˜ใ‚ตใ‚คใ‚บใงใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ใ™ในใฆ 11๏ผ…ใ‹ใ‚‰17๏ผ…ใฎ้–“ใซใ‚ใ‚‹ใฎใงใ€ๅคงใพใ‹ใซ่จ€ใฃใฆใ€่จˆ็ฎ—ใฎ้‡ๅญ้ƒจๅˆ†ใฎ็ต‚ใ‚ใ‚Šใซใƒฌใ‚ธใ‚นใ‚ฟใ‚’ๆธฌๅฎšใ™ใ‚‹ใจใ€ใใ‚Œใžใ‚Œใฎ็ตๆžœใ‚’ๅพ—ใ‚‹็ขบ็Ž‡ใฏใŠใŠใ‚ˆใๅŒใ˜ใงใ™ใ€‚

็ตๆžœใŒๅพ—ใ‚‰ใ‚ŒใŸใ‚‰ใ€ใใ‚Œใ‚’ๅ–ใฃใฆๅ‘จๆœŸใซๅค‰ใˆใพใ™ใ€‚ ใŸใจใˆใฐใ€ๅ€ค85ใ‚’ๆธฌๅฎšใ—ใฆ่ฆ‹ใคใ‘ใ‚‹ใจใ€(512 / 85 approx 6)ใฎๅ€คใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ ๅ‘จๆœŸใ‚’่ฆ‹ใคใ‘ใ‚‹ๆ‰‹็ถšใใฏๅฎŸ้š›ใซใฏใ“ใ‚Œใ‚ˆใ‚Š่ค‡้›‘ใงใ™ใŒใ€ใ“ใ“ใงใฏ่งฆใ‚Œใพใ›ใ‚“ใ€‚

ใใฎ่ค‡้›‘ใ•ใฏใ€็งใŸใกใŒ85ใ‚ˆใ‚Šใ‚‚ๅคงใใ„ๆ•ฐใ‚’ๅพ—ใ‚‹ใจใใซๅฟ…่ฆใจใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใฏใปใจใ‚“ใฉใฎๅ ดๅˆ่ตทใ“ใ‚Šใพใ™ใ€‚ ๆ™‚ใซใฏใ€ๆˆ‘ใ€…ใŒไฝฟ็”จใ™ใ‚‹ใ“ใจใŒใงใใชใ„ๆธฌๅฎšๆ™‚ใซๆ•ฐๅญ—0ใ‚’่ฆ‹ใคใ‘ใ‚‹ใฎใงใ€ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใฎ้‡ๅญ้ƒจๅˆ†ใ‚’ๅ†ๅฎŸ่กŒใ—ใ€ๆฌกๅ›žใซๅˆฅใฎ็ตๆžœใŒๅพ—ใ‚‰ใ‚Œใ‚‹ใ“ใจใ‚’ๆœŸๅพ…ใ—ใฆใ„ใพใ™ใ€‚

ๆœ€ๅพŒใฎใ‚นใƒ†ใƒƒใƒ—๏ผšใƒฆใƒผใ‚ฏใƒชใƒƒใƒ‰

ๅ…ˆใปใฉใ€ใƒฆใƒผใ‚ฏใƒชใƒƒใƒ‰ใฎใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใ‚’ไฝฟใฃใฆใ€2ใคใฎๆ•ดๆ•ฐใฎๆœ€ๅคงๅ…ฌ็ด„ๆ•ฐ๏ผˆgcd๏ผ‰ใ‚’่ฆ‹ใคใ‘ใ‚‹ๆ–นๆณ•ใ‚’่ฆ‹ใฆใใพใ—ใŸใ€‚ ไปŠใ‹ใ‚‰ใใ‚Œใ‚’ๅˆฉ็”จใ—ใพใ™ใ€‚

่ฆ‹ใคใ‹ใฃใŸๅ‘จๆœŸ((r)ใจๅ‘ผใถใ€ใ“ใฎๅ ดๅˆใฏ(r=6))ใ‚’ใจใ‚Šใ€ใ“ใ‚Œใ‚’็ดฏไน—ใซไฝฟ็”จใ—ใพใ™๏ผˆ(ฮฑ)ใจๅ‘ผใถใ€ใ“ใฎๅ ดๅˆใฏ(ฮฑ=2))ใ€‚

(a^{(r/2)}+1 = 2^{(6/2)}+1=2^3+1 = 9) and (a^{(r/2)}-1 = 2^3-1 = 7)

ๆœ€ๅพŒใซใ€ใ“ใ‚Œใ‚‰ใฎ2ใคใฎๆ•ฐๅญ—ใจๅ› ๆ•ฐๅˆ†่งฃใ—ใ‚ˆใ†ใจใ—ใฆใ„ใ‚‹ๆ•ฐๅญ—((N=21)) ใซใƒฆใƒผใ‚ฏใƒชใƒƒใƒ‰ใฎใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใ‚’ไฝฟใ„ใ€

gcd(9,21) = 3

gcd(7,21) = 7

(21 = 7 times 3)ใงใ€2ใคใฎๆ•ฐๅญ—ใ‚’่ฆ‹ใคใ‘ใพใ—ใŸ๏ผ

ยฉ Keio University
This article is from the free online

Understanding Quantum Computers

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now