Skip to content

Fix string and standard gate mismatch in commutation checker #13991

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

Merged
merged 3 commits into from
Mar 12, 2025

Conversation

mtreinish
Copy link
Member

Summary

This commit reworks the internals of the CommutationChecker to not rely on operation name except for where we do a lookup by name in the commutation library provided (which is the only key available to support custom gates). This fixes the case where a custom gate that overloads the standard gate name, previously the code would assume it to be a standard gate and internally panic when it wasn't. When working with standard gates (or standard instructions) we don't need to rely on string matching because we can rely on the rust data model to do the heavy lifting for us. This commit moves all the explicit handling of standard gates to use the StandardGate type directly and makes this logic more robust.

This also removes are usage of the once_cell library in qiskit-accelerate because it was used to create a lazy static hashsets of strings which are no longer needed because static lookup tables replace this when we stopped using string comparisons.

Fixes #13988

Details and comments

This commit reworks the internals of the CommutationChecker to not rely
on operation name except for where we do a lookup by name in the
commutation library provided (which is the only key available to support
custom gates). This fixes the case where a custom gate that overloads
the standard gate name, previously the code would assume it to be a
standard gate and internally panic when it wasn't. When working with
standard gates (or standard instructions) we don't need to rely on
string matching because we can rely on the rust data model to do the
heavy lifting for us. This commit moves all the explicit handling of
standard gates to use the StandardGate type directly and makes this
logic more robust.

This also removes are usage of the once_cell library in
qiskit-accelerate because it was used to create a lazy static hashsets
of strings which are no longer needed because static lookup tables replace
this when we stopped using string comparisons.

Fixes Qiskit#13988
@mtreinish mtreinish added stable backport potential The bug might be minimal and/or import enough to be port to stable Changelog: Bugfix Include in the "Fixed" section of the changelog labels Mar 11, 2025
@mtreinish mtreinish added this to the 2.0.0 milestone Mar 11, 2025
@mtreinish mtreinish requested a review from a team as a code owner March 11, 2025 13:29
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@coveralls
Copy link

coveralls commented Mar 11, 2025

Pull Request Test Coverage Report for Build 13808858937

Details

  • 47 of 91 (51.65%) changed or added relevant lines in 1 file are covered.
  • 7 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.01%) to 88.122%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/commutation_checker.rs 47 91 51.65%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 94.69%
crates/circuit/src/operations.rs 3 91.27%
crates/qasm2/src/lex.rs 3 92.23%
Totals Coverage Status
Change from base Build 13796441242: -0.01%
Covered Lines: 72665
Relevant Lines: 82460

💛 - Coveralls

Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

That's more elegant plus also correct 😛 I left some minor comments below.

Should we add an upgrade reno stating the change in behavior that custom gates matching standard gate names will no longer be fast-tracked through the commutation checker? I'm imaging a scenario where someone creates a custom gate named e.g. "ryy" and doesn't give it a matrix -- then it would've worked before 2.0 but doesn't anymore now, right?

}
}

if matches!(
Copy link
Contributor

Choose a reason for hiding this comment

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

This is now a logical change since it will eagerly return false on instructions, which is fine here since we don't check commutations between directives and these don't have defined matrices. Should we also check for OperationRef::Operation here, which also doesn't have a matrix and is not in the commutation library (it isn't right?)?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't view it as a logical change, just a small efficiency change while I was removing string usage, because we were matching a bit too restrictively before here. We know that no Instruction either standard (like measure, reset, etc) or from python (like Intialize). Since as you say we can't have a commutation any instruction as they're explicitly non-unitary.

I left Operation because I thought they could have a matrix (like Clifford) but looking at the operation trait for PyOperation we have it hard coded to None

fn matrix(&self, _params: &[Param]) -> Option<Array2<Complex64>> {
None
}
so you're correct we can add it here. It does make me wonder what we should do about Clifford though

Copy link
Contributor

@Cryoris Cryoris Mar 12, 2025

Choose a reason for hiding this comment

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

Hmm yes I based this off the Rust implementation for when operations always return None... but I'm fine keeping the code logically correct as is, since we can add operations that have matrices 👍🏻

(Otherwise it would be very easy to introduce a bug if we forget to remove the Operation case here once we add e.g. Cliffords to Rust)

@mtreinish
Copy link
Member Author

