From b217a64cae90d9b3c11a0ba4b23b1c30e261272e Mon Sep 17 00:00:00 2001 From: musasina Date: Sun, 10 Dec 2023 19:10:52 +0300 Subject: [PATCH] Updated add_data and deprecated _add_data #1268 --- .../framework/composite/composite_analysis.py | 8 ++ .../framework/experiment_data.py | 79 ++++++++----------- 2 files changed, 40 insertions(+), 47 deletions(-) diff --git a/qiskit_experiments/framework/composite/composite_analysis.py b/qiskit_experiments/framework/composite/composite_analysis.py index 04375417d9..d2975d7e49 100644 --- a/qiskit_experiments/framework/composite/composite_analysis.py +++ b/qiskit_experiments/framework/composite/composite_analysis.py @@ -125,8 +125,12 @@ def _run_analysis(self, experiment_data: ExperimentData): experiment_data.create_child_data() ======= +<<<<<<< HEAD experiment_data._add_data(component_expdata,experiment_data.data()) >>>>>>> 2dbba8ac (Passed test new start) +======= + experiment_data.add_data(experiment_data.data()) +>>>>>>> 0bd3a186 (Updated add_data and deprecated _add_data #1268) if len(self._analyses) != len(child_data): raise("analysis length and experiment lenggth are not same") @@ -134,7 +138,11 @@ def _run_analysis(self, experiment_data: ExperimentData): for sub_analysis, sub_data in zip(self._analyses, child_data): # Since copy for replace result is handled at the parent level # we always run with replace result on component analysis +<<<<<<< HEAD sub_analysis.run(sub_data, replace_results=True) +======= + self._analyses[i].run(sub_expdata, replace_results=True) +>>>>>>> 0bd3a186 (Updated add_data and deprecated _add_data #1268) # Analysis is running in parallel so we add loop to wait # for all component analysis to finish before returning diff --git a/qiskit_experiments/framework/experiment_data.py b/qiskit_experiments/framework/experiment_data.py index ff0234cb79..5db359be6d 100644 --- a/qiskit_experiments/framework/experiment_data.py +++ b/qiskit_experiments/framework/experiment_data.py @@ -19,7 +19,12 @@ from typing import Dict, Optional, List, Union, Any, Callable, Tuple, Iterator, TYPE_CHECKING from datetime import datetime, timezone from concurrent import futures +<<<<<<< HEAD from functools import wraps, partial +======= +from threading import Event +from functools import wraps, singledispatch +>>>>>>> 0bd3a186 (Updated add_data and deprecated _add_data #1268) from collections import deque, defaultdict import contextlib import copy @@ -730,17 +735,19 @@ def add_data( ) if not isinstance(data, list): data = [data] - + if data != [] and isinstance(data[0],dict): + marginalized_data = self._marginalized_component_data(data) # Directly add non-job data with self._result_data.lock: tmp_exp_data = ExperimentData() composite_flag = False - experiment_seperator = {} + experiment_seperator = defaultdict(lambda : ExperimentData()) for datum in data: if isinstance(datum, dict): - if "composite_metadata" in datum["metadata"]: + if "metadata" in datum and "composite_metadata" in datum["metadata"]: composite_flag = True +<<<<<<< HEAD marginalized_data = self._marginalized_component_data([datum]) for inner_datum in marginalized_data: #print(inner_datum) @@ -759,20 +766,21 @@ def add_data( self._result_data.append(datum) ======= +======= + experiment_seperator[datum["metadata"]["experiment_type"]].add_data(datum["metadata"]["composite_metadata"]) + +>>>>>>> 0bd3a186 (Updated add_data and deprecated _add_data #1268) elif "composite_metadata" in datum: composite_flag = True - marginalized_data = self._marginalized_component_data([datum]) - for inner_datum in marginalized_data: - #print(inner_datum) - if "experiment_type" in inner_datum[0]["metadata"]: - if inner_datum[0]["metadata"]["experiment_type"] in experiment_seperator: - experiment_seperator[inner_datum[0]["metadata"]["experiment_type"]].add_data(inner_datum[0]) - else: - experiment_seperator[inner_datum[0]["metadata"]["experiment_type"]] = ExperimentData() - experiment_seperator[inner_datum[0]["metadata"]["experiment_type"]].add_data(inner_datum[0]) + experiment_seperator[datum["experiment_type"]].add_data(datum["composite_metadata"]) +<<<<<<< HEAD self._result_data.append(datum) >>>>>>> 9eb2dba0 (Updated add_data tests passed #1268) +======= + if datum not in self._result_data: + self._result_data.append(datum) +>>>>>>> 0bd3a186 (Updated add_data and deprecated _add_data #1268) >>>>>>> e7f46c3a (Updated add_data tests passed #1268) elif isinstance(datum, Result): @@ -782,9 +790,17 @@ def add_data( self._add_result_data(datum) else: raise TypeError(f"Invalid data type {type(datum)}.") + if composite_flag: + for sub_expdata, sub_data in zip(self.child_data(), marginalized_data): + # Clear any previously stored data and add marginalized data + sub_expdata._result_data.clear() + sub_expdata.__add_data(sub_data) tmp_exp_data._set_child_data(list(experiment_seperator.values())) - self._set_child_data([tmp_exp_data]) + if self._child_data.values() != []: + self.add_child_data(tmp_exp_data) + else: + self._set_child_data([tmp_exp_data]) def __add_data( self, @@ -823,40 +839,6 @@ def __add_data( self._add_result_data(datum) else: raise TypeError(f"Invalid data type {type(datum)}.") - - def _add_data( - self, - component_expdata: List[ExperimentData], - data: Union[Result, List[Result], Dict, List[Dict]], - ) -> None: - """Add experiment data. - - Args: - data: Experiment data to add. Several types are accepted for convenience: - - * Result: Add data from this ``Result`` object. - * List[Result]: Add data from the ``Result`` objects. - * Dict: Add this data. - * List[Dict]: Add this list of data. - - Raises: - TypeError: If the input data type is invalid. - """ - #TODO: Continue from here - - if not isinstance(data, list): - data = [data] - - # self._marginalized_component_data() - # Directly add non-job data - - marginalized_data = self._marginalized_component_data(data) - - with self._result_data.lock: - for sub_expdata, sub_data in zip(component_expdata, marginalized_data): - # Clear any previously stored data and add marginalized data - sub_expdata._result_data.clear() - sub_expdata.__add_data(sub_data) def _marginalized_component_data(self, composite_data: List[Dict]) -> List[List[Dict]]: """Return marginalized data for component experiments. @@ -888,6 +870,9 @@ def _marginalized_component_data(self, composite_data: List[Dict]) -> List[List[ ): f_memory = marginal_memory(datum["memory"], composite_clbits) + if "composite_index" not in metadata: + continue + for i, index in enumerate(metadata["composite_index"]): if index not in marginalized_data: # Initialize data list for marginalized