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

Discard cache for standard gates in assign_parameters #13557

Merged
merged 4 commits into from
Dec 12, 2024

Conversation

raynelfss
Copy link
Contributor

Summary

This is an initial fix to #13504, and should precede #13543.

The following commits discard the cached gate when assigning parameters for a standard gate rather than trying to modify the Python object.

Details and comments

A more in-depth fix is present in #13543, but since it is such a big change, we've decided to include this smaller fix to allow more time to re-evaluate our approach.

When retrieving a PackedInstruction from the EquivalenceLibrary during the compose_transforms phase of the BasisTranslator the parameter modifications done in other places are not reflected on the cached gate, therefore leading to issues when running QISKIT_PARALLEL=TRUE due to a modified object reference when calling CircuitData::assign_parameters().

In the case of a standard gate, we were not removing the old cache but instead modifying it in place (which would not result in the expected outcome), while for other gate instances, we rebuild the non-cached item. Now we instead discard the cache in the StandardGate case too, getting rid of the stale shared reference.

@raynelfss raynelfss added bug Something isn't working priority: high Changelog: Bugfix Include in the "Fixed" section of the changelog Rust This PR or issue is related to Rust code in the repository mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Dec 11, 2024
@raynelfss raynelfss added this to the 1.3.1 milestone Dec 11, 2024
@raynelfss raynelfss requested a review from a team as a code owner December 11, 2024 18:48
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@raynelfss raynelfss linked an issue Dec 11, 2024 that may be closed by this pull request
@coveralls
Copy link

coveralls commented Dec 11, 2024

Pull Request Test Coverage Report for Build 12285836607

Details

  • 6 of 6 (100.0%) changed or added relevant lines in 1 file are covered.
  • 15 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.005%) to 88.937%

Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 92.2%
crates/qasm2/src/lex.rs 2 91.98%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 12275601740: 0.005%
Covered Lines: 79379
Relevant Lines: 89253

💛 - Coveralls

Copy link
Contributor

@kevinhartman kevinhartman left a comment

Choose a reason for hiding this comment

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

Thanks @raynelfss, this seems to work nicely.

I left just one comment and will merge once addressed.

Comment on lines 1394 to 1396
// Standard gates can all rebuild their definitions, so if the
// cached py_op exists, update the `params` attribute and clear out
// any existing cache.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we update this comment and perhaps add a GitHub link to the relevant issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

Fix incorrect behavior in :class:`.CircuitData` in which, upon parameter assignment,
we attempted to modify the cached operation inside of a ``PackedInstruction``. Now
we instead discard said cache prompting the ``PackedInstruction`` to build a new Python
operation should it be needed.
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to link the issue here, but let's do that in #13556.

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.

LGTM, this is how the original fix looked like 🙂 It seems that Kevin's comments is also addressed and I double checked that this fixed the original bug reported in #13504.

@Cryoris Cryoris added stable backport potential The bug might be minimal and/or import enough to be port to stable and removed bug Something isn't working labels Dec 12, 2024
@Cryoris Cryoris added this pull request to the merge queue Dec 12, 2024
Merged via the queue into Qiskit:main with commit 17a2ccc Dec 12, 2024
18 checks passed
mergify bot pushed a commit that referenced this pull request Dec 12, 2024
* Fix: Discard cache when assigning new parameters in `CircuitData`.

* Docs: Add release note

* Test: Add test case

* Docs: Add relevant comment

(cherry picked from commit 17a2ccc)
github-merge-queue bot pushed a commit that referenced this pull request Dec 12, 2024
)

* Fix: Discard cache when assigning new parameters in `CircuitData`.

* Docs: Add release note

* Test: Add test case

* Docs: Add relevant comment

(cherry picked from commit 17a2ccc)

Co-authored-by: Raynel Sanchez <[email protected]>
@raynelfss raynelfss mentioned this pull request Dec 12, 2024
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 mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library priority: high Rust This PR or issue is related to Rust code in the repository 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.

Basis translation can fail if run in parallel
5 participants