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

Add symengine only mode to qpy #10705

Closed
mtreinish opened this issue Aug 24, 2023 · 2 comments · Fixed by #10820
Closed

Add symengine only mode to qpy #10705

mtreinish opened this issue Aug 24, 2023 · 2 comments · Fixed by #10820
Assignees
Labels
mod: qpy Related to QPY serialization performance priority: high type: feature request New feature or request
Milestone

Comments

@mtreinish
Copy link
Member

What should we add?

Right now QPY solely uses sympy to represent ParameterExpression objects in a circuit being serialized. This is done because our preferred library for representing symbolic expressions, symengine doesn't support all the platforms that Qiskit does, so we can not always rely on symengine being available when a qpy payload is deserialized. Sympy being our fallback (and significantly slower) option is always available so it is the logical choice to serialize the expression and then if symengine is available we convert from sympy objects to symengine objects. However, while this maximizes compatibility it comes with a real runtime cost, especially for circuits with lots of unbound parameter expressions. To address this we should expose an option to qiskit.qpy.dump to assert that symengine will always be available. This would use symengine native serialization which won't have the same runtime issues. If the deserialization side doesn't have symengine installed qiskit.qpy.load should error saying the payload only supports installations with symengine.

@mtreinish mtreinish added this to the 0.45.0 milestone Aug 24, 2023
@mtreinish mtreinish added the mod: qpy Related to QPY serialization label Aug 24, 2023
@mtreinish
Copy link
Member Author

The current pattern I've found for direct symengine serialization is:

import symengine
from symengine.lib.symengine_wrapper import load_basic

input_expression = symengine.Symbol('x') * 2
data = input_expression.__reduce__()[1[0]
loaded_expression = load_basic(data)

I've opened an issue with symengine to expose this interface more ergonomically. But in the meantime this should work with both symengine 0.9.x and 0.10.x

@nkanazawa1989
Copy link
Contributor

Yes, I think this is pretty useful. I've closed my PR because of the limitation that you mentioned, but it's worth doing in terms of performance :)

#8232

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 performance priority: high type: feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants