Quantum Circuit Template Matching Optimization Method for Constrained Connectivity

: The execution of quantum algorithms requires two key considerations. On the one hand, it should meet the connectivity constraint requirements of quantum circuit mapping for quantum architectures, and on the other hand, it needs to consider reducing the probability of errors in the execution of quantum circuits as much as possible. This paper proposes a novel optimization technique based on template matching that to satisfy both requirements. The template matching optimization method can signiﬁcantly reduce the number of gates in a quantum circuit and further enhance its practicality. It stands as advanced optimization technology available today. Our method optimizes quantum logic circuits mapped onto quantum architecture by initially selecting their linear substructure. We then zone the circuit according to the gate dependency graph and optimize each block through template matching. Finally, we reorganize the circuit to obtain the optimized version as the ﬁnal result. Our proposed method is amenable to various quantum architectures. To evaluate its efﬁcacy, we conduct a comparative analysis with the t | ket (cid:105) and Qiskit compiler using a set of benchmark test circuits. Speciﬁcally, compare to the t | ket (cid:105) compiler method, the highest average optimization rate of our method can reach 25.75%. Compare with the Qiskit compiler method, the highest average optimization rate can reach 32.72%. Overall, our approach has signiﬁcant optimization advantages.


Introduction
Noisy Intermediate-Scale Quantum (NISQ) computers [1] have the potential to increase computational power for certain problem classes, with tens to hundreds of qubits.However, limitations exist concerning the number of qubits and decoherence [2], which degrades the quantum information stored in the qubits over time.These challenges restrict the number of quantum gates that can be applied to quantum circuits, and limit the complexity of computations that can be executed on quantum devices.Consequently, it is imperative to optimize quantum circuits executed on NISQ devices.A key method to optimization is to minimize the number of quantum gates by replacing or eliminating some gates in the circuit.In the long run, optimizing quantum circuits not only improves running time for quantum algorithms but also plays an essential role in ensuring high-quality operation of quantum devices in the near future.
Currently, most quantum circuit optimization methods assume that the circuit will be executed on a general-purpose quantum computer, regardless of the connectivity of the actual hardware structure.Examples of circuit optimization methods can be found in sources such as [3][4][5][6], which do not take into account the connectivity constraints of quantum computer.In order for a quantum logic circuit to operate on a quantum device, circuit mapping needs to be performed by inserting SWAP gates to enable any number of quantum gates in the presence of connection constraints.However, circuit mapping often results in excessive extra gates and increased circuit depth.Although efficient mapping methods [7][8][9][10][11] have been developed to generate physical quantum circuits that require as few gates as possible, there is still space for further optimization of mapping circuits.In order to avoid changing the connection constraints that the circuit has already satisfied, some optimization methods, such as the t|ket compiler method [12], simplify the mapping circuit only by eliminating adjacent reverse gates, so this optimization effect is minimal.
As quantum computing continues to advance at a rapid pace, it becomes increasingly critical to develop more efficient methods for optimizing quantum circuits.To address this challenge, a novel quantum circuit optimization method is proposed in this paper, which takes into account the practical constraints of quantum architecture connections.By doing so, this method minimizes the number of quantum gates used while ensuring that the quantum architecture connections are correctly accounted for, resulting in a significant improvement in the overall performance of mapped quantum circuit.
In this paper, we extend the template matching optimization method described in the reference [3].We introduce the concept of linear templates and utilize them for the purpose of matching optimization.Our goal is to optimize the mapped quantum circuit while maintaining its connectivity constraints.To achieve this goal, we propose several methods including linear substructure selection, quantum circuit zoning, zoning circuit optimization and reorganization.These techniques enable us to further optimize quantum circuits and enhance the performance of circuit operations on quantum devices.
Before optimization, existing templates are refactored by adding SWAP gates or replacing Bridge gates to fit the connectivity constraints of the linear topology.The optimization comprises four primary steps: (1) the identification of linear substructures based on the number of Controlled-NOT(CNOT) gates between qubits that are mapped to the quantum topology; (2) the zoning of circuits according to the selected linear substructures and based on the gate dependency graph; (3) the optimization of each quantum circuit zoning using block-by-block template matching optimization techniques; (4) reorganization of optimized circuits based on the original zoning rules to generate new circuits equivalent to the original but with reduced gates.To optimize all qubits in the quantum topology, the above steps are repeated several times.

