diff --git a/qiskit/qpy/binary_io/value.py b/qiskit/qpy/binary_io/value.py index 105d4364c07e..bb550c771ebc 100644 --- a/qiskit/qpy/binary_io/value.py +++ b/qiskit/qpy/binary_io/value.py @@ -290,6 +290,29 @@ def _read_parameter_expression_v3(file_obj, vectors, use_symengine): payload = file_obj.read(data.expr_size) if use_symengine: + # This is a horrible hack to workaround the symengine version checking + # it's deserialization does. There were no changes to the serialization + # format between 0.11 and 0.13 but the deserializer checks that it can't + # load across a major or minor version boundary. This works around it + # by just lying about the generating version. + symengine_version = symengine.__version__.split(".") + major = payload[2] + minor = payload[3] + if int(symengine_version[1]) != minor: + if minor not in (11, 13): + raise exceptions.QpyError( + f"Incompatible symengine version {major}.{minor} used to generate the QPY " + "payload" + ) + minor_version = int(symengine_version[1]) + if minor_version not in (11, 13): + raise exceptions.QpyError( + f"Incompatible installed symengine version {symengine.__version__} to load " + "this QPY payload" + ) + payload = bytearray(payload) + payload[3] = minor_version + payload = bytes(payload) expr_ = load_basic(payload) else: from sympy.parsing.sympy_parser import parse_expr diff --git a/releasenotes/notes/fix-qpy-symengine-compat-858970a9a1d6bc14.yaml b/releasenotes/notes/fix-qpy-symengine-compat-858970a9a1d6bc14.yaml new file mode 100644 index 000000000000..b018ac74ef42 --- /dev/null +++ b/releasenotes/notes/fix-qpy-symengine-compat-858970a9a1d6bc14.yaml @@ -0,0 +1,13 @@ +--- +fixes: + - | + Fixed an issue with :func:`.qpy.load` when loading a QPY payload that + contains a :class:`.ParameterExpression` that was generated when symengine + 0.11 was installed and trying to load it in an environment where symengine + 0.13 (the latest symengine release as of this qiskit release) or vice versa. + Previously, an error would have been raised by symengine around this version + mismatch. This has been worked around for these two specific symengine versions + but if you're trying to use different versions of symengine and there is a + mismatch with this version of Qiskit this might not work. You will need to + install Qiskit >1.3.0 to fix this mismatch issue more broadly for any potential + future version of symengine.