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 handling of ControlledGates in QPY #8055

Merged
merged 13 commits into from
Jun 17, 2022

Conversation

mtreinish
Copy link
Member

@mtreinish mtreinish commented May 12, 2022

Summary

This commit fixes the handling of ControlledGates in QPY. Previously the
extra parameters needed to reconstruct a custom controlled gate were not
encoded into the QPY payload. Fixing this required a version bump to the
QPY format to modify the payload for a custom instruction entry. Once we
added to the format the extra data required for a controlled gate, the
number of control qubits, the control state, and the base gate object,
the deserializer has enough information to recreate the custom
ControlledGate objects. However, fixing this exposed another bug with
standard library multicontrolled gates where they often didn't contain
sufficient data in the payload to reconstruct either.

Details and comments

Fixes #7999

TODO:

  • Fix failing unittest
  • Add format docs for QPY v5
  • Add release note

This commit fixes the handling of ControlledGates in QPY. Previously the
extra parameters needed to reconstruct a custom controlled gate were not
encoded into the QPY payload. Fixing this required a version bump to the
QPY format to modify the payload for a custom instruction entry. Once we
added to the format the extra data required for a controlled gate, the
number of control qubits, the control state, and the base gate object,
the deserializer has enough information to recreate the custom
ControlledGate objects. However, fixing this exposed another bug with
standard library multicontrolled gates where they often didn't contain
sufficient data in the payload to reconstruct either.

Fixes Qiskit#7999
@mtreinish mtreinish added on hold Can not fix yet Changelog: Bugfix Include in the "Fixed" section of the changelog labels May 12, 2022
@mtreinish mtreinish requested review from a team and nkanazawa1989 as code owners May 12, 2022 01:57
@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:

@mtreinish
Copy link
Member Author

I'm not sure that we want to backport this or not, it ended up being a bit more involved than I originally thought it would be.

This commit fixes the qpy test failure. This was caused by the omission
of the classical condition on a controlled gate when reconstructing the
circuit in deserialization. Fixing this oversight fixes the test
failures.
@coveralls
Copy link

coveralls commented May 12, 2022

Pull Request Test Coverage Report for Build 2518326855

  • 90 of 100 (90.0%) changed or added relevant lines in 3 files are covered.
  • 11 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.009%) to 84.265%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/qpy/binary_io/circuits.py 79 89 88.76%
Files with Coverage Reduction New Missed Lines %
qiskit/tools/parallel.py 11 67.69%
Totals Coverage Status
Change from base Build 2518065484: -0.009%
Covered Lines: 54885
Relevant Lines: 65134

💛 - Coveralls

@mtreinish mtreinish removed the on hold Can not fix yet label May 12, 2022
@mtreinish mtreinish changed the title [WIP] Fix handling of ControlledGates in QPY Fix handling of ControlledGates in QPY May 12, 2022
@mtreinish mtreinish added this to the 0.21 milestone May 12, 2022
@mtreinish mtreinish added Changelog: API Change Include in the "Changed" section of the changelog mod: qpy Related to QPY serialization labels May 12, 2022
Copy link
Contributor

@nkanazawa1989 nkanazawa1989 left a comment

Choose a reason for hiding this comment

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

Thanks Matthew. Your QPY update looks good to me though I don't quite follow the model of ControlledGate. I have impression that currently developers can too freely define new data model without considering the stability of QPY, and sometime we don't have enough bandwidth to track all changes. This is why I started to finalize the data model of pulse object before adding it.

If circuit model developer will continue to add or modify the data structure, perhaps it would be more sustainable to implement __getstate__ and __setstate__ in all circuit instructions and create QPY binary of it as a free-form dictionary with QPY serialized values?

(EDIT)
Perhaps it doesn't work in terms of backward compatibility of data.

@mtreinish
Copy link
Member Author

Yeah, it's hard to do that from a backwards compatibility standpoint. I also have concerns about how to implement that reliably/securely for arbitrary gate classes. To a certain extent this wasn't actually something new, the data model for circuits is very fractured in general. There are a lot of weird corner cases and classes that behave differently. There is work on trying to standardize it but it's slow moving. In the meantime though we have to deal with all these edge cases.

Copy link
Contributor

@nkanazawa1989 nkanazawa1989 left a comment

Choose a reason for hiding this comment

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

Thanks Matthew. Overall this looks great but I have some small questions.

qiskit/qpy/binary_io/circuits.py Outdated Show resolved Hide resolved
qiskit/qpy/binary_io/circuits.py Outdated Show resolved Hide resolved
qiskit/qpy/formats.py Show resolved Hide resolved
"type",
"num_qubits",
"num_clbits",
"custom_definition",
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curiosity, is it possible to express this with custom_definition_size of zero or non-zero rather than having explicit boolean flag along with size?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is possible, this was mostly an artifact from an earlier version of qpy and really isn't needed. I've just been avoiding changing it because it doesn't hurt and makes another incompatible change.

qiskit/qpy/binary_io/circuits.py Outdated Show resolved Hide resolved
This commit fixes the failing compat test which was incorrectly trying
to test ControlledGates with 0.20.2 generation. We should only run the
controlled gate tests starting with 0.21.0.
Copy link
Contributor

@nkanazawa1989 nkanazawa1989 left a comment

Choose a reason for hiding this comment

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

Thanks @mtreinish this looks good to me.

@mtreinish mtreinish mentioned this pull request Jun 17, 2022
8 tasks
@mergify mergify bot merged commit a34f0ce into Qiskit:main Jun 17, 2022
@mtreinish mtreinish deleted the qpy-controlled-gates branch June 23, 2022 23:11
rathishcholarajan added a commit to rathishcholarajan/qiskit-ibm-runtime that referenced this pull request Jul 1, 2022
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
rathishcholarajan added a commit to Qiskit/qiskit-ibm-runtime that referenced this pull request Jul 5, 2022
* Copy Qiskit/qiskit#8055

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Copy qpy changes from Qiskit/qiskit#8093

Co-authored-by: Jake Lishman <jake@binhbar.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>

* Copy Qiskit/qiskit#8200

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Copy of Qiskit/qiskit#7300

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Naoki Kanazawa <nkanazawa1989@gmail.com>

* Copy of Qiskit/qiskit#8235

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Jake Lishman <jake@binhbar.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Co-authored-by: Naoki Kanazawa <nkanazawa1989@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: API Change Include in the "Changed" section of the changelog Changelog: Bugfix Include in the "Fixed" section of the changelog mod: qpy Related to QPY serialization
Projects
None yet
Development

Successfully merging this pull request may close these issues.

QPY fails to deserialize some ControlledGates
5 participants