Quantum Computing with IBM QX Chapter 12 Shors
Quantum Computing with IBM QX Chapter 12 Shor’s Algorithm Dr. Charles Tappert The information presented here, although greatly condensed, comes almost entirely from the textbook
Chapter 12 Shor’s Algorithm n n n Finds prime factorization of a number Chapter starts with what prime factorization is Shor’s algorithm – most important QC algorithm n n The pure classical implementation The quantum implementation n n The large classical component The quantum period-finding subroutine IBM QX implementation Many factorization examples The Jupyter Notebook for this chapter is available at https: //github. com/Packt. Publishing/Mastering. Quantum-Computing-with-IBM-QX, Chapter 12
Chapter 12 Shor’s Algorithm Factoring large integers disrupts cryptography n n Efficiently factors an integer into two integers Practical use is factoring a large integer, a product of two primes, into the two primes Potentially cracks modern encryption systems like RSA that rely on the fact that factoring large integers is computationally difficult Potentially causing a huge disruption in the way we conduct our digital lives
Chapter 12 Shor’s Algorithm Shor’s algorithm overview n Important math concepts n Divisor – a number’s divisors divide into it evenly n n Coprime – two numbers are coprime if they don’t share any divisors besides 1 n n Divisors of 6 are 1, 2, 3, and 6 25 and 12 are coprime – only share divisor 1 Greatest Common Divisor (GCD) between two numbers is the largest divisor they both share n GCD of 6 and 21 is 3 – because divisors of 6 are 1, 2, 3, 6 and divisors of 21 are 1, 3, 7, 21
Chapter 12 Shor’s Algorithm Shor’s algorithm overview n n Given an integer N, we want to find a prime integer P between 1 and N where N/P = Q is also a prime integer The intuitive idea – a quantum computer might be useful in computing the factorization of N if we have two sine waves of period P and period Q, then at a point at length N the phase of P and Q should be zero, see figure
Chapter 12 Shor’s Algorithm Shor’s algorithm overview
Chapter 12 Shor’s Algorithm Shor’s algorithm overview n Factorization relates to finding the period of two sign waves. n n Given N = 35, we translate the factorization problem to finding two waves having a phase difference of zero at multiples of 35 on the x axis, then the period of those waves are the factors of N Shor's algorithm rewrites the prime factorization problem to involve period finding n For math details see Nielsen and Chuang book
Chapter 12 Shor’s Algorithm Shor’s algorithm described Portion in red done on a QC 1. Pick a random integer a, so that a < N. 2. Compute g = gcd(a, N): 3. If g ≠ 1, g is a factor of N, so we're done: two factors of N are g and N/g. 4. Or else find the period of f(x) =ax (mod N). That is, find the integer r after which f(x) repeats itself, meaning f(x) = f(x+r) or ax (mod N) = a(x+r) (mod N): that is, a r=1(mod N) 5. If r is odd, go back to the beginning. 6. If a r/2(mod N)=-1(mod N), go back to beginning. 7. Great! Two factors of N are gcd(a r/2 + 1, N ) and gcd(a r/2 - 1, N ). If these factors are simply 1 and N, go back to the beginning because we want more interesting factors. Note: if a r=1(mod N) and r is even, then (a r/2 + 1)(a r/2 - 1)=(a r-1) =0(mod N)
Chapter 12 Shor’s Algorithm Shor’s algorithm example N=15, a=6 1. Pick a random integer a, so that a < 15. Our code picked a = 6 in this example. 2. Compute g = gcd(a, N) = gcd(6, 15) = 3: 3. Since g = 3 is not one, we have found a non-trivial factor already. Two factors are g = 3 and N/g = 15/3 = 5. Check 3*5 = 15
Chapter 12 Shor’s Algorithm Shor’s algorithm example N=15, a=2 1. Pick a random integer a, so that a < 15. Our code picked a = 2 in this example. 2. Compute g = gcd(a, N) = gcd(2, 15) = 1: 3. Since g = 1, we have to keep going! 4. Find period of f(x) = ax (mod N) = 2 x (mod 15). This period is r = 4: 5. r is even, so we keep going! 6. ar/2(mod N)= 24/2(mod 15) = 4 ≠ 1 we keep going! 7. We've found two factors of 15. They are gcd(ar/2 + 1, N) = gcd(24/2+1, 15) = 5 and gcd(ar/2 + 1, N) = gcd(24/2 - 1, 15) = 3 Check 5*3 = 15
Chapter 12 Shor’s Algorithm Shor’s algorithm example N = 2257 1. Pick a random integer a, so that a<2257. We pick a=1344 2. Compute g = gcd(a, N) = gcd(1344, 2257) = 1: 3. Since g = 1, we have to keep going! 4. Find period of f(x) = ax (mod N) = 1344 x (mod 2257). This period is r = 180: 5. r is even, so we keep going! 6. ar/2 = 1344180/2(mod 2257) = 1768. Since 1768 ≠ -1 (mod 2257) = 2256, we keep going! 7. We've found two factors of 2257. They are gcd(ar/2 + 1, N) = gcd(1344180/2+1, 2257) = 61 and gcd(ar/2 + 1, N) = gcd(1344180/2 - 1, 2257) = 37 Check 61*37 = 2257
Chapter 12 Shor’s Algorithm Shor’s algorithm in Python
Chapter 12 Shor’s Algorithm Shor’s alg: classical implementation n To find the period of f(x)=ax(mod N), we need to find the points along x that are the closest together for which the function repeats itself We know f(0) = a 0 (mod N) = 1, so starting there, let's find the smallest x where the function is 1 again Code
Chapter 12 Shor’s Algorithm Shor’s alg: classical implementation n Run classical alg for N = 7, 15, 2257, and 837 n Output of two runs, 837 isn't product of primes
Chapter 12 Shor’s Algorithm Shor’s alg: quantum implementation n Our quantum goal is to implement step 4 of the algorithm and solve the following problem: n n n Find the period of f(x) = ax (mod N) by finding the integer r after which f(x) repeats itself, that is, f(x) = f(x+r) or ax (mod N) = a (x+r) (mod N) The classical algorithm starts at x = 1 and counts up until ax (mod N) = 1 when x = r. With quantum parallelism, we don’t need to count up but will be able to find r in a single step using Quantum Fourier Transform. See https: //arxiv. org/pdf/1507. 08852. pdf
Chapter 12 Shor’s Algorithm Shor’s alg: quantum implementation 1. Create a counting up quantum register x: the Hadamard gate operating on each of the k qubits to create a superposition between "0" and "1" 2. Design a quantum circuit to compute f(x) = ax (mod N). We could do this by mapping the problem onto classical logic gates, and then directly translating each gate into a quantum gate. This would result in a lot of qubits and gates. Another option is to find optimizations to reduce the number of qubits required. 3. We operate our f(x) circuit on our quantum register x. Then, we measure f(x) and see the result. Imagine that the result is f(x) = C. This will collapse the state of x into an equal superposition over all possible x's that could have led to f(x) = C. Since f(x) is periodic, these values will all differ from each other by multiples of the period. 4. Our final task is to extract information about the period from the quantum register x that now contains the superposition over all possible numbers that could have led to f(x) = C. To do that, we will take the Quantum Fourier Transform of the state x, which will cause the result to have peaks in amplitude around multiples of the period. Measuring this result will extract the period.
Chapter 12 Shor’s Algorithm Shor’s alg: quantum case N=15, a=2 n n The steps involve designing a special quantum circuit for each a < N that the classical algorithm might randomly choose. To simplify our example, assume that the classical portion of the algorithm randomly chooses a = 2 Pick a random integer a, so that a < 15. Our code picks 2 Compute g = gcd(a, N) = gcd(2, 15) = 1: Since g = 1, we have to keep going! Dispatch the problem of finding the period of f(x) = ax (mod N) = 2 x (mod 15) to a quantum computer 1. 2. 3. 4.
Chapter 12 Shor’s Algorithm Shor’s alg: quantum case N=15, a=2 n n n The naive solution of converting classical gates to quantum equivalents works but is not practical To implement the quantum component of Shor's algorithm on a practical quantum computer we will need to optimize the naive implementation to use fewer gates and qubits. Such optimizations rely on mathematical details and academic intricacies that are beyond the scope of this book. Instead, we prepare you to read a description of an implementation of such an optimization and from this description create an implementation and test the implementation you have created.
Chapter 12 Shor’s Algorithm Shor’s alg: quantum case N=15, a=2 n n Just as we skipped the detailed math for Quantum Fourier Transform and focused on translating a circuit depicting the optimized algorithm to the IBM QX, in this spirit we will find if an optimization to create the circuit for f(x) = ax (mod N) which will work for us. The paper https: //arxiv. org/pdf/1507. 08852. pdf depicts circuit c in figure 1 the case of a = 2, a = 7, a = 8, or a = 13, and circuit b of figure 1 the case of a = 11, and several qubits are reused. Instead of this, I've added additional qubits for clarity and redrawn the circuit diagram switching the order of the qubits
Chapter 12 Shor’s Algorithm Shor’s alg: quantum case N=15, a=2
Chapter 12 Shor’s Algorithm Shor’s alg: quantum case N=15, a=2 n n n This is a semiclassical quantum computing circuit – it contains classical bits controlling quantum gates The rest of this chapter is dedicated to implementing this circuit diagram on IBM QX. It's a wonderful culmination to all the material in the book, that is, to be able to reference a research paper and implement its results on IBM QX. Reading quantum circuit diagrams, reorganizing and redrawing them for clarity and purpose, and implementing them is a skill to take away from this book and it will be broadly applicable to any future quantum programming work.
Chapter 12 Shor’s Algorithm Subroutine to find g(x)=2 x(mod 15) n n In the circuit diagram, we see that we will need to implement a circuit for computing g(x) = 2 x (mod 15) Although we won't go into detail as to the full mathematics behind the optimized circuit, for the g(x) subcircuit in this section, we will trace why it works mathematically so that the generalization to other values might make more sense and seem less random.
Chapter 12 Shor’s Algorithm Subroutine to find g(x)=2 x(mod 15) n n The easiest quantum circuit to write conceptually is to simply take a classical program to compute g(x) = 2 x (mod 15), figure out which series of universal classical gates correspond to this program, and then convert to quantum gates. This approach will end up with a gigantic number of qubits, which isn't good for quantum computing. Instead, we will use some mathematical tricks to minimize the computation.
Chapter 12 Shor’s Algorithm Subroutine to find g(x)=2 x(mod 15) n n The first thing to realize is that since we are always taking 2 x (mod 15), the result g(x) will always be between 0 and 14. Instead, we will use some mathematical tricks to minimize the computation. The second thing to realize is that there is potential for cycles in the results. Here is one example: n n n 2*1 2*2 2*4 2*8 mod mod 15 15 = = 2 4 8 1 This can be written as a map: 1 | 2 | 4 | 8 | 1
Chapter 12 Shor’s Algorithm Subroutine to find g(x)=2 x(mod 15) n Here’s another example which can be written as the map: 3 | 6 | 12 | 9 | 3 n n n 2*3 mod 15 = 6 2*6 mod 15 = 12 2*12 mod 15 = 9 2*9 mod 15 = 3 All the possible cycles in the values of x are as follows (called a modular multiplication map): n n 1 3 5 7 | | 2|4|8|1 6 | 12 | 9 | 3 10 | 5 14 | 13 | 11 | 7
Chapter 12 Shor’s Algorithm Subroutine to find g(x)=2 x(mod 15) n To create the correct circuit for f(x) = 2 x (mod 15), we would need to do the following: 1. Find the groups of repetitions 2. Convert decimal to binary 3. Create an algorithm that changes one binary to the next, as in the example, consistently n The map in binary is 1 3 5 7 n | | 2 | 4 | 8 | 1 is written in binary as 0001 | 0010 | 0100 | 1000 | 0001 6 | 12 | 9 | 3 is written in binary as 0011 | 0110 | 1100 | 1001 | 0011 10 | 5 is written in binary as 0101 | 1010 | 0101 14 | 13 | 11 | 7 is in binary 0111 | 1110 | 1101 | 1011 | 0111 From the obvious patterns, just swapping the position of 1 s and 0 s might work
Chapter 12 Shor’s Algorithm Subroutine to find g(x)=2 x(mod 15) n The following quantum algorithm works to get from any starting input on the modular multiplication map to a result: n n n n 1. Convert decimal to binary 2. Translate each bit into a qubit 3. Swap the 0 th qubit and the third 4. Swap the 0 th qubit and the first 5. Swap the first and the second 6. Measure qubits 0, 1, 2, and 3 and place the result in a classical bit register 7. Convert the resulting bits to decimal to read off the result in decimal
Chapter 12 Shor’s Algorithm Subroutine to find g(x)=2 x(mod 15) n Let's see an example of g(6): n n n n 1. 2. 3. 4. 5. 6. 7. 6 is 0110 in binary Translate 0110 into |"0110"> Swap the 0 th qubit and the third to get |"0110"> Swap the 0 th qubit and the first to get |"1010"> Swap the first and the second to get |"1100"> Measure |"1100"> to get 1100 Convert 1100 to decimal to get the result: 12 Sure enough, if we look up 6 on the preceding modular multiplication map, we get 12, the right result. We can verify this for any input.
Chapter 12 Shor’s Algorithm Subroutine to find g(x)=2 x(mod 15) n n We use a quantum SWAP gate to exchange two qubits, implemented with three CNOT gates: To swap the 0 th qubit and the 1 st qubit, we use the following code in Qiskit, assuming the qc variable referred to a quantum circuit and the qr variable to a quantum register containing at least two qubits: qc. cx(qr[1], qr[0]) qc. cx(qr[0], qr[1]) qc. cx(qr[1], qr[0])
Chapter 12 Shor’s Algorithm Subroutine to find g(x)=2 x(mod 15) n Thus, to implement steps 3, 4, and 5, involving the qubit swaps, we would code the following in Qiskit: def mult_2 mod 15_quantum(qr, qc): # Swap 0 th qubit and 3 rd qubit qc. cx(qr[0], qr[3]) qc. cx(qr[3], qr[0]) qc. cx(qr[0], qr[3]) # Swap 0 th qubit and 1 st qubit qc. cx(qr[1], qr[0]) qc. cx(qr[0], qr[1]) qc. cx(qr[1], qr[0]) # Swap 1 st qubit and 2 nd qubit qc. cx(qr[1], qr[2]) qc. cx(qr[2], qr[1]) qc. cx(qr[1], qr[2])
Chapter 12 Shor’s Algorithm Subroutine to find g(x)=2 x(mod 15) n n In the notebook provided with this chapter, there is additional code to check this quantum algorithm as run on IBM QX (where prior to running, an input is initialized by setting the qubits in binary equal to the value of x as in step 2) against the classical implementation in Python, which is 2*x%15. The Jupyter Notebook for this chapter is available at https: //github. com/Packt. Publishing/Mastering. Quantum-Computing-with-IBM-QX, under Chapter 12
Chapter 12 Shor’s Algorithm Full algorithm implementation n We will first need to make our mult_2 mod 15_quantum algorithm controlled by an additional qubit to fit the circuit. That function performs qubit swaps between three different qubits. Luckily, there is already a controlled swap gate called cswap in Qiskit we can rely on to control the computation, so all we have to do is rewrite each of the three swaps using cswap. Porting the mult_2 mod 15_quantum function to controlled_mult_2 mod 15_quantum then goes as follows:
Chapter 12 Shor’s Algorithm Full algorithm implementation
Chapter 12 Shor’s Algorithm Full algorithm implementation n Refer back to circuit for rest of the implementation:
Chapter 12 Shor’s Algorithm Full algorithm implementation
Chapter 12 Shor’s Algorithm Recommend running Jupiter notebook code n n Although there a few additional sections of this chapter, we have covered the main sections For those interested in a better understanding of Shor’s algorithm, we highly recommend running the code available in the associated Jupiter notebook.
- Slides: 36