Skip to content

Commit

Permalink
Add json property to RE result. (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
msoeken authored Aug 17, 2023
1 parent fcd63c0 commit 8ac4f55
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions azure-quantum/azure/quantum/target/microsoft/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ def _plot(self, **kwargs):
plt.legend(loc="upper left")
plt.show()

@property
def json(self):
"""
Returns a JSON representation of the resource estimation result data.
"""
if not hasattr(self, "_json"):
import json
self._json = json.dumps(self._data)

return self._json

def _summary_data_frame(self, **kwargs):
try:
import pandas as pd
Expand Down
27 changes: 27 additions & 0 deletions azure-quantum/tests/unit/test_microsoft_qc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ def _ccnot_bitcode(self) -> bytes:
bitcode_filename = path.join(path.dirname(__file__), "qir", "ccnot.bc")
with open(bitcode_filename, "rb") as f:
return f.read()

def _mock_result_data(self) -> dict:
"""
A small result data for tests.
"""
return {
"physicalCounts": {
"physicalQubits": 655321,
"runtime": 1729,
"rqops": 314
},
"reportData": {"groups": [], "assumptions": []}
}

@pytest.mark.microsoft_qc
@pytest.mark.live_test
Expand Down Expand Up @@ -423,3 +436,17 @@ def test_estimator_protocol_specific_distillation_unit_specification_valid(self)
assert specification.as_dict() == {
"numUnitQubits": 1, "durationInQubitCycleTime": 2
}

def test_simple_result_as_json(self):
data = self._mock_result_data()
result = MicrosoftEstimatorResult(data)

import json
assert json.loads(result.json) == data

def test_batch_result_as_json(self):
data = [self._mock_result_data(), self._mock_result_data()]
result = MicrosoftEstimatorResult(data)

import json
assert json.loads(result.json) == data

0 comments on commit 8ac4f55

Please sign in to comment.