A quantum circuit must be preprocessed before implementing on NISQ devices due to the connectivity constraint. Quantum circuit mapping (QCM) transforms the circuit into an equivalent one that is compliant with the NISQ device's architecture constraint by adding SWAP gates. The QCM problem asks the minimal number of auxiliary SWAP gates, and is NP-complete. The complexity of QCM with fixed parameters is studied in the paper. We give an exact algorithm for QCM, and show that the algorithm runs in polynomial time if the NISQ device's architecture is fixed. If the number of qubits of the quantum circuit is fixed, we show that the QCM problem is NL-complete by a reduction from the undirected shortest path problem.
Moreover, the fixed-parameter complexity of QCM is W[1]-hard when parameterized by the number of qubits of the quantum circuit. We prove the result by a reduction from the clique problem. If taking the depth of the quantum circuits and the coupling graphs as parameters, we show that the QCM problem is still NP-complete over shallow quantum circuits, and planar, bipartite and degree bounded coupling graphs.

§ INTRODUCTION

Quantum computing is a new type of computation by employing the laws of quantum mechanics to perform complex tasks. It shows theoretical advantages over classical computing on some problems such as integer factorization and unsorted database search. Quantum computing has applications in many fields including cryptography, chemistry, biology, artificial intelligence and others.
The number of qubits in quantum computers has increased steadily in the past several years. Now is the noisy intermediate-scale quantum (NISQ) era <cit.>. The quantum devices have dozens to hundreds of qubits with rather limited coherence time, and only support a few kinds of elementary quantum gates with nonnegligible errors. Moreover, current NISQ devices have the connectivity constraints which require that any two-qubit operation can only be applied to adjacent qubits.
A quantum circuit must be transformed into a functionally equivalent circuit that is compliant with the connectivity constraint of the quantum device before executing <cit.>.
Quantum circuit mapping (QCM) is the process that transforms the circuit by adding SWAP gates <cit.>. Due to the error caused by the decoherence and inherent noise in the NISQ device, the number of auxiliary SWAP gates should be as small as possible.
The QCM problem is, given a quantum circuit, a quantum architecture (denoted by a coupling graph in the paper), and a number $k$, asking whether $k$ SWAP gates is enough to transform the quantum circuit into an equivalent one that is compliant with the quantum architecture's constraint.
Deciding the minimal number of swap actions is an NP-complete problem <cit.>, which implies that it is unlikely to find a polynomial time algorithm. There are many algorithms for QCM based on heuristic and approximation methods <cit.>, which provide efficient solutions by taking advantage of the inner structural features of the quantum circuits and quantum devices. For example, mapping the logical qubits interacting with other qubits frequently to the physical qubits with high connecting degree can reduce the number of SWAP gates dramatically in the transformation <cit.>, and by exploring a fixed number of layers in the circuit in advance can give a local optimal solution without the large time consumption <cit.>. So study the effects of the topology of quantum circuits and quantum architectures on the complexity of QCM is of importance for practical applications.

The paper <cit.> studied the time-overhead caused by adding gates to permute qubits on 1D, 2D and hypercube quantum architectures, and <cit.> proved the time-overhead over cyclic butterfly architectures, whose coupling graphs are 4-degree bounded. We study systematically the complexity of QCM with the number of qubits, the depth of the quantum circuits, the topology of quantum architectures and the number of SWAP gates needed as the parameters, respectively, in this paper. First an exact algorithm that computes the minimal number of SWAP gates for the transformation is given. We show that the algorithm runs in polynomial time if either the number of qubits in the quantum circuit or the number of nodes in the coupling graph is bounded by a constant. Moreover, QCM is NL-complete if the the number of qubits in the quantum circuit is fixed. The result is obtained by a reduction from the undirected shortest path problem for graphs with maximum degree 3, which proved to be NL-complete in the paper. We also show that QCM is W[1]-hard parameterized by the number of qubits. The result is obtained by a reduction from the clique problem. The $W[i]\,(i\geq 0)$ is a set of parameterized problems to capture the fixed-parameter intractability. Every $W[j]\,(j\geq 1)$-hard problem is believed to be fixed-parameter intractable. Hence, the QCM problem is fixed-parameter intractable, and unlikely to have a fully polynomial-time approximation scheme.

The depth of the quantum circuits and the coupling graphs are the other two parameters considered in the paper. The motivation is that shallow quantum circuits are the kind of quantum circuits with fixed depth. They are powerful for some work and easily to implement on NISQ devices due to the short running time <cit.>. On the other hand, most of the current NISQ device's architectures have low connecting degree, e.g., the IBM QX20 Tokyo architecture, the Rigetti 16Q-Aspen architecture, and the IBM heavy-hex lattice architecture.
We show that the QCM problem is still NP-complete on shallow quantum circuits. The result also holds if the input coupling graphs are either planar, bipartite and degree bounded, or grid graphs that are finite induced subgraphs of the (infinite) grids made up of the squares, regular hexagons and equilateral triangles, respectively. These results are obtained by reductions from two famous NP-complete problems: the Hamiltonian path problem and the Hamiltonian cycle problem. We also prove that QCM is NP-complete if the number of SWAP gates allowed is fixed. The main results in the paper are summarized in Table <ref>. Combing these results we see that the number of qubits is the key factors that affect the complexity of QCM. Finding efficient algorithms for QCM on quantum circuits with a reasonable number of qubits is theoretically possible.

The complexity of QCM with fixed parameters.

graphs    all     [c]4-degree
bounded     [c]planar,
bounded     [c]grid
graphs    [c]fixed
of nodes
fixed number of qubits      NL-complete           NL         NL               NL      NL
depth of 3            NP-complete         NP-complete        NP-complete      NP-complete      P
depth of 2            NP-complete         NP-complete         -     -        P

The paper is organized as follows. In Section <ref>, we give the basic definitions about quantum circuits and quantum circuit mapping. In Section <ref>, we study the complexity of QCM with fixed parameters. We give an exact algorithm for QCM to compute the minimal number of SWAP gates in Subsection <ref>, and consider its complexity when the number of qubits is bounded in Subsection <ref>. In Subsection <ref>, we prove the fixed-parameter complexity of QCM. In Subsection <ref>, we consider the complexity of QCM with the depth of quantum circuits and the coupling graphs as parameters. In Subsection <ref>, we prove the complexity of QCM when the number of SWAP gates is fixed. Finally, we conclude the paper in Section <ref>.

§ PRELIMINARIES

§.§ Basic definitions and notations

A qubit is the basic unit of quantum computing. The classical bit represents a logical state which has value either “$0$” or “$1$”,  a qubit can be in a state $\ket{\phi} = \alpha \ket{0} + \beta \ket{1}$ that is the superposition of two basis states $\ket{0}$ and $\ket{1}$, where $\alpha,\beta$ are complex numbers and $|\alpha|^2 + |\beta|^2 =1$. A quantum gate is a unitary operator on a group of qubits. A quantum circuit is composed of a set of qubits and a sequence of quantum gates that operate on these qubits. The quantum circuit is a common model for quantum computation, and can be described as a gate-array diagram, in which qubits are represented as horizontal lines and quantum gates are different blocks that operate on those lines.
Figure <ref> is a quantum circuit that contains 4 qubits and 7 quantum gates.
A set of quantum gates are universal if any quantum gate can be decomposed to a combination of quantum gates from it.
The two-qubit CNOT gate and all single-qubit gates are widely used universal quantum gates.
In this paper, we focus only on the quantum circuits composed of two-qubit quantum gates and single-qubit quantum gates.
We use the convention that the leftmost gate in the quantum circuit executes first, and denote a quantum circuit by $\langle Q, \Gamma \rangle$, where $Q$ is the set of qubits and $\Gamma$ is a sequence of gates sorted by the execution order.

The topology graph of a quantum circuit $\langle Q, \Gamma \rangle$ is an undirected graph $(Q,E_t)$, where $E_t$ is a set of edges over the qubits in $Q$, such that for any two qubits $q_i$ and $q_j$, $(q_i,q_j)\in E_t$ if and only if $q_i$ and $q_j$ are operated by a two-qubit gate in $\Gamma$.

The dependency graph of a quantum circuit  $\langle Q, \Gamma \rangle$ is an acyclic directed graph $(\Gamma,E_d)$, where every gate in $\Gamma$ is a node, and $E_d$ is a set of edges over the gates in $\Gamma$ such that for any two gates $g_i$ and $g_j$, $(g_i,g_j)\in E_d$ if and only if an output of $g_i$ is an input of $g_j$.

