diff --git a/qiskit/providers/ibmq/api/rest/job.py b/qiskit/providers/ibmq/api/rest/job.py index bb2d7fef9..ecea7c184 100644 --- a/qiskit/providers/ibmq/api/rest/job.py +++ b/qiskit/providers/ibmq/api/rest/job.py @@ -15,11 +15,14 @@ """Job REST adapter for the IBM Q Experience API.""" import pprint +import json from json.decoder import JSONDecodeError from typing import Dict, Any from marshmallow.exceptions import ValidationError +from qiskit.providers.ibmq.utils import json_encoder + from .base import RestAdapterBase from .validation import StatusResponseSchema from ..session import RetrySession @@ -137,7 +140,8 @@ def put_object_storage(self, url: str, qobj_dict: Dict[str, Any]) -> str: Returns: text response, that will be empty if the request was successful. """ - response = self.session.put(url, json=qobj_dict, bare=True) + data = json.dumps(qobj_dict, cls=json_encoder.IQXJsonEconder) + response = self.session.put(url, data=data, bare=True) return response.text def get_object_storage(self, url: str) -> Dict[str, Any]: diff --git a/qiskit/providers/ibmq/api/rest/root.py b/qiskit/providers/ibmq/api/rest/root.py index e3e352ba2..760d9c359 100644 --- a/qiskit/providers/ibmq/api/rest/root.py +++ b/qiskit/providers/ibmq/api/rest/root.py @@ -18,6 +18,8 @@ from typing import Dict, List, Optional, Any +from qiskit.providers.ibmq.utils import json_encoder + from .base import RestAdapterBase from .backend import Backend from .job import Job @@ -140,7 +142,8 @@ def job_submit( if job_tags: payload['tags'] = job_tags - return self.session.post(url, json=payload).json() + data = json.dumps(payload, cls=json_encoder.IQXJsonEconder) + return self.session.post(url, data=data).json() def submit_job_object_storage( self, diff --git a/qiskit/providers/ibmq/utils/json_encoder.py b/qiskit/providers/ibmq/utils/json_encoder.py new file mode 100644 index 000000000..4d3f543bb --- /dev/null +++ b/qiskit/providers/ibmq/utils/json_encoder.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- + +# 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 + + +class IQXJsonEconder(json.JSONEncoder): + """A json encoder for qobj""" + + 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) + return json.JSONEncoder.default(self, o)