qml.transforms.transpile¶
- transpile(tape, coupling_map, device=None)[source]¶
Transpile a circuit according to a desired coupling map
Warning
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.- Parameters
tape (QNode or QuantumTape or Callable) – A quantum tape.
coupling_map – Data specifying the couplings between different qubits. This data can be any format accepted by
nx.to_networkx_graph()
, currently including edge list, dict of dicts, dict of lists, NetworkX graph, 2D NumPy array, SciPy sparse matrix, or PyGraphviz graph.
- Returns
The transformed circuit as described in
qml.transform
.- Return type
qnode (QNode) or quantum function (Callable) or tuple[List[QuantumTape], function]
Example
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(circuit, coupling_map=[(0, 1), (1, 3), (3, 2), (2, 0)]) >>> transpiled_qnode = qml.QNode(transpiled_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