# Changing indices of qubits in QISKit

I was wondering if something like this is possible in QISKit: let's say we have two registers containing target and ancilla qubits:

$a_0$ -------------------

$a_1$--------------------

$\vdots$

$a_4$ ------------------

$t_0$ ------------------

$t_1$ ------------------

$\vdots$

$t_4$ ------------------

These two registers are stored in one quantum register qr. So to access $a_0$ we would type qr, to access $a_1$ - qr, ..., for $t_5$ - qr. We can pass this quantum register as an argument to some function:

foo(qr, ...)


What I want to do is to interleave the ancilla and target qubits:

$a_0$ -------------------

$t_0$--------------------

$\vdots$

$a_i$ ------------------

$t_i$ ------------------

$\vdots$

$a_4$ ------------------

$t_4$ ------------------

so to access $a_0$ I would type qr, for $t_1$ - qr and so on. Finally, I would like to pass such changed quantum register qr' again as an argument to some function

foo(qr', ...)


and in this function I would like to use these changed indices. Is this possible? Other solution I figured out was to pass array of indices for ancilla and target qubits, but I would like to avoid that. Another option would be to use swap gates on these qubits

• I think an array of indices is your best bet, but I'll cross post to the QISKit Slack for a second opinion. Jun 15 '18 at 13:22
• So you want to change the order of the qubits? You could use SWAP gates in order to perform the interleaving of the qubits. Jun 15 '18 at 13:52

The relationship between your indices can be captured by a map:

$$\{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 1, 6: 3, 7: 5, 8: 7, 9: 9\}$$

You can then use this to specify where operations get applied to in a register.

Here is a simple code in QISKit (generalizes to arbitrary register length):

from qiskit import *
from qiskit.tools.visualization import *

# build a register with k targets and k ancillas
k = 5
qr = QuantumRegister(2*k)
circ = QuantumCircuit(qr)

# apply cx between ancillas and targets
for i in range(k):
circ.cx(qr[i], qr[i+k])

circuit_drawer(circ) # specify the desired interleaving
# {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 1, 6: 3, 7: 5, 8: 7, 9: 9}
new_qubit_map = {i: 2*i if i < k else 2*(i-k)+1 for i in range(len(qr))}

# create the same circuit, but with the new interleaving
circ_2 = QuantumCircuit(qr)
for i in range(k):
circ_2.cx(qr[new_qubit_map[i]], qr[new_qubit_map[i+k]])

circuit_drawer(circ_2) 