$q_0$       H[wires=1,steps=1,style=dashed, rounded corners, inner sep=1pt]$g_1$
[0.5cm] 1[wires=2,steps=1,style=dashed, rounded corners, inner xsep=5pt, inner ysep=1pt]$g_3$
[0.5cm]     [0.5cm]1[wires=2,steps=1,style=dashed, rounded corners, inner sep=1pt]$g_6$

$q_1$            [0.5cm]
[0.5cm][wires=3,steps=1,style=dashed, rounded corners, inner xsep=5pt, inner ysep=1pt]$g_5$

$q_2$       T[wires=1,steps=1,style=dashed, rounded corners, inner sep=1pt]$g_2$
[0.5cm]1[wires=2,steps=1,style=dashed, rounded corners, inner xsep=5pt, inner ysep=1pt, label style=label position=below, yshift=-0.4cm]$g_4$
[0.5cm]     [0.5cm]     [0.5cm]

$q_3$            [0.5cm]
[0.5cm]H[wires=1,steps=1,style=dashed, rounded corners, inner sep=1pt, label style=label position=below, yshift=-0.4cm]$g_7$

[cells=nodes=draw, circle]
g_1 [r]     g_3 [r] [rd]     g_6

g_2 [r]     g_4 [r]     g_5 [d] [u]

$q_0$       H[wires=4,steps=1,style=dashed, inner sep=1pt, label style=yshift=0.2cm]layer1
[0.5cm] 1[wires=4,steps=1,style=dashed, inner xsep=5pt, inner ysep=1pt, label style=yshift=0.2cm]layer2
[0.5cm] [wires=4,steps=1,style=dashed, inner xsep=5pt, inner ysep=1pt, label style=yshift=0.2cm]layer3
[0.5cm]1[wires=4,steps=1,style=dashed, inner sep=1pt, label style=yshift=0.2cm]layer4

$q_1$            [0.5cm]

$q_2$       T
[0.5cm]     [0.5cm]     [0.5cm]

$q_3$            [0.5cm]

[cells=nodes=draw, circle,every arrow/.append style=dash]
q_0 [r]     q_1 [d]

q_2 [r]     q_3

The illustration of a quantum circuit (a), and (b) its dependency graph, (c) its layer partition, (d) is topology graph.

Figure <ref> and Figure <ref> are the dependency graph and the topology graph of Figure <ref>, respectively. Let $\Phi$ be a quantum circuit, and $g_i,g_j$ two gates in $\Phi$. We say that $g_j$ depends on $g_i$ if there is a path from $g_i$ to $g_j$ in the dependency graph of $\Phi$.

A quantum circuit $\Psi$ is a subcircuit of the quantum circuit $\Phi$ if $\Psi$ is obtained by removing some gates in $\Phi$ and all gates that they depend on are also be removed.

$q_0$      [0.5cm] 1[wires=2,steps=1,style=dashed, rounded corners, inner xsep=5pt, inner ysep=1pt]$g_3$
[0.5cm]     [0.5cm]1[wires=2,steps=1,style=dashed, rounded corners, inner sep=1pt]$g_6$

$q_1$      [0.5cm]
[0.5cm][wires=3,steps=1,style=dashed, rounded corners, inner xsep=5pt, inner ysep=1pt]$g_5$

$q_2$      [0.5cm]1[wires=2,steps=1,style=dashed, rounded corners, inner xsep=5pt, inner ysep=1pt, label style=label position=below, yshift=-0.4cm]$g_4$
[0.5cm]     [0.5cm]     [0.5cm]

$q_3$      [0.5cm]
[0.5cm]H[wires=1,steps=1,style=dashed, rounded corners, inner sep=1pt, label style=label position=below, yshift=-0.4cm]$g_7$

$q_0$          [0.5cm]     [0.5cm]1[wires=2,steps=1,style=dashed, rounded corners, inner sep=1pt]$g_6$

$q_1$      [0.5cm]     [0.5cm][wires=3,steps=1,style=dashed, rounded corners, inner xsep=5pt, inner ysep=1pt]$g_5$

$q_2$      [0.5cm]1[wires=2,steps=1,style=dashed, rounded corners, inner xsep=5pt, inner ysep=1pt, label style=label position=below, yshift=-0.4cm]$g_4$
[0.5cm]     [0.5cm]     [0.5cm]

$q_3$      [0.5cm]
[0.5cm]H[wires=1,steps=1,style=dashed, rounded corners, inner sep=1pt, label style=label position=below, yshift=-0.4cm]$g_7$

$q_0$      [0.5cm] 1[wires=2,steps=1,style=dashed, rounded corners, inner xsep=5pt, inner ysep=1pt]$g_3$
[0.5cm]     [0.5cm]1[wires=2,steps=1,style=dashed, rounded corners, inner sep=1pt]$g_6$

$q_1$      [0.5cm]
[0.5cm][wires=3,steps=1,style=dashed, rounded corners, inner xsep=5pt, inner ysep=1pt]$g_5$

$q_2$      [0.5cm]     [0.5cm]     [0.5cm]     [0.5cm]

$q_3$      [0.5cm]     [0.5cm]-2
[0.5cm]H[wires=1,steps=1,style=dashed, rounded corners, inner sep=1pt, label style=label position=below, yshift=-0.4cm]$g_7$

[cells=nodes=draw, circle]
g_3 [r] [rd]     g_6

g_4 [r]			     g_5 [d] [u]

[cells=nodes=draw, circle]

g_4	 [r]		       g_5 [d] [u]

[cells=nodes=draw, circle]
g_3 [r] [rd]        g_6

g_5 [d] [u]

The illustration of subcircuits. (a), (b), (c) are there subcircuits of Figure <ref>. (d), (e), (f) are their dependency graphs respectively.

The quantum circuits (a), (b), (c) in Figure <ref> are three subcircuits of Figure <ref>, and (d), (e), (f) are their dependency graphs, respectively.
Let $\Psi_1$ and $\Psi_2$ be two subcircuits of $\Phi$.
We say that $\Psi_1$ is smaller than $\Psi_2$ if $\Psi_1$ is a subcircuit of $\Psi_2$. $\Psi_1$ and $\Psi_2$ are incomparable if neither of them is smaller than the other one.
Let $S$ be a set of subcircuits of $\Phi$.
A subcircuit $\Psi_1$ in $S$ is minimal if no subcircuit in $S$ is smaller than $\Psi_1$.
The set $S$ is minimized if all subcircuits in it are minimal. Every pair of subcircuits in a minimized set are incomparable,
and we can minimize a subcircuit set by removing all non-minimal subcircuits in it.

A quantum circuit can be uniquely divided into layers which are maximal sets of gates such that all gates in the same layer can execute in parallel. The quantum circuit of Figure <ref> has 4 layers as illustrated in Figure <ref>.
The depth of a quantum circuit is the number of its layers. We use $depth(\Phi)$ to denote the depth of the quantum circuit $\Phi$.

§.§ Quantum circuit mapping

In a quantum circuit, all qubits are assumed to be all-to-all connected. However, in many quantum computers the connectivity of qubits has been reduced, and only adjacent qubits can interact with each other. A quantum computer can be abstracted by a graph that shows the connectivity between qubits.

[every arrow/.append style=dash]
[draw, circle, inner sep=2pt]$Q_0$  [r] [d]     [draw, circle, inner sep=2pt]$Q_1$  [r] [d]
[rd,shorten <= -4pt, shorten >= -4pt]     [draw, circle, inner sep=2pt]$Q_2$  [r] [d] [ld, shorten <= -4pt, shorten >= -4pt]     [draw, circle, inner sep=2pt]$Q_3$ [r] [d] [rd, shorten <= -4pt, shorten >= -4pt]     [draw, circle, inner sep=2pt]$Q_4$  [d] [ld, shorten <= -4pt, shorten >= -4pt]

[draw, circle, inner sep=2pt]$Q_5$  [r] [d] [rd, shorten <= -4pt, shorten >= -4pt]     [draw, circle, inner sep=2pt]$Q_6$  [r] [d]
[ld, shorten <= -4pt, shorten >= -4pt]     [draw, circle, inner sep=2pt]$Q_7$  [r] [d] [rd, shorten <= -4pt, shorten >= -4pt]     [draw, circle, inner sep=2pt]$Q_8$   [r] [d] [ld, shorten <= -4pt, shorten >= -4pt]     [draw, circle, inner sep=2pt]$Q_9$ [d]

[draw, circle, inner sep=1pt]$Q_{10}$ [r] [d]     [draw, circle, inner sep=1pt]$Q_{11}$ [r] [d] [rd, shorten <= -4pt, shorten >= -4pt]     [draw, circle, inner sep=1pt]$Q_{12}$ [r] [d] [ld, shorten <= -4pt, shorten >= -4pt]     [draw, circle, inner sep=1pt]$Q_{13}$ [r] [d]
[rd, shorten <= -4pt, shorten >= -4pt]     [draw, circle, inner sep=1pt]$Q_{14}$ [d] [ld, shorten <= -4pt, shorten >= -4pt]

