Nearest Neighbor Transformation of Quantum Circuits in 2D Architecture

In recent years, quantum computing has received extensive attention for its superior efficiency and application potential. For some physical architecture, one qubit can only interact with its adjacent qubits, SWAP gates are inserted to make a quantum circuit nearest neighbor compliant. The initial qubit placement algorithm in a 2D grid structure is proposed based on the constructed interaction cost metric model, and the method of dynamic grid decision is given. In order to obtain a better way of inserting SWAP gates, the gate level and circuit level interaction routing policies are put forward, and a heuristic pruning of interaction routing records is employed so as to reduce the runtime and additional quantum cost. Experimental results show that our proposed methods can achieve better performance than existing methods, and the average optimization rate of quantum cost is 21.76% and 17.23% respectively.


I. INTRODUCTION
Quantum Computing (QC) is an alternative to classical computation owing to its potential advantages in various important applications, including quantum image processing [1], cryptography [2], [3], machine learning [4], database searching [5], and others. Quantum circuit model is a common representation of quantum algorithms, and it is cascaded with a series of elementary quantum gates. Experimental observations suggest that interaction between qubits close to each other can reduce computation errors [6]. Several physical systems such as ion traps [7], quantum dots [8] and superconducting [9] can be employed to construct quantum circuits, which require that the nearest neighbor (NN) restriction should be satisfied, i.e., all interacting qubits in a quantum circuit are physically adjacent to each other.
Quantum circuits are often designed without consideration of their physical implementation. Several strategies of NN conversion have been proposed, which convert a quantum circuit to its equivalent form complying with the NN restriction by inserting SWAP gates. Each SWAP operation increases the running time and quantum cost, so reduction the number of SWAP gates is important.
The design of an optimal NN-compliant quantum circuit by inserting the minimal number of SWAP gates is an NP-complete problem [10]. Researchers have solved this problem by means of heuristic approaches that do not The associate editor coordinating the review of this manuscript and approving it for publication was Siddhartha Bhattacharyya . guarantee optimality. The one-dimensional (1D) or linear nearest neighbor (LNN) architecture is the simplest where the qubits are ordered linearly, and each qubit interacts with the previous and next qubits. Exhaustive [11] and exact search [12], [13] approaches are well-studied for smaller quantum circuits. Several heuristic search approaches are reported in the literature [14]- [18] that are scalable.
The two-dimensional (2D) architecture allows more number of adjacent neighbors for each qubit, so it can reduce the number of SWAP gates required for NN-compliance. Recently several works have been reported for efficient NN-compliant realization of quantum circuits in the 2D architecture. The problem of synthesizing a 2D NN-compliant quantum circuit is typically approached as a two-step process. First, an initial mapping of qubits in a 2D grid is identified (global ordering). Next, SWAP gates are actually inserted in order to make the circuit NN-compliant (local ordering).
In [19] the exact solution for the NN optimization of 2D quantum circuits was proposed, which provided a measure of reference for other heuristic mapping algorithms, but it can only be applied to small-scale circuits. In [20] the authors optimized the interaction path between the qubits through the sliding window technique, and the performance of the algorithm is affected by the size of the window. In [21], authors regarded the mapping of the quantum circuit as a multi-objective optimization problem, which was solved by using an evolutionary algorithm, but there is still a large optimization space in the construction of the qubit placement. A simple and effective algorithm to minimize the number of SWAP gates for adjacent interactions was proposed in [22], and the time required for processing does not exceed one second even for larger benchmark circuits, however, the precise grid approach can be improved to get better results. A forward-looking algorithm to reduce the number of SWAP gates for NN optimization in the 2D architecture was put forward in [23]. In order to meet the connectivity constraints in the mapping process, an exact solution was given in [24] for the qubit allocation problem, furthermore, the optimization algorithm needs to be extended to a more complex architecture.
In the present work, an initial qubit placement method and heuristic interaction routing strategies have been proposed and evaluated on some benchmark functions. The optimization objective is to minimize the number of inserted SWAP gates. Experimental results show that the proposed strategies have achieved better results compared to existing methods.
The remainder of the paper is organized as follows. Section II presents fundamental knowledge on quantum circuits, followed by a description of the proposed 2D global and local ordering approach in Section III and Section IV, Section V discusses the experimental results. Finally, concluding remarks are presented in Section VI.

A. THE NEAREST NEIGHBOR QUANTUM CIRCUIT
In quantum computing, the qubit is used as a basic unit of information, and it is different from classical bit. One qubit can be in a linear combination of two basis states, |0> and |1>, i.e. ψ = α|0 > +β|1 > where α 2 + β 2 = 1. However, the classical bit can only represent 0 or 1 at a time.
An n-qubit quantum gate operates on n qubits can be represented by a unitary matrix of size 2 n ×2 n , such as NCV gates, Clifford+T gates. In this paper, we consider quantum gates from NCV library [25] which includes NOT gate, CNOT gate, the controlled-V(CV) gate, and the controlled-V + (CV + ) gate.
A quantum circuit consists of some qubits and a sequence of quantum gates operating on these qubits. For example, a quantum circuit is depicted in Fig.1. Several quantum technologies restrict quantum gates to operate only on physically adjacent qubits, also known as nearest neighbor constraint. If the current two-qubit quantum gate satisfies the NN constraint, it is called the NN gate, otherwise it is called the non-NN gate. SWAP gates can be inserted before each non-NN quantum gate to make it satisfy the physical constraint in this regard. A SWAP gate can be realized using three CNOT gates operating on alternate qubits in sequence, as shown in Fig. 2. As a result, circuit transformation is required to make all non-NN gates in the original circuit turn into the NN gates, and the obtained circuit is called the NN quantum circuit.

B. COST METRICS
Some cost metrics have been proposed to estimate the cost of implementing the NN quantum circuits [23], the metrics associated with this paper are as follows.
1) Quantum cost (qc) is a generally used cost metric that is the number of elementary quantum gates.
2) SWAP count (nSwap) means the number of SWAP gates which are inserted before all non-NN gates.
3) Nearest neighbor cost (NNC) of a two-qubit quantum gate in 1D architecture is expressed as |c − t| −1 where c is the order in which the control qubit is placed in the circuit, and t is that of the target qubit. For the positions (x 1 , y 1 ) and (x 2 , y 2 ) of two qubits in a 2D architecture, the NNC is

A. INTERACTION GRAPH AND DEGREE OF ACTIVITY
For a quantum circuits with n quantum gates {g 1 , g 2 , · · · , g n } and m quantum bits {q 1 , q 2 , · · · , q m }, a directed qubit interaction graph IG = (V , E) with m vertices can be generated, where any vertex n i ∈ V (1 ≤ i ≤ m) represents each qubit, any directed edge e ij ∈ E(i = j) denotes the interaction between the vertex n i and n j , and the direction of the directed edge is from the control qubit to the target qubit. The weight of each edge w ij is the times of interaction. For the quantum circuit shown in Fig. 1, the generated IG is shown in Fig. 3. The adjacency matrix corresponding to the interaction graph in Fig.3 is shown in Fig. 4, where Adj i,j = w q i →q j .
Definition 1: For a qubit, its degree of activity (DoA) is defined as the times the qubit participates in quantum interactions throughout a quantum circuit, that is, the times VOLUME 8, 2020 the qubit is involved in the two-qubit gates. Suppose Adj is the adjacency matrix, and the DOA of qubit q i is calculated by formula (1).
The DoA corresponding to {q 0 , q 1 , q 2 , q 3 , q 4 , q 5 } in Fig.3 is {2, 6, 4, 3, 3, 4}. The value of DoA determines the times of interactions between the qubit and other qubits, so the higher the DoA, the closer the qubits should be close to each other.

B. INTERACTION COST
Two qubits q i and q j in the 2D grid, (x i , y i ) and (x j , y j ) are their positions, the Manhattan distance between them is as follows.
The number of interactions between q i and q j is represented by DoA(q i , q j ), and it is defined as the following formula.
During qubit layout, the interaction cost of q i placed at the current position is calculated as follows.
where k is the number of qubits which have already been placed into the grid. The function defines the interaction cost between the current qubit to be placed and all the qubits that have been placed. If the position selected when placing the current qubit is close to the qubits with which it often interacts, a lower cost can be obtained, and the less SWAP gates are needed.

C. INITIAL QUBIT PLACEMENT AND DYNAMIC GRID DECISION
In order to solve the problem of grid size selection, a dynamic grid selection method is put forward in this paper. Finding the mapping method with the least interaction cost has been proven to be a NP complete problem. In order to minimize the interaction cost between the qubits and make more quantum gates satisfy the NN constraint at first, we must give priority to the qubits that often interact with each other and ensure the qubits have as many NN unoccupied positions as possible, so as to reduce the probability of inserting SWAP gates and decrease the quantum cost in quantum interaction. All qubits are sorted in terms of the value of interaction cost, and they are stored in the priority queue PriorityQueue.
The initial qubits placement algorithm of 2D grid structure is given. The first qubit is popped up from the queue and it is placed at the center position of the grid. The second qubit is continued, and we find the NN unoccupied position based on the qubits that have been placed in the grid. Then, according to the cost function, the cost is calculated when placed in a NN position. After traversing all the possible positions, the position with the least cost is selected, the current qubit is placed into it, and the information of the placed qubits is updated. When all the qubits are placed, the qubits layout of the 2D grid structure can be obtained, and the size of the 2D grid can be determined. Because in the initial qubit layout, the qubit is arranged around the center of the grid, and owing to the characteristics of the cost function, there will be no imbalance in the qubit layout, so the size of the 2D grid established at this time is reasonable. The detailed flow is as Algorithm 1.

D. EXAMPLE FOR INITIAL PLACEMENT
For the quantum circuit as Fig. 1, by sorting the DoA of each qubit, the priority queue shown in Fig.5 can be obtained. An unoccupied 2D grid of sufficient size is built, and the first qubit q 1 of the current priority queue is placed into the center position of the 2D grid, then the next qubit q 5 in the priority queue is taken out. A total of four positions with the NN can be selected. The cost functions of the four free positions are calculated respectively, and the values are equal, so the position closest to the center qubit is selected. Then qubit q 2 in the priority queue is carried out, and so on, the placement scheme with the smallest cost function is selected, and the grid shown in Fig.6 is obtained. Finally, the minimized grid size needs to be generated according to the current qubit layout in the 2D grid. It can be seen that in this case, a 2D grid with 3 × 3 is obtained, and three free positions are generated. The qubits with high interaction frequency have satisfied the NN constraint in the grid, and can interact directly, which is in line with the expected goal of the method.

IV. PRIORITY-BASED LOCAL ORDERING APPROACH
For the initial qubit layout, the relationship between the qubits in the existing 2D grid cannot fully satisfy the NN constraints of all quantum gates. Therefore, during performing the interaction involved in the quantum gates in order, the layout state must be adjusted by inserting the SWAP gates to make the interacted two qubits in the NN state. For the current non-NN quantum gate, there are many ways to insert SWAP gates, the quantum cost caused by different ways is certain, it only depends on the NNC of the quantum gate, but it will cause the qubit layout state in the 2D grid to change, which affects the interaction of the subsequent quantum gates. Therefore, we must find a scheme to minimize the quantum cost of the whole NN quantum circuit, so it is the best that the current NN operation can have a positive impact on the interaction of the subsequent quantum gates.

A. GATE LEVEL INTERACTION ROUTING POLICY 1) INSERTION PATH
If two interacting qubits do not satisfy the NN constraint, they need to be moved closer to each other, one or more SWAP gates are inserted in between.
Definition 2: Let p 1 = (x 1 , y 1 ) and p 2 = (x 2 , y 2 ) are the positions of two qubits q 1 , q 2 , there exit several shortest paths between them to realize adjacent interaction, each of them is defined as insertion path.

2) INSERTION TRACE
Definition 3: In order to realize NN interaction of the non-NN gate, SWAP gates are inserted so that the control qubit and the target qubit are physically close to each other, the operation trace of inserting SWAP gates on an insertion path is defined as insertion trace.
The number of insertion traces between p 1 and p 2 for an insertion path is represented as nTrace(p 1 , p 2 ).
Theorem 2: Two non-NN positions (x 1 , y 1 ) and (x 2 , y 2 ), there exist |x 2 − x 1 | + |y 2 − y 1 | insertion traces for any insertion path, and the number of inserted SWAP gates is Proof: Let path is an insertion path between them, there are |x 2 − x 1 | + |y 2 − y 1 | + 1 ends on this path, the optional position of the control qubit or the target qubit is |x 2 − x 1 | + |y 2 − y 1 |, so there are |x 2 − x 1 | + |y 2 − y 1 | ways to insert SWAP gates. Although the qubit exchange mode in each way is different, the NN interaction of qubits is finally realized, and the number of inserted SWAP gates is equal to the sum of the number of movements of the control bit and target bit, i.e. |x 2 − x 1 | + |y 2 − y 1 | − 1.

B. INTERACTION ROUTING OF A NON-NN GATE
Definition 4: Suppose p 1 = (x 1 , y 1 ) and p 2 = (x 2 , y 2 ) are the positions of two non-NN qubits, there exist np insertion paths and nt insertion traces for each path, where np = nPath(p 1 , p 2 ) and nt = nTrace(p 1 , p 2 ), one of the np × nt ways is defined as interaction routing of a non-NN Gate.
For For example, there exists a path q 4 → q 1 → q 2 → q 3 in Fig.6, and j = 2 is considered. The first round moves q 4 to the j th position, i.e. q 1 → q 2 → q 4 , and no need for the second round, because q 4 and q 3 are adjacent.

C. CIRCUIT LEVEL INTERACTION ROUTING POLICY 1) INTERACTION ROUTING RECORDS
There is an exchange rule about two adjacent two-qubit gates g i and g j , if g i .c = g j .t and g i .t = g j .c, they can be exchanged and keep the circuit functional equivalent [26].
Definition 5: During quantum circuit mapping, each non-NN gate is dealt with in turn so that its control qubit and target qubit satisfy the NN constraint. The number of non-NN gates that have been processed is defined as number of NN operation, denoted by NNcount.
Definition 6: For the current non-NN gate g i , one of its NN interaction routing methods is used to make it satisfy the NN constraint, if the updated qubit placement make the subsequent k gates become NN gates, the (i + k + 1) th gate is called the farthest reachable quantum gate, represented as Gfar.
In order to record the current state in the circuit mapping process, the interaction routing record is introduced, and it contains: NNcount, Gfar, QC that has been extra generated by inserting SWAP gates (QCextra), and the detailed evolution of qubit placement. For all records, we construct a priority queue to reorder them in terms of the following rules.
First of all, the NNcount is compared, and the smaller the NNcount, the more likely it is to reach the farther quantum gate, so it has a higher priority. For records with the same NNcount, the Gfar is considered, the farther the Gfar is, which indicates that the insertion of the SWAP gates in the record is more favorable to the subsequent quantum gate. If the Gfar is the same, the smaller the QCextra is, which indicates that the insertion method of the SWAP gates can not only reach the farthest quantum gate, but also be realized by inserting the least SWAP gates, which is the best choice.

2) PREFERRED INTERACTION ROUTING RECORDS
In order to solve the problem of exponential increase of the algorithm in the process of qubit interaction routing selection, we pruned the routing records at a specific time, eliminated some records that are almost impossible to be locally optimal or globally optimal.
Definintion 7: The minimum required value of Gfar for the current non-NN quantum gate is defined as preferred threshold. qubit placements corresponding to all NN interaction routing policies iv. Two rounds of exchange to realize the nearest neighbor of the non-NN gate at the j th and (j+1) th positions. The first round is the adjacent exchange between the starting and the j th point, and the second round is the adjacent exchange between the end and the (j+1) th point.
If the value of Gfar is extremely small after several NN operations, the NN of the subsequent quantum gates cannot be well realized by this inserting method.
Definition 8: Preferred number defines the number of selected records that meet the preferred threshold.
For interaction routing records in the priority queue, we kept the first k preferred interaction routing records that satisfied the threshold v, and the others were pruned. For interaction routing records in the priority queue, we kept the first k preferred interaction routing records that satisfied the threshold v, and the others were pruned.

3) NN REALIZATION OF A NON-NN QUANTUM CIRCUIT
The NN realization of a non-NN quantum circuit inserts the SWAP gates by maintaining a priority queue, and the definition of the priority is the same as that in Algorithm 3: the less the number of NNcount, the farther the reachable quantum gate and the smaller the quantum cost, the higher the priority of the interaction routing record.

Algorithm 3: Get Priority Queue of Interaction Routing Records for a Non-NN Gate
Input: non-NN gate g i , current qubit placement grid [siz][size] Output: priority queue of interaction routing records pq  19 k ← k+1 20 end 21 return pq i. pos(g i .c) and pos(g i .t) return the position of the control bit and target bit of gate g i respectively ii. If g i and its subsequent successive two-qubit gates satisfy the exchange rule, these gates are marked executable iii. GfarArr stores Gfar for each placement Firstly, the initial qubit placement based on algorithm 1 is taken as the initial grid layout, and algorithm 3 is called to obtain all interaction routing for the first non-NN quantum gate, and the corresponding interaction routing records are generated and pushed to the priority queue. Then, as long as the priority queue is not empty, the highest priority record is taken out from the queue. It is judged whether all quantum interactions have been completed according to its reachable quantum gate. If so, the algorithm ends, and the interaction routing record that has completed all NN operations and has the minimum quantum cost in the priority queue is outputted. If not, it is determined whether the reachable quantum gate of the current record meets the preferred threshold, and if so, the interaction routing records are pruned. Then algorithm 3 is called to obtain the reachable quantum gate Gfar for the current record, thus new interaction routing records are generated and pushed to the priority queue. The above steps are repeated until the priority queue is empty or the first element of queue has realized the NN interaction. The process is as Algorithm 4.

D. EXAMPLE FOR LOCAL INSERTION OF SWAP GATES
For the quantum circuit as Fig.2, the initial qubit layout shown in Fig.6 is obtained. The gates g 0 and g 1 satisfy the NN constraint, g 2 acts on the q 2 and q 0 , so the NN operation is required. Call algorithm 2 to obtain all interaction routing information, because the Manhattan distance between q 0 and q 2 is 2, there are two insertion paths and two insertion traces for each path. Fig. 8 shows all interaction routing records generated by four ways. For the first two ways, not only the NN operation of g 2 is completed, but also this way can ensure that g 3 and g 4 meet the NN constraint. Therefore, we only need to consider the NN conversion from g 5 , whereas for the latter two ways, only g 2 can satisfy the NN constraint, and the quantum gate NN problem needs to be considered from g 3 . In these four ways, all need to insert a SWAP gate, resulting in an extra 3 quantum cost, but apparently at the same quantum cost, the first two are able to accomplish more quantum interactions with more advantages.
The first record (1,5,3) in Fig. 8 is selected. For this strategy, the NN operation of g 5 needs to be carried out. g 5 acts on q 1 and q 2 , algorithm 3 is called to generate interaction routing records. Fig. 9 lists all routing selection records to realize the NN of g 5 gate in the circuit. At this time, there are seven ways in the priority queue, the rest three cases in Fig. 6 plus four cases in Fig.9. Because there are several records with the NNcount 1 and 2 in the priority queue at the same time, the records with small number of NNcount are preferred. Due to space constraints, the placement history will no longer be shown in the priority queue below. Take out the second record with the NNcount of 1, Gfar of 5 and call algorithm 2 to generate new routing records and push them to the priority queue, and the updated priority queue shown in Fig.10(a). The red shaded part is the extracted record, and the green part is the next routing record obtained by the quantum gate in the record after the NN operation is completed.
Then the record with Gfar as 3 can be reached by taking out NNcount of 1, and algorithm 2 is called to generate new routing selection records and push them to the priority queue. With this loop, the priority queue shown in Fig. 10 After a large number of interaction routing search and local optimal selection, all the routing selection records in the priority queue have completed 3 NN operations, and the green part records have completed all quantum interactions. Therefore, the minimum quantum cost in the green part record is selected as the optimal solution of the algorithm, the minimum quantum cost is 9, and a total of three SWAP gates are inserted.

