Quantum Algorithms and Experiment Implementations Based on IBM Q

: With the rapid development of quantum theory and technology in recent years, especially the emergence of some quantum cloud computing platforms, more and more researchers are not satisfied with the theoretical derivation and simulation verification of quantum computation (especially quantum algorithms), experimental verification on real quantum devices has become a new trend. In this paper, three representative quantum algorithms, namely Deutsch-Jozsa, Grover, and Shor algorithms, are briefly depicted, and then their implementation circuits are presented, respectively. We program these circuits on python with QISKit to connect the remote real quantum devices (i.e., ibmqx4, ibmqx5) on IBM Q to verify these algorithms. The experimental results not only show the feasibility of these algorithms, but also serve to evaluate the functionality of these devices.


Introduction
Quantum computation [Nielsen and Chuang (2002)] can be understood as the method of information processing using the physical properties of quantum states on a quantum computer. With quantum mechanics utilized in the information processing, many important research findings are proposed in recent decades, such as quantum key distribution (QKD) [Bennett and Brassard (1984); Artur (1991)], quantum secure sharing (QSS) [Liu, Chen, Xu et al. (2012) ;Chen, Tang, Xu et al. (2018) ;Liu, Xu, Zhang et al. (2019)], quantum key agreement (QKA) [Huang, Su, Liu et al. (2017) ;Liu, Xu, Yang et al. (2018)], quantum secure direct communication (QSDC) [Liu, Chen, Li et al. (2008) ;Liu, Chen, Ma et al. (2009) ;Xu, Chen and Li (2015)], quantum private comparison (QPC) [Liu, Liu, Wang et al. (2013) ; ;Liu, Liu, Chen at al. (2014) ;], quantum sealed-bid auction (QSBA) [Liu, Wang, Yuan et al. (2016); Liu, Wang, Ji et al. (2014)], remote preparation of quantum states [Liu, Chen, Liu et al. (2015); Chen, Sun, Xu et al. (2017) ;Qu, Wu, Wang et al. (2017)], quantum steganography [Qu, Cheng, Liu et al. (2019) ;Qu, Chen, Ji et al. (2018)], delegating quantum computation [Liu, Chen, Ji et al. (2017)], and quantum-based database query scheme [Liu, Gao, Chen et al. (2019); Liu, Xu, Wang et al. (2019)]. On the other hand, quantum parallelism greatly accelerates the computation of some special computational tasks. For example, Deutsch-Jozsa algorithm [Deutsch and Jozsa (1992)] can determine whether the function is constant or balanced with only one query; Grover algorithm [Grover (1996)] has a quadratic speedup to the problem of conducting a search through some unstructured search space; Shor algorithm [Shor (1999)] can factor the prime factor of large numbers in polynomial time (which makes quantum computer easy to crack the current RSA-based cryptosystems); and some quantum machine learning algorithms [Lloyd, Mohseni and Rebentrost (2013); ; Liu, Gao, Wang et al. (2019); Liu, Chen, Wang et al. (2020); Liu, Li, Zheng et al. (2019)] are also far superior to classical algorithm. However, the correctness or security verification of the above algorithms or protocols is mostly based on theoretical derivation [Childs, Kothari and Somma (2015), Pan, Yu, Yi et al. (2019)] or experiment simulations [Vandersypen, Steffen, Breyta et al. (2001)]. With the release of quantum cloud computing platform in recent few years, such as D-Wave Leap [Dwave (2018) [Xin, Huang, Liu et al. (2018)], some researchers tried to verify quantum protocols or algorithms on the real quantum computers. In 2017, Gangopadhyay et al. [Gangopadhyay, Manabputra, Behera et al. (2017)] proposed two generalization algorithms based on Deutsch-Jozsalike algorithm and demonstrated experimental verification of the first algorithm by using IBM 5-qubit device (i.e., 5-qubit IBM Q). And then Srinivasan et al. [Srinivasan, Behera and Panigrahi (2017)] verified Gaussian elimination method for solving system of equations at IBM 5-qubit device. In 2018, Roy et al. [Roy, Behera, Pan et al. (2018)] demonstrated the violation of the entropic noncontextual inequality in a four-level quantum system, by using the 5-qubit IBM Q. As far as we know, most research results of experimental verification are based on IBM 5-qubit device. Besides, their circuit design is directly carried out on the web page, which is only suitable for small-scale quantum circuits (the length of quantum circuit, i.e., maximal number of cascaded quantum gates on a single quantum line, are limited to 80 on the web page). With the increase of the scale of the problem, the feasibility and expansibility of this web mode are relatively poor. At the end of 2017, IBM released an open-source quantum computation framework, QISKit [QISKit (2018)], which allows the users to implement remote quantum experimental verification of IBM Q through localized python programming. For this kind of localized programming mode based on QISKit, the length of quantum circuit is no longer limited to 80 and the design of quantum functional circuits can be packaged in the form of functions for reusing and expansion. In this paper, we use QISKit and Forest to directly program three representative quantum algorithms, namely Deutsch-Jozsa, Grover and Shor algorithms, and connect the remote real quantum devices (i.e., ibmqx4 and ibmqx5) to verify these algorithms in real quantum computer. The remaining part of the paper is organized as follows: In Section 2, preliminaries about quantum computation, IBM Q and QISKit are briefly introduced. In Section 3, three representative algorithms, Deutsch-Jozsa, Grover and Shor algorithms, are depicted, and then their implementation circuits are presented. Subsequently, the experimental results of these algorithms are analyzed in detail in graphical form. Finally, Section 4 is dedicated for conclusion.

Quantum computation
The basic concept of the classical information world is bit. Similarly, quantum computation and quantum information are based on similar concepts: qubit. Classic bit have only one state: either 0 or 1. Corresponding in the qubit, we denote 0 and 1 as: 0 and 1 .
is called the Dirac token. The qubit can fall outside 0 and 1 . Qubit can be a linear combination of these two states, often referred to as superposition state, 0 1 .
Here, α and β represent the probability amplitude of 0 and 1 . 0 and 1 can be represented by vectors, which is shown in Eq. (2).
Then, the superposition state is represented by a vector as follows: In a geometric sense, the state of the qubit is required to be normalized to length 1, which means 2 2 1 α β + = .
A quantum computer is built from a quantum circuit containing wires and elementary quantum gates to carry around and manipulate the quantum information. Quantum gates are divided into single qubit gates and multiple qubit gates. Quantum gate can all be represented in the form of a matrix U . The unitary limit ( † U U I = , where † U is a conjugate transpose of U , obtained by U transpose and complex conjugate of U ) is the only limitation on quantum gates [Nielsen and Chuang (2002)]. Each valid quantum gate can be represented as a unitary matrix. For visual display, in Tab. 1 below we list some line symbols and matrix representations used in this paper. In the actual quantum circuit, we use special line symbol to represent the quantum gate, and a line symbol represents a quantum gate that can manipulate the quantum state, such as, If X is used to manipulate the quantum state = 0 + 1 ψ α β , the result of the operation can be obtained by multiplying the vector, A projective measurement is described by an observable M , a Hermitian operator on the state space of the system being observed. The observable has a spectral decomposition, where m P is the projector onto the eigenspace of M with eigenvalue m . The possible outcomes of the measurement correspond to the eigenvalues m , of the observable. Upon measuring the state ψ , the probability of getting result m is given by Given that outcome m occurred, the state of the quantum system immediately after the measurement is ( )

IBM Q
In 2016, IBM opened the IBM quantum experience prototype 5-qubit device to the public.
A year later, they announced the launch of IBM Q [IBMquantum (2017)], the industry's first initiative to build commercially available universal quantum computers for business and science. In the same year, they proposed two devices with 5 qubits named ibmqx2 and ibmqx4. In 2018, a third public device with 16 qubits (ibmqx5) was added which can be accessed using QISKit. Recently, they have announced that they successfully built and tested a 20-qubit device for their client. Meanwhile, their simulator is up to 32 qubits.
In IBM Q, all devices provide a lot of elementary gates, such as: X -gate, H -gate, cXgate (control-NOT gate), cZ -gate (control-Z gate), ccX -gate (control-control-NOT gate, namely Toffoli gate) and so on. The coupling map of ibmqx4 and ibmqx5 are shown in Fig. 1. Generally, two-qubit gates are possible between neighboring qubits that are connected by a super-conduction bus resonator. The IBM Q experience uses the crossresonance interaction as the basis for the cX -gate. This interaction is stronger when choosing the qubit with higher frequency to be the control qubit, and the lower frequency qubit to be the target, so the frequencies of the qubits determine the direction of the gate.  (2018)] is a collection of software for working with short depth quantum circuits and building near term applications and experiments on quantum computers. In QISKit, a quantum program is an array of quantum circuits. The program work flow consists of three stages: building, compiling and running.
(1) Building allows you to make different quantum circuits that represent the problem you are solving.
(2) Compiling allows you to rewrite them to run on different backends (simulators or real chips of different quantum volumes, sizes, fidelity, etc.).
(3) Running launches the jobs. After the jobs have been run, the data are collected. There are methods for putting this data together, depending on the program. In other words, QISKit includes python-based tools for creating, manipulating, visualizing and studying quantum states, tools for characterizing qubits, scripts for batch processing, as well as a compiler to map the desired experiment onto real hardware. Different from the IBM Q web page experiment mode, this kind of programming call mode can overcome the cumbersomeness of drawing complex circuit diagrams on web pages, and has the advantage of easy expansion of composite quantum gates and easy preservation of experimental data.

