transpile(tape: pennylane.tape.tape.QuantumTape, coupling_map: Union[List, networkx.classes.graph.Graph])[source]

Transpile a circuit according to a desired coupling map


This transform does not yet support measurements of Hamiltonians or tensor products of observables. If a circuit is passed which contains these types of measurements, a NotImplementedError will be raised.

  • tape (function) – A quantum function.

  • coupling_map (list[tuple(int, int)] or nx.Graph) – Either a list of tuples(int, int) or an instance of networkx.Graph specifying the couplings between different qubits.


the transformed quantum function

Return type



Consider the following example circuit

def circuit():
    qml.CNOT(wires=[0, 1])
    qml.CNOT(wires=[2, 3])
    qml.CNOT(wires=[1, 3])
    qml.CNOT(wires=[1, 2])
    qml.CNOT(wires=[2, 3])
    qml.CNOT(wires=[0, 3])
    return qml.probs(wires=[0, 1, 2, 3])

which, before transpiling it looks like this:

0: ──╭●──────────────╭●──╭┤ Probs
1: ──╰X──╭●──╭●──────│───├┤ Probs
2: ──╭●──│───╰X──╭●──│───├┤ Probs
3: ──╰X──╰X──────╰X──╰X──╰┤ Probs

Suppose we have a device which has connectivity constraints according to the graph:

0 --- 1
|     |
2 --- 3

We encode this in a coupling map as a list of the edges which are present in the graph, and then pass this, together with the circuit, to the transpile function to get a circuit which can be executed for the specified coupling map:

>>> dev = qml.device('default.qubit', wires=[0, 1, 2, 3])
>>> transpiled_circuit = qml.transforms.transpile(coupling_map=[(0, 1), (1, 3), (3, 2), (2, 0)])(circuit)
>>> transpiled_qnode = qml.QNode(circuit, dev)
>>> print(qml.draw(transpiled_qnode)())
0: ─╭●────────────────╭●─┤ ╭Probs
1: ─╰X─╭●───────╭●────│──┤ ├Probs
2: ─╭●─│──╭SWAP─│──╭X─╰X─┤ ├Probs
3: ─╰X─╰X─╰SWAP─╰X─╰●────┤ ╰Probs

A swap gate has been applied to wires 2 and 3, and the remaining gates have been adapted accordingly