Variational quantum compiling with double Q-learning

Quantum compiling aims to construct a quantum circuit V by quantum gates drawn from a native gate alphabet, which is functionally equivalent to the target unitary U. It is a crucial stage for the running of quantum algorithms on noisy intermediate-scale quantum (NISQ) devices. However, the space for structure exploration of quantum circuit is enormous, resulting in the requirement of human expertise, hundreds of experimentations or modifications from existing quantum circuits. In this paper, we propose a variational quantum compiling (VQC) algorithm based on reinforcement learning (RL), in order to automatically design the structure of quantum circuit for VQC with no human intervention. An agent is trained to sequentially select quantum gates from the native gate alphabet and the qubits they act on by double Q-learning with \epsilon-greedy exploration strategy and experience replay. At first, the agent randomly explores a number of quantum circuits with different structures, and then iteratively discovers structures with higher performance on the learning task. Simulation results show that the proposed method can make exact compilations with less quantum gates compared to previous VQC algorithms. It can reduce the errors of quantum algorithms due to decoherence process and gate noise in NISQ devices, and enable quantum algorithms especially for complex algorithms to be executed within coherence time.


Introduction
Current available quantum devices are not large-scale and fault-tolerant, but noisy intermediate-scale quantum (NISQ) devices [1]. IBM's and Rigetti's quantum devices fall into this category. Many quantum algorithms have been implemented by parameterized quantum circuits (PQCs) on NISQ devices. Hybrid quantum-classical algorithms based on PQCs are successfully applied to attacking scaled-down problems in chemistry and combinatorial optimization [2,3,4]. Besides, they have been applied to many machine learning tasks, e.g., classification [5,6], clustering [7] and generative model [8,9]. These algorithms are not restricted to theory and simulation. They have been tested on real quantum devices such as IBM's and Rigetti's quantum devices.
Although NISQ device is a milestone in the field, there are several constraints, which separate them from fully utilizing their capabilities for a quantum advantage. The connectivities between qubits are limited, i.e., multi-qubit gates cannot apply to arbitrary qubits. Besides, the native gate alphabet is restricted. Different quantum devices have their own topologies, i.e., the arrangement of physical qubits, the interactions supported between them, and the native gate alphabet of available quantum gates. For example, IBM and Rigetti's quantum devices have different topologies. Even different quantum devices developed by IBM (e.g., 5-qubit, 16-qubit and 20-qubit devices) have different topologies. Thus, a quantum circuit cannot directly execute on NISQ devices. Similar as software must be complied to machine code in classical computers, quantum algorithms must be compiled before running on NISQ devices by taking into account the specific constraints. Quantum compiling is a crucial step in the execution of quantum algorithms on NISQ devices, which receives increasing attention in industry and academia [10,11,12]. Quantum compiling aims to construct a quantum circuit by quantum gates drawn from NISQ device's native gate alphabet, which is functionally equivalent to the target unitary. Many techniques have been proposed to successfully compile one-qubit gates [13,14,15,16]. However, compiling multi-qubit unitaries is more difficult as the exponentially increasing complexity of the problem. Hybrid quantum-classical algorithm has been applied to quantum compiling [17,18,19,20], which is referred to variational quantum compiling (VQC). In VQC, a parameterized quantum circuit is trained to be functionally equivalent to the target unitary. Heya et al proposed a gate optimization algorithm to construct a target multi-qubit gate by parameterized single-qubit gates and multi-qubit gates [17]. A circuit template with fixed gate types and sequence is used to compile a 7-qubit unitary with 186 gates based on energy dissipation [18]. Circuit design is a key problem of quantum compiling, which requires human expertise and hundreds of experimentations or modifications from existing quantum circuits. Khatri et al proposed to perform circuit design based on simulated annealing for single-qubit and two-qubit quantum unitaries [19]. However, the structure of the trainable unitary is set to the same structure as the target unitary for the target unitaries with more than two qubits. Most of current quantum compiling Variational quantum compiling with double Q-learning 3 algorithms for large-scale gate sequences use a specific template as the searching space of the optimization over circuit structure increases exponentially with the number of quantum gates [18,19,20,21,22]. However, the quantum circuits designed by this strategy might not be optimal and require more quantum gates. The performance of the quantum algorithm decreases with runtime because of the noise and limited coherence time in current NISQ devices. The increasing number of quantum gates will lead to more gate noises and increase the running time, resulting in the performance degradation. Variable structure ansatzes have been considered in [23] to discover quantum algorithms for computing the overlap between two states, which have shorter depths than the Swap Test.
In this paper, we proposed to automate the process of circuit design for quantum compiling based on reinforcement learning. An agent is trained to sequentially choose quantum gates and the qubits they act on by double Q-learning with -greedy exploration strategy [24] and experience replay [25]. In -greedy exploration strategy, the agent firstly performs a number of explorations by randomly generating quantum circuits, and then slowly transforms smoothly from exploration to exploitation to generate higher performing quantum circuits according to its findings. As the proposed VQC based on double Q-learning does not rely on human experience, it may have more potential to discover novel structures of circuits compared to VQC based on specific templates, which might inspire designers of quantum algorithms. Similar work can be found in Ref. [26], which proposed to compile single-qubit gates with elementary gates from a finite universal set based on deep reinforcement learning. However, their work is related to topological compiling and is a special case of quantum compiling by considering the action of target unitary on a fixed input state. In addition, reinforcement learning has also been applied for Quantum Approximate Optimization Algorithm problems [27].
The rest of the paper is organized as follows. Some related work is introduced in Section 2. In Section 3, we present the proposed circuit design in VQC based on double Q-learning. We report the performance of the proposed algorithm compared to current VQC by numerical simulation in Section 4. Finally, we summarize the results of this paper and discuss the future work in Section 5.

