-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Create a secure internal path for custom prefixes in registers. #14005
Conversation
Prior implementations would replace the Register's prefix attribute inplace which is an unsafe operation. The following commits add a secure path for a provisional replacement of a register's prefix name to fix changed unsafe behavior from Qiskit#13860.
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 13925424331Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
There was a problem hiding this comment.
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 have a unit test for this path. The reproducing code that I found looks quite hacky, and this is probably why the issue hadn't surfaced until now. It's a test for the Aer estimator primitive that builds a circuit, adds measurements (via circuit.measure_all()
), and then calls a part of the aer estimator code that does:
circuit = self._circuits[i].copy()
circuit.measure_all()
.... # stuff
circuit.remove_final_measurements()
Calling measure_all()
twice after a shallow copy doesn't look like a great thing to do, but I guess we shouldn't hit this error if someone does it. I can see if I can isolate this into a more reasonable example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me after @ElePT's comment is addressed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot! The PR looks good to me too. Here is the minimal reproducible code for the issue, I can't add it as a direct suggestion but I think it's enough to test this fix:
from qiskit import QuantumCircuit
qc = QuantumCircuit(1)
qc.measure_all()
qc.measure_all()
- Add test-case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you've got a linter error in CI as well 🙂
Co-authored-by: Kevin Hartman <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I found a tiny typo in the test comment but it's not important.
Co-authored-by: Elena Peña Tapia <[email protected]>
* FIx: Create an internal path for custom prefixes in registers. Prior implementations would replace the Register's prefix attribute inplace which is an unsafe operation. The following commits add a secure path for a provisional replacement of a register's prefix name to fix changed unsafe behavior from #13860. * Fix: Address review comments - Add test-case * Apply suggestions from code review Co-authored-by: Kevin Hartman <[email protected]> * Fix: Address more review comments * Fix: Lint error * Update test/python/circuit/test_circuit_operations.py Co-authored-by: Elena Peña Tapia <[email protected]> --------- Co-authored-by: Kevin Hartman <[email protected]> Co-authored-by: Elena Peña Tapia <[email protected]> (cherry picked from commit 56a16ab)
…) (#14048) * FIx: Create an internal path for custom prefixes in registers. Prior implementations would replace the Register's prefix attribute inplace which is an unsafe operation. The following commits add a secure path for a provisional replacement of a register's prefix name to fix changed unsafe behavior from #13860. * Fix: Address review comments - Add test-case * Apply suggestions from code review Co-authored-by: Kevin Hartman <[email protected]> * Fix: Address more review comments * Fix: Lint error * Update test/python/circuit/test_circuit_operations.py Co-authored-by: Elena Peña Tapia <[email protected]> --------- Co-authored-by: Kevin Hartman <[email protected]> Co-authored-by: Elena Peña Tapia <[email protected]> (cherry picked from commit 56a16ab) Co-authored-by: Raynel Sanchez <[email protected]>
Summary
The following commits add the internal method
_new_with_prefix
, which allows for the one-time replacement of the set prefix name and use of the same instance counter.Details and comments
Prior implementations would replace the
Register
's prefix attribute in place which is an unsafe operation. The following commits add a secure path for a provisional replacement of a register's prefix name to fix changed unsafe behavior from #13860 and attempts to fix #14003.