Closed ecpeterson closed 2 years ago
I like the proposed solution of an analysis pass to detect for gates outside the basis and condition running the basis translation passes on that. We only added the _unroll
piece to the optimization loop in https://github.com/Qiskit/qiskit-terra/pull/6133 and https://github.com/Qiskit/qiskit-terra/pull/5671 as a stop gap for two passes injecting gates outside the basis into the circuit, mainly commutative cancellation (which has been fixed to be basis aware since https://github.com/Qiskit/qiskit-terra/pull/5672 ) and gate/cx direction. But if we detected whether or not we actually needed to do that seems like a reasonable improvement on that.
I'd like to work on this!
Can someone supply something like a MWE? @irajput passed me the following example. After the first pair of passes ConsolidateBlocks
, UnitarySynthesis
, the subsequences with more than one pair of SX
s are gone and do not reappear. Is the circuit not complicated enough?
from qiskit.circuit.library import QuantumVolume
from qiskit import *
from qiskit.test.mock import FakeBelem
qcomp = FakeBelem()
qv_circuit=QuantumVolume(3)
qv_circuit.draw()
from IPython.display import display
from qiskit.converters import dag_to_circuit
from qiskit.transpiler import TransformationPass
def callback_func(pass_,dag, time, property_set, count):
if True:
# if isinstance (pass_, TransformationPass):
circuit = dag_to_circuit(dag)
print(pass_.name())
display(circuit.draw(output='mpl'))
transpile(qv_circuit,backend=qcomp, optimization_level=3,callback=callback_func,translation_method='synthesis').draw(output='mpl')
EDIT: I have checked out terra v0.17 here. EDIT: Mon Aug 9 07:20:15 PM EDT 2021. @irajput gave me an corrected version of the example posted here.
Information
What is the current behavior?
synthesis(..., optimization_level=3, translation_method='synthesis')
emits programs with inefficient 1Q subsequences.Steps to reproduce the problem
Supply any sufficiently complex circuit (e.g., a quantum volume circuit) to the synthesis routine with optimization level turned up.
What is the expected behavior?
Each 1Q subsequence should be efficient, e.g., should apply
sx
no more than twice.Suggested solutions
The
_unroll
set of passes (UnitarySynthesis
specifically) is un-doing some of the work of_opt
(Optimize1qDecompose
specifically). I think what’s happening is:ConsolidateBlocks
produces operation strings likeu q[0] q[1]; v q[1] q[2];
, whereu
andv
are opaque two-qubit unitaries.UnitarySynthesis
subsequently throws single-qubit sequences ontoq[1]
afteru
and beforev
.For an 8x8 quantum volume circuit, this pushes the depth up from ~340 to ~400 — a significant effect.
Omitting
_unroll
entirely works fine for QV circuits targeting IBM devices, but the purpose of_unroll
was to re-nativize any non-native output that_opt
may have emitted. So long as we're concerned about that, we'll have to install a more delicate fix. For instance, we might run an analysis pass which checks for instructions outside ofbasis_gates
, and only apply_unroll
if any are found — though I'm not sure that this is sufficient logic.