-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Conversation
One or more of the the following people are requested to review this:
|
Pull Request Test Coverage Report for Build 7744276070
💛 - Coveralls |
MODIFIER_DEF = namedtuple("MODIFIER_DEF", ["type", "num_ctrl_qubits", "ctrl_state", "power"]) | ||
MODIFIER_DEF_PACK = "!1cIId" | ||
MODIFIER_DEF_SIZE = struct.calcsize(MODIFIER_DEF_PACK) |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
There was a problem hiding this 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
@@ -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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Co-authored-by: Matthew Treinish <[email protected]>
There was a problem hiding this 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
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
forPowerModifier
; 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), andnum_ctrls
,ctrl_state
andpower
.Second, I am handling annotated operations as "custom instructions" (similar to
ControlledGate
s) which already provides a convenient method to store/load the "base operation". The list of modifiers is stored ininstruction_params
.