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 qpy support for Annotated Operations #11505

Merged
merged 21 commits into from Feb 1, 2024

Conversation

alexanderivrii
Copy link
Contributor

@alexanderivrii alexanderivrii commented Jan 7, 2024

Summary

This PR is built on top of #11495 and adds QPY support for annotated operations, addressing the other part of #11088.

(Update: removed a question on serializing/deserializing floating-point numbers which is relevant for storing power for PowerModifier; changed qpy format to store double).

Details and comments

I feel that I have cut a few corners in the implementation, suggestions for improvement are welcome.

First, instead of creating separate types for storing inverse modifiers, power modifiers and control modifiers, I am storing a "generic" modifier which includes the type (i.e. inverse, power or control), and num_ctrls, ctrl_state and power.

Second, I am handling annotated operations as "custom instructions" (similar to ControlledGates) which already provides a convenient method to store/load the "base operation". The list of modifiers is stored in instruction_params.

@alexanderivrii alexanderivrii requested a review from a team as a code owner January 7, 2024 16:25
@qiskit-bot
Copy link
Collaborator

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

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

@alexanderivrii alexanderivrii added this to the 1.0.0 milestone Jan 7, 2024
@alexanderivrii alexanderivrii added the mod: qpy Related to QPY serialization label Jan 7, 2024
@coveralls
Copy link

coveralls commented Jan 8, 2024

Pull Request Test Coverage Report for Build 7744276070

  • -4 of 57 (92.98%) changed or added relevant lines in 4 files are covered.
  • 18 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.02%) to 89.32%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/qpy/binary_io/circuits.py 44 46 95.65%
qiskit/qpy/type_keys.py 5 7 71.43%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 6 90.68%
crates/qasm2/src/parse.rs 12 96.23%
Totals Coverage Status
Change from base Build 7744260861: -0.02%
Covered Lines: 60188
Relevant Lines: 67385

💛 - Coveralls

@mtreinish mtreinish self-assigned this Jan 23, 2024
Comment on lines +143 to +145
MODIFIER_DEF = namedtuple("MODIFIER_DEF", ["type", "num_ctrl_qubits", "ctrl_state", "power"])
MODIFIER_DEF_PACK = "!1cIId"
MODIFIER_DEF_SIZE = struct.calcsize(MODIFIER_DEF_PACK)
Copy link
Member

Choose a reason for hiding this comment

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

I think this requires bumping the QPY version to 11 as this is a new struct to represent the modifiers. If someone tried to load a file using this with 0.45 which uses the same qpy version this would result in an error when the loader encounters the annotated operation (actually the new type key would trigger this too).

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 see, thanks.

Does this mean that I need to change this line

if version >= 5 and type_key == type_keys.CircuitInstruction.ANNOTATED_OPERATION:

to version >= 11 and ... or does this require more drastic changes? In addition, is this related to #11644?

Copy link
Member

Choose a reason for hiding this comment

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

You'll need to change that condition, also bump https://github.com/Qiskit/qiskit/blob/main/qiskit/qpy/common.py#L23 to 11, and also update the format documentation to describe what's new in version 11. I have another PR #11646 that actually is adding version 11 already, you can use this as a model. The two will merge conflict but we can handle that as there will only be version 11 in this release (there is only one format version for each qiskit release).

#11644 will have implications for this, as we'll need to error if someone uses an annotated operation and the version < 11 (this was something I didn't have to worry about in #11646)

@jakelishman jakelishman linked an issue Jan 31, 2024 that may be closed by this pull request
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.

This LGTM, just a couple comments inline. The other thing I think would be good to add here is a backwards compatibility test in https://github.com/Qiskit/qiskit/blob/main/test/qpy_compat/test_qpy.py that verifies annotated ops can be represented between versions into the future.

qiskit/qpy/binary_io/circuits.py Outdated Show resolved Hide resolved
@@ -587,7 +626,7 @@ def _write_instruction(file_obj, instruction, custom_operations, index_map, use_
not hasattr(library, gate_class_name)
and not hasattr(circuit_mod, gate_class_name)
Copy link
Member

Choose a reason for hiding this comment

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

Do we not export AnnotatedOperation from qiskit.circuit? I feel like it should be there as it is a public interface. If we are wouldn't this line pick it up as an attribute of the circuit module?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, that was an omission on my part. 77d044d exports this together with the modifiers. This also removes the need to check the name in the if above.

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, thanks for the update

@mtreinish mtreinish added this pull request to the merge queue Feb 1, 2024
Merged via the queue into Qiskit:main with commit 86a707e Feb 1, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mod: qpy Related to QPY serialization
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add qpy support for Cliffords and AnnotatedOperations
4 participants