forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Speed up InverseCancellation when there's nothing to cancel (Qiskit#1…
…1211) * Speed up InverseCancellation when there's nothing to cancel In the cases when there is nothing to cancel in the DAGCircuit the InverseCancellation pass would iterate over the entire circuit twice, once to search for self inverse gates and then a second time to search for other inverse pairs. This is typically fairly fast because the actual search is done in rust, but there is a python callback function that is called for each node. Depending on the size of the circuit this could add up to a significant amount of time. This commit updates the logic in the pass to first check if there are any operations being asked to cancel for either stage, and if there are then only do the iteration if there are any gates in the circuit in the set of intructions that will be cancelled, which means we'll need to do an iteration. These checks are all O(1) for any sized dag, so they're much lower overhead and will mean the pass executes much faster in the case when there isn't anything to cancel. * Speed-up when there is a partial match Building off of the previous commit this speeds up the inverse cancellation pass when only some of the inverse pairs are not present in the DAG, but others are present. In this case the previous commit would still iterate over the full dag multiple times even when we know some of the inverse pairs are not present in the DAG. This commit updates the logic to not call collect_runs() if we know it's going to be empty or there is no cancellation opportunity. * Expand test coverage * Add more tests * Fix handling of parameterized gates This commit adds a fix for an issue that was caught while tuning the performance of the pass. If a parameterized self inverse was passed in the pass would incorrectly treat all instances of that parameterized' gate as being a self inverse without checking the parameter values. This commit corrects this oversight to handle this case to only cancel gates when the optimization is correct.
- Loading branch information
Showing
3 changed files
with
175 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
releasenotes/notes/fix-parameterized-self-inverse-7cb2d68b273640f8.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
fixes: | ||
- | | ||
Fixed an issue with the :class:`~.InverseCancellation` pass where it would | ||
incorrectly cancel gates passed in as self inverses with a parameter | ||
value, if a run of gates had a different parameter value. For example:: | ||
from math import pi | ||
from qiskit.circuit.library import RZGate | ||
from qiskit.circuit import QuantumCircuit | ||
from qiskit.transpiler.passes import InverseCancellation | ||
inverse_pass = InverseCancellation([RZGate(0)]) | ||
qc = QuantumCircuit(1) | ||
qc.rz(0, 0) | ||
qc.rz(pi, 0) | ||
inverse_pass(qc) | ||
would previously have incorrectly cancelled the two rz gates. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters