Skip to content
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

Adapt commutation checker to abstract circuits #11948

Merged
merged 6 commits into from
Mar 6, 2024

Conversation

sbrandhsn
Copy link
Contributor

Summary

addresses #11911 and PR #11929

Details and comments

Quantum circuit compilation can be improved by running optimization passes based on commutative analysis before solving the routing problem. Formerly, commutative analysis was only applied to "physical circuits", i.e. entirely specified by Instructions that are readily defined by the unitary matrices of the instructions. Applying commutative analysis before routing requires handling abstract instructions (Operators) that may or may not be defined by a matrix directly. However, the matrix specification of an instruction is required to determine its commutativity.

This PR extends the current commutation analysis that only works on physical circuits defined over Instructions to handle abstract instructions, i.e. checking whether an instruction has a matrix representation and exiting early if otherwise before proceeding to determine the commutation relation.

@sbrandhsn sbrandhsn requested a review from a team as a code owner March 5, 2024 11:46
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core

@coveralls
Copy link

coveralls commented Mar 5, 2024

Pull Request Test Coverage Report for Build 8176499038

Details

  • 31 of 33 (93.94%) changed or added relevant lines in 1 file are covered.
  • 48 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.02%) to 89.306%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/commutation_checker.py 31 33 93.94%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 2 91.44%
qiskit/circuit/instruction.py 8 93.84%
qiskit/qpy/binary_io/circuits.py 38 91.61%
Totals Coverage Status
Change from base Build 8144782269: 0.02%
Covered Lines: 59140
Relevant Lines: 66222

💛 - Coveralls

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this lgtm, I left a couple small comments inline. Could you also add some tests to validate the checker works with annotated operations correctly?

qiskit/circuit/commutation_checker.py Outdated Show resolved Hide resolved
# this might be runtime-intensive in general so we should attempt to branch out before running this
# it may be more runtime-efficient to check for entries in the commutation library before
try:
Operator(op)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I expect this will come up in practice very often, but if this is expensive to compute in some cases wouldn't it be better to only do this once? Right now you'll run this twice, once to check for a skip and later again as part of _commute_matmul.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree. However, I would need to refactor the commutation checker quite a bit and I wanted to do this PR quickly. :-) Is it okay to leave it as is? I also suspect that this part of the function should not be executed often in practice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to leave it as is, but maybe put a TODO comment or something in there to indicate longer term it'd be better to de-duplicate the Operator() call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shifted the Operator() resolution to _commute_matmul - while this would lead to unnecessary checks for operations that do not have a suitable Operator resolution, I would expect these checks to be comparatively lightweight.

@sbrandhsn
Copy link
Contributor Author

I added a couple of tests for annotated operations. I was surprised to find that CommutativeCancellation only handles a hard-coded list of 2q gates.

@mtreinish
Copy link
Member

I added a couple of tests for annotated operations. I was surprised to find that CommutativeCancellation only handles a hard-coded list of 2q gates.

Oh, yeah that is a surprise, maybe this is a good reason we should migrate to CommutativeInverseCancellation in the preset pass managers instead of CommutativeCancellation. @alexanderivrii is there anything blocking us from doing that (in a different new PR)?

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code-wise this LGTM now, I just had an inline suggestion in the release note to provide a bit more context about the feature for users. A code example showing what you can do with it now never hurts either.

…599.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
@sbrandhsn
Copy link
Contributor Author

Makes sense, thanks!

@mtreinish mtreinish added the Changelog: New Feature Include in the "Added" section of the changelog label Mar 6, 2024
@mtreinish mtreinish added this to the 1.1.0 milestone Mar 6, 2024
@mtreinish mtreinish added the mod: transpiler Issues and PRs related to Transpiler label Mar 6, 2024
@mtreinish mtreinish enabled auto-merge March 6, 2024 18:37
@mtreinish mtreinish added this pull request to the merge queue Mar 6, 2024
Merged via the queue into main with commit 97aa967 Mar 6, 2024
14 checks passed
IsmaelCesar pushed a commit to comp-quantica/qiskit-terra that referenced this pull request Mar 13, 2024
* Update commutation_checker.py

* reno

* new tests

* Update commutation_checker.py

* remove Operator resolution in commutation pre-check

* Update releasenotes/notes/abstract-commutation-analysis-3518129e91a33599.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
@sbrandhsn sbrandhsn deleted the abstract-commutation-checker branch March 20, 2024 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog mod: transpiler Issues and PRs related to Transpiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants