diff --git a/qiskit_ibm_provider/utils/json.py b/qiskit_ibm_provider/utils/json.py index 380d9b000..d9eaf285c 100644 --- a/qiskit_ibm_provider/utils/json.py +++ b/qiskit_ibm_provider/utils/json.py @@ -203,6 +203,8 @@ 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.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()} 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. + diff --git a/test/integration/test_serialization.py b/test/integration/test_serialization.py index f00cd4f83..a906c3a39 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,13 @@ 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.