# How to re-ordering physical qubit location into desired one?

Suppose original quantum circuit have 4 qubits and some quantum operation. Also suppose the result of original quantum circuit state is '1100'. Then apply qubit mapping then mapping state is like below

1 -> 0
3 -> 1
0 -> 2
2 -> 3


Then the result of quantum circuit after qubit mapping will be '1010'. But I want to know how to change these physical qubit state location. So ultimately, I want to see the result after qubit mapping like '1100' which is the same result of original quantum circuit.

How can I do that?

• You can try swap gates, maybe with some ancilla. Jul 24 at 6:26
• I don't agree with your opinion. Because swap only can change the coupling graph of physical quantum hardware. But what I really want to see is how to re-ordering physical qubit location to check the mapped quantum circuit's result more easily.
– 김동민
Jul 24 at 7:46

I will answer using Python, but the underlying method can be easily adapted to another language.

You can represent the mapping you describe in your question

1 -> 0
3 -> 1
0 -> 2
2 -> 3


as a map, or a Python dictionary in this case (it can also be represented as a simple array, but this complicates a little bit the code):

logical2hardware_mapping = {0: 1, 1: 3, 2: 0, 3: 2}


From this map representation, you can post-process your result quite easily by re-arranging the bits:

import typing as ty

def rearrange_hardware_result(hardware_result: str, mapping: ty.Dict[int, int]) -> str:
# 1. Invert the mapping
hardware2logical_mapping = {v: k for k, v in mapping.items()}
# 2. Construct the rearranged result
return "".join(
hardware_result[hardware2logical_mapping[i]]
for i in range(len(hardware_result))
)


Finally, you can test the function on your example and check that it works as expected:

hardware_result = "1010"
print(rearrange_hardware_result(hardware_result, logical2hardware_mapping))
# Output "1100"


Side note: I worked quite a bit with these issues of mapping and how to change them. The most important thing to do at the very beginning in order to understand what you are doing is to note somewhere what is represented. For example in your mapping, you do not explicitly say what the indices on the left-hand side of the arrows are, same for the right-hand side ones. Once the meaning of the indices you are using is clear, everything starts to become easier.