Deutsch-Jozsa algorithm
3.1.1 Algorithm procedure The Deutsch-Jozsa problem [Deutsch and Jozsa (1992)] is defined as follows. Consider a function ( ) f x that takes as input n -bit strings x and returns 0 or 1. The goal is to decide whether f is a constant function that takes the same value Next, the function f is evaluated using Now interfere terms in the superposition using a Hadamard transform on the query register, ( ) ( ) Note that the amplitude for the state 0 In the case where f is constant the amplitude for 0 n ⊗ is +1 or -1 , depending on the constant value ( ) f x takes. Because 3 ϕ is of unit length it follows that all the other amplitudes must be 0, and an observation will yield 0s for all qubits in the query register. If f is balanced then the positive and negative contributions to the amplitude for 0 n ⊗ cancel, leaving an amplitude of zero, and a measurement must yield a result other than 0 on at least one qubit in the query register. Summarizing, if we measure all 0 s in the query register then the function f is constant; otherwise the function f is balanced.  , then the function is constant; otherwise the function is balanced. Besides, we program it on python with QISKit which will be connected to ibmqx4 and ibmqx5. Then, the real experimental verification is implemented which can remotely connect the real quantum devices and the code for this circuit is stored on the local computer that exactly can be used again.    Fig. 6. The exact result to get 1 is 1. Due to noise interference, equipment performance and experimental results will be affected. Then, we can find that the results of ibmqx4 are stable around 0.86, while the results of ibmqx5 are stable around 0.95. Comparing the mean and variance of each group of data, we can find that ibmqx5 has higher stability and computing performance.

