From 06397b9246c27ce6b1968b3edffb2d68c08cbf5b Mon Sep 17 00:00:00 2001 From: "Diego M. Rodriguez" Date: Wed, 1 May 2019 10:17:42 +0200 Subject: [PATCH] Make use of noise model serialization Make use of the optional `serializable` parameter for noise models, delegating the serialization on Aer itself instead of manually converting to simple types in this codebase. --- qiskit/providers/ibmq/utils.py | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/qiskit/providers/ibmq/utils.py b/qiskit/providers/ibmq/utils.py index 75e5f446f..6de4c623c 100644 --- a/qiskit/providers/ibmq/utils.py +++ b/qiskit/providers/ibmq/utils.py @@ -7,33 +7,9 @@ """Utilities related to the IBMQ Provider.""" -import json - -from numpy import ndarray - from qiskit.qobj import QobjHeader -class AerJSONEncoder(json.JSONEncoder): - """JSON encoder for NumPy arrays and complex numbers. - - This functions as the standard JSON Encoder but adds support - for encoding: - complex numbers z as lists [z.real, z.imag] - ndarrays as nested lists. - """ - - # pylint: disable=method-hidden,arguments-differ - def default(self, obj): - if isinstance(obj, ndarray): - return obj.tolist() - if isinstance(obj, complex): - return [obj.real, obj.imag] - if hasattr(obj, "as_dict"): - return obj.as_dict() - return super().default(obj) - - def update_qobj_config(qobj, backend_options=None, noise_model=None): """Update a Qobj configuration from options and noise model. @@ -54,8 +30,7 @@ def update_qobj_config(qobj, backend_options=None, noise_model=None): # Append noise model to configuration. if noise_model: - config['noise_model'] = json.loads(json.dumps(noise_model, - cls=AerJSONEncoder)) + config['noise_model'] = noise_model.as_dict(serializable=True) # Update the Qobj configuration. qobj.config = QobjHeader.from_dict(config)