From ba2447f821d9d85c76156122a0cfe9456df3dd00 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 6 Feb 2024 20:57:04 -0500 Subject: [PATCH 1/3] Cast use_symengine input to a bool This commit works around a bug in Qiskit 0.45.x, 0.46.0, and 1.0.0rc1 with the `use_symengine` flag on `qpy.dump()`. The dump function has a bug when it receives a truthy value instead of a bool literal that it will generate a corrupt qpy because of a mismatch between how the encoding was processed (the encoding is incorrectly set to sympy in the file header but uses symengine encoding in the actual body of the circuit. This is being fixed in Qiskit/qiskit#11730 for 1.0.0, and will be backported to 0.46.1. But to ensure compatibility with 0.45.x, 0.46.0, and 1.0.0rc1 while waiting for those releases we can workaround this by just casting the value to a boolean. --- qiskit_ibm_runtime/utils/json.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qiskit_ibm_runtime/utils/json.py b/qiskit_ibm_runtime/utils/json.py index 1c2e7c287..2dbc9e413 100644 --- a/qiskit_ibm_runtime/utils/json.py +++ b/qiskit_ibm_runtime/utils/json.py @@ -215,7 +215,7 @@ def default(self, obj: Any) -> Any: # pylint: disable=arguments-differ if hasattr(obj, "to_json"): return {"__type__": "to_json", "__value__": obj.to_json()} if isinstance(obj, QuantumCircuit): - kwargs = {"use_symengine": optionals.HAS_SYMENGINE} + kwargs = {"use_symengine": bool(optionals.HAS_SYMENGINE)} if _TERRA_VERSION[0] >= 1: # NOTE: This can be updated only after the server side has # updated to a newer qiskit version. @@ -239,13 +239,13 @@ def default(self, obj: Any) -> Any: # pylint: disable=arguments-differ data=obj, serializer=_write_parameter_expression, compress=False, - use_symengine=optionals.HAS_SYMENGINE, + use_symengine=bool(optionals.HAS_SYMENGINE), ) return {"__type__": "ParameterExpression", "__value__": value} if isinstance(obj, ParameterView): return obj.data if isinstance(obj, Instruction): - kwargs = {"use_symengine": optionals.HAS_SYMENGINE} + kwargs = {"use_symengine": bool(optionals.HAS_SYMENGINE)} if _TERRA_VERSION[0] >= 1: # NOTE: This can be updated only after the server side has # updated to a newer qiskit version. From 9bc86040cd8c5a235c5c6c4378f751ab60c6802a Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 7 Feb 2024 09:14:40 -0500 Subject: [PATCH 2/3] Fix mypy failures --- qiskit_ibm_runtime/utils/json.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qiskit_ibm_runtime/utils/json.py b/qiskit_ibm_runtime/utils/json.py index 2dbc9e413..19e38e784 100644 --- a/qiskit_ibm_runtime/utils/json.py +++ b/qiskit_ibm_runtime/utils/json.py @@ -215,7 +215,7 @@ def default(self, obj: Any) -> Any: # pylint: disable=arguments-differ if hasattr(obj, "to_json"): return {"__type__": "to_json", "__value__": obj.to_json()} if isinstance(obj, QuantumCircuit): - kwargs = {"use_symengine": bool(optionals.HAS_SYMENGINE)} + kwargs: dict[str, object] = {"use_symengine": bool(optionals.HAS_SYMENGINE)} if _TERRA_VERSION[0] >= 1: # NOTE: This can be updated only after the server side has # updated to a newer qiskit version. @@ -245,7 +245,7 @@ def default(self, obj: Any) -> Any: # pylint: disable=arguments-differ if isinstance(obj, ParameterView): return obj.data if isinstance(obj, Instruction): - kwargs = {"use_symengine": bool(optionals.HAS_SYMENGINE)} + kwargs: dict[str, object] = {"use_symengine": bool(optionals.HAS_SYMENGINE)} if _TERRA_VERSION[0] >= 1: # NOTE: This can be updated only after the server side has # updated to a newer qiskit version. From c291a91fb30deda4a5da4555c5cf75fad8ce3919 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 7 Feb 2024 09:26:48 -0500 Subject: [PATCH 3/3] Mypy fixes again --- qiskit_ibm_runtime/utils/json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_ibm_runtime/utils/json.py b/qiskit_ibm_runtime/utils/json.py index 19e38e784..6b1468bea 100644 --- a/qiskit_ibm_runtime/utils/json.py +++ b/qiskit_ibm_runtime/utils/json.py @@ -245,7 +245,7 @@ def default(self, obj: Any) -> Any: # pylint: disable=arguments-differ if isinstance(obj, ParameterView): return obj.data if isinstance(obj, Instruction): - kwargs: dict[str, object] = {"use_symengine": bool(optionals.HAS_SYMENGINE)} + kwargs = {"use_symengine": bool(optionals.HAS_SYMENGINE)} if _TERRA_VERSION[0] >= 1: # NOTE: This can be updated only after the server side has # updated to a newer qiskit version.