Preliminaries
In this section, we provide the fundamental definitions and notations related to quantum circuits.

Quantum Gate and Quantum Circuit
Quantum computing [13] provides a computing paradigm based on quantum bits.Quantum bits can not only represent Boolean 0 and Boolean 1, but also represent the superposition of the two.A quantum bit |ϕ = α|0 + β|1 such that |α| 2 + |β| 2 = 1.If α = 1, then |ϕ represents the classical 0; if β = 1, then |ϕ represents the classical 1.Quantum gates are used to operate on qubits in quantum computer.
A quantum circuit is a computational model consisting of quantum gates as the basic elements.These gates are operations selected from a quantum gate library [14], which is a collection of quantum gates capable of implementing any reversible function.A quantum logic circuit can be represented as a network structure diagram composed of a cascade of qubits and quantum gates.The circuit can also be represented by a unitary matrix, which is calculated as the product of matrices representing individual gates.Each horizontal line in the circuit diagram represents a qubit, and n parallel horizontal lines represent n qubits recorded as q 0 , q 1 , . . ., q n from the top down.Quantum gates execute sequentially according to their position in the circuit from left to right, recorded as g 0 , g 1 , . . ., g m .A CNOT circuit is defined as a quantum circuit that is only cascaded by CNOT gates.Figure 1 shows a CNOT circuit with 5 CNOT gates.
Due to the higher error rate of two-qubit gates [1], including CNOT gates, compared to single-qubit gates in NISQ devices, and the consideration of the nearest neighbor between the control and target bits, this paper solely focuses on optimizing CNOT gates for quantum circuit optimization.

Quantum Cost
The number of gates in a quantum circuit and the number of physical operations required to achieve each gate are important factors in measuring computational efficiency and computational success, hence the name quantum cost [15].Interference from the external environment can lead to the decoherence of quantum systems, so quantum computing must be completed in a finite coherence time.This requires that the quantum cost of quantum circuits be minimized, and the circuit with the smallest quantum cost to achieve a particular function is called an optimal circuit [16][17][18].Although finding the optimal circuit is a QMA (Quantum Merlin Arthur) problem [19], various practical methods for simplifying quantum circuits (such as [20][21][22]) have been proposed to reduce the cost of implementation.If the number of quantum gates is used as the quantum cost standard, a CNOT gate represents a quantum cost of 1.

Quantum Topology
The topology of a quantum architecture represents the connectivity between qubits in a quantum computing device.To ensure that a quantum logic circuit can be executed on a quantum computing device, it must satisfy the connectivity constraints of the quantum topology.There are currently many types of quantum architectures, including ibmq_montreal, ibm_perth, Rigetti Agave, ibmq_guadalupe, etc. [23].Figure 2a

Quantum Circuit Mapping
When executing a quantum circuit on a quantum computer, the qubits in the logic circuit need to be mapped to physical qubits on the target quantum architecture.For certain constraints of quantum architectures, such as superconducting quantum architectures, when mapping two qubit gates, their control and target qubits must map to physically connected adjacent qubits.Due to the limited connectivity of physical qubits on current devices, it is generally considered difficult to find an initial quantum graph satisfying all double qubit gates in the entire circuit.Therefore, mapping algorithms and optimization strategies are needed to address this limitation and find the best mapping of physical qubits, which may involve additional qubit gates to compensate for the lack of connectivity [7][8][9][10][11].In this paper, the mapping quantum circuit is optimized considering the connectivity constraint, so a better mapping method is used to obtain the experimental quantum circuit.
We compare IBM Qiskit [24] with the t|ket compiler [12], and for the original quantum circuit, this experiment uses the general optimization method in the t|ket compiler to optimize the circuit and perform qubit mapping, which can efficiently produce shorter circuit.

Quantum Circuit Templates
Definition 1.If a quantum logic circuit T consists of a series of unitary quantum gates U i satisfying the gate unitary matrix multiplication U |T| • • • U 1 = 1 (where |T| is the number of gates in the template), then the circuit T is called the template [25].Figure 3 shows a CNOT gate template.
, then we can reduce the number of quantum gates in circuit C by substitution.That is to say, in order to reduce more quantum gates, the longer the matching gate sequence U a • • • U b , the better [3].
The template library contains all the different templates, and the templates used in this article are mainly referenced in Qiskit [24].