Related work
Variational quantum compiling (VQC) aims to compile a target unitary U to a trainable quantum gate sequence V which has approximately the same action as U on all input states [17,19,20]. Another kind of VQC only considers a specific input state, e.g., |ψ 0 = |0 n [18]. In this paper, we focus on the former case as it has more application scenarios.
In VQC, the trainable gate sequence V can be expressed in terms of the native gate alphabet of NISQ devices being used. The native gate alphabet A = {G k (θ)} k consists of gates G k (θ) that are native to the NISQ device. k is a discrete parameter used to Variational quantum compiling with double Q-learning 4 identify the type of quantum gate and the qubit it acts on. θ is the parameter of the quantum gate. If the quantum gate has no parameter, e.g., Hadamard gate, then θ = []. The goal of VQC is to search a gate sequence V k ( θ) = G k L (θ L )G k L−1 (θ L−1 )...G k 1 (θ 1 ) with L gates to minimize the difference between the target unitary U and V k ( θ), i.e., where k = {k L , k L−1 , ..., k 1 } is a vector of indices describing which gates are used and which qubits they act on. θ = {θ L , θ L−1 , ..., θ 1 } is a vector of continuous parameters associated with the quantum gates in k. Thus, it needs to make numerous choices to construct a quantum circuit, i.e., the number of quantum gates of each type, the qubits they act on, the ordering of quantum gates, and the parameters of quantum gates, which makes the space of quantum circuit structures extremely large and infeasible for an exhaustive manual search. C(U, V k ( θ) quantifies how close the trained unitary V k ( θ) is to the target unitary U . It can be calculated by the Hilbert-Schmidt inner product between unitaries U and V [19], i.e., where d = 2 n is the Hilbert-space dimension where n is the number of qubits in U . C(U, V ) = 0 if and only if unitary U and unitary V differ by a global phase factor. C(U, V ) can be calculated by the quantum circuit in Fig. 1 [19]. At the beginning of the circuit, a maximally-entangled state between n-qubit systems A and B is prepared with n Hadamard gates and n CNOT gates. Then the unitaries U and V * act on system A and B, respectively, where V * is the complex conjugate of unitary V . The undoing unitary is implemented with n Hadamard gates and n CNOT gates. At last, C(U, V ) is calculated by the probability of the all-zeros outcome for the Bell-basis measurement. For large problem sizes, the global Hilbert-Schimdt Test exhibits Barren Plateau phenomena [28,29]. In this paper, local Hilbert-Schmidt Test [19] is used for the large problem sizes to address this problem: where C (i) local (U, V ) is the same as the global Hilbert-Schmidt Test C(U, V ) except that only qubits i and i + n are measured, i.e., the probability of the all-zeros outcome on qubits i and i + n for the Bell-basis measurement. C local (U, V ) is efficient to compute on a quantum computer and scales well with the size of the problem [19].
The optimization over the continuous parameters θ can be performed by a gradient descent algorithm [19]. The gradient with respect to θ l of C(U, V k ( θ)) is Variational quantum compiling with double Q-learning where θ ± l = θ ± π 2 e l , and e l is the lth unit vector in the parameter space corresponding to θ l , i.e., θ l ← θ l ± π 2 , with other angles θ − {θ l } unchanged. The derivation of Eq. (4) adopts the parameter shift rule [30], which does not need any additional quantum gates and has been widely used in many hybrid quantum-classical algorithms [9,19,20]. Note that for the parameter shift rule to be valid, the generator of the parameterized gate needs to have two unique eigenvalues.
The calculation of k in Eq. (1) is an optimization over gate structures. Many current quantum compiling algorithms use a specific template [18,19,20,21]. Simulated annealing is also used for the optimization over gate structure [20,31]. However, the searching space of gate structures increases exponentially with the number of gates, i.e., the total number of quantum gates in the circuit, where simulated annealing is less efficient. In this paper, we focus on the optimization over gate structure and propose to find an optimal gate structure by reinforcement learning.

Design of quantum circuits with double Q-learning
Reinforcement learning aims to make optimal decision using experiences. Q-learning is a model-free reinforcement learning algorithm incorporating -greedy strategy and experience replay, which has been proved powerful in large state space [32,33,34,35]. The first AI program to beat a professional human Go player, i.e., AlphaGO, is fundamentally Q-learning. In this paper, we adopt Q-learning to automatically design a quantum circuit on the large space of quantum circuit structures. Q-learning maps {state, action} pair to Q-value, which is an estimation of how good it takes the action at the state. The fundamental thing of Q-learning is to iteratively update the Q-values of {state, action} pairs based on the immediate reward and the future reward after taking the action.
In this paper, we train a learning agent to sequentially choose quantum gates from the native gate alphabet until a termination state as described in Fig. 2(a). Each state is defined as a tuple of all relevant parameters, i.e., (gate, qubit, number of gates), which denote the type of the selected quantum gate, the qubit the gate acts on and (gate, qubit, number of gates) ... the number of gates in current gate sequence. In order to make sure the state-action graph is directed and acyclic, the parameter number of gates is added to the state space. Another advantage of equipping the state with number of gates is that it can limit the number of quantum gates in the compiled quantum circuit. Any state with the maximum number of gates can only transition to a termination state. The selection of a quantum gate and the qubit it acts on can be regarded as an action in Q-learning.
The number of actions in the current state depends on the native gate alphabet and the number of qubits. For example, without considering the connectivity constraint, the number of actions is np+qn(n−1) for compiling an n-qubit unitary with the native gate alphabet including p one-qubit gates and q two-qubit gates. Some restrictions should be considered to make the learning processing tractable. At first, the state with j gates is only permitted to transition to a state with j + 1 gates, which ensures that the stateaction graph is directed and acyclic. Fig. 2(a) shows feasible states, action space and the state transition from s k to s k+1 by taking action a k , where k is the number of gates in current state. The quantum circuit in Fig. 2(b) is generated by the red solid line in (a). The gate Variational quantum compiling with double Q-learning 7 selection process is a Markov decision process. The design of a quantum circuit can be regarded as an action selection trajectory τ i . The reward r of the transition from state s to s by action a may be stochastic according to a distribution p r|s,a,s . The agent aims to maximize the expected reward over all possible trajectories τ , i.e., max τ i ∈τ R τ i . The total expected reward for a trajectory τ i is Given a state s t ∈ S and subsequent action a ∈ A(s t ) where S is all possible states and A(s t ) is all possible actions in state s t , the maximum total expected reward is denoted by Q (s t , a), i.e., Q-value of the state-action pair (s t , a). Based on Bellman's equation, we can get It can be formulated as an iterative update where a = arg max a ∈A(s t+1 ) Q(s t+1 , a ). α ∈ [0, 1] is the learning rate, which determines the importance of the newly acquired information compared to old information. α = 0 means that the agent learns nothing from the newly acquired information, while α = 1 indicates the agent only considers the most recent information ignoring prior knowledge. γ is the discount factor, which makes a balance between the immediate reward and the future reward. γ = 0 means that the agent only considers immediate reward. The future reward is more important when γ is larger. r t is the intermediate reward when moving from state s t to s t+1 . When a state goes from s t to a termination state s T by an action a T , Eq. (7) can be simplified as where r T is the termination reward. It can be calculated by where C(U, V ) is measured by Hilbert-Schmidt Test of current quantum circuit and the target unitary U using the circuit in Fig. 1. The intermediate reward r t can be estimated by reward shaping [36], which can speed up the training process, i.e., r t = r T /L, where L is the number of gates in the quantum circuit. The detail process of double Q-learning for circuit design in VQC is shown in Algorithm 1. At first, Q-values are initialized by the average of the termination reward r T over a number of randomly generated quantum circuits, i.e., the quantum circuits generated when = 1. For each j ∈ , E j circuits are generated based on -greedy strategy. The parameters θ of these quantum circuits are optimized based on the gradient descent approach in Algorithm 4 of Ref. [19]. Then the reward can be calculated by Hilbert-Schmidt Test with the quantum circuit in Fig. 1, and is stored in a replay memory as well as its corresponding circuit structure. The Q-values are periodically Variational quantum compiling with double Q-learning 8 updated by a batch samples randomly drawn from the replay memory. The flowchart of Q-learning is shown in Fig. 2(c). The performance of Q-learning is not satisfying in stochastic Markov decision processes due to the overestimation of the action values resulted from using the maximum action value as an approximation for the maximum expected action value. In this paper, we use double Q-learning [37], which uses two Q-value functions Q 1 and Q 2 to determine the value of the next state. In -greedy strategy, a quantum gate is selected randomly with probability and based on Q-value with probability 1− . At first, with a high , the agent can explore different structures of quantum circuits by randomly choosing actions. As the agent explores the environment, decreases and the agent starts to exploit the environment. As two-qubit gates have much lower fidelities than single-qubit gates in current NISQ devices, it is of great significance to minimize the number of two-qubit gates in the compiled circuit [38,39]. It can be done by modifying the reward function in Eq. (9) as where C p = n CNOT /L is a penalty term which motivates the agent to use fewer CNOT gates in the designed circuit. λ is a weighted parameter which balances C(U, V ) and the number of CNOT gates in the circuit. n CNOT and L are the number of CNOT gates and quantum gates in the circuit. The searching space of the optimization over circuit structure increases exponentially with the number of quantum gates L. In many applications, L grows polynomially with the number of qubits. This issue is crucial for large problem sizes. To address this problem, we can add some common blocks to the candidate gate alphabet, which largely decreases the computation complexity of the proposed algorithm for large problem sizes. This strategy can be regarded as a crossover of variable structure ansatze and fixed-template compiling, which not only makes the algorithm scale well with the size of the problem, but also decreases the number of quantum gates in the compiled circuit.

Numerical simulation
In this section, we verify the performance of the proposed variational quantum compiling (VQC) based on double Q-learning on 2 to 8-qubit unitaries.

VQC on two-qubit unitaries
In this section, we compile five two-qubit unitaries including controlled-phase (CS), controlled-Hadamard (CH), controlled-Z (CZ), Mølmer-Sørensen XX gate with θ = 3π/2 (XX(3π/2)) and two-qubit quantum Fourier transform (QFT2) in a simulator. Similar to the previous work [19], we consider the native gate alphabet Variational quantum compiling with double Q-learning 9 Algorithm 1 Double Q-learning for circuit design in VQC Input: = { 1 , 2 , ..., m }: parameters of -greedy strategy; E j : the number of generated quantum circuits for j ; K: the size of minibatch for Q-value update; L: the maximum gate number of the quantum circuit; γ: the discount factor; α: the learning rate; A: the native gate alphabet. Output: V : the compiled quantum circuit.
1: set the replay memory M = [] and set r = 0 2: set Q 1 (s, a) = q 0 , Q 2 (s, a) = q 0 , ∀s ∈ S, ∀a ∈ A(s), where q 0 is the average of the termination reward r T over a number of randomly generated quantum circuits and the action space depends on the native gate alphabet A 3: for each j in do 4: for i = 1 to E j do 5: remark: generate the ith quantum circuit with -greedy strategy 6: set gate sequence V i = [] and current state s = 'Start' 7: for l = 1 to L do 8: with probability j select a random action a from A(s) 9: otherwise select action a = arg max a∈A(s) (Q 1 (s, a) + Q 2 (s, a)) 10: perform a transition(s = s ) from current state to s after taking action a 11: append (s, a ) to V i 12: generate a quantum circuit based on V i

13:
optimize the gate parameters θ based on the gradient descent approach in [19] 14: calculate the reward r i T according to Eq. for k = 1 to K do 20: randomly select {V k , r k T } from the replay memory 21: generate a random number y between 0 and 1 22: for each (s t , a t ) in V k do 23: if y < 0.5 then 24: a )), 25: where a = arg max a∈A(s t+1 ) Q 1 (s t+1 , a) Q 2 (s t , a t ) = (1 − α)Q 2 (s t , a t ) + α(r t + γQ 1 (s t+1 , a )), 28: where a = arg max a∈A(s t+1 ) Q 2 (s t+1 , a) where R x (π/2) and R z (θ) are one-qubit gates while CNOT is a two-qubit gate. θ is a continuous parameter of R z gate which denotes the angle to rotate around the z-axis on the Bloch sphere. In this simulation, we do not consider the connectivity constraint of NISQ devices. Thus, the number of actions (|A(s)|) for each state s is 6. We can  simply implement quantum compiling for NISQ devices with connectivity constraint by modifying the action space A(s), i.e., removing the actions which are not permitted due to the connectivity constraint. A replay memory is used to store the circuit structures and their corresponding costs C(U, V ) after each iteration in the experience replay. The parameter K indicates the agent samples K quantum circuits with their corresponding costs from the replay memory to update the Q-values. The learning rate α, the discount factor γ and the batch size K are empirical parameters. According to previous works [35,40] and the results of serval values, we set α, γ and K to be 0.02, 0.9 and 128.
In -greedy strategy, the agent either selects a quantum gate based on Q-values with probability 1 − or randomly selects a quantum gate with probability . In our experiment, decreases from 1.0 to 0.1, which makes the agent transform smoothly from exploration to exploitation. The number of quantum circuits generated for each is shown in Table 1. A large number of randomly generated quantum circuits are trained at = 1.0 in order to let the agent see more quantum circuit structures. Fig. 3 shows the minimum cost achieved by VQC based on double Q-learning as the increasing of the number of gates in the quantum circuit. The cost C(U, V ) decreases with the increasing number of quantum gates in V and finally converges to 0, i.e., U = V . For CS, CZ and XX(3π/2) gates, a quantum circuit with 5 quantum gates from the native gate alphabet can achieve zero cost. To compile CH gate, it needs at least 6 quantum gates. QFT2 is the most difficult unitary to compile which needs 10 quantum gates. The numbers of CNOT gates in the compiled circuits for CH, CS, CZ, XX(3π/2) and QFT2 are 1, 2, 2, 2 and 3, respectively. We remark that the theoretical lower bound of CNOT gates required for the implementation of an arbitrary two-qubit unitary is 3 [41,42].
We also compare the number of quantum gates in the VQC based on double Qlearning to the one based on simulated annealing [19] in Table 2 and find that we achieve an exact compilation with the same number of quantum gates. Fig. 4 shows the structures of the compiled circuits by VQC based on double Q-learning for the two-qubit unitares. The rotation angles of the quantum gates in these circuits are illustrated in Table A1 in Appendix A.

