Quantum Circuit Mapping

Many quantum computing architectures limit the pairs of qubits that two-qubit operations can be applied to. This is commonly described by a device’s coupling map. To execute a generic quantum circuit (with arbitrary interactions between its qubits) on such an architecture, the circuit needs to be mapped. This involves qubit allocation, where logical qubits are assigned to physical qubits in an initial layout, and routing, where the original circuit is augmented with SWAP gates such that it adheres to the target device’s coupling map.

Consider the following circuit.

 1from qiskit import QuantumCircuit
 2
 3qc = QuantumCircuit(4)
 4qc.h(0)
 5qc.cx(0, 1)
 6qc.cx(0, 2)
 7qc.cx(0, 3)
 8
 9qc.barrier()
10
11qc.t(0)
12qc.t(1)
13qc.t(2)
14qc.t(3)
15
16qc.barrier()
17
18qc.cx(0, 3)
19qc.cx(0, 2)
20qc.cx(0, 1)
21
22qc.measure_all()
23
24qc.draw(output="mpl")
_images/df71bf0fb7a643c2fd264c4358fda1716e13aa9819c6ba7f8c1dffc72f7f4f61.svg

Now assume this circuit shall be mapped to a \(4\)-qubit architecture defined by the following coupling map:

Linear 4-qubit Architecture

In QMAP this architecture can be manually defined as follows.

 1from mqt.qmap.sc import Architecture
 2
 3arch = Architecture(
 4    4,
 5    {
 6        (0, 1),
 7        (1, 0),
 8        (1, 2),
 9        (2, 1),
10        (2, 3),
11        (3, 2),
12    },
13)

The quantum circuit qc can not be run directly on this architecture since it contains gates that act on qubits not connected on the device architecture. Naively inserting SWAP gates that permute the logical-to-physical qubit mapping on the fly may yield the following compiled circuit.

_images/c4088132a691aa4f3c417341b1074f7b43e8fa7f0ca28fcf70db1442ae263bfe.svg

Over the course of the mapping, four SWAP gates have been introduced to satisfy the connectivity constraints of the device’s architecture. Since every additional gate increases the probability of errors, this is a very costly overhead for such a small circuit.

Keeping the number of additionally introduced gates as small as possible is key for ensuring the successful execution of the quantum circuit. Finding an optimal mapping for a quantum circuit is an NP-hard problem Botea et al.. QMAP offers two dedicated techniques for tackling that problem:

  • An exact mapping approach (based on [2], [3]) that guarantees (gate-optimal) solutions and is typically suitable for up to 8 qubits.

  • A heuristic mapping approach (based on [4], [5]) that allows to determine efficient mapping solutions in a scalable fashion for up to hundreds of qubits.

Exact Mapping

The exact mapper implemented in QMAP maps quantum circuits using the minimal number of SWAP gates. To this end, it encodes the mapping task as a MaxSAT problem and subsequently solves it using the SMT solver Z3. Due to the NP-hardness of the mapping task, this approach is only scalable up to roughly eight qubits in most scenarios.

Note

On directional architectures, it can be significantly cheaper to surround a CNOT gate with four Hadamard operations (effectively exchanging its control and target) instead of adding a SWAP gate. For these architectures, QMAP minimizes the number of additional SWAP and H gates.

Using the exact mapper is as simple as:

1from mqt.qmap.plugins.qiskit.sc import compile
2
3qc_mapped, res = compile(qc, arch, method="exact", post_mapping_optimizations=False)
4
5qc_mapped.draw(output="mpl")
_images/be382c8eac0ff351e6353d126b7fb1d6273258c173d0a34d5897ed556b5c84a3.svg
1print(f"Additional SWAPs: {res.output.swaps}")
2print(f"Runtime:          {res.time:f}")
Additional SWAPs: 2
Runtime:          0.028705

The resulting solution only requires two SWAP gates for mapping the circuit.

Note

The exact mapping method implemented in QMAP is optimal with respect to the number of additional SWAP gates needed for mapping a given circuit. It is not guaranteed to be optimal with respect to the number of additional gates needed for mapping a given circuit, e.g., any sequence of a SWAP gate and a CNOT gate acting on the same qubits can be simplified to just two CNOT gates. Such an optimization pass is conducted by default in the compile method after the circuit has been mapped. However, this cost reduction is not accounted for in the SAT formulation at the moment.

Heuristic Mapping

The heuristic mapper implemented in QMAP uses A*-search to efficiently traverse the immense search space of the mapping problem. It effectively trades optimality for runtime. This allows to reliably determine suitable mappings for circuits with up to hundreds of qubits. Using the heuristic mapper works completely analogous to the exact mapper.

1qc_mapped, res = compile(qc, arch, method="heuristic", post_mapping_optimizations=False)
2
3qc_mapped.draw(output="mpl")
_images/5adc361719126c7c5e473115a57a3beccf88627daf18a11152e9000041bb116d.svg
1print(f"Additional SWAPs: {res.output.swaps}")
2print(f"Runtime:          {res.time:f}")
Additional SWAPs: 3
Runtime:          0.000056

While this solution is not optimal anymore it only requires one more SWAP gate and even for such a small example the heuristic mapper is orders of magnitudes faster than the exact mapper.