Gate Dependency Graph
The gate dependency graph [26] is a representation of quantum circuits.First, the vertices in the graph correspond to the individual gates in the circuit.The graph has an edge i → j from vertex i to vertex j if by repeatedly interchanging commuting gates in the circuit, one can bring gate i immediately to the left of gate j, but gates i and j themselves do not commute.In other words, in the commuted circuit gate j immediately follows gate i, but one cannot change the order of gates i and j [3].The quantum gate g 0 in Figure 4a, g 1 , g 2 can be exchanged with each other to obtain the quantum circuit as shown in Figure 4b, and the quantum circuit of (a) and (b) is equivalent.From this, a gate dependency graph representation can be obtained as shown in Figure 4c.

Selection of Linear Substructure
When mapping quantum logic circuits to quantum architectures, it is important to consider the connectivity constraints of the quantum architectures.There are many types of quantum architectures, but not all of the existing templates may be suitable for meeting the connectivity requirements of quantum architectures.If an existing template is used for matching optimization directly, the resulting optimized circuit may not satisfy the connectivity constraints of the quantum architectures.Therefore, it is essential to carefully consider the specific architecture and connectivity constraints of the target architecture when mapping quantum circuits to quantum architectures.
For example, Figure 5a is a known quantum logic circuit, it conforms to the connectivity constraint of the linear architecture.Figure 5b is a known template.If the quantum gates g 1 , g 2 , g 3 , g 4 are matched with the gates g 3 , g 4 , g 5 , g 6 in the circuit of Figure 5a, the matching gates g 3 , g 4 , g 5 , g 6 in the circuit are replaced with the gate g 5 in the template, and the circuit is optimized as shown in Figure 5c.It can be seen that there is a non-nearest neighbor gate g 3 in the optimized circuit Figure 5c, which does not conform to the connectivity constraint of the linear architecture.
To address the aforementioned issue and find a solution that can be applied to all architectures, it is necessary to identify a common substructure that exists in all architectures.By ensuring that the template satisfies the connectivity of this substructure, it is possible to optimize the circuit without altering the connectivity constraints of the circuit.In this way, the resulting optimized circuit will be compatible with the connectivity requirements of the target quantum architecture, without compromising the functionality of the original circuit.It is therefore crucial to identify and leverage common substructures when mapping quantum circuits to quantum architectures.Definition 2. A quantum linear topological structure is a type of quantum network topology where the qubits are arranged in a linear chain or one-dimensional array, and the connections between them are limited to nearest-neighbor interactions.
Quantum linear topology is generally a part of the topology of quantum computing system, that is, quantum linear topology is a substructure of the topology of quantum computing system.As shown in Figure 2 in Figure 2 ibmq_guadalupe are the linear topology of this quantum topology.Definition 3. A template is called a linear template if it satisfies the connectivity constraint of quantum linear topology.
For templates in a given template library, templates in the library that do not satisfy the linear topology can be reconstructed to generate a new template library.Template reconstruction method is mainly: first, find the template in the non-neighbor CNOT gate; then, by inserting the SWAP gate or introducing the Bridge gate, the non-neighboring CNOT gate is neighbored to satisfy the topological connectivity.Finally, a new template conforming to the connectivity constraint of linear topology is obtained.Such reconstructed templates can perform template matching optimization on all circuits that satisfy linear topological connectivity.
Indeed, the ubiquity of linear structures in all topologies makes them a useful basis for optimizing the mapping of quantum circuits to various quantum topologies.By utilizing linear templates, we can develop techniques that are applicable across different topologies, rather than designing solutions specific to each individual topology.
Therefore, selecting the appropriate linear substructure in the quantum topology is a crucial step in achieving optimal results in quantum circuit optimization.By identifying and leveraging linear substructures in quantum topologies, we can greatly improve the effectiveness of quantum circuit optimization techniques, resulting in more efficient quantum computations.