Should we add an upgrade reno stating the change in behavior that custom gates matching standard gate names will no longer be fast-tracked through the commutation checker? I'm imaging a scenario where someone creates a custom gate named e.g. "ryy" and doesn't give it a matrix -- then it would've worked before 2.0 but doesn't anymore now, right?

I don't think it's necessary in this case, it's not really supported to have a non-standard gate reuse a name like this. Its only the qasm path which make me think we should fix this. There is no API guarantee that the commutation checker will fast track anything either, as long as it's correct output that's fine. In the case of a custom "ryy" gate we should fall back to matrix based approach (assuming it has a definition or explicit matrix set) so it should all work as documented.

@mtreinish mtreinish requested a review from Cryoris March 12, 2025 12:25
@Cryoris Cryoris added this pull request to the merge queue Mar 12, 2025
Merged via the queue into Qiskit:main with commit abb0cf9 Mar 12, 2025
20 checks passed
mergify bot pushed a commit that referenced this pull request Mar 12, 2025
* Fix string and standard gate mismatch in commutation checker

This commit reworks the internals of the CommutationChecker to not rely
on operation name except for where we do a lookup by name in the
commutation library provided (which is the only key available to support
custom gates). This fixes the case where a custom gate that overloads
the standard gate name, previously the code would assume it to be a
standard gate and internally panic when it wasn't. When working with
standard gates (or standard instructions) we don't need to rely on
string matching because we can rely on the rust data model to do the
heavy lifting for us. This commit moves all the explicit handling of
standard gates to use the StandardGate type directly and makes this
logic more robust.

This also removes are usage of the once_cell library in
qiskit-accelerate because it was used to create a lazy static hashsets
of strings which are no longer needed because static lookup tables replace
this when we stopped using string comparisons.

Fixes #13988

* Rename is_commutation_skipped() is_parameterized()

(cherry picked from commit abb0cf9)

# Conflicts:
#	crates/accelerate/src/commutation_checker.rs
@mtreinish mtreinish deleted the fix-standard-gate-commutation branch March 12, 2025 17:14
github-merge-queue bot pushed a commit that referenced this pull request Mar 12, 2025
…#13991) (#14004)

* Fix string and standard gate mismatch in commutation checker (#13991)

* Fix string and standard gate mismatch in commutation checker

This commit reworks the internals of the CommutationChecker to not rely
on operation name except for where we do a lookup by name in the
commutation library provided (which is the only key available to support
custom gates). This fixes the case where a custom gate that overloads
the standard gate name, previously the code would assume it to be a
standard gate and internally panic when it wasn't. When working with
standard gates (or standard instructions) we don't need to rely on
string matching because we can rely on the rust data model to do the
heavy lifting for us. This commit moves all the explicit handling of
standard gates to use the StandardGate type directly and makes this
logic more robust.

This also removes are usage of the once_cell library in
qiskit-accelerate because it was used to create a lazy static hashsets
of strings which are no longer needed because static lookup tables replace
this when we stopped using string comparisons.

Fixes #13988

* Rename is_commutation_skipped() is_parameterized()

(cherry picked from commit abb0cf9)

# Conflicts:
#	crates/accelerate/src/commutation_checker.rs

* Update commutation_checker.rs

---------

Co-authored-by: Matthew Treinish <[email protected]>
Co-authored-by: Elena Peña Tapia <[email protected]>
raynelfss pushed a commit to raynelfss/qiskit that referenced this pull request Mar 20, 2025
…13991)

* Fix string and standard gate mismatch in commutation checker

This commit reworks the internals of the CommutationChecker to not rely
on operation name except for where we do a lookup by name in the
commutation library provided (which is the only key available to support
custom gates). This fixes the case where a custom gate that overloads
the standard gate name, previously the code would assume it to be a
standard gate and internally panic when it wasn't. When working with
standard gates (or standard instructions) we don't need to rely on
string matching because we can rely on the rust data model to do the
heavy lifting for us. This commit moves all the explicit handling of
standard gates to use the StandardGate type directly and makes this
logic more robust.

This also removes are usage of the once_cell library in
qiskit-accelerate because it was used to create a lazy static hashsets
of strings which are no longer needed because static lookup tables replace
this when we stopped using string comparisons.

Fixes Qiskit#13988

* Rename is_commutation_skipped() is_parameterized()
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 stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Overloaded gate names cause panic in commutation checker
4 participants