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

Support standalone expr.Var in QPY #11651

Merged
merged 2 commits into from
Apr 30, 2024
Merged

Support standalone expr.Var in QPY #11651

merged 2 commits into from
Apr 30, 2024

Conversation

jakelishman
Copy link
Member

Summary

This necessitates adding some extra definitions to the circuit header. When standalone variables are used, we do not re-encode the entire variable (which would take a miniumum of ~20 bytes per variable), but instead just emit an index into the order that the variables were defined at the top of the circuit.

At present, each control-flow scope will store the Var nodes anew, so it's not a perfect only-store-once system. This is consistent with how registers are handled, though, and the QPY format isn't particularly memory optimised as things stand.

Details and comments

I need to write a couple more tests, including control-flow scopes, but this is the system in principle. Equality tests are stymied by not having access to QuantumCircuit.__eq__ with Var because of the lack of support in DAGCircuit.

Close #10930.

@jakelishman jakelishman added Changelog: New Feature Include in the "Added" section of the changelog mod: qpy Related to QPY serialization labels Jan 26, 2024
@jakelishman jakelishman requested a review from a team as a code owner January 26, 2024 18:42
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core
  • @mtreinish
  • @nkanazawa1989

@coveralls
Copy link

coveralls commented Jan 26, 2024

Pull Request Test Coverage Report for Build 8893323077

Warning: 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

  • 123 of 127 (96.85%) changed or added relevant lines in 6 files are covered.
  • 986 unchanged lines in 82 files lost coverage.
  • Overall coverage increased (+0.1%) to 89.483%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/qpy/type_keys.py 11 15 73.33%
Files with Coverage Reduction New Missed Lines %
qiskit/circuit/library/standard_gates/r.py 1 97.62%
qiskit/transpiler/target.py 1 93.74%
crates/qasm2/src/expr.rs 1 94.03%
qiskit/primitives/backend_sampler_v2.py 1 99.21%
qiskit/circuit/library/generalized_gates/permutation.py 1 93.1%
qiskit/primitives/containers/data_bin.py 1 97.92%
qiskit/circuit/library/standard_gates/rxx.py 1 97.44%
qiskit/circuit/library/standard_gates/u2.py 1 96.67%
qiskit/circuit/library/standard_gates/rzx.py 1 97.44%
qiskit/quantum_info/operators/operator.py 1 94.94%
Totals Coverage Status
Change from base Build 8653934653: 0.1%
Covered Lines: 61093
Relevant Lines: 68273

💛 - Coveralls

@jakelishman
Copy link
Member Author

This is being deferred to 1.1 along with the rest of the Var and Store handling. The PR as written depends on #11666 for its tests to pass, because the last test triggers the bug described in that PR.

@jakelishman
Copy link
Member Author

Huh, I forgot I'd already made this PR.

Anyway, I rebased this branch over main, cherry-picked in #11820 and #11666 which are required, and I'll rebase them out again once they're merged.

@jakelishman jakelishman added the on hold Can not fix yet label Feb 19, 2024
This necessitates adding some extra definitions to the circuit header.
When standalone variables are used, we do not re-encode the entire
variable (which would take a miniumum of ~20 bytes per variable), but
instead just emit an index into the order that the variables were
defined at the top of the circuit.

At present, each control-flow scope will store the `Var` nodes anew, so
it's not a perfect only-store-once system.  This is consistent with how
registers are handled, though, and the QPY format isn't particularly
memory optimised as things stand.
@jakelishman
Copy link
Member Author

Now rebased over all necessary fixes.

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

Overall I think this LGTM. I just left a couple comments and questions inline. The only real blocker is there is a missing field in the HEADER struct documentation I think, and I think we should have some negative tests for trying to dump qpy with an older version that uses classical vars. Also maybe having a backwards compat test that uses vars would be good too.

qiskit/qpy/binary_io/circuits.py Show resolved Hide resolved
qiskit/qpy/__init__.py Show resolved Hide resolved
struct {
char uuid_bytes[16];
char usage;
uint16_t name_size;
Copy link
Member

Choose a reason for hiding this comment

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

One day I'm expecting for a user to complain that qpy fails when they try to serialize a circuit with a 65536 character circuit name or something like that. But so far I guess people are keeping their names reasonable lengths.

Copy link
Member Author

Choose a reason for hiding this comment

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

You might enjoy #12117, in that case haha.

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

LGTM now, thanks for adding the additional tests.

@mtreinish mtreinish added this pull request to the merge queue Apr 30, 2024
Merged via the queue into Qiskit:main with commit 9c22197 Apr 30, 2024
13 checks passed
@jakelishman jakelishman deleted the var/qpy branch April 30, 2024 12:16
ElePT pushed a commit to ElePT/qiskit that referenced this pull request May 31, 2024
* Support standalone `expr.Var` in QPY

This necessitates adding some extra definitions to the circuit header.
When standalone variables are used, we do not re-encode the entire
variable (which would take a miniumum of ~20 bytes per variable), but
instead just emit an index into the order that the variables were
defined at the top of the circuit.

At present, each control-flow scope will store the `Var` nodes anew, so
it's not a perfect only-store-once system.  This is consistent with how
registers are handled, though, and the QPY format isn't particularly
memory optimised as things stand.

* Add backwards compatibility tests
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 mod: qpy Related to QPY serialization
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support manual variables in QPY
4 participants