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 serialization of substitution of type ParameterExpression #13890

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jlapeyre
Copy link
Contributor

@jlapeyre jlapeyre commented Feb 19, 2025

When substitution history ParameterExpression._qpy_replay is serialized, there was no branch for the case that the substituted value is of type ParameterExpression.

This commit fixes this oversight.

  • I have added the tests to cover my changes.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.

Tests need to be added.

Closes #13879

When substitution history `ParameterExpression._qpy_replay` is serialized,
there was no branch for the case that the substituted value is of type
`ParameterExpression`.
This commit fixes this oversight.
@mtreinish mtreinish added Changelog: Bugfix Include in the "Fixed" section of the changelog mod: qpy Related to QPY serialization stable backport potential The bug might be minimal and/or import enough to be port to stable labels Feb 19, 2025
@coveralls
Copy link

coveralls commented Feb 19, 2025

Pull Request Test Coverage Report for Build 13427794082

Details

  • 13 of 13 (100.0%) changed or added relevant lines in 1 file are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.002%) to 88.103%

Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 2 94.29%
crates/qasm2/src/lex.rs 3 91.98%
Totals Coverage Status
Change from base Build 13421430619: -0.002%
Covered Lines: 78358
Relevant Lines: 88939

💛 - Coveralls

@jlapeyre
Copy link
Contributor Author

jlapeyre commented Feb 19, 2025

EDIT: Fixed in fa0d254

As it stands, this PR is not an adequate fix because, although, serialization does not error, deserialization of the result does error.

It looks like the current fix is inadequate because the value of a symbol in the replay is assumed to not be a ParameterExpression. In particular, the header written will not correspond to the data written. Both strings and numbers are written. Since the header is nonsense, an attempt is made to read numeric data into a string, which results in an invalid utf8 codec.

* Added a branch for reading `ParameterExpression` in _qpy_replay

* Added a missing argument (version) in an existing call in code path that
  was previously untested

* Remove a bit of useless code introduced in last commit

* run black
@jlapeyre jlapeyre marked this pull request as ready for review February 20, 2025 01:31
@jlapeyre jlapeyre requested a review from a team as a code owner February 20, 2025 01:31
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

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

@jlapeyre
Copy link
Contributor Author

I ported this to Qiskit v1.3.2 and verified that it works.

On the main branch, I can't test with qiskit-ibm-runtime, because it is incompatible with the qiskit development branch.

The fix applied to the stable release Qiskit v1.3.2 is not worth much, because deserializing on the server end fails of course. It will need to be updated, too.

Because of this, it looks like this will require a version bump in qpy.

@mtreinish mtreinish added this to the 1.4.1 milestone Feb 21, 2025
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 this LGTM, I left one small comment inside about trying to minimize the diff for backport reasons. The other thing which might be good is adding a backwards compatibilty test to https://github.com/Qiskit/qiskit/blob/main/test/qpy_compat/test_qpy.py to cover this case so that we ensure we can load payloads with these types of expressions from qiskit >= 1.4.1 with QPY 13.

Because of this, it looks like this will require a version bump in qpy.

I'm on the fence about this. From the way the format spec is written this was just an oversight in the implementation so I view it as a straight bug in the v13 implementation that neither serialization or deserialization works with nested substituted parameter expressions. But your also correct that no v13 implementation out there works with this model yet which makes it feel like doing this in v14 is a better choice.

I'm inclined to just leave this as a v13 fix, and not require v14 for it since the format is still parsable in v13, it will just error because of the deserialization bug.

@@ -229,14 +232,22 @@ def _write_parameter_expression(file_obj, obj, use_symengine, *, version):
file_obj.write(elem_header)
file_obj.write(symbol_data)
file_obj.write(value_data)
for symbol in extra_symbols.values():
for symbol in extra_expressions.values():
Copy link
Member

Choose a reason for hiding this comment

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

Do you mind reverting the name change here? I agree with the change in the naming, but since we're probably going to backport this PR for 1.4.1 I think it's better to minimize the diff to reduce the risk of regression. So we can keep the change here to just the functional component below adding the missing handling of the PARAMETER_EXPRESSION type in the extra expression/symbols.

We can do the renaming in a standalone PR that we don't backport.

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 mod: qpy Related to QPY serialization stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

QPY serialization of ParameterExpression throws error
4 participants