-
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
Add Qiskit native QPY ParameterExpression serialization #13356
Merged
Merged
Commits on Oct 22, 2024
-
Add Qiskit native QPY ParameterExpression serialization
With the release of symengine 0.13.0 we discovered a version dependence on the payload format used for serializing symengine expressions. This was worked around in Qiskit#13251 but this is not a sustainable solution and only works for symengine 0.11.0 and 0.13.0 (there was no 0.12.0). While there was always the option to use sympy to serialize the underlying symbolic expression (there is a `use_symengine` flag on `qpy.dumps` you can set to `False` to do this) the sympy serialzation has several tradeoffs most importantly is much higher runtime overhead. To solve the issue moving forward a qiskit native representation of the parameter expression object is necessary for serialization. This commit bumps the QPY format version to 13 and adds a new serialization format for ParameterExpression objects. This new format is a serialization of the API calls made to ParameterExpression that resulted in the creation of the underlying object. To facilitate this the ParameterExpression class is expanded to store an internal "replay" record of the API calls used to construct the ParameterExpression object. This internal list is what gets serialized by QPY and then on deserialization the "replay" is replayed to reconstruct the expression object. This is a different approach to the previous QPY representations of the ParameterExpression objects which instead represented the internal state stored in the ParameterExpression object with the symbolic expression from symengine (or a sympy copy of the expression). Doing this directly in Qiskit isn't viable though because symengine's internal expression tree is not exposed to Python directly. There isn't any method (private or public) to walk the expression tree to construct a serialization format based off of it. Converting symengine to a sympy expression and then using sympy's API to walk the expression tree is a possibility but that would tie us to sympy which would be problematic for Qiskit#13267 and Qiskit#13131, have significant runtime overhead, and it would be just easier to rely on sympy's native serialization tools. The tradeoff with this approach is that it does increase the memory overhead of the `ParameterExpression` class because for each element in the expression we have to store a record of it. Depending on the depth of the expression tree this also could be a lot larger than symengine's internal representation as we store the raw api calls made to create the ParameterExpression but symengine is likely simplifying it's internal representation as it builds it out. But I personally think this tradeoff is worthwhile as it ties the serialization format to the Qiskit objects instead of relying on a 3rd party library. This also gives us the flexibility of changing the internal symbolic expression library internally in the future if we decide to stop using symengine at any point. Fixes Qiskit#13252
Configuration menu - View commit details
-
Copy full SHA for 87e2a93 - Browse repository at this point
Copy the full SHA 87e2a93View commit details
Commits on Oct 23, 2024
-
Configuration menu - View commit details
-
Copy full SHA for 0f84dba - Browse repository at this point
Copy the full SHA 0f84dbaView commit details
Commits on Oct 24, 2024
-
Configuration menu - View commit details
-
Copy full SHA for 969e471 - Browse repository at this point
Copy the full SHA 969e471View commit details -
Configuration menu - View commit details
-
Copy full SHA for 54ef86e - Browse repository at this point
Copy the full SHA 54ef86eView commit details -
Configuration menu - View commit details
-
Copy full SHA for 7d1fbfe - Browse repository at this point
Copy the full SHA 7d1fbfeView commit details
Commits on Oct 31, 2024
-
Configuration menu - View commit details
-
Copy full SHA for 2b352d7 - Browse repository at this point
Copy the full SHA 2b352d7View commit details -
Configuration menu - View commit details
-
Copy full SHA for 5d7b79a - Browse repository at this point
Copy the full SHA 5d7b79aView commit details -
Configuration menu - View commit details
-
Copy full SHA for 2df6582 - Browse repository at this point
Copy the full SHA 2df6582View commit details -
Configuration menu - View commit details
-
Copy full SHA for 60a2172 - Browse repository at this point
Copy the full SHA 60a2172View commit details
Commits on Nov 5, 2024
-
Pivot to stack based postfix/rpn deserialization
This commit changes how the deserialization works to use a postfix stack based approach. Operands are push on the stack and then popped off based on the operation being run. The result of the operation is then pushed on the stack. This handles nested objects much more cleanly than the recursion based approach because we just keep pushing on the stack instead of recursing, making the accounting much simpler. After the expression payload is finished being processed there will be a single value on the stack and that is returned as the final expression.
Configuration menu - View commit details
-
Copy full SHA for a903387 - Browse repository at this point
Copy the full SHA a903387View commit details -
Configuration menu - View commit details
-
Copy full SHA for 90e2c3b - Browse repository at this point
Copy the full SHA 90e2c3bView commit details
Commits on Nov 6, 2024
-
Apply suggestions from code review
Co-authored-by: Elena Peña Tapia <[email protected]>
Configuration menu - View commit details
-
Copy full SHA for b72856d - Browse repository at this point
Copy the full SHA b72856dView commit details -
Configuration menu - View commit details
-
Copy full SHA for 8ae6589 - Browse repository at this point
Copy the full SHA 8ae6589View commit details -
Configuration menu - View commit details
-
Copy full SHA for 11f65ad - Browse repository at this point
Copy the full SHA 11f65adView commit details -
Configuration menu - View commit details
-
Copy full SHA for 6e75ea5 - Browse repository at this point
Copy the full SHA 6e75ea5View commit details -
Correctly handle non-commutative operations
This commit fixes a bug with handling the operand order of subtraction, division, and exponentiation. These operations are not commutative but the qpy deserialization code was treating them as such. So in cases where the argument order was reversed qpy was trying to flip the operands around for code simplicity and this would result in incorrect behavior. This commit fixes this by adding explicit op codes for the reversed sub, div, and pow and preserving the operand order correctly in these cases.
Configuration menu - View commit details
-
Copy full SHA for 8c2205f - Browse repository at this point
Copy the full SHA 8c2205fView commit details -
Configuration menu - View commit details
-
Copy full SHA for 33953fe - Browse repository at this point
Copy the full SHA 33953feView commit details -
Configuration menu - View commit details
-
Copy full SHA for 34479e3 - Browse repository at this point
Copy the full SHA 34479e3View commit details
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.