Skip to content

Commit

Permalink
Support shots as np.int64 (#744)
Browse files Browse the repository at this point in the history
* Fixed bug where shots defined as np.number

* Added test

* black

* add reno

---------

Co-authored-by: Kevin Tian <kevin.tian@ibm.com>
  • Loading branch information
merav-aharoni and kt474 committed Oct 16, 2023
1 parent efeda5b commit 98dc9f4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions qiskit_ibm_provider/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixed a bug where ``shots`` passed in as a numpy type were not being
serialized correctly.
8 changes: 8 additions & 0 deletions test/integration/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 98dc9f4

Please sign in to comment.