From e2e4463d1737d0ced19da572bd2e99a4e8ce5e31 Mon Sep 17 00:00:00 2001 From: Kevin Tian Date: Thu, 1 Jun 2023 13:15:27 -0400 Subject: [PATCH] Fix circuit metadata serialization bug (#648) * Fix serialization bug * Revert "Fix serialization bug" This reverts commit 72a08bc8c5b92833334d8a2edbd3950c08f39259. * Fix serialization bug * add reno * add unit test --- qiskit_ibm_provider/utils/json.py | 4 +++- .../fix-serialization-bug-adabc48b48cc1ccd.yaml | 6 ++++++ test/unit/test_serialization.py | 14 ++++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/fix-serialization-bug-adabc48b48cc1ccd.yaml diff --git a/qiskit_ibm_provider/utils/json.py b/qiskit_ibm_provider/utils/json.py index 57823968b..c64434e7e 100644 --- a/qiskit_ibm_provider/utils/json.py +++ b/qiskit_ibm_provider/utils/json.py @@ -212,7 +212,9 @@ def default(self, obj: Any) -> Any: # pylint: disable=arguments-differ if isinstance(obj, QuantumCircuit): value = _serialize_and_encode( data=obj, - serializer=lambda buff, data: dump(data, buff), # type: ignore[no-untyped-call] + serializer=lambda buff, data: dump( + data, buff, RuntimeEncoder + ), # type: ignore[no-untyped-call] ) return {"__type__": "QuantumCircuit", "__value__": value} if isinstance(obj, Parameter): diff --git a/releasenotes/notes/fix-serialization-bug-adabc48b48cc1ccd.yaml b/releasenotes/notes/fix-serialization-bug-adabc48b48cc1ccd.yaml new file mode 100644 index 000000000..65a5345fc --- /dev/null +++ b/releasenotes/notes/fix-serialization-bug-adabc48b48cc1ccd.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fixed an issue where circuit metadata was not being serialized correctly + resulting in a type error. + diff --git a/test/unit/test_serialization.py b/test/unit/test_serialization.py index 02e217108..d3b5cefc7 100644 --- a/test/unit/test_serialization.py +++ b/test/unit/test_serialization.py @@ -13,12 +13,13 @@ """Test serializing and deserializing data sent to the server.""" import json - +import numpy as np from qiskit import assemble from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister from qiskit.circuit import Parameter from qiskit_ibm_provider.utils.json_encoder import IBMJsonEncoder +from qiskit_ibm_provider.utils.json import RuntimeEncoder from ..ibm_test_case import IBMTestCase @@ -26,7 +27,7 @@ class TestSerialization(IBMTestCase): """Test data serialization.""" def test_exception_message(self): - """Test executing job with Parameter in methadata.""" + """Test executing job with Parameter in metadata.""" quantum_register = QuantumRegister(1) classical_register = ClassicalRegister(1) my_circ_str = "test_metadata" @@ -65,3 +66,12 @@ def test_encode_replace(self): '{"t1": 1, "null": null, "a": 0.2, "list": [1, 2, {"ld": 1, "2": 3, "alfa": 0.1}]}', IBMJsonEncoder().encode(test_dir), ) + + def test_circuit_metadata(self): + """Test serializing circuit metadata.""" + + circ = QuantumCircuit(1) + circ.metadata = {"test": np.arange(0, 10)} + payload = {"circuits": [circ]} + + self.assertTrue(json.dumps(payload, cls=RuntimeEncoder))