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

Allow correct retrieval of circuit with initialize instruction from qpy file #11206

Merged

Conversation

SoranaAurelia
Copy link
Contributor

Summary

This PR fixes #11158 . It adds the necessary conditions and cases for treating the StatePreparation instruction when reading from a qpy file.

Details and comments

When loading a circuit that had an initialize instruction (with an index or a label as parameter) from a qpy file, a QiskitError was thrown because these cases were not treated when the circuit was rebuilt, more specifically in the read instruction function.

@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Nov 6, 2023
@coveralls
Copy link

coveralls commented Nov 6, 2023

Pull Request Test Coverage Report for Build 6909812262

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.

  • 7 of 7 (100.0%) changed or added relevant lines in 1 file are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.02%) to 85.898%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 3 91.67%
Totals Coverage Status
Change from base Build 6892258856: 0.02%
Covered Lines: 65945
Relevant Lines: 76771

💛 - Coveralls

Comment on lines +1693 to +1698
with io.BytesIO() as fptr:
dump(qc, fptr)
fptr.seek(0)
new_circuit = load(fptr)[0]
self.assertEqual(qc, new_circuit)
self.assertDeprecatedBitProperties(qc, new_circuit)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe a bit out of the scope of this ticket, but I saw that this piece of code of 5 lines (or equivalent code

qpy_file = io.BytesIO()
dump(qc, qpy_file)
qpy_file.seek(0)
new_circ = load(qpy_file)[0]
self.assertEqual(qc, new_circ)
self.assertDeprecatedBitProperties(qc, new_circ)

) is repeated throughout this file a lot of times. Wouldn't it be better if we had in this test file a similar function as the one in qpy/test_circuit_load_from_qpy ?
def assert_roundtrip_equal(self, circuit):
"""QPY roundtrip equal test."""
qpy_file = io.BytesIO()
dump(circuit, qpy_file)
qpy_file.seek(0)
new_circuit = load(qpy_file)[0]
self.assertEqual(circuit, new_circuit)
self.assertEqual(circuit.layout, new_circuit.layout)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we could potentially improve this. This particular file is older that test/qpy/test_circuit_load_from_qpy.py, and to be honest, I don't know why this one still exists - I thought it had been moved into the test/python/qpy one some time ago. You're right that it would be better in a follow-up PR. If you'd like to make such a PR, it would be welcome.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like to make a PR with the refactoring of this file. I will open an issue and associated PR shortly! Thanks

@SoranaAurelia SoranaAurelia changed the title Allow saving and retriving circuit with initialize instruction from qpy file Allow correct retrieval of circuit with initialize instruction from qpy file Nov 8, 2023
@SoranaAurelia SoranaAurelia marked this pull request as ready for review November 8, 2023 09:24
@SoranaAurelia SoranaAurelia requested a review from a team as a code owner November 8, 2023 09:24
@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
  • @nkanazawa1989

@jakelishman jakelishman added this to the 0.45.1 milestone Nov 15, 2023
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! Please could you add a "bugfix" release note briefly explaining the change?

Comment on lines +1693 to +1698
with io.BytesIO() as fptr:
dump(qc, fptr)
fptr.seek(0)
new_circuit = load(fptr)[0]
self.assertEqual(qc, new_circuit)
self.assertDeprecatedBitProperties(qc, new_circuit)
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we could potentially improve this. This particular file is older that test/qpy/test_circuit_load_from_qpy.py, and to be honest, I don't know why this one still exists - I thought it had been moved into the test/python/qpy one some time ago. You're right that it would be better in a follow-up PR. If you'd like to make such a PR, it would be welcome.

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!

@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: qpy Related to QPY serialization labels Nov 17, 2023
@jakelishman jakelishman added this pull request to the merge queue Nov 17, 2023
Merged via the queue into Qiskit:main with commit 9111d0f Nov 18, 2023
mergify bot pushed a commit that referenced this pull request Nov 18, 2023
…py file (#11206)

* treated cases for StatePreparation initialization when reading from qpy file

* added test for state preparation from qpy

* added docstring to test function

* added fix release note

* Fixup release note

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 9111d0f)
github-merge-queue bot pushed a commit that referenced this pull request Nov 18, 2023
…py file (#11206) (#11274)

* treated cases for StatePreparation initialization when reading from qpy file

* added test for state preparation from qpy

* added docstring to test function

* added fix release note

* Fixup release note

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 9111d0f)

Co-authored-by: SoranaAurelia <52232581+SoranaAurelia@users.noreply.github.com>
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 Community PR PRs from contributors that are not 'members' of the Qiskit repo mod: qpy Related to QPY serialization stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
Status: Done
4 participants