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

Support shots as np.int64 #744

Merged
merged 7 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading