From 14112feefc27b8163a05bcd5c86879e1c13abd78 Mon Sep 17 00:00:00 2001 From: Jessie Yu Date: Thu, 23 Nov 2023 20:06:07 -0500 Subject: [PATCH] v2 result --- qiskit_ibm_runtime/base_primitive.py | 4 ++-- qiskit_ibm_runtime/constants.py | 2 ++ .../utils/estimator_result_decoder.py | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/qiskit_ibm_runtime/base_primitive.py b/qiskit_ibm_runtime/base_primitive.py index f6fd81893..d34fc8d9c 100644 --- a/qiskit_ibm_runtime/base_primitive.py +++ b/qiskit_ibm_runtime/base_primitive.py @@ -145,7 +145,7 @@ def _run(self, tasks: Union[list[EstimatorTask], list[SamplerTask]]) -> RuntimeJ inputs=primitive_inputs, options=runtime_options, callback=options_dict.get("environment", {}).get("callback", None), - result_decoder=DEFAULT_DECODERS.get(self._program_id()), + result_decoder=DEFAULT_DECODERS.get((self._program_id(), self.version)), ) if self._backend: @@ -158,7 +158,7 @@ def _run(self, tasks: Union[list[EstimatorTask], list[SamplerTask]]) -> RuntimeJ options=runtime_options, inputs=primitive_inputs, callback=options_dict.get("environment", {}).get("callback", None), - result_decoder=DEFAULT_DECODERS.get(self._program_id()), + result_decoder=DEFAULT_DECODERS.get((self._program_id(), self.version)), ) @property diff --git a/qiskit_ibm_runtime/constants.py b/qiskit_ibm_runtime/constants.py index 3a5568cab..215dc4e1a 100644 --- a/qiskit_ibm_runtime/constants.py +++ b/qiskit_ibm_runtime/constants.py @@ -41,4 +41,6 @@ "estimator": [ResultDecoder, EstimatorResultDecoder], "circuit-runner": RunnerResult, "qasm3-runner": RunnerResult, + ("sampler", 2): [ResultDecoder, SamplerResultDecoder], + ("estimator", 2): [ResultDecoder, EstimatorResultDecoder], } diff --git a/qiskit_ibm_runtime/utils/estimator_result_decoder.py b/qiskit_ibm_runtime/utils/estimator_result_decoder.py index b4423d14c..f9ade0b07 100644 --- a/qiskit_ibm_runtime/utils/estimator_result_decoder.py +++ b/qiskit_ibm_runtime/utils/estimator_result_decoder.py @@ -12,12 +12,13 @@ """Estimator result decoder.""" -from typing import Dict +from typing import Dict, List import numpy as np from qiskit.primitives import EstimatorResult from ..program.result_decoder import ResultDecoder +from ..qiskit.primitives.task_result import TaskResult class EstimatorResultDecoder(ResultDecoder): @@ -31,3 +32,16 @@ def decode(cls, raw_result: str) -> EstimatorResult: values=np.asarray(decoded["values"]), metadata=decoded["metadata"], ) + + +class EstimatorV2ResultDecoder(ResultDecoder): + """Class used to decode v2 estimator results""" + + @classmethod + def decode(cls, raw_result: str) -> List[TaskResult]: + """Convert the result to a list of TaskResult.""" + decoded: Dict = super().decode(raw_result) + return EstimatorResult( + values=np.asarray(decoded["values"]), + metadata=decoded["metadata"], + )