VQC on three-qubit unitaries
We firstly perform a simulation of the proposed method on fully-connected qubits in Section 4.2.1. Then the limited connectivity of qubits on IBM's 5-qubit quantum device ('Ibmq ourense') is considered in Section 4.2.2. In Section 4.2.3, a penalty term is Variational quantum compiling with double Q-learning (1) (1) (1)   [20], we consider the following three-qubit unitaries: three-qubit W-state preparation (WSP3), Toffoli gate (CCNOT) and three-qubit quantum Fourier transform (QFT3). We use the same native gate alphabet as the previous work [20], i.e., where R x (θ) and R y (θ) are one-qubit rotation gates around x-axis and y-axis. Similar to the previous section, the learning rate α, the discount factor γ and the batch size K are set to 0.02, 0.9 and 128, respectively. Compared to two-qubit unitaries, it is much more difficult to compile three-qubit unitaries as it needs more quantum gates and the searching space of the optimization over gate structures increases exponentially with the number of gates. Thus, we enlarge the number of quantum circuits in each as shown in Table 3. Fig. 5 shows the result of proposed VQC with different numbers of quantum gates in V for different target unitaries, i.e., WSP3, CCNOT and QFT3. The cost C(U, V ) decreases with the increasing number of quantum gates in the circuit V and converges to 0 for all the target unitaries. WSP3 is the easiest to compile among the three target unitaries, which needs 7 quantum gates to make an exact compilation. QFT3 and CCNOT are more difficult to compile which need 22 and 25 quantum gates, respectively. The numbers of CNOT gates in the compiled circuits for WSP3, CCNOT and QFT3 are 2, 10 and 7, respectively. We remark that the theoretical lower bound of CNOT gates required for the implementation of an arbitrary three-qubit unitary is 14 [41,42].
We also compare the performance of the circuit structure designed by the proposed method and the found structures in Ref. [20], i.e., alternating-pair ansatz with two layers and the target-inspired ansatz. In each layer of alternating-pair ansatz, dressed CNOT gates (see Fig. 6(a)) act on alternating pairs of neighboring qubits as described in Fig. 6(b). Target-inspired ansatz is constructed by removing all one-qubit gates and replacing each remaining gate with a dressed CNOT in the gate sequence transformed by IBM's simulator. Due to the limitation of space, we just illustrate an example of the target-inspired ansatz for WSP3 in Fig. 6(c). The structures of target-inspired ansatz for CCNOT and QFT3 can be generated in the same manage. Table 4 shows the cost achieved by alternating-pair ansatz with two layers and the target-inspired ansatz. For WSP3, both alternating-pair ansatz and target-inspired ansatzs can achieve a cost close to 0. However, they use 52 quantum gates while the Variational quantum compiling with double Q-learning ...
, 1) (i, n), 1) qubits, the lth gate) n, 2) ... n, 3) ...  proposed VQC only needs 7 quantum gates. 25 quantum gates are used to make an exact compilation for CCNOT in the proposed method. Although alternating-pair ansatz uses twice as many quantum gates, i.e., 52 gates, it cannot make an exact compilation. Target-inspired ansatz can achieve a low cost close to zero. However, it uses three times as many gates as the proposed method. Besides, it needs to have knowledge of the gate sequences of the target unitaries transformed by IBM's simulator. For QFT3, 22 quantum gates are used by our method to make an exact compilation, which is much fewer than the other two methods. Alternating-pair ansatz cannot make an exact compilation although it uses 52 quantum gates. The cost achieved by targetinspired ansatz is higher than the proposed method although it uses more than three times as many quantum gates as the proposed method. The simulation results show the importance of the optimization over gate sequence in quantum compiling. The Table 4. The cost C(U, V ) achieved by the proposed VQC and the structures in [20], i.e., alternating-pair ansatz with two layers and the target-inspired ansatz. (1) (1)  proposed VQC based on reinforcement learning can make an exact compilation with fewer quantum gates, which reduces the errors of quantum algorithm due to decoherence process and gate noise in NISQ devices, and enables quantum algorithms especially for complex algorithms to be executed within coherence time. Fig. 7 shows the structures of the quantum circuits generated by our method for the three-qubit unitaries. The depth of the complied circuits for WSP3, QFT3 and CCNOT are 4, 17 and 19, although the numbers of quantum gates in the circuit are 7, 22 and 25. The rotation angles of the quantum gates in these circuits are illustrated in Table A2 in Appendix A.  proposed method on the topology of IBM's 5-qubit quantum device, i.e., 'Ibmq ourense', as shown in Fig. 8. As WSP3, QFT3 and CCNOT are three-qubit unitaries, we use the first three qubits of 'Ibmq ourense'. The proposed algorithm can be applied to quantum devices without completely connected qubits by simply removing CNOT i,j gate from the native gate alphabet A in Algorithm 1 if there is no connection between qubit i and qubit j. For 'Ibmq ourense', the native gate alphabet can be denoted by CNOT 01 , CNOT 10 , CNOT 12 , where R i z and R i y denote the R z and R y gates act on qubit i. CNOT ij indicates the CNOT gate acts on qubits i and j where i and j are the control and target qubits, respectively. Fig. 9 shows the result of quantum compiling considering the connectivity of IBM's 5-qubit quantum device. Similar to the completely connected case, the cost C(U, V ) decreases with the increasing number of quantum gates in the compiled circuit V and converges to 0 for all the target unitaries. However, more quantum gates are needed in CCNOT and QFT3 as CNOT 02 and CNOT 20 are not available. For WSP3, the number of quantum gates is the same as the one in the completely connected case as it dose not need CNOT 02 and CNOT 20 (see Fig. 7).

