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

More conservative caching in the CommutationChecker (backport #13600) #13677

Merged
merged 1 commit into from
Jan 16, 2025

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot commented Jan 16, 2025

Summary

Fixes #13570.

This PR might require some discussion on what assumptions we have on custom Instructions. However, if we cannot assume that an instruction's definition is fully specified by it's params, then we cannot use the params as key in the commutation library. (We also sometimes put additional information outside of params.)
This PR makes the commutation cache more conservative, by only additionally caching standard gate relations, where we know the instructions are defined by it's params.

Details and comments

The commutation checker currently caches commutations of any instruction with float-only params (with some hardcoded exceptions). This leads to problems if the instruction's definition depends on more than just the params, e.g. we could have a custom gate defined as

class MyEvilRXGate(Gate):
    """A RX gate designed to annoy the caching mechanism (but a realistic gate nevertheless)."""

    def __init__(self, evil_input_not_in_param: float):
        """
        Args:
            evil_input_not_in_param: The RX rotation angle.
        """
        self.value = evil_input_not_in_param
        super().__init__("<evil laugh here>", 1, [])

    def _define(self):
        self.definition = QuantumCircuit(1)
        self.definition.rx(self.value, 0) 

Calling the commutation checker with an instance of this gate will then cache the first commutation, under the key [] (since the params are empty) and subsequent queries of the cache return the first commutation, regardless of the evil rotation angle. For example

all_commuter = MyEvilRXGate(0)  # this will with anything
some_rx = MyEvilRXGate(1.6192)  # this should not commute with H

print(scc.commute(all_commuter, [0], [], HGate(), [0], []))  # True, as we expect
# this should be False, but queries the first occurence of MyEvilRXGate(0) and is True!
print(scc.commute(some_rx, [0], [], HGate(), [0], []))

Another solution could be to explicitly state the assumption that the gate must be fully specified by the params attribute, and we'd have to update some gates in the library.

I ran the test/benchmarks/utility_scale.py benchmarks and could not see a regression on my laptop.


This is an automatic backport of pull request #13600 done by Mergify.

* conservative commutation check

* tests and reno

* reno in the right location

* more tests for custom gates

(cherry picked from commit 93d796f)
@mergify mergify bot requested a review from a team as a code owner January 16, 2025 17:46
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

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

  • @Qiskit/terra-core

@github-actions github-actions bot added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Jan 16, 2025
@github-actions github-actions bot added this to the 1.3.2 milestone Jan 16, 2025
@Cryoris Cryoris enabled auto-merge January 16, 2025 17:47
@coveralls
Copy link

Pull Request Test Coverage Report for Build 12814601784

Details

  • 10 of 10 (100.0%) changed or added relevant lines in 2 files are covered.
  • 19 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.01%) to 88.908%

Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 93.21%
crates/qasm2/src/lex.rs 6 91.23%
crates/qasm2/src/parse.rs 12 96.69%
Totals Coverage Status
Change from base Build 12810169204: -0.01%
Covered Lines: 79159
Relevant Lines: 89035

💛 - Coveralls

@Cryoris Cryoris added this pull request to the merge queue Jan 16, 2025
Merged via the queue into stable/1.3 with commit 98f02d7 Jan 16, 2025
20 checks passed
@mtreinish mtreinish deleted the mergify/bp/stable/1.3/pr-13600 branch January 16, 2025 19:46
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants