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

limiting matrix-based commutativity check #10495

Merged

Conversation

alexanderivrii
Copy link
Contributor

@alexanderivrii alexanderivrii commented Jul 25, 2023

Summary

Fixes #10488.

The matrix-based commutativity check in CommutationChecker requires to construct 2^N x 2^N matrices, leading to memory problems when N is large.

I have added a new argument max_num_qubits to CommutationChecker::commute that allows to skip the commutativity check if the number of qubits of either operation exceeds this amount.

Note that simpler versions of the commutativity check not involving matrix multiplication continue to work as before (e.g., two quantum operations over disjoint sets of qubits commute regardless of the number of qubits).

Details and comments

This actually changes the behavior of commutation checking when the number of qubits is in the range from 4 to 12 (when the matrices could be constructed and multiplied). However personally I don't think we will be missing a lot of optimization opportunities. I am even thinking that we should maybe limit max_num_qubits to 2, what are your thoughts on this?

Implementation-wise, I was a bit unsure whether to make max_num_qubits to be a global constant, or to pass it as the argument to CommutationChecker::__init__, or to pass it as the argument to CommutationChecker::commute (this is what I have chosen at the moment). I can see a potential use-case of having the same instance of CommutationChecker, and then first doing some optimizations based on a lighter-weight version of the check, and then possibly more optimizations based on heavier-weight version of the check (the same instance allows to use the cached results from previous calls).

@alexanderivrii alexanderivrii requested a review from a team as a code owner July 25, 2023 09:22
@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 Jul 25, 2023

Pull Request Test Coverage Report for Build 5656018131

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 15 unchanged lines in 4 files lost coverage.
  • Overall coverage decreased (-0.01%) to 85.929%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 93.76%
qiskit/extensions/unitary.py 1 93.75%
crates/qasm2/src/parse.rs 6 97.13%
crates/qasm2/src/lex.rs 7 91.65%
Totals Coverage Status
Change from base Build 5650486720: -0.01%
Covered Lines: 73045
Relevant Lines: 85006

💛 - Coveralls

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

This looks like a reasonable limit to me, and one that's very unlikely to ever be tripped (the bug report from a stress test notwithstanding), since I'm not aware of much hardware that has support for 4+q operations natively (although I suppose the MS interaction in ions can do that).

Looks like lint's complaining a bit - you can probably safely disable too-many-returns in the function that's tripping it, if you think that combining some of the early returns will make the function harder to understand not easier. The line length thing I think is in the docstring, which black won't touch, so you'll need to edit it manually.

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

The code looks good now, thanks. A reason I'm slightly hesitant to tag this for backport to 0.25.0 is that strictly I think this is more of a new feature, and the bugfix is very low priority. Perhaps we should (in addition to the fix note) put a feature note in about the new option to CommutationChecker, and merge this for the October Qiskit release instead? What do you think, Sasha?

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Thanks for the quick updates!

For context: Sasha and I spoke privately and agreed that this would be safer as a feature note targetting Qiskit 0.45 rather than the stable 0.25.0 branch, because of the potential changed behaviour. The bugfix is low priority, since it will only trigger with particular very large circuits when no basis gates are set (or a backend that claims to support arbitrary-sized mcx/mct operations).

@jakelishman jakelishman added Changelog: New Feature Include in the "Added" section of the changelog Changelog: Bugfix Include in the "Fixed" section of the changelog labels Jul 25, 2023
@jakelishman jakelishman added this to the 0.45.0 milestone Jul 25, 2023
@jakelishman jakelishman added the mod: transpiler Issues and PRs related to Transpiler label Jul 25, 2023
@jakelishman jakelishman added this pull request to the merge queue Jul 25, 2023
Merged via the queue into Qiskit:main with commit 07b06f9 Jul 25, 2023
13 checks passed
to24toro pushed a commit to to24toro/qiskit-terra that referenced this pull request Aug 3, 2023
* limiting matrix-based commutativity check

* adding tests with the order of gates fixed

* lint tweaks

* improved released notes
@alexanderivrii alexanderivrii deleted the restrict-commutation-checker-qubits branch October 23, 2023 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog 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.

commutation analysis lead to numpy.core._exceptions._ArrayMemoryError with large mct
4 participants