From 5745825421ced0d0f6a1ac197209d4f4299d5e08 Mon Sep 17 00:00:00 2001 From: merav-aharoni Date: Tue, 3 Oct 2023 13:11:47 +0000 Subject: [PATCH 1/4] Fixed bug where shots defined as np.number --- qiskit_ibm_provider/utils/json.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/qiskit_ibm_provider/utils/json.py b/qiskit_ibm_provider/utils/json.py index 380d9b000..ff3a282ac 100644 --- a/qiskit_ibm_provider/utils/json.py +++ b/qiskit_ibm_provider/utils/json.py @@ -204,8 +204,7 @@ def default(self, obj: Any) -> Any: # pylint: disable=arguments-differ value = _serialize_and_encode(obj, np.save, allow_pickle=False) return {"__type__": "ndarray", "__value__": value} if isinstance(obj, np.number): - # Maybe we should encode the numpy data type here for better accuracy. - return {"__type__": type(obj.item()).__name__, "__value__": obj.item()} + return obj.item() if isinstance(obj, set): return {"__type__": "set", "__value__": list(obj)} if isinstance(obj, Result): From ad28e15dffa4d58aeb2434d59aa218a1fbcde058 Mon Sep 17 00:00:00 2001 From: merav-aharoni Date: Tue, 3 Oct 2023 13:57:25 +0000 Subject: [PATCH 2/4] Added test --- test/integration/test_serialization.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/integration/test_serialization.py b/test/integration/test_serialization.py index f00cd4f83..019f5d683 100644 --- a/test/integration/test_serialization.py +++ b/test/integration/test_serialization.py @@ -14,6 +14,7 @@ from typing import Set, Any, Dict, Optional from unittest import SkipTest, skip +import numpy as np import dateutil.parser from qiskit import transpile, schedule, QuantumCircuit @@ -197,6 +198,12 @@ def test_convert_complex(self): self.assertEqual(val[0], 0.2) self.assertEqual(val[1], 0.1) + def test_np_number(self): + """Test using np.number""" + shots = 100 + result = self.sim_backend.run(self.bell, shots=np.int64(shots)).result() + self.assertEqual(result.results[0].shots, shots) + print(result.results[0]) def _find_potential_encoded(data: Any, c_key: str, tally: set) -> None: """Find data that may be in JSON serialized format. From f96a04e75dc1af30756d2d03ce1e4c261b6d52ea Mon Sep 17 00:00:00 2001 From: merav-aharoni Date: Tue, 3 Oct 2023 13:59:24 +0000 Subject: [PATCH 3/4] black --- test/integration/test_serialization.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/integration/test_serialization.py b/test/integration/test_serialization.py index 019f5d683..a906c3a39 100644 --- a/test/integration/test_serialization.py +++ b/test/integration/test_serialization.py @@ -205,6 +205,7 @@ def test_np_number(self): self.assertEqual(result.results[0].shots, shots) print(result.results[0]) + def _find_potential_encoded(data: Any, c_key: str, tally: set) -> None: """Find data that may be in JSON serialized format. From a992ac01ebd1108501c8a020f7804e5f5e04aec9 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Mon, 16 Oct 2023 13:00:05 -0400 Subject: [PATCH 4/4] add reno --- qiskit_ibm_provider/utils/json.py | 5 ++++- .../notes/numpy-int-shots-encoding-63dc2498cf28760d.yaml | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/numpy-int-shots-encoding-63dc2498cf28760d.yaml diff --git a/qiskit_ibm_provider/utils/json.py b/qiskit_ibm_provider/utils/json.py index ff3a282ac..d9eaf285c 100644 --- a/qiskit_ibm_provider/utils/json.py +++ b/qiskit_ibm_provider/utils/json.py @@ -203,8 +203,11 @@ def default(self, obj: Any) -> Any: # pylint: disable=arguments-differ return {"__type__": "ndarray", "__value__": obj.tolist()} value = _serialize_and_encode(obj, np.save, allow_pickle=False) return {"__type__": "ndarray", "__value__": value} - if isinstance(obj, np.number): + if isinstance(obj, np.int64): return obj.item() + if isinstance(obj, np.number): + # Maybe we should encode the numpy data type here for better accuracy. + return {"__type__": type(obj.item()).__name__, "__value__": obj.item()} if isinstance(obj, set): return {"__type__": "set", "__value__": list(obj)} if isinstance(obj, Result): diff --git a/releasenotes/notes/numpy-int-shots-encoding-63dc2498cf28760d.yaml b/releasenotes/notes/numpy-int-shots-encoding-63dc2498cf28760d.yaml new file mode 100644 index 000000000..e8efaefcf --- /dev/null +++ b/releasenotes/notes/numpy-int-shots-encoding-63dc2498cf28760d.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fixed a bug where ``shots`` passed in as a numpy type were not being + serialized correctly. +