If the question is about adding SWAP
gates and then revert that swapping, you can create a circuit with the swaps in the front, then put your remapped circuit, and then finish with the inverse of the first one. Here is the process:
- Create a circuit with the
SWAP
gates:
A transpiler pass called LayoutTransformation
allows to create a circuit composed by SWAP
gates that change a layout out to another one ([0, 2, 4, 1, 3, 5]
, in this case). It is a bit obscure to use. Let me know if you want to know more:
from qiskit.transpiler.passes import LayoutTransformation
from qiskit.transpiler import Layout, CouplingMap
swap_front = QuantumCircuit(circ.num_qubits)
layout_transformation = LayoutTransformation(CouplingMap.from_full(circ.num_qubits),
Layout.generate_trivial_layout(*circ.qregs),
Layout.from_intlist([0, 2, 4, 1, 3, 5], *circ.qregs))
swap_front = layout_transformation(swap_front).inverse()
swap_front.barrier() # you can remove this line. Only here for didactic purpose
swap_front.draw('mpl')
- Remap your circuit to the new layout:
from qiskit import transpile
new_circ = transpile(circ, initial_layout=[0, 2, 4, 1, 3, 5])
new_circ.draw('mpl')
With the parameter initial_layout
you can choose a physical qubit for each of the qubits in your register. In this case m_0 -> 0
, m_1 -> 2
, ...
- Undo the swapping:
If a circuit is invertible, you can inverse it with the method inverse
. The inverse of the first circuit can undo the swapping
swap_back = swap_front.inverse()
swap_back.draw('mpl')
- Putting all together:
final = swap_front + new_circ + swap_back
final.draw('mpl')