Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Handle qpy serialization across versions (#809)
Browse files Browse the repository at this point in the history
* Handle qpy serialization

* fix lint
  • Loading branch information
kt474 authored Feb 6, 2024
1 parent 8597a4c commit f537ad7
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions qiskit_ibm_provider/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,17 @@
)
from qiskit.result import Result
from qiskit.version import __version__ as _terra_version_string
from qiskit.qpy import load
from qiskit.utils import optionals

from ..qpy import (
_write_parameter,
from qiskit.qpy import (
_write_parameter_expression,
_read_parameter_expression,
_read_parameter_expression_v3,
_read_parameter,
load,
dump,
)

from qiskit.qpy.binary_io.value import _write_parameter, _read_parameter

_TERRA_VERSION = tuple(
int(x)
Expand Down Expand Up @@ -224,10 +223,15 @@ 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}
if _TERRA_VERSION[0] >= 1:
# NOTE: This can be updated only after the server side has
# updated to a newer qiskit version.
kwargs["version"] = 10
value = _serialize_and_encode(
data=obj,
serializer=lambda buff, data: dump(
data, buff, RuntimeEncoder, use_symengine=optionals.HAS_SYMENGINE
data, buff, RuntimeEncoder, **kwargs
), # type: ignore[no-untyped-call]
)
return {"__type__": "QuantumCircuit", "__value__": value}
Expand All @@ -243,17 +247,23 @@ def default(self, obj: Any) -> Any: # pylint: disable=arguments-differ
data=obj,
serializer=_write_parameter_expression,
compress=False,
use_symengine=optionals.HAS_SYMENGINE,
)
return {"__type__": "ParameterExpression", "__value__": value}
if isinstance(obj, Instruction):
kwargs = {"use_symengine": 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.
kwargs["version"] = 10
# Append instruction to empty circuit
quantum_register = QuantumRegister(obj.num_qubits)
quantum_circuit = QuantumCircuit(quantum_register)
quantum_circuit.append(obj, quantum_register)
value = _serialize_and_encode(
data=quantum_circuit,
serializer=lambda buff, data: dump(
data, buff, use_symengine=optionals.HAS_SYMENGINE
data, buff, **kwargs
), # type: ignore[no-untyped-call]
)
return {"__type__": "Instruction", "__value__": value}
Expand Down

0 comments on commit f537ad7

Please sign in to comment.