Fix InverseCancellation for symmetric pairs#16153
Fix InverseCancellation for symmetric pairs#16153Cryoris wants to merge 2 commits intoQiskit:mainfrom
InverseCancellation for symmetric pairs#16153Conversation
Symmetric gates should get cancelled irrespective of the qubit order. This did not happen in InverseCancellation, which didn't consistently cancel CS and CSdg gates in the default setup since their qubit argument were out of order. Coincidentally _it did_ cancel these gates in a specific order as a by product since the boolean logic was not grouped correctly, as reported in Qiskit#15855. This commit fixes this behavior. This commit does not more generally enable handling of symmetric, user-supplied gates.
|
One or more of the following people are relevant to this code:
|
jakelishman
left a comment
There was a problem hiding this comment.
This is definitely a more complete/appropriate "fix", though we're bordering on "new feature" here imo, and we might want a separate backport PR to 2.4 if there's a demonstrable bug. I don't actually think there is an observable bug, because this only triggers in the default Rust path, and in the Rust path there's only cs/csdg which are symmetric. For example:
from qiskit.circuit import Gate, QuantumCircuit
from qiskit.transpiler import passes
class Left(Gate):
def __init__(self):
super().__init__("left", 2, [])
def inverse(self):
return Right()
class Right(Gate):
def __init__(self):
super().__init__("right", 2, [])
def inverse(self):
return Left()
qc = QuantumCircuit(2)
qc.append(Left(), [0, 1])
qc.append(Right(), [1, 0])
passes.InverseCancellation([(Left(), Right())])(qc).draw()shows no cancellation (but they do cancel if you set the qargs the same).
Perhaps let's either treat the existing PR as "refactor for future correctness", or retarget it to 2.5 and try to extend the symmetric flag to Python-space too?
Coverage Report for CI Build 25503982007Warning Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes. Coverage decreased (-0.01%) to 87.608%Details
Uncovered ChangesNo uncovered changes found. Coverage Regressions21 previously-covered lines in 4 files lost coverage.
Coverage Stats
💛 - Coveralls |
The observable bug is that on 2.4 CSdg-CS cancel but CS-CSdg don't lol (that's related to the incorrect precedence in the boolean check reported in the original issue). So on 2.4 we have qc = QuantumCircuit(2)
qc.cs(1, 0)
qc.csdg(0, 1)
tqc = InverseCancellation()(qc) # gates are not cancelled, but should bebut qc = QuantumCircuit(2)
qc.csdg(0, 1)
qc.cs(1, 0)
tqc = InverseCancellation()(qc) # gates are cancelled Based on this I think it would be good to backport this, but expose the symmetry feature to Python for 2.5, no? |
|
Ah ok, then yeah I'm good to backport as a bug fix. |
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
|
I am wondering whether we really need this very-very specific feature "eliminate symmetric CS/CSdg gates" in qc = QuantumCircuit(4)
qc.cz(0, 2)
qc.cz(0, 2)
qct = InverseCancellation()(qc)
print(qct)We could definitely add other symmetric gates, though is there a need for this, since In the review to #16124, I was suggesting to only include the actual bug-fix which we would then backport to 2.4. |
|
Sasha: I'd do this PR over the other just because it also fixes the bug, but doesn't break the coincidental-but-actually-correct |
| Fixed a bug in :class:`.InverseCancellation` where pairs of symmetric gates | ||
| (such as :class:`.CSGate` and :class:`.CSdgGate`) were not correctly cancelled | ||
| if their qubit arguments were the same but in different order. Symmetric | ||
| gates should get cancelled irrespective of the qubit order. |
There was a problem hiding this comment.
If you write the bugfix note like this, we need to handle the cz case Sasha mentioned too.
| Fixed a bug in :class:`.InverseCancellation` where pairs of symmetric gates | ||
| (such as :class:`.CSGate` and :class:`.CSdgGate`) were not correctly cancelled | ||
| if their qubit arguments were the same but in different order. Symmetric | ||
| gates should get cancelled irrespective of the qubit order. |
There was a problem hiding this comment.
From reading the release note, it seems like we are asserting that InverseCancellation should cancel all symmetric pairs of gates, which it does not.
There was a problem hiding this comment.
Lol, this is the second time I start writing a comment and see that Jake already beat me to it.
|
My only other concern is if the extra change has a negative impact on performance, after all An aside: |
|
Sahsa: you mean O1 right? We don't run an optimisation loop at all at O0. |
|
Jake: I thought we run it in the |
|
Yeah, O0 is totally null in optimisations - the As to asv: I don't think |
Symmetric gates should get cancelled irrespective of the qubit order. This did not happen in InverseCancellation, which didn't consistently cancel CS and CSdg gates in the default setup since their qubit argument were out of order. Coincidentally it did cancel these gates in a specific order as a by product since the boolean logic was not grouped correctly, as reported in #15855. This commit fixes this behavior.
This commit does not more generally enable handling of symmetric, user-supplied gates.
Fixes #15855. Alternative fix to #16124.
AI/LLM disclosure