From ee8f4f7f178f58c9a5cdc61ecb905cc715cd42c8 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Wed, 14 Sep 2022 20:56:10 -0400 Subject: [PATCH 1/2] add RunnerResult --- qiskit_ibm_runtime/__init__.py | 2 + qiskit_ibm_runtime/runner_result.py | 74 +++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 qiskit_ibm_runtime/runner_result.py diff --git a/qiskit_ibm_runtime/__init__.py b/qiskit_ibm_runtime/__init__.py index 4656971ac..01ff21c24 100644 --- a/qiskit_ibm_runtime/__init__.py +++ b/qiskit_ibm_runtime/__init__.py @@ -199,6 +199,7 @@ def result_callback(job_id, result): RuntimeOptions RuntimeEncoder RuntimeDecoder + RunnerResult """ import logging @@ -218,6 +219,7 @@ def result_callback(job_id, result): from .estimator import Estimator from .sampler import Sampler from .options import Options +from .runner_result import RunnerResult # TODO remove when terra code is released from .qiskit.primitives import ( diff --git a/qiskit_ibm_runtime/runner_result.py b/qiskit_ibm_runtime/runner_result.py new file mode 100644 index 000000000..a2a7ed520 --- /dev/null +++ b/qiskit_ibm_runtime/runner_result.py @@ -0,0 +1,74 @@ +# This code is part of Qiskit. +# +# (C) Copyright IBM 2021. +# +# 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. + +"""Circuit-runner result class""" + +from typing import List, Union +import json + +from qiskit.result import Result, QuasiDistribution +from qiskit.result.postprocess import _hex_to_bin +from qiskit.exceptions import QiskitError + +from .program import ResultDecoder + + +class RunnerResult(Result, ResultDecoder): + """Result class for Qiskit Runtime program circuit-runner.""" + + @classmethod + def decode(cls, data: str) -> "RunnerResult": + """Decoding for results from Qiskit runtime jobs.""" + return cls.from_dict(json.loads(data)) + + def get_quasiprobabilities( + self, experiment: Union[int, List] = None + ) -> Union[QuasiDistribution, List[QuasiDistribution]]: + """Get quasiprobabilites associated with one or more experiments. + + Parameters: + experiment: Indices of experiments to grab quasiprobabilities from. + + Returns: + A single distribution or a list of distributions. + + Raises: + QiskitError: If experiment result doesn't contain quasiprobabilities. + """ + if experiment is None: + exp_keys = range(len(self.results)) + else: + exp_keys = [experiment] # type: ignore[assignment] + + dict_list = [] + for key in exp_keys: + if "quasiprobabilities" in self.data(key).keys(): + shots = self.results[key].shots + hex_quasi = self.results[key].data.quasiprobabilities + bit_lenth = len(self.results[key].header.final_measurement_mapping) + quasi = {} + for hkey, val in hex_quasi.items(): + quasi[_hex_to_bin(hkey).zfill(bit_lenth)] = val + + out = QuasiDistribution(quasi, shots) + out.shots = shots + dict_list.append(out) + else: + raise QiskitError( + 'No quasiprobabilities for experiment "{}"'.format(repr(key)) + ) + + # Return first item of dict_list if size is 1 + if len(dict_list) == 1: + return dict_list[0] + else: + return dict_list From 257ffc9bab0df7027335994b6411b6b8dc259f62 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Fri, 16 Sep 2022 00:44:26 -0400 Subject: [PATCH 2/2] remove from init & move file --- qiskit_ibm_runtime/__init__.py | 2 -- qiskit_ibm_runtime/{ => utils}/runner_result.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) rename qiskit_ibm_runtime/{ => utils}/runner_result.py (98%) diff --git a/qiskit_ibm_runtime/__init__.py b/qiskit_ibm_runtime/__init__.py index 01ff21c24..4656971ac 100644 --- a/qiskit_ibm_runtime/__init__.py +++ b/qiskit_ibm_runtime/__init__.py @@ -199,7 +199,6 @@ def result_callback(job_id, result): RuntimeOptions RuntimeEncoder RuntimeDecoder - RunnerResult """ import logging @@ -219,7 +218,6 @@ def result_callback(job_id, result): from .estimator import Estimator from .sampler import Sampler from .options import Options -from .runner_result import RunnerResult # TODO remove when terra code is released from .qiskit.primitives import ( diff --git a/qiskit_ibm_runtime/runner_result.py b/qiskit_ibm_runtime/utils/runner_result.py similarity index 98% rename from qiskit_ibm_runtime/runner_result.py rename to qiskit_ibm_runtime/utils/runner_result.py index a2a7ed520..7db4f1989 100644 --- a/qiskit_ibm_runtime/runner_result.py +++ b/qiskit_ibm_runtime/utils/runner_result.py @@ -19,7 +19,7 @@ from qiskit.result.postprocess import _hex_to_bin from qiskit.exceptions import QiskitError -from .program import ResultDecoder +from ..program import ResultDecoder class RunnerResult(Result, ResultDecoder):