1

I have a circuit with two registers, m, and q.

from qiskit import execute, Aer, IBMQ, QuantumCircuit

m = QuantumRegister(3, "m")
q = QuantumRegister(3, "q")
cl = ClassicalRegister(2, "cl")
circ = QuantumCircuit( m, q,cl)

circ.x([q[0],q[1],q[2]]) 

How can I SWAP them, in Qiskit, in a way that the order of qubits turns to m0,q0,m1,q1,m2,q2.

user9318
  • 111
  • 2
  • 1
    What do you mean by swapping? Like, do you want a different order in the drawing of the circuit? – luciano Mar 29 '21 at 09:53
  • Not quite! I'd like to use Qiskit's SWAP gate to swap the Qubits physically (for the algorithms I'm implementing). I was able to reorder the qubits using numpy's `zip`, but this is not exactly what I'm looking for. Is there any way we could do this in Qiskit? Seems to be trivial but it is actually not that easy. – user9318 Mar 29 '21 at 11:18

1 Answers1

0

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:

  1. 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')

enter image description here

  1. 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')

enter image description here

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, ...

  1. 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')

enter image description here

  1. Putting all together:
final = swap_front + new_circ + swap_back
final.draw('mpl')

enter image description here

luciano
  • 205
  • 2
  • 10
  • Thanks @Luciano, but is this reversible? The idea of the algorithm is to zip-swap two registers, do some operations, and then reverse the swapping before the next operations and the measurement. – user9318 Mar 30 '21 at 05:46
  • I think I understand your question better now. Editing. Let me know! – luciano Mar 30 '21 at 09:56
  • Thanks for the update. In my first comment, I was talking specifically about `initial_layout` and whether it is reversible. Which I guess it is not. I am familiar with `inverse()` in general. The other problem with `initial_layout` is that I have other operations before the swap. So it creates a layout that I don't want to start my circuit with. – user9318 Mar 31 '21 at 08:08
  • transpile with `initial_layout` will convert your circuit to a "physically mapped circuit" and that's not reversable indeed. I suggest you compose your final circuit by hand... I dont fully get your use case yet. – luciano Mar 31 '21 at 08:30