mapomatic icon indicating copy to clipboard operation
mapomatic copied to clipboard

Deflate circuit should retain qubit register ordering to prevent incorrect mapping upon transpilation to new layout

Open derek-wang-ibm opened this issue 3 years ago • 4 comments

As an example, consider a logical circuit (circuit A) with logical qubits 0, 1, and 2. Let's assume there's only one two-qubit gate between logical qubits 0 and 2. Therefore, optimal transpilation to a physical architecture with nearest-neighbor coupling between physical qubits will result in a circuit (circuit B) with e.g. logical qubit 0, 1, and 2 mapping to physical qubits 0, 2, and 1.

Now, say we want to use mapomatic to find the best qubits on said device. The standard procedure in the README is to first deflate the circuit (to circuit C). This results in a circuit with three logical qubits labelled, in order, 0, 1, and 2--note that the original qubit register ordering has been lost! So say we find that the best physical qubits are 3, 4, and 5 for logical qubits 0, 2, and 1 that have been mistakenly labelled 0, 1, and 2 by mm.deflate_circuits as qubits 0, 1, and 2. If we now re-transpile not the deflated circuit (circuit C), but the original transpiled circuit (circuit B) onto this new layout, because the qubit ordering has been lost, we'll find that logical qubits 0, 1, and 2 will be mapped onto physical qubits 3, 4, and 5. But we know that logical qubits 0 and 2 have a 2Q gate, so because qubits 3 and 5 are not directly coupled, transpilation will necessarily add a (costly) SWAP! Note, however, that the logical circuit is preserved, however, because the ordering of the classical registers does not get lost during deflation, which may contribute to why this issue had no been previously noticed.

And finally, one can argue that one should not use the "best" layout found for circuit C on another circuit (circuit B, or an entirely differently circuit D), so one could just apply this new layout directly on the deflated circuit (circuit C) and avert these problems. However, it is often the case that one finds the best layout for one circuit and uses that circuit again and again with different parameters (say, in a variational problem) or repeated different times (say, for time evolution). In these cases, the circuit has the same coupling map and the evaluated cost function is likely to result in the same layout for different parameters/repetition number, so it is advantageous to just use mapomatic one time on one prototypical circuit and then transpile all other circuits to the same layout.

derek-wang-ibm avatar Aug 24 '22 13:08 derek-wang-ibm

I am not sure I follow here. Why are you not using the deflated circuit here and just inflating it to the correct layout? In some sense the labeling of qubits is irrelevant, but I do agree that at least the numeric ordering should be preserved. I will look into that.

For the second part, yes, I would not use mapomatic over and over again for changes to angles in a parameterized circuit as they have zero error, being performed by virtual gates.

nonhermitian avatar Aug 24 '22 13:08 nonhermitian

A quick look into this suggests that we need to sort the active qubit indices before creating the mapping from original to deflated circuit

nonhermitian avatar Aug 24 '22 13:08 nonhermitian

Yes, sorting the active qubit indices would probably fix this.

One might not want to just inflate the deflated circuit in cases where the mapping is to be re-used across different circuits that, because of some structure, will likely result in the same mapping. True, perhaps the example of a parametrized circuit is not a good one, but time evolution certainly is when one wants to measure some observable for N time steps. The evolution circuit is just repeated again and again, so there's no need to find the best qubits for each time step--it's more efficient to transpile the circuit for one of the time steps, find the best layout, and apply it during transpilation of circuits for each time step. With the qubit ordering lost, however, this transpilation on all circuits may result in unnecessary SWAPs.

derek-wang-ibm avatar Aug 24 '22 14:08 derek-wang-ibm

Actually, it looks like we are sorting already: https://github.com/Qiskit-Partners/mapomatic/blob/ade8cc814e711cf2bc2a57bdce20d2169e8d6d70/mapomatic/circuits.py#L38

But looking into this did uncover a different issue #43

Do you have a concrete example of the issue you are having?

nonhermitian avatar Aug 24 '22 15:08 nonhermitian