forked from Qiskit/qiskit-ibmq-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_encoder.py
66 lines (56 loc) · 2.11 KB
/
json_encoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
# pylint: disable=method-hidden
"""Custom JSON encoders."""
import json
from typing import Any
from qiskit.circuit.parameterexpression import ParameterExpression
class IQXJsonEncoder(json.JSONEncoder):
"""A json encoder for qobj"""
def __encode(self, param: Any) -> Any:
"""
Convert dictionary to contain only JSON serializable types. For example,
if the key is a Parameter we convert it to a string.
"""
if isinstance(param, dict):
param_bind_str = {}
for key in param.keys():
value = self.__encode(param[key])
if isinstance(key, (bool, float, int, str)) or key is None:
param_bind_str[key] = value
else:
param_bind_str[str(key)] = value
return param_bind_str
elif isinstance(param, list):
return [self.__encode(p) for p in param]
else:
return param
def encode(self, o: Any) -> str:
"""
Return a JSON string representation of a Python data structure.
"""
new_o = self.__encode(o)
return super().encode(new_o)
def default(self, o: Any) -> Any:
# Convert numpy arrays:
if hasattr(o, 'tolist'):
return o.tolist()
# Use Qobj complex json format:
if isinstance(o, complex):
return (o.real, o.imag)
if isinstance(o, ParameterExpression):
try:
return float(o)
except (TypeError, RuntimeError):
val = complex(o)
return val.real, val.imag
return json.JSONEncoder.default(self, o)