Topological Linear Substructure Selection
When optimizing a quantum circuit by template matching, the number of qubits and quantum gates of the optimized quantum circuit should not be less than the number of qubits and quantum gates of the smallest template circuit in the template library.
As the number of qubits in a quantum circuit increases, the number of quantum gates in the circuit also increases.For a quantum circuit that contains more gates, the types and numbers of template circuits that can be matched in the template library also increase, thus allowing for better optimization.This is because more complex quantum circuits generally require more gates to be implemented, and more gates mean that the quantum system can perform more operations and transformations.Therefore, quantum circuits with more gates are better suited to express complex operations, leading to better performance.
In addition, more complex quantum circuits can enable a greater variety of operations and logic operations, resulting in more types and numbers of template circuits that can be matched.This will facilitate a more comprehensive and detailed optimization of the quantum circuit, thus improving system reliability and implementation efficiency.
Therefore, when selecting a linear substructure, it is advisable to choose a substructure with as many qubits as possible, and ensure that the corresponding quantum logic circuit contains as many quantum gates as possible.
Suppose l qubits are selected in the topology, expressed as the set S = Q s1 , . . ., Q sl , then there are k qubits adjacent (connected) to the qubits in S, expressed as the number of quantum gates between the logic circuit qubit pairs (q i1 , q j1 ) and (q i2 , q j2 ) corresponding to different adjacent qubit pairs (Q ti1 , Q sj1 ) and (Q ti2 , Q sj2 ) composed of qubits in T and S may be different.
As shown in Figure 6, when we select the (Q 0 , Q 1 , Q 3 , Q 5 , Q 6 ) qubits on the linear substructure in the topology, there are also two qubits, Q 2 andQ 4 , adjacent to (connected) to Q 1 and Q 5 , respectively.From the corresponding quantum logic circuit in Figure 7, we can see that there are CNOT gates g 4 and g 7 between q 1 q 2 and q 4 q 5 qubits, respectively., which are not within the upper gate of the qubit we selected.Their presence affects the number of qubits and gates of the quantum circuit when the template matching optimization.Definition 4. The barrier gate is a two-qubit gate with its control qubit and target qubit respectively located on the selected qubit and other qubits.
Barrier gate g 7 prevents the merger of its successor gate g 8 with its predecessor gate g 0 .When optimization, because of this barrier, the gates on the selected linear substructure cannot be directly optimized together.When there are more such barrier gates, the entire circuit is zoned into more local circuits, which reduces the number of qubits and gates on the quantum local circuit to be optimized.Therefore, if there are fewer barrier gates in a quantum circuit, more optimization can be performed through template matching using quantum local circuits containing more gates.To satisfy template matching conditions and maximize the number of qubits and quantum gates, it's important to consider the number of quantum gates between adjacent qubits in the corresponding quantum logic circuit.In order to select the maximum number of quantum gates in the quantum logic circuit and minimize the number of barrier gates, the following qubit selection rules are applied in the topology.
In the quantum topology, for a selected qubit Q i , if there are more than m adjacent qubits Q j1 , . . ., Q jm , m ≥ 2 with Q i , then in the quantum logic circuit corresponding to (q i , q j1 ), . . ., (q i , q jm ), the two qubits with the highest number of gates between the two qubits are preferred.A weight α is given below, with the larger the α the better the result.
where N a is the number of quantum gates on the chosen qubit; N b is the number of barrier gates.
For qubit requirements, we use the outermost qubit in the topology as the starting point (as shown in Figure 2 , and then select subsequent qubits based on the number of gates between qubits.This allows us to select as many qubits as possible while ensuring the fewest number of barrier gates.This is illustrated in Algorithm 1.
Take the ibm_perth topology in Figure 2 and the quantum circuit in Figure 7 as examples.Firstly, according to the number of gates between every two qubits, the outermost Q 0 , Q 1 two qubits of the topology are selected; Then, for the successor qubits of the qubit Q 1 , Q 2 and Q 3 , because there are more qubits on the corresponding quantum logic circuit between Q 1 , Q 3 , the qubit Q 3 is selected; then, the Q 5 , Q 6 are selected sequentially; finally, the linear substructure shown in the red qubit in Figure 6 is obtained.
The specific first round linear substructure selection algorithm is described as follows: Algorithm 1: The first round of linear substructure selection algorithm Input : Topology G(V, E), the number of gates between qubits Output : The selected substructure qubits 1. Initialize a list W to store qubits 2. for v ∈ V do 3. for e ∈ E do 4. if v i have the least adjacent connection nodes then 5.
for v j ∈ v i connection points do 6.
if e ij has maximum weight then 7.
Add v i v j to to the list of W 8.
else if no adjacent nodes can be selected then 9. break 10.
end if 11. end for 12. end for 13. return W

Circuit Zoning
After selecting the linear substructure, the circuit cannot be optimized directly due to the presence of other qubits that are coupled to those on the chosen substructure.The quantum gates between these qubits, also known as barrier gates, limit the number of qubits that can be included in the selected substructure.This effect is discussed in more detail in Section 3.
As shown in Figure 8, the circuit is zoned into two parts by the barrier gate g 7 , a quantum circuit consisting of a gate (g 0 , g 1 , g 2 , g 3 , g 5 , g 6 ) and the other is a quantum circuit consisting of a gate (g 8 , g 9 , g 10 , g 11 , g 12 , g 13 , g 14 ).Therefore, before optimizing the circuit, it is necessary to zone the gates in the circuit through the gate dependency graph according to the limitations of the quantum gate.Then, the template matching optimization method is used to optimize the circuits of the zoning one by one.The zoning of the circuit is mainly based on the gate dependency graph.The specific steps are as follows: Step 1: Determine whether the first quantum gate on the quantum logic circuit corresponding to the selected qubit is a barrier gate.If it is not a barrier gate, take the first gate as the starting point and proceed to the second step.If it is a barrier gate, start with a successor gate that is not a barrier gate and proceed to the second step.
Step 2: Determine whether the successor gate of the starting point has a barrier gate.If it does, then the barrier gate and all the gates that are subsequently affected are not within the bounds of the zoning.If the other successor gates are not barrier gates, zone them together.
Step 3: Continue to determine whether the gate behind it has a barrier gate according to step 2. Repeat this process until the last zoned gate is followed only by barrier gates, and then the circuit zoning of the first part ends.
Step 4: Begin with the undivided quantum gate that is not a barrier gate and continue the zoning through steps 2 and 3.
Step 5: Repeat step 4 until the last gate on the quantum circuit is reached and the zoning is complete.
Using Figure 8 as an example, we start by selecting gate g 0 as the starting point on the qubit.The successor gate g 1 is not a barrier gate, so both g 0 and g 1 are zoned together.The successor gates g 2 and g 3 are also zoned together.
The successor gate of the g 3 gate is g 4 g 5 g 6 , where g 4 is a barrier gate without a successor gate.However, g 5 and g 6 are not barrier gates, so they are zoned together.Since g 5 has no successor gate, the zoning is interrupted.
The successor gate of g 6 is only the barrier gate g 7 , so the first part of the zoning ends here.The zoned gates produce the quantum circuit shown in Figure 9.
The second part of the zoning starts with the successor gate g 8 of the g 7 gate.As g 8 is not a barrier gate, it is zoned.The successor gates do not barrier the zoning, and we obtain the quantum circuit shown in Figure 10 using the zoned gates.At this point, all gates in the circuit are zoned.This is illustrated in Algorithm 2.
The specific zoning algorithm is described as follows: Algorithm 2: Circuit zoning Input : Gate dependency graph Output : Zoning circuits 1. Initialize a list C to store gates 2. for i ∈ 0, 1, . . ., n do 3. if j ∈ min gates on selected qubit then 4. Add g j to the list of C 5.
if one successor gate of g j ∈ block gates then 6. continue 7.
else if one successor gate of g j ∈ gates on selected qubit then 8.
Add the successor gate g j+a to the list of C 9.
else if the only successor gate of g j ∈ block gates then 10. break 11.
if one successor gate of g i ∈ block gates then 13. continue 14.
else if one successor gate of g i ∈ gates on selected qubit then 15.
Add the successor gate g i+a to the list of C

Circuit Optimization and Reorganization
After zoning the quantum circuit into local circuits using zoning, a set of local quantum circuits are obtained, which are all composed of nearest neighbor interactions since the selected qubits are arranged linearly.We then use the stencil matching optimization technique for circuit optimization, utilizing a linear template shown in Figures 11 and 12.
The template library used is derived from IBM Qiskit compiler.The non-nearest neighbor template circuits in the library have been modified into linear templates in order to match the topology of the selected qubit.This paper adopts the template matching algorithm described in [4].The main idea is as follows: first, all possible initial matches between a gate in the circuit C and a gate in the template T are explored, and the qubits are assigned.Then, while preserving the initial match, the optimal match is maximized.Finally, the matched gates in circuit C are replaced by the inverse of the unmatched gate group in template T.  As shown in Figure 9, the circuit is first scanned and compared with the template in Figure 11.The first gate g 0 in the circuit matches the first gate g 0 in the template.The qubits q 0 , q 1 , and q 2 in the template correspond to qubits q 0 , q 1 , and q 3 in the circuit, respectively.
As the scanning continues, the successor gate g 1 of the gate g 0 in the circuit matches the successor gate g 1 of the gate g 0 .Further scanning reveals that gates g 2 , g 3 , and g 5 of the circuit match gates g 2 , g 3 , and g 4 of the template in succession.
However, the successor gate g 6 of gate g 5 in the circuit does not match the successor gate g 5 of gate g 4 in the template, and the matching process is halted.Considering that the number of matched gates is greater than half of the number of gates in the template, replace g 0 , g 1 , g 2 , g 3 , and g 5 in the circuit with the remaining gate g 7 , g 6 , g 5 in the template circuit.
The optimized circuit shown in Figure 13 has two fewer quantum gates than the circuit shown in Figure 9.
Similarly, the quantum circuit in Figure 10 is matched and optimized by the template of Figure 12 to obtain the quantum circuit as shown in Figure 14, reducing two quantum gates.
After block-by-block optimization, the optimized circuit must also be reorganized.The reorganization step involves incorporating an empty quantum circuit within the circuit according to the zoning of the circuit in the fourth part.Then, the quantum gate of each part is included into the circuit according to its qubit information.The quantum circuit in Figure 8 is zoned into three parts based on the gate dependency graph.Besides the two optimized local circuits, there are two barrier gates in the middle, as shown in Figure 15.When reorganizing the circuit, the optimized circuit in the first part (Figure 13) is sequentially transferred to an empty quantum circuit according to the qubit and gate order in the original circuit.The two barrier gates are then added accordingly.The quantum gate in the optimized circuit of the second part (Figure 14) is added next.Finally, the resulting circuit shown in Figure 16 is obtained, which reduces the total number of quantum gates by four.This concludes the first round of quantum circuit optimization.To reduce as many quantum gates as possible for the entire quantum circuit, two or more rounds of circuit optimization are required.Compared with the first round of optimization, the overall steps of the subsequent rounds of optimization are unchanged, except for the addition of a selection condition in the linear substructure selection stage.That is, based on the qubits on the linear substructure that have been selected in the 1 ∼ i(i > 1) round, the selection of the linear substructure in the i + 1 round should give priority to the qubits that have not been selected in the previous i round.Until the qubits on the j(1 ≤ j) round topology are selected when the linear substructure is selected, then this round of optimization is the last round of optimization of the last round of the current circuit.This ensures that the selection of linear substructures is not repeated for each round and allows each qubit on the topology to be optimized.Based on Figure 16, the second round of optimization process builds on the circuit after the first round of optimization.Firstly, the selection of linear substructures prioritizes the unselected qubits, excluding the two qubits already selected Q 2 and Q 4 , as shown in Figure 17.Then, since the number of quantum gates between the qubits on the quantum logic circuit corresponding to Q 2 Q 1 and Q 4 Q 5 is the same, one of the two qubits, Q 2 or Q 4 , is randomly chosen as the starting point.In the selection process, Q 2 is chosen, and Q 0 , Q 3 are selected as the successor qubits of Q 1 .Because there are more quantum gates between Q 1 and Q 3 , Q 3 is selected as the next qubit.Then, Q 5 is selected, and although there are more quantum gates between Q 5 and Q 6 , Q 4 is preferred as it is an unselected qubit for the subsequent qubits Q 4 and Q 6 of Q 5 .
Therefore, in this way, the second round selects a linear substructure composed of Q 2 , Q 1 , Q 3 , Q 5 , Q 4 qubits, covering all the qubits on the entire topology.After the second round of circuit zoning optimization and reorganization, the entire quantum circuit optimization is successfully completed.
As from the reference [27], the linear substructure of a quantum topology can be expressed as an r-noncommutative graph on finite rings.Therefore, Algorithm 3 is suited for determining the optimal path for any given quantum topology that is represented by r-noncommutative graph on finite rings.
The specific other rounds of linear substructure selection algorithms are described as follows: Algorithm 3: The second round of linear substructure selection algorithm Input : Topology G(V, E), qubits have been selected Output : The selected substructure qubits 1. Initialize a list W 1 to store qubits 2. for v ∈ V do 3. for e ∈ E do 4.
if v i have the least adjacent connection nodes then 5.
for v j ∈ v i connection points do 6.
if e ij has maximum weight or v j not selected then 7.
Add v i v j to to the list of W 1 8.
else if no adjacent nodes can be selected then 9. break 10.
end if 11. end for 12. end for 13. return W 1

Experimental Results and Analysis
In this experiment, the proposed optimization algorithms were implemented in Python language to verify their feasibility and advantages.To test their efficacy, a set of benchmark circuits, covering multiple qubits with varying quantum architectures, was utilized.The experimental results of this paper were compared with those obtained from the t|ket compiler and Qiskit optimization in a previous study.
Table 1 employs a linear quantum architecture, while Table 2 employs an ibm_perth quantum architecture.Additionally, Table 3 employs the ibmq_guadalupe quantum architecture for experimentation.The "Circuit name" in the table is the name of the circuit, "n" is the number of qubits of the quantum circuit, "Original CNOTs" is the number of CNOT gates in the initial circuit, "Mapped CNOTs" is the number of CNOT gates optimized and mapped by the compiler after t|ket ."Tket [12]" represents the number of CNOT gates optimized by the t|ket compiler."Topt CNOTs" indicates the number of CNOT gates optimized by the method proposed in this article, taking into account the constraints of the quantum architecture connections based on the circuit derived from the "Mapped CNOTs" circuit."Qiskit [24]" denotes the number of CNOT gates optimized by the Qiskit compiler, and "%" indicates the optimization rate of the proposed method compared to the t|ket〉 or Qiskit compiler.results.Overall, the proposed method presents a new perspective on quantum circuit optimization with connectivity constraints and exhibits promising potential for wide-ranging applications in the field of quantum computing.
,b show the topology of ibm_perth and ibmq_guadalupe, respectively.(a) (b)

Figure 3 .
Figure 3. Circuit template example.Template matching optimization: It is assumed that the gate sequence U a • • • U b (1 ≤ a ≤ b ≤ |T|) is found in the circuit C, which matches a set of gate sequences in the template T. According to Definition 1, a series of gates in the template satisfy U |T| • • • U 1 = 1, and each gate U i has an inverse gate U † i , then the gate sequenceU a • • • U b can be equivalently expressed as U † a−1 • • • U † 1 U † |T| • • • U b+1 .If the quantum cost of U † a−1 • • • U † 1 U † |T| • • • U b+1 gate sequence is lower than that of U a • • • U b, then we can reduce the number of quantum gates in circuit C by substitution.That is to say, in order to reduce more quantum gates, the longer the matching gate sequence U a • • • U b , the better[3].The template library contains all the different templates, and the templates used in this article are mainly referenced in Qiskit[24].

Figure 4 .
Figure 4. Different equivalent representations of quantum circuits.(a) Example of a quantum circuits.(b) The circuit is modified by interchanging the order of commuting gates.(c) Gate dependency graph.

Figure 6 .
Figure 6.Linear substructure selection (selected qubits are highlighted with red circles).

Figure 7 .
Figure 7. Example of a quantum circuit.

Figure 9 .
Figure 9.The first part of the quantum circuit.

Figure 10 .
Figure 10.The second part of the quantum circuit.

Figure 11 .
Figure 11.The first template circuit.

Figure 13 .
Figure 13.The first part optimizes the post-quantum circuit.

Figure 14 .
Figure 14.The second part optimizes the post-quantum circuit.

Figure 15 .
Figure 15. Circuit zoning.The circuit is zoned into three sections, namely 1, 2, and 3, as highlighted in the figure.

Figure 17 .
Figure 17.The second round of linear substructure selection.The red circle represents the qubit selected in the first round, while the blue circle represents the newly selected qubit in the second round.
16. else if the only successor gate of g i ∈ block gates then 17.

Table 1 .
Experimental comparison results on linear quantum architecture.