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

Commit

Permalink
Fix circuit metadata serialization bug (#648)
Browse files Browse the repository at this point in the history
* Fix serialization bug

* Revert "Fix serialization bug"

This reverts commit 72a08bc.

* Fix serialization bug

* add reno

* add unit test
  • Loading branch information
kt474 authored Jun 1, 2023
1 parent 114a216 commit e2e4463
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion qiskit_ibm_provider/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ def default(self, obj: Any) -> Any: # pylint: disable=arguments-differ
if isinstance(obj, QuantumCircuit):
value = _serialize_and_encode(
data=obj,
serializer=lambda buff, data: dump(data, buff), # type: ignore[no-untyped-call]
serializer=lambda buff, data: dump(
data, buff, RuntimeEncoder
), # type: ignore[no-untyped-call]
)
return {"__type__": "QuantumCircuit", "__value__": value}
if isinstance(obj, Parameter):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixed an issue where circuit metadata was not being serialized correctly
resulting in a type error.
14 changes: 12 additions & 2 deletions test/unit/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
"""Test serializing and deserializing data sent to the server."""

import json

import numpy as np
from qiskit import assemble
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.circuit import Parameter

from qiskit_ibm_provider.utils.json_encoder import IBMJsonEncoder
from qiskit_ibm_provider.utils.json import RuntimeEncoder
from ..ibm_test_case import IBMTestCase


class TestSerialization(IBMTestCase):
"""Test data serialization."""

def test_exception_message(self):
"""Test executing job with Parameter in methadata."""
"""Test executing job with Parameter in metadata."""
quantum_register = QuantumRegister(1)
classical_register = ClassicalRegister(1)
my_circ_str = "test_metadata"
Expand Down Expand Up @@ -65,3 +66,12 @@ def test_encode_replace(self):
'{"t1": 1, "null": null, "a": 0.2, "list": [1, 2, {"ld": 1, "2": 3, "alfa": 0.1}]}',
IBMJsonEncoder().encode(test_dir),
)

def test_circuit_metadata(self):
"""Test serializing circuit metadata."""

circ = QuantumCircuit(1)
circ.metadata = {"test": np.arange(0, 10)}
payload = {"circuits": [circ]}

self.assertTrue(json.dumps(payload, cls=RuntimeEncoder))

0 comments on commit e2e4463

Please sign in to comment.