Simulation considering the number of CNOT gates
In this section, we decrease the number of CNOT gates by adding a penalty term to the reward function as described in Eq. (10). The weighted parameter λ is set to be 0.1. We consider the connectivity of qubits on IBM's 5-qubit quantum device. The simulation result in Table 5 shows that the penalty term in the reward function can reduce the number of CNOT gates in the circuit. However, more gates are needed to get zero cost.

VQC on larger unitaries
In this section, we consider larger unitaries, i.e., 4, 5, 6, 7 and 8 qubits. For larger unitaries, Local Hilbert-Schimdt Test is used to measure the difference between the where ..CNOT 34 CNOT 12 and U 4 ( θ ) = n−1 i=0 R z (θ i ). CNOT ij denotes the CNOT gate where i and j are the control and target qubits, respectively. For large problem sizes, we can add common blocks to the gate alphabet to decrease the computation complexity. This strategy can be regarded as a crossover of variable structure ansatze and fixed-template compiling. It can construct the quantum circuits with some common blocks instead of selecting quantum gate one by one. In this paper, we consider the following blocks:  Figure 10. Illustrations of common blocks in five-qubit system. Table 6. The minimum cost achieved by VQC based on double Q-learning for 4, 5, 6, 7 and 8-qubit unitaries. C local (U, V ) denotes the local cost between the compiled circuit and the target unitary. C(U, V ) is the global cost between the target unitary and the compiled circuit generated by C local (U, V ) (i.e., the same structure and the same gate parameters).
We illustrate these blocks on a five-qubit system in Fig. 10. The learning rate α, the discount factor γ and the batch size K are set to 0.2, 1 and 128, respectively. The number of quantum circuits generated at each is the same as the simulation of three-qubit unitaries. For larger unitaries, local Hilbert-Schimdt Test is used to quantify how close the trained unitary is to the target unitary instead of global Hilbert-Schimdt Test due to the barren plateau phenomena. Table 6 shows the local cost achieved by the proposed method for 4, 5, 6, 7 and 8-qubit unitaries. By adding common blocks to the candidate gate alphabet, the local cost can converge to 0 for all the target unitaries with 4 to 8 qubits. We also evaluate the global cost C(U, V ) with the quantum circuits generated by C local (U, V ) (i.e., the same circuit with the same gate parameters). The global costs also converge to 0 for all qubits, which indicates exact compilations of the target unitaries.