[draw, circle, inner sep=1pt]$Q_{15}$ [r]     [draw, circle, inner sep=1pt]$Q_{16}$ [r]     [draw, circle, inner sep=1pt]$Q_{17}$ [r]     [draw, circle, inner sep=1pt]$Q_{18}$ [r]     [draw, circle, inner sep=1pt]$Q_{19}$

[every arrow/.append style=dash]
[draw, circle, inner sep=2pt]$Q_1$ [r] [ld,shorten <= -4pt, shorten >= -4pt]    [draw, circle, inner sep=2pt]$Q_2$ [rd, shorten <= -4pt, shorten >= -4pt]               [draw, circle, inner sep=2pt]$Q_3$ [r] [ld, shorten <= -4pt, shorten >= -4pt]     [draw, circle, inner sep=2pt]$Q_4$
[rd, shorten <= -4pt, shorten >= -4pt]

[draw, circle, inner sep=2pt]$Q_5$ [d]               [draw, circle, inner sep=2pt]$Q_6$ [r] [d]     [draw, circle, inner sep=2pt]$Q_7$ [d]                      [draw, circle, inner sep=2pt]$Q_8$ [d]

[draw, circle, inner sep=2pt]$Q_9$ [rd, shorten <= -4pt, shorten >= -4pt]               [draw, circle, inner sep=1pt]$Q_{10}$ [r]
[ld, shorten <= -4pt, shorten >= -4pt]     [draw, circle, inner sep=1pt]$Q_{11}$ [rd, shorten <= -4pt, shorten >= -4pt]                            [draw, circle, inner sep=1pt]$Q_{12}$ [ld, shorten <= -4pt, shorten >= -4pt]

[draw, circle, inner sep=1pt]$Q_{13}$ [r]     [draw, circle, inner sep=1pt]$Q_{14}$             [draw, circle, inner sep=1pt]$Q_{15}$ [r]     [draw, circle, inner sep=1pt]$Q_{16}$

The coupling graphs of (a) IBM QX20 Tokyo and (b) Rigetti 16Q-Aspen.

The coupling graph of a quantum computer $\mathbf{QM}$ is an undirected connected graph $(V,E_c)$, where $V$ is the set of qubits in $\mathbf{QM}$, and for any $q_i,q_j\in V$, $(q_i,q_j)\in E_c$ if and only if $q_i, q_j$ can be operated directly by a two-qubit gate in $\mathbf{QM}$.

Due to the nearest neighbor (NN) constraint, a quantum circuit need to be transformed into an equivalent and compliant one before executing in a quantum computer. This process is quantum circuit mapping that contains two main steps: initial placement and routing.
The initial placement is to find a mapping from logical qubits in the quantum circuit to the physical qubits in the quantum computer in order to minimize non-NN interactions.
If two logical qubits that need to interact are not NN, we can move them by adding SWAP gates. Figure <ref> shows a circuit using 2 SWAP gates to make $q_0$ and $q_3$ adjacent. The routing step is to find the best way to move logical qubits, i.e. reduces the number of SWAP gates as less as possible.

[row sep=0.5cm,between origins]
$q_0$                 $q_0$

$q_1$            1        $q_3$

$q_2$       1              $q_1$

$q_3$                  $q_2$

[step=1cm,gray,very thin] (0,0) grid (3,3);
[fill, circle, inner sep=1.5pt] at (0,2) ;
[fill, circle, inner sep=1.5pt] at (1,2) ;
[] at (2,2) $\times$;
[] at (2,1) $\times$;
[] at (0.3,1.6) $q_0$;
[] at (1.3,1.6) $q_1$;
[] at (2.3,1.6) $q_2$;
[] at (2.3,0.6) $q_3$;

[step=1cm,gray,very thin] (0,0) grid (3,3);
[fill, circle, inner sep=1.5pt] at (0,2) ;
[] at (1,2) $\times$;
[] at (2,2) $\times$;
[fill, circle, inner sep=1.5pt] at (2,1) ;
[] at (0.3,1.6) $q_0$;
[] at (1.3,1.6) $q_1$;
[] at (2.3,1.6) $q_3$;
[] at (2.3,0.6) $q_2$;

[step=1cm,gray,very thin] (0,0) grid (3,3);
[fill, circle, inner sep=1.5pt] at (0,2) ;
[fill, circle, inner sep=1.5pt] at (1,2) ;
[fill, circle, inner sep=1.5pt] at (2,2) ;
[fill, circle, inner sep=1.5pt] at (2,1) ;
[] at (0.3,1.6) $q_0$;
[] at (1.3,1.6) $q_3$;
[] at (2.3,1.6) $q_1$;
[] at (2.3,0.6) $q_2$;

Move qubit states by SWAP gates. (a) A quantum circuit swapping the states of $q_1$, $q_2$ and $q_3$. (b) The movement of logical qubits in the quantum computer.

The quantum circuit mapping (QCM) problem is

INPUT: A quantum circuit $\Phi$, a coupling graph $G$, and a number $k$.

OUTPUT: Yes, if $\Phi$ can be transformed to satisfy $G$'s NN constraint using at most $k$ SWAP gates; no, otherwise.

The QCM problem is NP-complete.

To prove QCM is in NP, we can guess an initial mapping $\pi$ and $k$ swap operations and verify that whether $\Phi$ can be transformed to satisfy $G$'s NN constraint from $\pi$ by the $k$ swap operations. The verification can be done in polynomial time.
The hardness of the problem is witnessed by the subgraph isomorphism problem that is NP-complete <cit.>.
That is, if $k=0$, then $\Phi$ satisfies $G$'s NN constraint iff the topology graph of $\Phi$ is a subgraph of $G$.

§ THE COMPLEXITY OF QCM WITH PARAMETERS

In this section, we shall study the complexity of QCM parameterized by the number of qubits, the depth of quantum circuits, the type of coupling graphs, and the number of swap operations. First an exact algorithm to compute the minimal number of swap operations needed is given for QCM.

§.§ An exact algorithm for QCM

Given a quantum circuit $\Phi=\langle Q, \Gamma \rangle$ and a coupling graph $G=(V,E_c)$, define

\[
\begin{cases}
k, & |Q| \leq |V|\\
\infty, & |Q| > |V|
\end{cases}
\]

where $k$ is the least number of SWAP gates needed to transform $\Phi$ to satisfy $G$'s NN  constraint.
For simplicity, we always assume $|Q| \leq |V|$ in the remainder of this paper.
The distance between two nodes $a,b$ in $G$ is the length of the shortest path from $a$ to $b$.
The diameter of a graph $G$ is $\max\{d(a,b)\,|\,a,b\in V\}$, where $d(a,b)$ is the distance between $a$ and $b$. We use $dia(G)$ to denote the diameter of $G$. The following lemma gives an upper bound for $g(\Phi,G)$.

$g(\Phi,G)\leq t \cdotp (dia(G)-1)$, where $t$ is the number of two-qubit gates in $\Phi$.

Every two qubits mapped to $G$ can be made adjacent using at most $dia(G)-1$ swap operations.
Suppose that there are $t$ two-qubit gates in $\Phi$, so $t\cdotp(dia(G)-1)$ SWAP gates are enough to transform $\Phi$ to satisfy $G$'s NN constraint.

In the following we give an upper bound for the number of subcircuits of $\Phi$.
Let $\Phi$ be a quantum circuit with $n$ qubits and $l$ layers, we construct a black and white colored $n\times l$ grid $D$ ($n$ rows and $l$ columns) such that if qubit $q_i$ is operated by a gate in the $j$-th layer, then the node $(i,j)$ is black in $D$, and all nodes $(i,j')\,(j<j'\leq l)$ after it are also black in $D$. Then we compress the consecutive columns whose black nodes coincide in each row to one column to get a new grid $D'$. We call $D'$ the circuit type of $\Phi$.
It is easily seen that there are at most $n$ columns in $D'$.
In Figure <ref> is an example that shows the procedure from a quantum circuit to its circuit type.

[step=1cm,gray,very thin] (0,0) grid (3,3);
[fill, circle, inner sep=1.5pt] at (0,3) ;
[fill, circle, inner sep=1.5pt] at (1,3) ;
[fill, circle, inner sep=1.5pt] at (2,3) ;
[fill, circle, inner sep=1.5pt] at (3,3) ;
[fill, circle, inner sep=1.5pt] at (0,2) ;
[fill, circle, inner sep=1.5pt] at (1,2) ;
[fill, circle, inner sep=1.5pt] at (2,2) ;
[fill, circle, inner sep=1.5pt] at (3,2) ;
[fill, circle, inner sep=1.5pt] at (1,1) ;
[fill, circle, inner sep=1.5pt] at (2,1) ;
[fill, circle, inner sep=1.5pt] at (3,0) ;
[fill, circle, inner sep=1.5pt] at (3,1) ;

