Skip to content

Commit

Permalink
Updated add_data and deprecated _add_data qiskit-community#1268
Browse files Browse the repository at this point in the history
  • Loading branch information
Musa-Sina-Ertugrul committed Apr 22, 2024
1 parent 4c3dad6 commit b217a64
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 47 deletions.
8 changes: 8 additions & 0 deletions qiskit_experiments/framework/composite/composite_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,24 @@ 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")

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
Expand Down
79 changes: 32 additions & 47 deletions qiskit_experiments/framework/experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b217a64

Please sign in to comment.