Grover algorithm 3.2.1 Algorithm procedure
The problem with the Grover algorithm [Grover (1996)] can be described as follows. Search a target item from N unclassified items. A classic computer is a query until you find the target. On average, if you look up 2 N times, the probability of finding it is one half. Based on parallel processing capability of quantum computing, we only need N times, and the probability of finding it is close to 1 (Grover algorithm). Grover algorithm provides only a quadratic speed up, however, even quadratic speedup is considerable when N is large. Grover quantum search algorithm is based on the basic idea of the initial amplitude superposition of unitary transformation, the repeat application of Grover quantum iterative process is aim to suppress the probability amplitude of the non-target item and enlarge the probability amplitude of the target item to be searched. Finally, in the best case, the target item is searched by the probability of approaching 1. The detailed implementation steps of the Grover quantum search algorithm can be seen in Fig. 7. Step 1. Initialize, use H gate to produce a state of equal amplitude, and then apply 0 n n H ⊗ to get x .
Step 2. Apply the oracle reflection w U to the state, such that shown in Deutsch-Jozsa algorithm above). This transformation means that the amplitude in front of the target state becomes negative, which in turn means that the average amplitude has been lowered.
Step 3. Apply an additional reflection Step 4. Repeat Steps 2-3 N times. The action of the reflection s U in the amplitude bar diagram can be understood as a reflection about the average amplitude. Since the average amplitude has been lowered by the first reflection, this transformation boosts the negative amplitude of target state to roughly three times its original value, while it decreases the other amplitudes.

