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 1 year 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