Skip to content

Commit

Permalink
Cast use_symengine input to a bool (#1385)
Browse files Browse the repository at this point in the history
* 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.

* Fix mypy failures

* Mypy fixes again
  • Loading branch information
mtreinish authored Feb 7, 2024
1 parent a78c5ed commit 7890170
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions qiskit_ibm_runtime/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: 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.
Expand All @@ -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.
Expand Down

0 comments on commit 7890170

Please sign in to comment.