[step=1cm,gray,very thin] (0,0) grid (2,3);
[fill, circle, inner sep=1.5pt] at (0,3) ;
[fill, circle, inner sep=1.5pt] at (1,3) ;
[fill, circle, inner sep=1.5pt] at (2,3) ;
[fill, circle, inner sep=1.5pt] at (0,2) ;
[fill, circle, inner sep=1.5pt] at (1,2) ;
[fill, circle, inner sep=1.5pt] at (2,2) ;
[fill, circle, inner sep=1.5pt] at (1,1) ;
[fill, circle, inner sep=1.5pt] at (2,1) ;
[fill, circle, inner sep=1.5pt] at (2,0) ;

From a quantum circuit to its circuit type.

Let $n\geq 1$ and $T_n$ be a set of colored $n\times m$  grids ($1\leq m \leq n$) such that a grid is in $T_n$ iff it satisfies:

* At least one node is black in the first column.

* If the node $(i,j)$ is black, then the nodes $(i,j')\,(j< j' \leq m)$ are also black.

* For every $1< j \leq m$, there is $1\leq i \leq n$ such that $(i,j)$ is black, $(i,j-1)$ is white.

The grid in $T_n$ is monotone in the sense that the black nodes in every column grow from left to right. As an example, Figure <ref> shows four grids in $T_4$.

[step=1cm,gray,very thin] (0,0) grid (3,3);
[fill, circle, inner sep=1.5pt] at (0,0) ;
[fill, circle, inner sep=1.5pt] at (1,0) ;
[fill, circle, inner sep=1.5pt] at (2,0) ;
[fill, circle, inner sep=1.5pt] at (3,0) ;
[fill, circle, inner sep=1.5pt] at (1,1) ;
[fill, circle, inner sep=1.5pt] at (2,1) ;
[fill, circle, inner sep=1.5pt] at (3,1) ;
[fill, circle, inner sep=1.5pt] at (2,2) ;
[fill, circle, inner sep=1.5pt] at (3,2) ;
[fill, circle, inner sep=1.5pt] at (3,3) ;

[step=1cm,gray,very thin] (0,0) grid (2,3);
[fill, circle, inner sep=1.5pt] at (0,1) ;
[fill, circle, inner sep=1.5pt] at (1,2) ;
[fill, circle, inner sep=1.5pt] at (2,0) ;
[fill, circle, inner sep=1.5pt] at (1,1) ;
[fill, circle, inner sep=1.5pt] at (2,1) ;
[fill, circle, inner sep=1.5pt] at (2,2) ;
[fill, circle, inner sep=1.5pt] at (2,3) ;

[step=1cm,gray,very thin] (0,0) grid (1,3);
[fill, circle, inner sep=1.5pt] at (0,1) ;
[fill, circle, inner sep=1.5pt] at (0,2) ;
[fill, circle, inner sep=1.5pt] at (1,2) ;
[fill, circle, inner sep=1.5pt] at (1,3) ;
[fill, circle, inner sep=1.5pt] at (1,1) ;

[step=1cm,gray,very thin] (0,0) grid (0,3);
[fill, circle, inner sep=1.5pt] at (0,1) ;
[fill, circle, inner sep=1.5pt] at (0,2) ;
[fill, circle, inner sep=1.5pt] at (0,0) ;
[fill, circle, inner sep=1.5pt] at (0,3) ;

Four grids in $T_4$.

If $\Phi$ is a quantum circuit with $n$ qubits and $l$ layers, then there are at most $n!\cdot (2l)^n$ subcircuits of $\Phi$.

Every subcircuit $\Phi'$ of $\Phi$ corresponds to an $n\times l$ grid and its circuit type is an $n \times m$ grid ($m\leq n$).
Let $m\leq \min\{n,l\}$, there are at most $l \choose m $ possibilities to expand an $n\times m$ grid to an $n\times l$ grid by duplicating columns.
Since ${l \choose m} \leq l^m \leq l^n$, there are at most $l^n$ subcircuits of $\Phi$ that have the same circuit type.

Note that $T_n$ contains the circuit types of all quantum circuits of $n$ qubits.
The number of $n\times n$ grids in $T_n$ is at most $n!$. An $n\times m\,(1\leq m < n)$ grid in $T_n$ can be obtained by removing some columns from an $n\times n$ grid, so $|T_n| \leq n!\cdot 2^n$.
Therefore, the total number of subcircuits of $\Phi$ is at most $n!\cdot 2^n \cdot l^n = n!\cdot (2l)^n$.

For any minimized subcircuit set $S$ of $\Phi$, $|S|\leq n!\cdot (2l)^n$.

Given a quantum circuit $\Phi$ with $n$ qubits and $t$ gates, and a coupling graph $G=(V,E_c)$ with $b$ nodes, $g(\Phi,G)$ is computable in $O(b^{3n+3} t n + (n!)^2 4^n t^{2n+2} b^{2n+3})$ time.

There are at most $\frac{b!}{(b-n)!}$ mappings from the qubits in $\Phi$ to the nodes in $G$. We compute the minimal number of SWAP gates needed for each initial mapping, then $g(\Phi,G)$ is the smallest one among them.
We do a breadth-first search in Algorithm <ref>. In each iteration, we consider all possible swap operations, and transform the quantum circuit according to them.
We reduce $\Phi$ after each swap operation by removing all gates that are already nearest neighbored and do not depend on any other gates to get a subcircuit.
To prohibit search space explosion, only the minimal subcircuits are saved. The procedure is repeated until an empty circuit occurs. The outline of Algorithm <ref> is in the following.

Step 1 Given an initial mapping $\pi_0$, reduce $\Phi$ by removing NN-gates under $\pi_0$ to get a subcircuit $\Phi'$. Set $\Pi=\{(\pi_0,\Phi')\}$ and $\Pi'=\emptyset$.

Step 2 For each $(\pi,\Phi_1,\dots,\Phi_d)\in \Pi$ do Step 3 until an empty circuit occurs.

Step 3 For each edge in $E_c$, make a swap operation on the qubits attached to the edge to get a new mapping $\pi'$.

Step 3.1 Reduce each $\Phi_i\,(1\leq i \leq d)$ under $\pi'$ to get a set $\{\Phi_1',\dots,\Phi_{d}'\}$, and minimize it to get a new set $\{\Phi_1'',\dots,\Phi_{d'}''\}$ that contains only the minimal subcircuits.

Step 3.2  If there is a tuple $\mathbf{Tu} = (\pi',\Psi_1,\dots,\Psi_{e}) \in \Pi'$ with the same mapping $\pi'$, then minimize the set $\{\Psi_1,\dots,\Psi_{e},\Phi_1'',\dots,\Phi_{d'}''\}$ to get a new set $\{\Psi_1',\dots,\Psi_{e'}'\}$, and replace $\mathbf{Tu}$ with $(\pi',\Psi_1',\dots,\Psi_{e'}')$ in $\Pi'$.
Otherwise, put $(\pi',\Phi_1'',\dots,\Phi_{d'}'')$ in $\Pi'$.

Step 4 If Step 2 is finished and no empty circuit occurs, then replace $\Pi$ by $ \Pi'$ and set $\Pi' =\emptyset$, and repeat Step 2 again. Otherwise, return the number of repeated times.

A quantum circuit $\Phi$ and a coupling graph $G=(V,E_c)$.
The value of $g(\Phi,G)$.

initial mapping $\pi_0$
$\Phi'\leftarrow$ reduce $\Phi$ under $\pi_0$, $\Pi\leftarrow\{(\pi_0,\Phi')\},\Pi'\leftarrow\emptyset$, $N_{\pi_0} \leftarrow 0$
$\Phi'=\emptyset$$Find \leftarrow Yes $$Find \leftarrow No$
$Find= No$
$(\pi,\Phi_1,\dots,\Phi_d)\in \Pi$
edge $y \in E_c$
Swap the logical qubits attached to $y$ to get a new mapping $\pi'$
$(\Phi_1',\dots,\Phi_{d}')\leftarrow$ reduce $(\Phi_1,\dots,\Phi_d)$ under $\pi'$
$(\Phi_1'',\dots,\Phi_{d'}'')\leftarrow$ minimize $(\Phi_1',\dots,\Phi_{d}')$
$\exists \Phi_i''=\emptyset$$Find\leftarrow Yes$
$\exists (\pi',\Psi_1,\dots,\Psi_{e})\in \Pi'$
$\{\Psi_1',\dots,\Psi_{e'}'\}\leftarrow$ minimize $\{\Psi_1,\dots,\Psi_{e},\Phi_1'',\dots,\Phi_{d'}''\}$
Replace $(\pi',\Psi_1,\dots,\Psi_{e})$ with $(\pi',\Psi_1',\dots,\Psi_{e'}')$ in $\Pi'$
$\Pi'\leftarrow \Pi'\cup (\pi',\Phi_1'',\dots,\Phi_{d'}'')$

