forked from Qiskit/qiskit-ibmq-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define custom json encoder for qobj payloads
The qobj payload has a couple of quirks related to how qobj objects can be constructed (and will be constructed after Qiskit/qiskit#3383 merges) that aren't handled by the default json encoder. The first is that complex numbers are converted to the form "(real, imaginary)", and the second is that numpy arrays are sometimes part of the payload. Instead of relying on a layer outside of the provider coercing these objects to the expected format this commit changes how we push qobj payloads to the iqx api so that we run it through a json encoder that understands how to do this. After this commit qiskit-ibmq-provider owns it's own payload format and the generation of the expected json payload format instead of relying on an external library to handle it for us. Related to Qiskit#553
- Loading branch information
Showing
3 changed files
with
39 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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): | ||
# 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) |