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

Singletonized Measure and Reset instructions #11170

Merged
merged 3 commits into from Nov 14, 2023

Conversation

Matrixmang0
Copy link
Contributor

@Matrixmang0 Matrixmang0 commented Nov 1, 2023

Summary

  • Singletonized instructions Measure and Reset
  • Resolved circular import error during test run
  • Drafted release notes highlighting the features and upgrades

Details and comments

This PR partially addresses the issue #10953. It involves singletonizing Measure and Reset instructions. However, Barrier is not singletonized after discussing with the maintainer, as the base singleton code needed some additional machinery to handle instructions like Barrier.
Running tox test suite resulted in circular import error, which also has been solved with the guidance of the maintainer.
Finally added the release notes, elaborating on the features and upgrades brought upon by this pull request.

@Matrixmang0 Matrixmang0 requested a review from a team as a code owner November 1, 2023 17:09
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Nov 1, 2023
@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

@mtreinish mtreinish added the Changelog: New Feature Include in the "Added" section of the changelog label Nov 1, 2023
@mtreinish mtreinish added this to the 1.0.0 milestone Nov 1, 2023
@coveralls
Copy link

coveralls commented Nov 1, 2023

Pull Request Test Coverage Report for Build 6773342731

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.

  • 10 of 10 (100.0%) changed or added relevant lines in 4 files are covered.
  • 96 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.005%) to 86.949%

Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/target.py 5 93.78%
crates/qasm2/src/lex.rs 6 91.67%
qiskit/primitives/backend_estimator.py 6 96.19%
qiskit/transpiler/passes/layout/sabre_layout.py 9 78.03%
qiskit/dagcircuit/dagcircuit.py 70 90.88%
Totals Coverage Status
Change from base Build 6747766403: -0.005%
Covered Lines: 74361
Relevant Lines: 85523

💛 - Coveralls

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Thanks very much for this! It looks good, and it's also pretty good news that there was only one place in Qiskit that was using the instructions as non-singletons (and even then, it'll be almost never used because basically nobody uses conditional measurements or resets with OpenQASM 2).

Could you also add a _singleton_lookup_key to Measure and Reset, using stdlib_singleton_key? They have the same instantiation signature as standard-library gates, so we can use that function. You can see an example in qiskit/circuit/library/standard_gates/x.py in the XGate definition, right under the __init__.

Can we also add a couple of brief tests in test/python/circuit/test_singleton.py that Measure() and Reset() both return singletons, and that QuantumCircuit.measure (and reset) both add singleton instances onto circuits?

Other than that, this is good to go thanks.

@Matrixmang0
Copy link
Contributor Author

Thank you so much for the review!

Sure sir, will add a commit soon enough covering all these changes.

@jakelishman
Copy link
Member

(Don't worry about pressing "update branch", by the way - our merging procedures involve using a merge train that will automatically handle bringing a PR up to date.)

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Thank you for looking at this! All the library code looks good to me, thanks, I've just left a few suggestions about the test code.

Comment on lines 329 to 334
def test_return_type_singleton_instructions(self):
measure = Measure()
self.assertEqual(measure.__class__.__name__, "_SingletonMeasure")

reset = Reset()
self.assertEqual(reset.__class__.__name__, "_SingletonReset")
Copy link
Member

Choose a reason for hiding this comment

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

This test isn't quite right - technically it'll pass at the moment, but it's reliant on private internal details of the logic. A singleton is an object which is literally the same object on each return - in Python, that means a is b. You can still the style of assertIs tests above for how we test singletons.

Comment on lines 355 to 364

class ESPReset(Reset):
pass

reset_base = Reset()
esp_reset = ESPReset()
self.assertIs(esp_reset, ESPReset())
self.assertIsNot(esp_reset, reset_base)
self.assertIs(reset_base.base_class, Reset)
self.assertIs(esp_reset.base_class, ESPReset)
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to duplicate this test for Reset - it was enough to just test it for Measure, since at its heart, it was testing inheritance properties, rather than anything specific to Measure.

Comment on lines 336 to 343
def test_singleton_instruction_integration(self):
qc = QuantumCircuit(1, 1)
measure = Measure()
reset = Reset()
qc.append(measure, [0], [0])
qc.append(reset, [0])
self.assertEqual(qc.data[0].operation.__class__.__name__, "_SingletonMeasure")
self.assertEqual(qc.data[1].operation.__class__.__name__, "_SingletonReset")
Copy link
Member

Choose a reason for hiding this comment

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

For this test, the intention was to test the QuantumCircuit.measure and QuantumCircuit.reset methods, rather than the direct use of Measure and Reset constructors, which we tested in the test above this one.

@Matrixmang0
Copy link
Contributor Author

Thank you very much!
I have made the following corrections in the previous commit

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Thank you for this!

@jakelishman jakelishman added this pull request to the merge queue Nov 14, 2023
@jakelishman jakelishman modified the milestones: 1.0.0, 1.0.0pre1 Nov 14, 2023
@Matrixmang0
Copy link
Contributor Author

Thank you so much for the approval! Excited to continue learning and contributing.
Looking forward to more opportunities!

Merged via the queue into Qiskit:main with commit 7412951 Nov 14, 2023
14 checks passed
@Matrixmang0 Matrixmang0 deleted the singletonize-instructions branch November 14, 2023 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog Community PR PRs from contributors that are not 'members' of the Qiskit repo performance
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

5 participants