An Experimental Study of Shor's Factoring Algorithm on IBM Q
Mirko Amico, Zain H. Saleem, Muir Kumph

TL;DR
This paper reports an experimental implementation of Shor's factoring algorithm on IBM's superconducting quantum chip for small numbers, using optimized circuits and a novel method to determine the period without continued fractions.
Contribution
It demonstrates a practical realization of Shor's algorithm on current quantum hardware with reduced qubit count and gate complexity, and introduces a new approach to period estimation.
Findings
Successful factorization of small numbers on IBM Q hardware
Use of semi-classical Fourier transform to minimize qubits and gates
Quantitative error estimation via statistical overlap
Abstract
We study the results of a compiled version of Shor's factoring algorithm on the ibmqx5 superconducting chip, for the particular case of , and . The semi-classical quantum Fourier transform is used to implement the algorithm with only a small number of physical qubits and the circuits are designed to reduce the number of gates to the minimum. We use the square of the statistical overlap to give a quantitative measure of the similarity between the experimentally obtained distribution of phases and the predicted theoretical distribution one for different values of the period. This allows us to assign a period to the experimental data without the use of the continued fraction algorithm. A quantitative estimate of the error in our assignment of the period is then given by the overlap coefficient.
Peer Reviews
No public reviews on file for this paper yet. If you reviewed it on a platform where reviews are public (OpenReview, ICLR, NeurIPS, ICML), you can paste yours below so the community can read it here.
Videos
No videos yet. Explain this paper in a talk, walkthrough, or lecture? Add one.
An Experimental Study of Shor’s Factoring Algorithm on IBM Q
Mirko Amico
The Graduate School and University Center, The City University of New York, New York, NY 10016, USA
Zain H. Saleem
Theoretical Research Institute of Pakistan Academy of Sciences, Islamabad 44000, Pakistan
Muir Kumph
IBM T.J. Watson Research Center, Yorktown Heights, NY 10598, USA
Abstract
We study the results of a compiled version of Shor’s factoring algorithm on the ibmqx5 superconducting chip, for the particular case of , and . The semi-classical quantum Fourier transform is used to implement the algorithm with only a small number of physical qubits and the circuits are designed to reduce the number of gates to the minimum. We use the square of the statistical overlap to give a quantitative measure of the similarity between the experimentally obtained distribution of phases and the predicted theoretical distribution one for different values of the period. This allows us to assign a period to the experimental data without the use of the continued fraction algorithm. A quantitative estimate of the error in our assignment of the period is then given by the overlap coefficient.
I Introduction
Shor’s factoring algorithm shor is a well known example of a quantum algorithm outperforming the best known classical algorithm. Experimental implementation of the algorithm with physical qubits however remains a challenge because of the errors introduced by the large number of qubits and gates required to execute the algorithm. In this article we provide a proof-of-principle demonstration of a compiled version of Shor’s factoring algorithm to factor the numbers , and using five, six and seven superconducting qubits, respectively. Similar experiments have been done on setups like NMR vandersypen , trapped ions monz , photons lu ; lanyon ; lopez , photonic chips politi and superconducting qubits lucero ; coles . However, with the exception of Refs. monz ; lopez , all these realizations involve an oversimplified version of the algorithm which is equivalent to coin flipping smolin and no quantum hardware is needed to obtain the same results.
In our implementation, classical processing is used alongside quantum computation to overcome the lack of key-functions of the device used. Furthermore, the number of physical qubits and the circuit depth are reduced to the minimum in order to minimize the effects of noise. The data is presented as estimates of the probability distribution of the values returned by the period register. While obtaining the probability distributions of the period register requires running the algorithm many times, as opposed to just once with the original continued fraction expansion, this allows the performance of quantum computers running this algorithm to be more directly evaluated. To measure the success of the experiments in different ways, the results are analyzed in both a qualitative way, with probability plots kleiner , and a quantitative way, with the square of statistical overlap (SSO) chiaverini . Probability plots are a useful tool to visualize differences between probability distributions, while the SSO provides a quantitative measure of their similarity. Using the the overlap coefficient (see below), we can also use the SSO to assign a period to the experimental data, avoiding the continued fraction algorithm which does not work for such low number of qubits. Also, the overlap coefficient (OVL) gives an estimate of the probability that the experiment succeeded. The results of the experiments are in good agreement with the theory for and . However, the experiment succeeded for only about 14% of the time, where the cumulative errors coming from the high number of two-qubit gates became too large.
The article is organized in the following way. A brief overview of Shor’s factoring algorithm is given in Sec. II. Sec. III describes the hardware used for the experiment. In Sec. IV the implementation of the factoring experiment for , and , respectively, is described. The results obtained from running the algorithm on the ibmqx5 quantum processor are analyzed and discussed in Sec. V. Conclusions follow in Sec. VI.
II Overview of Shor’s factoring algorithm
The factoring algorithm invented by P. Shor shor relies on the relation between the problem of factoring and the problem of order finding, for which a quantum speed-up exists. In fact, finding the prime factors of a number is equivalent to finding the exponent for which the function , where is an integer smaller than picked at random. Such exponent is called the order, or period, of . Let us briefly review the quantum part of the algorithm before diving into the details of the experiment. Two quantum registers are needed for the computation. One register is used to store the value of the period, called period register, and the other to store the results of the computation, called computational register. The size of both registers depends on the number to be factored. In particular, the period register should have a number of qubits in the interval and the computational register should be large enough to be able to represent the number , resulting from the modular exponentiation function (MEF) , thus requiring qubits.
At the beginning of the quantum algorithm, the two registers are initialized to the state , where the subscripts and denote the period register and the computational register, respectively. The period register stores all the possible values of the exponent , which will give an estimate of the period, by creating a uniform superposition of all possible bit-strings through Hadamard gates on all qubits , where . While the computational register stores the results of the MEF, . After the first step, the two registers are in the state . Then, the quantum Fourier transform (QFT) is applied to the period register so that . As a result of the QFT, interference between all the possible states occurs. If the period register is then measured, a value of the phase is measured with probability . Rewriting in terms of the period as , where and are integers, the probability of an outcome can be written as . Clearly, a value of such that , where is an integer, will be observed with high probability.
The final part of the algorithm involves classical processing of the measurement obtained in the quantum part. The value of the period can be found from the fraction by using the continued fraction algorithm. Or as done in this paper, by running the algorithm many times to get a direct estimate of the probability distribution of the values for the period register. A comparison between the measured probability distribution and the theoretically predicted distribution for the period can be made using the SSO and the best fit gives the most likely period. If the period calculated in this way is odd or , the algorithm fails and one restarts by picking a different base . If is even, can be factored into . The final step is to check if has a common divisor with by checking that gcd. If that’s true, then the two factors of are gcd and gcd.
As mentioned earlier, the execution of this version of the algorithm requires qubits in the computational register to perform the modular exponentiation and at least another qubits in the period register to perform the QFT. Thus the complete algorithm requires a total number of qubits. Even the factoring of a number as small as needs qubits in the input register to execute this algorithm, which is still a challenge for today’s physical realizations of quantum computers. However, Kitaev kitaev observed that for the purpose of algorithms like Shor’s, where one doesn’t need the information on the relative phase of the output states but only their measured probability amplitudes, one can replace the fully coherent quantum Fourier transform with the semi-classical quantum Fourier transform (sc-QFT). In the sc-QFT, one of the qubits of the period register is measured each time. The result of the measurement of the qubit is then used to determine the type of measurement on the next one. This enables the replacement of the qubits of the period register with a single qubit measured multiple times. For the case of factoring , Kitaev’s approach reduces the total number of qubits required to and for the case of and to and , respectively, which are small enough numbers for the presently available hardware to handle. This decrease in the system size, however, comes with the drawback of requiring in-sequence single-qubit readout and state re-initialization together with feed-forward of gate settings based on previous measurement results. The implementation of the sc-QFT has been described in griffiths ; chiaverini and realized in monz . At present the IBM quantum computer doesn’t perform in-sequence single qubit read out and qubit re-initialization. Below, we provide a procedure for going around this hurdle to implement the sc-QFT on the IBM Q device.
III Hardware
We use the IBM ibmqx5 chip with sixteen superconducting qubits to implement our experiments for factoring the numbers , and . The qubits are distributed on the plane, as two adjacent arrays of eight qubits each with couplings shown in Fig. 1.
The qubits’ relaxation time ranges from s and their dephasing time from s. The single-qubit gates have a high fidelity, measured at at the time of the experiment. The multi-qubit gate fidelity was measured around depending on the pairs of qubits considered. All gate errors are measured using simultaneous randomized benchmarking. Another source of error comes from the read-out of the states of the qubits, which amounts to roughly an error of . Using these parameters, the effects of noise can be incorporated in the simulation, obtaining a more accurate prediction for the output of the device.
IV Experiment
Following the example given in monz , we implement the quantum part of Shor’s factoring algorithm using the circuit depicted in Fig. 2. As can be seen in the circuit diagram in Fig. 2, rotations of the control qubit depend on the outcome of each of its measurement in the previous steps. Since the ibmqx5 chip does not allow for qubit reset and conditional operation based on measurements, which are required to implement the sc-QFT suggested by Kitaev, we implement the algorithm as three separate quantum circuits as shown in Fig. 3.
In the first circuit, the system is initialized in the state and the first bit, , encoding the value of the period is measured at the end. In the second circuit, the initial state is prepared. Different states are prepared depending on the value of measured in the previous circuit. Rotation gates on the period register are also inserted conditional on the value of before measuring the second bit encoding the value of the period, . In the third circuit, depending on the values of and , the qubit registers are initialized to and rotation gates are inserted before the measurement of . The possible quantum states of the computational register can be computed classically for the full algorithm, conditional on the measurement results of the period register. This is just the result of successive modular exponentiation. At the beginning of each circuit, except the first one, there are two possible states of the computational register that have to be prepared depending on the value of the period register measured in the previous stage. If the measurement of the period register gives [math], then the computational register is prepared to the state . If the period register gives , the computational register is initialized to . This means that for an implementation with stages, a superposition of product states have to be prepared. However, the state at the -th stage is, at worst, the result of modular exponentiations. Thus, breaking the circuit in this way, only adds an extra number of gates which is polynomial in the number of stages : , due to the gates needed for the state initialization at each stage. This retains the scalability of the implementation given in Ref. monz .
In the following experiments, we limit ourselves to the choice of one, or two, bases to avoid redundancy. We specifically choose a non-trivial base (in the sense of smolin ) for which a working quantum processor is needed to find the results. One could adopt the same approach to treat any such non-trivial bases. To understand what happens in the case of a trivial base, consider factoring . The possible periods for any of the bases are all powers of two. This means that any even value of the phase measured from the period register will give a fraction proportional to which always allows one to find the period. In facts, by analyzing the state of the quantum registers along the circuit, it is possible to see that no quantum interference happens between the states in the computational register. Therefore, in this case the correct results can be obtained regardless of the quality of the entangling gates of the device, as long as one can entangle the period register with the computational register. To show that the quantum processor ibmqx5 is giving us the correct answer by exploiting quantum interference it is sufficient to run the experiment for one of the possible bases. This is in turn is related to the quality of the entangling gates and the noise of the device. Thus, the ability to factor higher and higher using a non-trivial base (one which has a period that is not a power of 2) gives a benchmark of the performance of the device.
In the experiment for the case, five input qubits are required. One qubit initialized to for the period register, acting as a control qubit, and all other qubits initialized to the state belonging to the computational register. Alongside the quantum registers, we also need a three-bit classical register to store the results of the measurement of the control qubit, which encodes the value of the period.
The case is the simplest possible case and it does not provide an example where quantum interference between the states of the computational register brings an advantage to the computation. For this reason, we attempt to factor the second smallest number which is a product of two primes, . In this case, there are bases for which the period is not a power of two, thus constructive quantum interference between states in the computational register is needed to increase the likelihood of finding the correct result. An example of such case was first demonstrated in Ref. lopez .
We implement an algorithm for factoring with base using three bits of precision for the estimation of the phase which encodes the period. In this case the quantum register is composed of five qubits in the computational register and one qubit in the period register. We adopt the same methodology used previously, breaking each stage of the modular exponentiation and manually feeding the output of each section as input to the next. This means that the circuit will have three stages of modular exponentiation, where a single bit of the phase which encodes the period is estimated at each stage (details in Appendix A). Therefore, the circuit looks like the one in Fig. 3. The modular exponentiation circuit are specifically designed to calculate , where we choose the base . This base has periods , thus cannot easily be represented in binary. Therefore, the accuracy of the estimation of the period depends on the number of bits used for the phase estimation.
The same method is applied to factor with base . In this case we need six qubit in the computational register and one qubit in the period register. As in the case of , the period of is , therefore cannot be easily represented in binary. As a result of running the quantum algorithm we obtain a probability distribution for the estimated phase which is peaked around the multiples of . We use a three-bit register for the estimation of the phase which encodes the period. Again, the circuit for running the algorithm is realized as shown in Fig. 2, each stage, estimating one bit of the phase, is implemented separately and then joined through a classical algorithm. The individual circuits which compute the MEF at the different stages can be found in Appendix A.
V Results and Data Analysis
Figs. 4a, 5a, 6a and 7a show the results obtained running the quantum part of the factoring algorithm on the ibmqx5 superconducting device. Depicted are the experimental relative probabilities found (in blue) side by side with the expectation values which can be computed theoretically (in green) for each value of the estimated phase for the bases used. The algorithm was run 1000 times for each base.
The success of the experiment is evaluated in two different ways. We use probability plots to give a qualitative estimation of the correctness of the results, while the square of the statistical overlap (SSO) is used as a quantitative measure. Probability plots kleiner are a useful tool to visually compare two distributions. In a probability plot, one distribution is plotted against the other. If the two distributions are identical, the plot will show a straight line (). The amount of deviation from the straight line is an indication of the difference between the two probability distributions plotted. For the case at hand, this means plotting on the plane a point for each value of the phase, where the value of the coordinate is given by the theoretical value of the probability distribution for that phase and the value of the coordinate is given by the corresponding experimental value found. The data are then fitted with a straight line for comparison with the ideal case. Error-bars on the fit are given as a range of y-values compatible with the error on the fit coming from both slope and offset of the fitted line at a fixed x-value. Thus, all straight lines contained within these error-bars are compatible with the experimental data within the estimated error for the fit. The probability plots between the experimental distribution and the expected theoretical one for each case are shown in Figs. 4b, 5b, 6b and 7b. In the case of , the data in Figs. 4b and 5b are on a straight line very close to the line (tagged as ”Ideal” on the plots). For the case, the data lie on a straight line parallel to the ideal line as can be seen from Fig. 6b. This means that there is an offset in the relative frequency of each phase in our experimental distribution. However, the overall shape coincides with the theoretical one, indicating that the difference in relative frequencies between phases is preserved. Finally, for , the data in Fig. 7b lie on a straight line which is very far from the line, indicating an important deviation of the experimental results from the theoretically expected ones. In facts, looking at the histogram in Fig. 7a shows that the experimental results were affected by noise, which tends to make all phases equally probable. In summary, for and the fit is close to the ideal line (within the error bars) but for it is not. Therefore, we believe that the probability plots provide a good qualitative measure of the similarity between probability distributions, as they correctly describe the similarity which is apparent by the comparison of the histograms of the distributions.
Next we give a quantitative measure of the correctness of the results. In particular, we want to answer the question: given the experimental data obtained, what is the likelihood that this data comes from a given probability distribution? The answer to this question will reveal two aspects of our experiment. First, it will allow us to assign a period to the results without the need for the continued fraction algorithm. Second, it will give us a measure of the error we make in the assignment. Our method of assigning the period to the experimental data relies on the following observation: the probability of obtaining a certain phase is
[TABLE]
independently of the base chosen. As a function of the estimated phase , the probability distribution is completely characterized by the values of the parameters and , the period and the number of bits used to encode the value of the period, respectively. Therefore, independently of the base chosen, there correspond a fixed probability distribution to each value of and . To determine the period to assign to the experimental data, we compare the probability distribution obtained experimentally, with all the possible probability distributions given by values of from to , for fixed number of bits encoding the period. The period of the theoretical distribution which is most similar to the experimental data is then assigned to the experiment.
Following monz , we use the square of the statistical overlap (SSO) introduced in chiaverini as a measure of similarity between probability distributions. The SSO is defined as
[TABLE]
where and are the measured and expected output-state probabilities of state , respectively.
One can calculate the error on the SSO from the Poissonian counting error of the data, assuming Gaussian propagation of errors
[TABLE]
For each base used in the experiments, we calculate the SSO of with all possible . To better visualize which most resembles the data, we plot unit area normalized Gaussian distributions with the SSO as the mean and as the standard deviation. The Gaussian whose value of the mean is closest to 1 comes from the most similar to . Therefore, we assign period to our data. While the spread of each Gaussian gives an indication of the error in the calculation of the SSO. To quantitatively determine the error in the assignment of the period, we calculate the area of overlap between the Gaussian distribution with the highest SSO and the second closest one. This is done through the overlap coefficient inman between the normal distributions. The OVL is defined as
[TABLE]
where is the normal distribution with the highest SSO and is the normal distribution with second highest SSO. The OVL tells us what is the probability that the assignment is done incorrectly i.e. the highest SSO for our experimental data comes from a different theoretical probability distribution than the assigned one. Thus, we quantify the error on our assignment as where denotes the period of the distribution with the highest SSO and the period of the distribution with the second highest SSO.
The results of the comparison for all experiments are presented in Figs. 4c, 5c, 6c, and 7c. Figs. 4c and 5c show the SSO of the experimental distributions and their deviations obtained for , and , respectively. For , the highest SSO is for the theoretical distribution corresponding to the period . Thus, we assign the period to the experimental distribution obtained. The error we make in assigning the period instead of period , which is the closest match, is . For , the highest SSO is which correspond to . The error in the assignment of with respect to , which has the second highest SSO, is . The results obtained for with are shown in Fig. 6c. Here, it is more difficult to determine the period with certainty. The highest SSO is , which corresponds to the theoretical distribution with . The error in assigning to the experimental data is . Therefore, there is a chance that we assigned the period incorrectly and the true period was instead. For the case of and , the results presented in Fig. 7c show that the highest SSO between the experimental data and the theoretical distribution corresponding to all possible periods is for , although this is not the expected period. There is another close match with an SSO of for , which is the correct one. The error in assigning period to the experimental data instead of is . Thus, in this case it is quite difficult to discern the correct period.
VI Conclusions
Although the results are obtained with a compiled and simplified version of Shor’s factoring algorithm, our purpose is to show a way to proceed with the implementation of generic algorithms on the approximate quantum computers available now. In practice, the non-negligible noise and the lack of key functions of the device force us to rethink how to design algorithms that can work on these machines. As it is evident from this work, one needs to supplement the deficiencies of the hardware with a more detailed theoretical analysis and classical processing. By doing so, one can reduce the length of the circuit needed to implement the algorithm, mitigating the effects of noise, and overcoming the lack of particular functions assumed for a general-purpose quantum computer. We emphasize that the simplification by inspection done here was possible only due to the small size of the circuit. Larger circuits would require a more sophisticated optimization. We used different methods to evaluate the success of the experiment. The first one is the probability plot, which gives a qualitative measure of the similarity between the distribution of the experimental data and the expected theoretical distributions. The second one is the SSO, which gives a quantitative measure of the similarity between probability distributions. By using the SSO, we introduced a new way to assign a certain period to the probability distribution obtained from the experimental data. In this way, we avoid using the continued fraction algorithm, which fails when the number of bits used to encode the value of the period is particularly low, as in our situation. To correctly quantify the error which can be made in this assignment, the OVL between different candidates for the period is calculated. Overall, the experimental results obtained from running the algorithm on the ibmqx5 device are in agreement with the theoretical expectation values. Excellent agreement is found for , while deviations from the theoretical results become more noticeable for . Eventually, the algorithm fails to factor . This is due to the cumulative errors coming from the increasing number of two-qubits gates necessary to implement the more complex MEF needed for this case.
Acknowledgements.
We acknowledge use of the IBM Q for this work. The views expressed are those of the authors and do not reflect the official policy or position of IBM or the IBM Q team. The authors are grateful to N. T. Bronn and R. Ya. Kezerashvili for the valuable and stimulating discussions.
Appendix A circuits for the MEF
Here we present the procedure used to implement the MEF in the experiments for factoring and . These were specifically designed to reduce the number of gates to the minimum and mitigate the effects of noise. To make the approach scalable, one would need an automatic way to generate the modular exponentiation circuits as proposed in Ref. monz .
The circuits used for the MEF in the experiment for factoring are shown in Fig. 8. The MEF in the first circuit of Fig. 3 shown in Fig. 8a is the identity operation for any base , making it a deterministic step. The output of the first circuit, is then fed into the second one. As shown in Ref. monz , the MEF here reduces to a very simple circuit depending on the base selected for factoring. If the base is any one of the elements of the set , the modular exponentiation function is again the identity shown in Fig. 8a and this step turns again into a deterministic step. If the base is one of the elements of the set , the MEF has the same simple circuit for any of these , which can be seen from Fig. 8b. The MEF for the two bases and for the third circuit are given in Figs. 8c and 8d, respectively.
The circuits of the MEF used in the experiment of factoring are presented in Fig. 9. The experiment was conducted only with the base , therefore all circuits have been designed only for this base. The MEF for the first circuit is shown in Fig. 9a. For the second circuit, the MEF in Fig. 9b was used. In the third circuit, depending on the values of the bits of the period register measured in the previous stages, different states are prepared as input. For this reason, different modular exponentiation circuits are designed according to the results of the measurements of the period register. The various possibilities are shown in Figs. 9c, 9d, 9e and 9f corresponding to the four possible outcomes , , and , respectively.
The MEFs implemented in the experiment of factoring are depicted in Fig. 10. The circuits are designed for the algorithm with base . The MEF for first, second and third circuits are shown in Figs. 10a, 10b, and 10c, respectively. In this case, one circuits which works for any input was designed for the MEF at each stage.
The reference list from the paper itself. Each links out to its DOI / PubMed record.
- 1(1) P. Shor, in Proc. 35th Annu. Symp. on the Foundations of Computer Science, edited by S. Goldwasser (IEEE Computer Society Press, Los Alamitos, California, 1994), p. 124-134.
- 2(2) L. M. K. Vandersypen, M. Steffen, G. Breyta, C. S. Yannoni, M. H. Sherwood, and I. L. Chuang, Nature 414 , 883 (2001).
- 3(3) T Monz, D Nigg, E A. Martinez, M F. Brandl, P Schindler, R Rines, S X. Wang, I L. Chuang, R Blatt, Science Vol 351 , 1068-1070 (2016).
- 4(4) C. Y. Lu, D. E. Browne, T. Yang, and J. W. Pan, Physical Review Letters 99 , 250504 (2007).
- 5(5) E. Martin-Lopez, A. Laing, T. Lawson, R. Alvarez, X.-Q. Zhou, and J. L. O’Brien, Nature photonics 6 , 11 (2012).
- 6(6) B.P. Lanyon, T.J. Weinhold, N.K. Langford, M. Barbieri, D.F.V. James, A. Gilchrist, A.G. White, Physical Review Letters 99 , 250505 (2007).
- 7(7) A. Politi, J. C. F. Matthews, and J. L. O’ Brien, Science 325 , 1221 (2009).
- 8(8) E. Lucero, R. Barends, Y. Chen, J. Kelly, M. Mariantoni, A. Megrant, P. O’Malley, D. Sank, A. Vainsencher, J. Wenner, T. White, Y. Yin, A. N. Cleland and John M. Martinis, Nature Physics volume 8 , 719-723 (2012).
