qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

preset passmanagers should perform gate optimization before routing

Open nonhermitian opened this issue 1 year ago • 3 comments

What should we add?

Consider the following circuit:

qc = QuantumCircuit(N)
for kk in range(N - 1):
    qc.cx(kk, N - 1)
qc.x(N - 1)
qc.z(N - 2)
for kk in range(N - 2, -1, -1):
    qc.cx(kk, N - 1)

This circuit should collapse down to a pair of X and Z gates via commutation and collapsing pairs of CX gates. However, the preset passmanagers do not catch this. Instead, collapsing requires the routing passes to find the correct mapping that allows for later clean up to be successful. Other SDKs do this, and the performance is markedly better because routing is not required.

nonhermitian avatar Feb 28 '24 14:02 nonhermitian

This is very easy to do (just add the pass to the DefaultInitPassManager as needed) and I agree it makes a ton of sense. I'm thinking we should do this at optimization levels 2 and 3, as that's where we currently do commutative cancellation today. What do you think?

mtreinish avatar Feb 28 '24 17:02 mtreinish

I think that makes sense. I do agree that this is low-hanging fruit. I think where it is added depends on what is the proposed "default" preset passmanger. Namely, other SDKs defaults will do the same, so I my thinking is we should be able to do the same "by default", and let more advance users do custom PMs

nonhermitian avatar Feb 28 '24 18:02 nonhermitian

Oh, sorry my comment was more left as a hint for where to put it in the code so it gets pulled in by transpile and generate_preset_pass_manager for everyone. It's the name of the plugin class we use for the init stage by default: https://github.com/Qiskit/qiskit/blob/d47a227d982710e7933d744dfd95f6550ff07ed2/qiskit/transpiler/preset_passmanagers/builtin_plugins.py#L68 we just need to add either CommutativeCancellation or CommutativeInverseCancellation to the pass manager construction there to do this.

mtreinish avatar Feb 28 '24 21:02 mtreinish