Experimental implementation and analysis
Taking 3-qubit quantum state as an example, we show the search technique of Grover algorithm. To better validate the experiment, the circuit design of the algorithm process can be as shown is shown in the Fig. 8. Besides, the equivalent circuit of Toffoli gate is shown in Fig. 9. , next, we apply ccZ gate as w U to specify a target quantum state (the amplitude of the target quantum state is negative), and then we can agree that 111 is the target state. Finally, apply the s U mentioned above to enlarge the amplitude of the target quantum state 111 . Then, the real experimental verification is implemented which can remotely connect the devices and the code for this circuit is stored on the local computer that exactly can be used again.
The results of running the circuit on python with QISKit can be shown in Fig. 10. The exact result to get 111 is 1. Due to noise interference, equipment performance and experimental results will be affected. Then, we can find that the results of ibmqx4 are stable around 0.53, while the results of ibmqx5 are stable around 0.69. Comparing the mean and variance of each group of data, we can find that ibmqx5 has higher stability and computing performance.

Shor algorithm 3.3.1 Algorithm procedure
Shor algorithm [Shor (1994)] is able to factor large numbers efficiently, which has the potential to undermine contemporary encryption. It consists of a quantum order finding algorithm (QOFA) which provids the order for code implementation to return the factors.
Recently, Grosshans et al. [Grosshans, Lawson, Morain et al. (2015)] proposed a quantum factoring algorithm which optimized Shor algorithm at factoring safe semiprimes. A semiprime is a product of two primes, hence finding any nontrivial factor of a semiprime amounts to finding its complete prime factorization. In number theory, Euler's totient function [Kaliski (2005)] ( ) N ϕ counts the number of integers x in the range 1 x n ≤ ≤ when the greatest common divisor ( , ) gcd N x is equal to 1. According to Miller [Miller (1976)], N can be efficiently factored after knowing both Referring to Tab. 2, there are 8 possibilities for the order of a modulo N . Since the value of a is obviously independent of the number being factored [Smolin, Smith and Vargo (2013)

Experimental implementation and analysis
QOFA is the main component of this algorithm which provides the order for code implementation. Tab. 3 lists the order r of different safe semiprime N when 2 a = .
Obviously, we can find that 6 r = . Based on the constant-optimized quantum circuits for the modular multiplication and exponentiation [Igor and Saeedi (2012)  The result of running the circuit on python with QISKit can be shown in Fig. 17 which corresponds to Eq. (14). The exact result to get 1 is 1. Due to noise interference, equipment performance and experimental results will be affected. Then, we can find that the results of ibmqx5 are stable around 0.92 (ibmqx4 has only 5 qubits which cannot realize the circuit).

Conclusions
In this paper, three representative algorithms (i.e., Deutsch-Jozsa, Grover and Shor algorithms) are studied and we design their implementation circuits based on the theoretical research and program the corresponding programs on python with QISKit which realize the remote connection to the real quantum devices (i.e., ibmqx4 and ibmqx5) on IBM Q. These experimental results show the feasibility of these algorithms and serve to assess the functionality and fidelity of these devices. From the results, we can find that the stability and computing performance of ibmqx5 are higher than ibmqx4. Different from the web page experiment mode, this kind of programming call mode, which uses the tool kit API to connect to the devices, can overcome the cumbersomeness of drawing complex circuit diagrams on web pages, and has the advantage of easy expansion of composite quantum gates and easy preservation of experimental data. Besides, we can customize the composite gates we want on python to achieve more functionality, not limited to the set of gates provided by these platforms. We will continue to study the quantum algorithms, and try to improve the quantum algorithms and design the corresponding circuits to verify the feasibility of the algorithms on the real quantum computer.