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 #13600

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Cryoris
Copy link
Contributor

@Cryoris Cryoris commented Dec 23, 2024

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.

@Cryoris Cryoris added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Dec 23, 2024
@Cryoris Cryoris added this to the 1.3.2 milestone Dec 23, 2024
@Cryoris Cryoris requested a review from a team as a code owner December 23, 2024 15:00
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@coveralls
Copy link

Pull Request Test Coverage Report for Build 12468768413

Details

  • 10 of 10 (100.0%) changed or added relevant lines in 2 files are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.01%) to 88.963%

Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 92.2%
crates/qasm2/src/lex.rs 4 92.48%
Totals Coverage Status
Change from base Build 12420636821: 0.01%
Covered Lines: 79440
Relevant Lines: 89296

💛 - Coveralls

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.

Commutation Checker with PauliGate
3 participants