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

Detect duplicates in QuantumCircuit.compose #11451

Merged
merged 4 commits into from
Jan 8, 2024

Conversation

jakelishman
Copy link
Member

Summary

Previously, QuantumCircuit.compose would silently attempt to compose multiple qubits onto the same state, which would fail in weird ways if any multi-qubit instructions were acting on these collapsed qubits.

Details and comments

Fix #4970.

Previously, `QuantumCircuit.compose` would silently attempt to compose
multiple qubits onto the same state, which would fail in weird ways if
any multi-qubit instructions were acting on these collapsed qubits.
@jakelishman jakelishman 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 mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Dec 21, 2023
@jakelishman jakelishman added this to the 0.45.2 milestone Dec 21, 2023
@jakelishman jakelishman requested a review from a team as a code owner December 21, 2023 14:08
@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 Dec 21, 2023

Pull Request Test Coverage Report for Build 7448349417

Warning: This coverage report may be inaccurate.

We've detected an issue with your CI configuration that might affect the accuracy of this pull request's coverage report.
To ensure accuracy in future PRs, please see these guidelines.
A quick fix for this PR: rebase it; your next report should be accurate.

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.004%) to 87.574%

Totals Coverage Status
Change from base Build 7416093664: -0.004%
Covered Lines: 59484
Relevant Lines: 67924

💛 - Coveralls

Cryoris
Cryoris previously approved these changes Jan 5, 2024
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, now the raised error makes a lot more sense 👍🏻

@Cryoris Cryoris added this pull request to the merge queue Jan 5, 2024
@jakelishman
Copy link
Member Author

In retrospect, I'll remove this from "stable backport", because the bug's been open for ages without huge complaints, and we're close to 1.0 anyway, so there's little need to increase the risk of the 0.45.2 release.

@jakelishman jakelishman removed the stable backport potential The bug might be minimal and/or import enough to be port to stable label Jan 5, 2024
@jakelishman jakelishman modified the milestones: 0.45.2, 1.0.0 Jan 5, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 5, 2024
@Cryoris Cryoris added this pull request to the merge queue Jan 5, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 5, 2024
After the move of `QuantumCircuit.data` to Rust-space, some of the state
modification had started happening before the error checking, which
could potentially leave an in-place modification in a partially applied
state if an exception triggered during processing.

This also ensures that the qubits and clbits arguments are checked, even
if the actual data being composed is empty.
@jakelishman
Copy link
Member Author

The new test here caught a sort-of bug in #11214 where there was an early return for empty data in the composed operation without checking the qubits and clbits (the errors weren't raised). Alone that's not much of a problem, but the bigger issue that it turned up was that global-phase and calibrations were added to the dest circuit before error checking, which meant that if the composition raised an exception, we could be leaving the dest in an partially mutated state, which could cause issues for circuit-reuse after exception handling if we were doing an in-place composition.

@Cryoris
Copy link
Contributor

Cryoris commented Jan 8, 2024

Nice catch 👍🏻 there seem to be some other modifications to dest happening before, such as changing the dt unit and the duration. Should we make sure these are also modified only after the check?

@jakelishman
Copy link
Member Author

jakelishman commented Jan 8, 2024

The duration and unit question came up in #11214 and the result was a resounding "uh" - we don't really know what those are for at a circuit level, nor what meaning they're supposed to have (what if a circuit is made of incompatible durations with incompatible units?).

Seems easy enough to change, at any rate - done in 48116a. While it appears like I moved it after some possible return paths, those return paths all (necessarily) call QuantumCircuit.append, which will zero out the duration and unit as well.

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 thanks for the updates!

@Cryoris Cryoris added this pull request to the merge queue Jan 8, 2024
Merged via the queue into Qiskit:main with commit d48ba00 Jan 8, 2024
13 checks passed
@jakelishman jakelishman deleted the validate-compose-arguments branch January 8, 2024 16:18
ShellyGarion pushed a commit to ShellyGarion/qiskit-terra that referenced this pull request Jan 18, 2024
* Detect duplicates in `QuantumCircuit.compose`

Previously, `QuantumCircuit.compose` would silently attempt to compose
multiple qubits onto the same state, which would fail in weird ways if
any multi-qubit instructions were acting on these collapsed qubits.

* Ensure error-checking happens before any modification

After the move of `QuantumCircuit.data` to Rust-space, some of the state
modification had started happening before the error checking, which
could potentially leave an in-place modification in a partially applied
state if an exception triggered during processing.

This also ensures that the qubits and clbits arguments are checked, even
if the actual data being composed is empty.

* Delay mutation until the last possible time
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

QuantumCircuit.compose does not raise an error with invalid qubits argument
4 participants