-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix commutation analysis to group gates into blocks of pairwise commuting gates #10618
Fix commutation analysis to group gates into blocks of pairwise commuting gates #10618
Conversation
One or more of the the following people are requested to review this:
|
Pull Request Test Coverage Report for Build 5848445316
💛 - Coveralls |
Nice! Of course, feel free to add me. What about missed optimization from #8020 (comment)? If I understand correctly, your fix misses it too? I think a separate issue for optimizing it should be added then. |
Co-authored-by: Randl <[email protected]>
Thanks @Randl. I have added you as a co-author (your name appears in commit d4fa355, and when the PR is merged, all the commits will be squashed together). Regarding potentially missed optimizations, Qiskit now has quite a bunch of single-qubit optimization passes, and I would expect that the specific optimization you mention is already handled by some other pass. Actually, could you please describe your general optimization idea? |
The idea is that checking all previous gates may be too strict, or, more generally, there grouping is too weak, as discussed in this comment #8056 (comment) e.g., if C commutes with A and B but A and B do not commute, then |
Oh, thanks, I see what you mean now. At some point I have added a pass
As @jakelishman commented, we also have the |
Update: I have removed the long post with old flaky benchmarking results. I have rerun the benchmarks again and there is no noticeable change in performance. For posterity here is the asv benchmarking command used.
|
…qiskit-terra into fix-commutation-analysis
Pull Request Test Coverage Report for Build 6547337139
💛 - Coveralls |
…qiskit-terra into fix-commutation-analysis
I don't understand the reason for the error I am seeing after updating the branch (and I don't think it's related to the changes here). I will update the branch and see if the error still persists. My previous concern was whether the change in behavior (checking that a gate commutes with all of the gates in the current block vs. only the last gate in the block) increases runtime. I have locally rerun the |
That test failure ought to be unrelated - it's similar to something we've seen before. The default clock available on Windows is lower resolution than it is on macOS or Linux, and so the algorithms tests that "some non-zero execution time was reported" are more likely to fail on Windows if we do our job quickly: see #8577, for example. |
This looks good to me. Possible follow-up work regarding the worst-case quadratic problem mentioned above: For very long blocks of commuting gates, it might be worth maintaining a set of unique gates in the block as you build it. So gates already in this set would be inserted into the block rather than checking commutation explicitly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
Summary
This fixes the unsound optimization problem described in #8020:
CommutativeAnalysis
groups nodes into blocks so that in each block every gate is only required to commute with the immediately preceding and the immediately following gates, whileCommutativeCancellation
cancels gates assuming that all the gates in a block pairwise commute. This PR changesCommutativeAnalysis
to group nodes into blocks of pairwise commuting gates.As an example, when the list of gates on a given wire is
['X', 'I', 'H', 'I', 'X']
, the old behavior grouped all of the gates into a single block and unsoundly canceled pairs of X-gates, while the new behavior groups gates into 3 blocks['X', 'I'], ['H', 'I'], ['X']
and avoids this unsound cancellation.Details and comments
This unsound optimization problem was described by @Randl more than a year ago in PR #8056. Looking at that PR again, I now see that it actually suggests a fix that is almost identical to the one in this PR: when deciding whether to put a new gate into the current block we should check whether this new gate commutes with every gate in the block rather than only with the very last gate of the block. I guess the reason that #8056 was closed rather than merged is that it originally only added the failing test but not the fix. @Randl, may I put you as a co-author to this PR?
As
CommutativeCancellation
is used by default when transpiling with optimization levels 2 or 3, it makes sense to finally fix this. However, the updated commutativity check is now worst-case quadratic and in theory may lead to some slow-downs. Though intuitively I don't think that we should see slow-downs: first, as soon as a gate does not commute with some gate in the given block, we end the block and start a new one; second, the underlyingCommutationChecker
hashes the results and avoids expensive matrix multiplications. But to be completely sure, @mtreinish, what kind of benchmarking would you recommend me to run?Fixes #10928