$\Pi \leftarrow \Pi', \Pi'\leftarrow\emptyset, N_{\pi_0} \leftarrow N_{\pi_0} + 1$

$N\leftarrow \min\{N_{\pi_0} |\pi_0 \text{ is an initial mapping}\}$

An exact algorithm for computing $g(\Phi,G)$.

In Step 1, it takes $O(t)$ time to reduce $\Phi$. Each mapping occurs at most once in $\Pi$, so $|\Pi|\leq \frac{b!}{(b-n)!}$, and Step 2 repeats at most $\frac{b!}{(b-n)!}$ times.
Step 3 repeats at most $|E_c|$ times.
By Lemma <ref>, Step 4 repeats at most $t \cdotp (dia(G)-1)$ times.
Note that the cardinality of $\{\Phi_1,\dots,\Phi_d\}$ is at most $n!\cdot (2l)^n$ for any tuple $(\pi,\Phi_1,\dots,\Phi_d)\in \Pi$ by Corollary <ref>, where $l$ is the number of layers of $\Phi$.
For the time complexity of Step 3, in Step 3.1, it takes $O(n!\cdot (2l)^n\cdotp t)$ time to reduce $\Phi_1,\dots,\Phi_d$, and $O((n!)^2\cdot (2l)^{2n}\cdotp t)$ time to minimize the new subcircuit set. Thus, Step 3.1 takes $O((n!)^2\cdot (2l)^{2n}\cdotp t)$ time.
In Step 3.2, it takes $O(\frac{n\cdot b!}{(b-n)!})$ time to check whether there is a tuple in $\Pi'$ with the same mapping, and $O((n!)^2\cdot (2l)^{2n}\cdotp t)$ time to minimize them if there is one. Thus, Step 3.2 takes $O(\frac{n\cdot b!}{(b-n)!} + (n!)^2\cdot (2l)^{2n}\cdotp t)$ time.
The total running time of Algorithm <ref> is

\[
\frac{b!}{(b-n)!} \cdotp \left( t \cdotp (dia(G)-1) \cdotp \frac{b!}{(b-n)!} \cdotp |E_c| \cdotp O(\frac{n\cdot b!}{(b-n)!} + (n!)^2\cdot (2l)^{2n}\cdotp t)  + O(t)\right).
\]

Since $l\leq t$, $\frac{b!}{(b-n)!}\leq b^n$, $dia(G)-1 \leq b$ and $|E_c|\leq b^2$, we see that $g(\Phi,G)$ is computable in $O(b^{3n+3} t n + (n!)^2 4^n t^{2n+2} b^{2n+3})$ time.

If the number of qubits in $\Phi$ is fixed to a constant, then $g(\Phi,G)$ is computable in polynomial time.

By Proposition <ref>,  if the number of qubits in $\Phi$ is fixed to a constant $c$, then $g(\Phi,G)$ is computable in $O(t^{2c+2}b^{3c+3})$ time. This completes the proof.

If the number of nodes in the coupling graph $G$ is bounded by a constant, or $G$ is a fixed graph, then $g(\Phi,G)$ is computable in polynomial time.

§.§ The complexity of QCM with fixed number of qubits

The QCM problem is computable in polynomial time if the number of qubits is bounded. We further show that it is NL-complete. Before stating the result to be proved, we give two lemmas in the following. The undirected shortest path (USP) problem is

INPUT: An undirected graph $G$, two nodes $\mathbf{s},\mathbf{t}$ in $G$, and a number $k$.

OUTPUT: Yes, if the distance between $\mathbf{s}$ and $\mathbf{t}$ is at most $k$; no, otherwise.

The USP problem is NL-complete <cit.>. We shall show that it is still NL-complete for bounded degree graphs.
For undirected graphs, the degree of a node is the number of edges that are incident to it. The degree of an undirected graph is the maximum of its nodes' degrees.
For directed graphs, the outdegree (indegree) of a node is the number of outcoming (incoming) edges that are incident to it. The outdegree (indegree) of a directed graph is the maximum of its nodes' outdegrees (indegrees).

The USP problem for graphs with maximum degree 3 is NL-complete.

Obviously, the problem is in NL.
To prove the hardness, we make a reduction from the ST-connectivity problem that is NL-complete <cit.>.  Given an instance $(H,\mathbf{s},\mathbf{t})$ of the ST-connectivity problem, where
$H$ is a directed graph and $\mathbf{s},\mathbf{t}$ are two nodes in $H$, it asks whether there is a directed path from $\mathbf{s}$ to $\mathbf{t}$.

Let $a$ be a node in $H$. Denote by $outdegree(a)$ ($indegree(a)$) the outdegree (indegree) of $a$, $deg(H)$ the maximum of the outdegree and indegree of $H$. Define $d = \lceil \log(deg(H)+1) \rceil$, and $T^d_{out}(a)$ ($T^d_{in}(a)$) to be a binary tree of height $d$ such that the number of leaf nodes is $outdegree(a)+1$ ($indegree(a)+1$), and all leaf nodes have depth $d$.
Suppose that $H$ contains $m$ nodes $a_1(=\mathbf{s}),\dots,a_m(=\mathbf{t})$. Define an undirected graph $G$ to contain $m$ rows and each row to contain $m$ nodes.
Let $a_{(i,j)}$ be the $j$-th node in the $i$-th row ($1\leq i \leq m, 1\leq j \leq m$).
For every node $a_{(1,j)}$ ($a_{(m,j)}$) where $1\leq j \leq m$, we attach a tree $T^d_{out}(a_{(1,j)})$ ($T^d_{in}(a_{(m,j)})$) that is an isomorphic copy of $T^d_{out}(a_j)$ ($T^d_{in}(a_j)$) to it, i.e, connect it to the root of the tree.
For every other node $a_{(i,j)}$ ($1< i < m, 1\leq j \leq m$), we attach two trees $T^d_{out}(a_{(i,j)})$ and $T^d_{in}(a_{(i,j)})$ that are isomorphic copies of $T^d_{out}(a_j)$ and $T^d_{in}(a_j)$ respectively to it.
For $1\leq i <m$, we merge a leaf node of $T^d_{out}(a_{(i,j)})$ and a leaf node of $T^d_{in}(a_{(i+1,j)})$, and if there is an edge form $a_j$ to $a_{j'}$ in $H$, then we merge a leaf node of $T^d_{out}(a_{(i,j)})$ and a leaf node of $T^d_{in}(a_{(i+1,j')})$.
Using the trees $T^d_{out}$ and $T^d_{in}$ as the connecting gadget guarantees that the degree of every node in $G$ is at most 3, and the distance between two nodes $a_{(i,j)}$, $a_{(i+1,j')}$ in two consecutive rows is $2d+2$ iff $j=j'$ or there is an edge from $a_j$ to $a_{j'}$ in $H$.

If there is a directed path $\mathbf{s}\rightarrow a_{j}\rightarrow a_{j'}\rightarrow\cdots \rightarrow a_{j''}\rightarrow\mathbf{t}$ in $H$, then there is a path $a_{(1,1)}-\cdots -a_{(2,j)}-\cdots - a_{(3,j')}-\cdots -a_{(i,j'')}-\cdots -a_{(i+1,m)}-\cdots -a_{(m,m)}$ of length $(m-1)(2d+2)$ in $G$. Conversely, if there is a path form $a_{(1,1)}$ to $a_{(m,m)}$ with length at most $(m-1)(2d+2)$, we can deduce that the length is exactly $(m-1)(2d+2)$, since the distance between a node in the first row and a node in the last row is at least $(m-1)(2d+2)$ by the construction of $G$. A path from $\mathbf{s}$ to $\mathbf{t}$ in $H$ can be found by the path form $a_{(1,1)}$ to $a_{(m,m)}$ with length $(m-1)(2d+2)$ in $G$.

So we can reduce $(H,\mathbf{s},\mathbf{t})$ to the instance $(G,a_{(1,1)},a_{(m,m)},(m-1)(2d+2))$ such that there is a directed path from $\mathbf{s}$ to $\mathbf{t}$ iff the distance between $a_{(1,1)}$ and $a_{(m,m)}$ is at most $(m-1)(2d+2)$ in $G$, where the degree of $G$ is at most 3.

When transforming $\Phi$ over a coupling graph $G$ with the initial mapping $\pi$, we define $lw(\Phi,G,\pi)$ to be the least number $h$ of swap operations $\alpha_1,\dots,\alpha_h$ such that

* if $\Psi$ is a subcircuit obtained by reducing $\Phi$ after these $h$ swap operations, then it has the same number of qubits as that of $\Phi$, and

* there is a swap operation $\alpha$ such that if $\Psi'$ is obtained by reducing $\Phi$ after the swap operations $\alpha_1,\dots,\alpha_h,\alpha$, then the number of qubits in $\Psi'$ is strictly less than that of $\Phi$.

Set $lw(\Phi,G)=\min\{lw(\Phi,G,\pi)\mid \pi \text{ is an initial mapping}\}$.

Suppose that $\Phi_1$ and $\Phi_2$ have the same number of qubits, and $\Phi$ is obtained by concatenating $\Phi_1$ and $\Phi_2$, then $lw(\Phi,G)\geq lw(\Phi_1,G)+lw(\Phi_2,G)$.

Given an arbitrary initial mapping $\pi$, $lw(\Phi_1,G)$ is the least number of swap operations
from which a subcircuit of $\Phi$ containing some gates in $\Phi_1$ and all gates in $\Phi_2$ can be obtained. Obviously, any $lw(\Phi_2,G)$ swap operations in the following will produce a subcircuit that has the same number of qubits as that of $\Phi$. Therefore, $lw(\Phi,G)\geq lw(\Phi_1,G)+lw(\Phi_2,G)$.

We will denote by $\Phi^{*h}$ the quantum circuit constituted of $h$ consecutive copies of $\Phi$. It is easily seen that $g(\Phi^{*h},G)\geq lw(\Phi^{*h},G) \geq h\cdot lw(\Phi,G)$ by the above lemma.

If the number of qubits in $\Phi$ is fixed, then the QCM problem is NL-complete.

We first give a nondeterministic algorithm for the QCM problem in Algorithm <ref>. It works as follows.

Step 1 For each initial mapping $\pi_0$ do the following.

Step 2 Reduce $\Phi$ under $\pi_0$ to get a subcircuit of $\Phi$.

Step 3 Nondeterministically guess a swap operation, change to a new mapping $\pi$ and reduce the previous subcircuit to a new subcircuit under $\pi$.

Step 4 Repeat Step 3 at most $k$ times. Accept if an empty circuit occurs, reject otherwise.

A quantum circuit $\Phi$, a coupling graph $G$, and a number $k$.
Yes, if $k$ SWAP gates are enough to tranform $\Phi$; no otherwise.

initial mapping $\pi_0$
Set $N = k$ and $\pi = \pi_0$
$\Psi \leftarrow$ reduce $\Phi$ under $\pi$
$N \geq 1$
$\Psi$ is emptybreak and yes
Nondeterministically guess a swap operation and update $\pi$
$\Psi \leftarrow$ reduce $\Psi$ under $\pi$
$N = N-1$

A nondeterministic algorithm for QCM.

Assume that $\Phi$ has $n$ qubits and $l$ layers. Let $\Psi$ be a subcircuit of $\Phi$, by the definition of circuit type, $\Psi$ corresponds to an $n\times l$ grid $D$ and can be compressed to an $n\times m$ ($m\leq n$) grid $D'$ that is the circuit type of $\Psi$. Define $T_{\Psi}=[(l_1,Q_1),\dots,(l_m,Q_m)]$, where $1\leq l_i\leq l$ and the $D$'s $l_i$-th column is the first one that is the same as the $D'$'s $i$-th column, and $Q_i$ is the set of nodes that are black in the $i$-th column of $D'$.
$\Psi$ can be easily recovered from $\Phi$ and $T_{\Psi}$, we shall use $T_{\Psi}$ instead of $\Psi$ in Algorithm <ref> to save space.

We now consider the space complexity of Algorithm <ref>.
Suppose that there are $b$ nodes in $G$.
The number $N$ and the mapping $\pi$ use $O(\log k)$ and $O(n\log b)$ space to store, respectively. Since $\Psi$ is replaced by $T_{\Psi}$, the space used is $O(n(\log l + n\log n))$. And it takes $O(\log b)$ space to store a swap operation. So the total space used by the algorithm is $O(\log k + n(\log b +\log l + n\log n))$. By Lemma <ref>, we can assume that $k \leq n\cdot l\cdot (dia(G)-1)$. Since $n$ is a constant for all inputs, Algorithm <ref> runs in $O(\log (b+l))$ space.
Hence, the QCM problem is in NL when the number of qubits in $\Phi$ is bounded by a constant.

To prove the hardness, we reduce the USP problem for graphs with maximum degree 3, which is NL-complete by Lemma <ref>, to the QCM problem where the quantum circuit $\Phi$ has $10$ qubits.
Let $\Phi^5_{clique}$ be a quantum circuit with 5 qubits and every pair of qubits are operated by a CNOT gate (see Figure <ref>). The topology graph of $\Phi^5_{clique}$ is a clique of size 5.

1     2     3     4

1     2       3

1                                 2

subgraph K_n [n=5,clockwise,radius=2cm];

The illustration of (a) the quantum circuit $\Phi^5$, and (b) its topology graph.

Given an instance $(G',\mathbf{s},\mathbf{t},k)$ of the USP problem where $G'$ is of maximum degree 3, we define an instance $(\Phi,G,k-1)$ of the QCM problem as follows.

* $G$ is obtained by attaching two cliques of size 4 to $\mathbf{s}$ and $\mathbf{t}$ in $G'$, respectively, i.e., there are edges connecting $\mathbf{s}$ (and $\mathbf{t}$) to all nodes of the clique.

* $\Phi$ is constituted of two parallel copies of $(\Phi^5_{clique})^{*h}$ followed by a CNOT gate that operates on the first qubits of the two copies, where $h$ is the number of nodes in $G'$.

If the distance between $\mathbf{s}$ and $\mathbf{t}$ is at most $k$, we can map the qubits of the two copies $(\Phi^5_{clique})^{*h}$ to the two cliques containing $\mathbf{s}$ and $\mathbf{t}$ in $G$, respectively. Hence, only the last CNOT gate need to be made nearest neighbor when executing it. This can be realized by moving $\mathbf{s}$ or $\mathbf{t}$ along the shortest path between them, where at most $k-1$ SWAP gates are enough.
On the other hand, suppose that $\Phi$ can be transformed to satisfy $G$'s NN constraint using at most $k-1$ SWAP gates.
If the qubits in $(\Phi^5_{clique})^{*h}$ are mapped to the clique containing $\mathbf{s}$ or $\mathbf{t}$, it will use $k'-1$ SWAP gates to  make the last CNOT gate nearest neighbor, where $k'$ is the distance between $\mathbf{s}$ and $\mathbf{t}$. Otherwise, if $(\Phi^5_{clique})^{*h}$ is not implemented on the clique of size 5, then it needs at least $h$ SWAP gates by the construction of $G$ and Lemma <ref>.
Since $k'\leq h$ and at most $k-1$ SWAP gates are enough to transform $\Phi$ over $G$, we have $k'-1\leq k-1$, which implies the distance between $\mathbf{s}$ and $\mathbf{t}$ in $G'$ is at most $k$.

§.§ Fixed-parameter complexity of QCM

Though QCM is computable in polynomial time when the number of qubits is bounded, the algorithm is impractical due to the large hidden constants.
We make an experiment of algorithm <ref> on a linear coupling graph and random quantum circuits with 3 and 4 qubits. Form Table <ref> and Figure <ref> we see that the running time of the quantum circuits of 4 qubits grows much faster than that of the quantum circuits of 3 qubits. This implies that the QCM problem is hard to solve if the number of qubits is fixed to a large number.
Actually, we show that QCM is fixed-parameter intractable parameterized by the number of qubits in the quantum circuit.

title= The value of $g(\Phi, G)$,
xlabel=Number of gates in the circuit,
ylabel=Number of SWAP gates,
legend style=at=(0.25,0.85), anchor=center,
table data11.dat;
table data2.dat;
3 qubits, 4 qubits

title=The running time of Algorithm <ref>,
xlabel=Number of gates in the circuit,
ylabel=Time in seconds,
legend style=at=(0.25,0.85), anchor=center,
table data31.dat;
table data4.dat;
3 qubits, 4 qubits

An illustration of the number of SWAP gates and running time in Table <ref>.

The minimal number of SWAP gates and running time of Algorithm <ref> on a linear coupling graph and random quantum circuits with 3 and 4 qubits, respectively.
2*$gt$$qt$     2c|3 qubits     2c|4 qubits
$gt_{add}$       $time$       $gt_{add}$       $time$
3       0.063     $1.95\times 10^{-4}$      0.111       $5.59 \times 10^{-4}$
4       0.177     $2.91\times 10^{-4}$      0.243       $1.07\times 10^{-3}$
5       0.299     $4.20\times 10^{-4}$      0.471       $2.03\times 10^{-3}$
6       0.461     $5.76\times 10^{-4}$      0.687       $3.27\times 10^{-3}$
7       0.578     $7.06\times 10^{-4}$      1.015       $6.62\times 10^{-3}$
8       0.726     $8.96\times 10^{-4}$      1.276       $1.10\times 10^{-2}$
9       0.901     $1.12\times 10^{-3}$      1.568       $1.46\times 10^{-2}$
10      1.033     $1.31\times 10^{-3}$      1.794       $2.05\times 10^{-2}$
11      1.191     $1.58\times 10^{-3}$      2.117       $4.18\times 10^{-2}$
12      1.318     $1.86\times 10^{-3}$      2.406       $5.29\times 10^{-2}$
13      1.466     $2.22\times 10^{-3}$      2.691       $9.34\times 10^{-2}$
14      1.616     $2.63\times 10^{-3}$      2.933       $1.67\times 10^{-1}$
15      1.792     $3.08\times 10^{-3}$      3.278       $3.15\times 10^{-1}$
16      1.930     $3.62\times 10^{-3}$      3.604       $8.37\times 10^{-1}$
17      2.081     $4.14\times 10^{-3}$      3.818       $2.013$
18      2.247     $4.92\times 10^{-3}$      4.102       $5.814$
19      2.379     $5.69\times 10^{-3}$      4.390       $10.33$
20      2.452     $6.23\times 10^{-3}$      4.798       $29.50$
21      2.756     $8.30\times 10^{-3}$      5.056       $49.50$
22      2.769     $8.35\times 10^{-3}$      -      -
23      2.929     $9.58\times 10^{-3}$      -      -
24      3.111     $1.14\times 10^{-2}$      -      -
25      3.262     $1.36\times 10^{-2}$      -      -

$gt$: the number of gates in the circuit; $qt$: the number of qubits in the circuit; $gt_{add}$: the average number of SWAP gates added; $time$: the average running time in seconds.

A problem is fixed-parameter tractable if it is computable in time $f(p)\cdotp |x|^{O(1)}$, where $|x|$ is the size of input $x$, $p$ is a parameter, and $f$ is an arbitrary computable function.
Hence, if $p$ is fixed and $f(p)$ is relatively small, the problem can still be considered as tractable.
The $W$ hierarchy is a collection of complexity classes of parameterized problems to capture the fixed-parameter intractability. For $i\geq 0$, $W[i]\subseteq W[i+1]$ and $W[0]$ is the set of fixed-parameter tractable problems.
$W[1]$ can be regarded as a parameterized version of the complexity class NP.
Every $W[j]\,(j\geq 1)$-hard problem is believed to be fixed-parameter intractable.
For more details of the parameterized complexity we refer the reader to <cit.>.

The QCM problem parameterized by the number of qubits in $\Phi$ is $W[1]$-hard.

We make a fixed-parameter reduction from the clique problem to the QCM problem.
The clique problem is that given an undirected graph $G$ and a number $n$, decide whether there is a clique of size $n$ in $G$. The clique problem is NP-complete, and it is $W[1]$-complete when parameterized by the size $n$ of the clique <cit.>.

Given an instance $(G,n)$ of the clique problem, we reduce it to the instance $(\Phi^n_{clique},G,0)$ of the QCM problem.
$\Phi^n_{clique}$ is a quantum circuit with $n$ qubits and every pair of qubits are operated by a CNOT gate. It is easily seen that the topology graph of $\Phi^n_{clique}$ is a clique of size $n$. By the construction of $(\Phi^n_{clique},G,0)$ we see that the clique problem is fixed-parameter reducible to the QCM problem with the size of the clique and the number of qubits as parameters, respectively.
It is obvious that $G$ contains a clique of size $n$ iff $\Phi^n_{clique}$ satisfies $G$'s NN constraint without using any SWAP gates.

The QCM problem is an NP optimization problem aiming to find the least number of swap operations.
Since an NP optimization problem has a fully polynomial-time approximation scheme only if it is fixed-parameter tractable <cit.>.
Proposition <ref> implies the hardness of the QCM problem.

§.§ The complexity of QCM on constrained quantum circuits and coupling graphs

Shallow quantum circuits are the kind of quantum circuits with fixed depth. They are strictly more expressive than the classical shallow circuits <cit.>. Due to their robustness to noise and decoherence, shallow quantum circuits are easily to implement on NISQ devices. In the following we consider the QCM problem on shallow quantum circuits.
First we prove a proposition about the degree of shallow quantum circuits' topology graphs.

The topology graph of every quantum circuit with maximum depth $d$ is a graph with maximum degree $d$, and every graph with maximum degree $d$ is the topology graph of a quantum circuit with maximum depth $d+1$.

Suppose that $\Phi$ is a quantum circuit with maximum depth $d$. Since a qubit is operated by at most one two-qubit gate in a layer, every qubit in $\Phi$ is operated by at most $d$ two-qubit gates. Hence, the topology graph of $\Phi$ is of maximum degree $d$ .

Conversely, given a graph $H$ with maximum degree $d$, we construct a quantum circuit $\Phi$ with maximum depth $d+1$ and its topology graph is $H$. By Vizing's theorem, we can use at most $d+1$ colors to color every edge in $H$ such that no two incident edges have the same color. So the edges in $H$ can be partitioned into at most $d+1$ sets where the edges in the same set do not have common endpoints. Define the numbers of qubits and layers of $\Phi$ to equal the numbers of nodes and colored edge sets of $H$, respectively, and two qubits are operated by a CNOT gate in the $i$-th layer iff there is an edge connecting them in the $i$-th edge set. It follows easily that $H$ is the topology graph of $\Phi$.

The QCM problem is NP-complete on shallow quantum circuits and bounded degree coupling graphs. More precisely, it is NP-complete if

* the quantum circuit has maximum depth 3, and the coupling graph is a planar bipartite and 3-degree bounded graph, or

* the quantum circuit has maximum depth 2, and the coupling graph is a 4-degree bounded graph.

We make reductions from the Hamiltonian cycle problem which is NP-complete for planar bipartite graphs with maximum degree 3 <cit.>.
The undirected path and cycle graphs have maximum degree 2. By Proposition <ref>, there are quantum circuits with maximum depth 3 such that their topology graphs are path or cycle graphs.
Let $n\geq 1$, define quantum circuit $\Phi^n_{path} = \langle Q, \varGamma \rangle$, where

* $Q = \{q_1,\dots,q_n\}$, and $\Gamma = (g_1,\dots,g_{n-1})$ is a sequence of CNOT gates,

* for every $q_i\in Q\,(1\leq i < n)$, the qubits $q_i,q_{i+1}$ are operated by the gate $g_i$,

* the gates $\{g_i\mid i\text{ is odd}\}$ are in the 1st layer and the other gates are in the 2nd layer.

Define quantum circuit $\Phi^n_{cycle}$ by adding a CNOT gate $g_n$ to $\Phi^n_{path}$ such that $g_n$ is the last gate to execute in the circuit and operates on $q_1,q_n$. Figure <ref> shows the quantum circuits $\Phi^n_{path}$ and $\Phi^n_{cycle}$ where $n$ is odd.  It is easily seen that the topology graphs of $\Phi^n_{path}$ and $\Phi^n_{cycle}$ are a path and a cycle, respectively.
We have $depth(\Phi^n_{path})=2$, and $depth(\Phi^n_{cycle})=2$ if $n$ is even and $depth(\Phi^n_{cycle})=3$ if $n$ is odd.

$q_1$     1

$q_2$            1

$q_3$     1

$q_{n-2}$     1

$q_{n-1}$           1

[every arrow/.append style=dash]
[draw, circle, inner sep=2pt]$q_1$ [d]

[draw, circle, inner sep=2pt]$q_2$ [d]

[draw, circle, inner sep=2pt]$q_3$ [d]

[draw, circle, inner sep=2pt]$q_n$ [u]

$q_1$     1          7

$q_2$            1

$q_3$     1

$q_{n-2}$     1

$q_{n-1}$           1

in 1,...,3

[draw, circle, inner sep=2pt] at (72 * (- 1):) $q_\s$;
[black] (72 * (- 1)+:) arc (72 * (- 1)+:72 * ()-:);

[draw, circle, inner sep=2pt] at (216:) $q_4$;
[black,dashed] (216+:) arc (216+:288-:);
[draw, circle, inner sep=2pt] at (288:) $q_n$;
[black] (288+:) arc (288+:360-:);

An illustration of (a) the quantum circuit $\Phi^n_{path}$ whose topology graph is a path, and (b) the quantum circuit $\Phi^n_{cycle}$ whose topology graph is a cycle.

To prove (1),  we reduce an instance $G$ of the Hamiltonian cycle problem, where $G$ is a planar bipartite and 3-degree bounded graph, to the instance $(\Phi^n_{cycle},G,0)$ of the QCM problem, where $n$ is the number of nodes in $G$. $\Phi^n_{cycle}$ has maximum depth 3. $G$ has a Hamiltonian cycle iff $\Phi^n_{cycle}$ satisfies $G$'s NN constraint without using any SWAP gates.

To prove (2), given a graph $G$ with maximum degree 3 and $n$ nodes, we make the reduction as follows.

* If there is a node $a$ with degree 2 in $G$, then define a graph $H$ by attaching two new nodes to $a$ and one of its adjacent nodes, respectively. The two new nodes have degree 1, and the degree of $H$ is at most 4, since the degree of one neighbor of $a$ is increased exactly by 1. We reduce $G$ to the instance $(\Phi^{n+2}_{path},H,0)$ of QCM.

* If no node in $G$ has degree 2,  then $H$ is obtained by attaching a new node to a node $a$ in $G$, and connecting one endpoint $b$ of a new edge $(b,c)$ to the neighbors of $a$. The degree of $b$ is at most 4, the degrees of $c$ and the new node connected to $a$ are 1, and the degrees of neighbors of $a$ are increased by 1. So $H$ has maximum degree 4. We reduce $G$ to the instance $(\Phi^{n+3}_{path},H,0)$ of QCM.

$G$ has a Hamiltonian cycle iff $H$ has a Hamiltonian path (the two new nodes with degree 1 are the start and end points, respectively) iff $\Phi^{n+2}_{path}$ (or $\Phi^{n+3}_{path}$) satisfies $H$'s NN constraint without using any SWAP gates.

The newest IBM quantum devices have adopted the heavy-hex lattice architecture, in which each unit cell consists of a hexagonal arrangement of qubits. The heavy-hex lattice is equivalent to the square lattice up to a constant overhead <cit.>.
Both of them are grids constituted of regular unit graphs.
The squares, regular hexagons and equilateral triangles are the only three kinds of regular polygons that can tile the plane by themselves.

(0,0) grid (7,6);

[hexa/.style= shape=regular polygon,regular polygon sides=6, minimum size=1cm,draw,rotate=30]

ȷin 0,...,6
ıin 0,...,6[hexa] at ((ı-Mod(ȷ,2)/2)*sin(60),ȷ*0.75);

[triag/.style= shape=regular polygon,regular polygon sides=3, minimum size=1cm,draw,rotate=0]
ȷin 0,...,6
ıin 0,...,6[triag] at ((ı-Mod(ȷ,2)/2)*sin(60),ȷ*0.75);

(-0.88,5) – ++(0:6.08);
(-0.44,-0.25) – ++(120:0.85);
(-0.44,1.25) – ++(120:0.85);
(-0.44,2.75) – ++(120:0.85);
(-0.44,4.25) – ++(120:0.85);
(5.63,1.25) – ++(60:-0.85);
(5.63,2.75) – ++(60:-0.85);
(5.63,4.25) – ++(60:-0.85);

There kinds of grids that are made of (a) the squares, (b) the regular hexagons and (c) the equilateral triangles.

The grid graph is a finite induced subgraph of the (infinite) grid shown in Figure <ref>.
Using a similar proof of Proposition <ref>, the following proposition can be proved by the fact that the Hamiltonian cycle problem is still NP-complete on square grid graphs <cit.>, regular hexagon grid graphs <cit.> and equilateral triangle graphs <cit.>.

The QCM problem is NP-complete on grid coupling graphs.

§.§ The complexity of QCM with fixed number of SWAP operations

The number of swap operations is another parameter of the QCM problem. By Lemma <ref> we know that for any quantum circuit $\Phi$ and coupling graph $G$, an upper bound of it can be computed easily. We prove that the complexity of QCM with a fixed number of swap operations is still NP-complete.

The QCM problem is NP-complete for each fixed $k\geq 0$.

It is trivial for the case $k=0$, where the subgraph isomorphism problem can be reduced to this problem.

Suppose that $k\geq 1$. We make a reduction from the Hamiltonian cycle problem that is still NP-complete on graphs of maximum degree 3 <cit.>. The proof is similar to that of Proposition <ref>.
Given an undirected graph $H$ of maximum degree 3, we construct an instance $(\Phi,G,k)$ such that there is a Hamiltonian cycle in $H$ iff $\Phi$ can be transformed to satisfy $G$'s NN constraint using at most $k$ SWAP gates.
Define $\Phi$ to contain parallelly two quantum circuits $(\Phi^5_{clique})^{*2k}$ and $(\Phi^n_{cycle})^{*2k}$ followed by a CNOT gate that operates on the first qubits of them, where $n$ is the number of nodes in $H$. Define $G$ to be a graph that connects a clique of size 5 to a node in $H$ using a path of length $k+1$.

If there is a Hamiltonian cycle in $H$, we can map the qubits of $(\Phi^5_{clique})^{*2k}$ to the clique of size 5 in $G$, and the qubits of $(\Phi^n_{cycle})^{*2k}$ to the Hamiltonian cycle in $H$. To make the last CNOT gate nearest neighbor, we can move the qubits operated by it along the path between the clique and the cycle, where at most $k$ SWAP gates are enough.
If there is not a Hamiltonian cycle in $H$, it needs more than $k$ SWAP gates whatever we transform $\Phi$ on $G$ by Lemma <ref>. So if $\Phi$ can be transformed to satisfy $G$'s NN constraint using at most $k$ SWAP gates, then there must be a Hamiltonian cycle in $H$.

§ DISCUSSION

Quantum circuit mapping is an important procedure for running quantum circuits on NISQ devices. It transforms quantum circuits to be compliant with the nearest neighbor constraint by adding SWAP gates. The QCM problem is an NP-complete optimization problem aiming to find the minimal number of SWAP gates. So it is unlikely to get a polynomial time algorithm for it.
We study the parameterized complexity of QCM in the paper. First we give an exact algorithm that computes the minimal number of SWAP gates. The complexity analysis shows that the algorithm runs in polynomial time if the coupling graph is fixed. And if the number of qubits of the quantum circuit is fixed to a constant, the QCM problem is NL-complete, which is believed strictly below the complexity class P. We prove by a reduction from the undirected shortest path problem for graphs with maximum degree 3. Further, taking the number of qubits of the quantum circuit as a parameter, we show that QCM is W[1]-hard by a reduction from the clique problem. Every problem in W[1] is considered as fixed-parameter intractable. Hence, the QCM problem is unlikely to have a fully polynomial-time approximation scheme.

The depth of the quantum circuits and the type of the coupling graphs are the other two parameters considered in the paper. We show that the QCM problem is still NP-complete over shallow quantum circuits, and planar, bipartite and degree bounded coupling graphs. These results indicate that the number of qubits is the key factors that affect the complexity of QCM. Efficient algorithms for the quantum circuit mapping from quantum circuits with a reasonable number of qubits to a fixed quantum device are theoretically possible, and actually many such algorithms already exist. Algorithm <ref> can be easily adapted to save the swap actions to get a solution while computing the minimal number. But it is not practical since the result in Figure <ref> shows that the time increases significantly as the number of qubits becomes larger. So finding a novel algorithm is one of the future work.

All active IBM Quantum devices have adopted the heavy-hex lattice architecture. It is a kind of grid constituted of regular hexagons with high scalability. We also prove that QCM is NP-complete on three kinds of grid coupling graphs that are finite induced subgraphs of the (infinite) grids made up of the squares, regular hexagons and equilateral triangles, respectively. But it is still open for the complexity of QCM over some simple coupling graphs, e.g., the linear and cycle coupling graphs. Furthermore, if the coupling graph is fixed, the complexity of QCM is obviously in NL by Proposition <ref>, we conjecture that it is in LOGSPACE. This means that the effective quantum circuit mapping for a specified quantum device is always possible. Whether QCM is fixed-parameter tractable parameterized by the number of nodes in the coupling graph is another open problem. These can be the future work of the paper.