Conclusion
Circuit design aims to find a proper structure of quantum circuit, which includes selecting which quantum gates, which qubits they act on and the order of these gates. It largely affects the performance and is crucial in variational quantum compiling. In this paper, we model the circuit design process as a Markov decision process and design a proper structure of quantum circuit to achieve an exact compilation with double Qlearning. The proposed VQC can automatically generate a high-performance quantum circuit by double Q-learning with -greedy exploration strategy and experience replay without any human expertise and labor. Another advantage is that the proposed method can minimize the number of quantum gates to achieve an exact compilation, which is especially important for NISQ devices as the algorithm performance degrades with runtime due to decoherence process and gate noise.
Simulation results on two-qubit unitaries (i.e., CZ, CH, CS, XX(3π/2) and QFT2) and three-qubit unitaries (i.e., WSP3, CCNOT and QFT3) show the satisfying performance of the proposed method. Compared to two typical structures in VQC, i.e., alternating-pair ansatz and alternating-pair ansatz, the proposed method can achieve lower cost with much less quantum gates. The proposed method can be easily applied to quantum devices with limited connectivity of qubits. The simulation considering the connectivity of qubits on IBM's quantum device shows that the proposed VQC can make exact compilations of WSP3, CCNOT and QFT3. Considering the low fidelity of two-qubit gates, we motivate the agent to use fewer CNOT gates in the compiled circuit by a penalty term in the reward function. Simulation result shows that it can make an exact compilation with fewer CNOT gates at the cost of more one-qubit gates. Ref. [20] provided rigorous theorems stating that the optimal variational parameters in quantum compiling are resilient to various noises such as measurement noise, gate noise and Pauli channel noise. Hence, the proposed method can be practically useful for noisy intermediate-scale quantum devices.