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

Fix QuantumCircuit.compose on unusual types and registers #9206

Merged
merged 8 commits into from
Jan 11, 2023

Conversation

jakelishman
Copy link
Member

@jakelishman jakelishman commented Nov 25, 2022

Summary

QuantumCircuit.compose previously had its own handling for converting bit specifiers into bit instances, meaning its functionality was often surprisingly less permissive than append, or just incorrect. This swaps the method to use the standard conversion utilities.

The logic for handling mapping conditional registers was previously the old logic in DAGCircuit, which was called without respect to re-ordering of the clbits during the composition. The existing test case surrounding this that is modified by this commit made an incorrect assertion; in general, the condition was not the same after the composition. In this particular test case it was ok because both bits were being tested for the same value, but had the condition value been 0b01 or 0b10 it would have been incorrect.

This commit updates the register-mapping logic to respect the reordering of clbits, and to create a new aliasing register to ensure the condition is represented exactly, if this is required. This fixes a category of bug where too-small registers could incorrectly be mapped to larger registers. This was not a valid transformation, because it created assertions about bits that were previously not involved in the condition's comparison.

Details and comments

Fix #6583
Fix #6584
Fix #8691

Note that with this change in place, the circuit visualisation tools may well not display the circuits correctly; they don't handle aliasing registers well at the moment. Similarly, I don't know precisely how well these objects will transport through the legacy Qobj paths; the circuits being described here may well not be representable in the Qobj model. If that is the case, though, it'll be up to assemble to raise a suitable error; the representation here is what is correct for QuantumCircuit.

`QuantumCircuit.compose` previously had its own handling for converting
bit specifiers into bit instances, meaning its functionality was often
surprisingly less permissive than `append`, or just incorrect.  This
swaps the method to use the standard conversion utilities.

The logic for handling mapping conditional registers was previously the
old logic in `DAGCircuit`, which was called without respect to
re-ordering of the clbits during the composition.  The existing test case
surrounding this that is modified by this commit made an incorrect
assertion; in general, the condition was not the same after the
composition.  In this particular test case it was ok because both bits
were being tested for the same value, but had the condition value been
0b01 or 0b10 it would have been incorrect.

This commit updates the register-mapping logic to respect the reordering
of clbits, and to create a new aliasing register to ensure the condition
is represented exactly, if this is required.  This fixes a category of
bug where too-small registers could incorrectly be mapped to larger
registers.  This was not a valid transformation, because it created
assertions about bits that were previously not involved in the
condition's comparison.
@jakelishman jakelishman added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Nov 25, 2022
@jakelishman jakelishman added this to the 0.23.0 milestone Nov 25, 2022
@jakelishman jakelishman requested a review from a team as a code owner November 25, 2022 19:05
@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 the following people are requested to review this:

  • @Qiskit/terra-core

This test was actually attempting to catch invalid state of
`QuantumCircuit.compose`, but this series of commits changes the
underlying function so that this test no longer needs to raise; there is
valid alternative behaviour.
@coveralls
Copy link

coveralls commented Nov 26, 2022

Pull Request Test Coverage Report for Build 3893910206

  • 39 of 40 (97.5%) changed or added relevant lines in 1 file are covered.
  • 4 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.003%) to 84.641%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/quantumcircuit.py 39 40 97.5%
Files with Coverage Reduction New Missed Lines %
qiskit/dagcircuit/dagcircuit.py 4 89.35%
Totals Coverage Status
Change from base Build 3893909087: -0.003%
Covered Lines: 64495
Relevant Lines: 76198

💛 - 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.

One small question, otherwise LGTM!

qiskit/circuit/quantumcircuit.py Show resolved Hide resolved
test/python/circuit/test_compose.py Outdated Show resolved Hide resolved
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!

@mergify mergify bot merged commit 2629079 into Qiskit:main Jan 11, 2023
@jakelishman jakelishman deleted the fix-compose branch January 16, 2023 12:22
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
4 participants