V. EXPERIMENTAL RESULTS
The performance of the initial qubit placement algorithm and the local SWAP insertion strategies proposed in this paper were tested. The experiments are based on RevLib benchmark dataset [27].
The experimental environment CPU is Intel (R) Xeon (R) Gold 5115 CPU @ 2.40GHz, memory 64GB, Ubuntu 16.04 operating system, C++ programming language. The value of preferred number has been set to 10, indicating that the 10 records with the highest priority are retained in the priority queue. If the reachable quantum gate number Gfar of the interaction routing record is very small after several NN operations, it is shown that the insertion method of the SWAP gates cannot have positive effect on the subsequent quantum gates and it can be discarded.
In this paper, the performance of the algorithm is evaluated by the number of extra added SWAP gates(nSWAP) and the quantum cost(qc) of the quantum circuit after NN operations. nq represents the number of logical qubits, ng is the number of fundamental quantum gates in the original circuit, and the quantum cost is: qc = ng+3×nSWAP. The results compared to [22] and [23] are shown in TABLE 1.

A. IMPROVEMENTS IN SWAP GATES AND QUANTUM COST
A total of 26 benchmark circuits are tested, and the maximum number of gates up to 4452. The experimental results show that the number of SWAP gates of this method on most datasets is smaller than that of the method in [22] except on 4gt12-v1_89, and only half the number of SWAP gates need to be inserted on some test data, for example on aj-e11_165, cycle10_2_110, hwb5_55, hwb7_62, rd53_135. The last two columns give the optimization rate of the proposed method in terms of quantum cost, and the average optimization rate compared with [22] is 21.76%.
Compared with the method in [23], the results of the optimization have been obtained on almost all of the tested data, except for 4gt13-v1_93 and hwb4_52, they have equal results with our scheme. In this case, the average optimization rate is 17.23%.
The two sets of comparative data show that the proposed method can reduce the number of SWAP gates inserted and thus reduce the quantum cost of the NN circuit.

B. SCALABILITY OF PROPOSED METHOD
Experiments are also carried on some larger benchmarks, such as cycle10_2_110, hwb7_62, sym9_148, the gate numbers are 1224, 2683 and 4452 respectively, so as to verify the scalability of the proposed method.
Compared with [22], the number of inserted SWAP gates on three circuits is decreased by 312, 782, 1957 respectively, and the optimization rate in quantum cost is 31.33%, 32.66% and 45.80%. Compared with [23], the number of inserted SWAP gates on three circuits is decreased by 322, 574, 1233 respectively, and the optimization rate in quantum cost is 32.01%, 26.25% and 34.74%. The experimental results show that the proposed method has achieved obvious improvement on these data, especially on sym9_148 with the largest number of gates, and the maximum optimization rate is up to 45.80%, which shows that our method has better applications on larger-scale circuits.

VI. CONCLUSION
An initial qubit placement method based on the constructed interaction cost model is given. Moreover, gate level and circuit level interaction routing policies with efficient preferred technology are proposed. Experimental results show that the proposed methods have lower quantum cost on most test data than existing methods, especially on some large-scale benchmarks, which means that this method is meaningful in dealing with the mapping problem of non-NN quantum circuits. However, for some more large-scale quantum circuits, the runtime will be greatly increased, and the further optimization method also needs to be studied.