Skip to content

Commit

Permalink
Updated add_data method qiskit-community#1268
Browse files Browse the repository at this point in the history
  • Loading branch information
Musa-Sina-Ertugrul committed Nov 4, 2023
1 parent 5c59e08 commit cc0b89a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
29 changes: 24 additions & 5 deletions qiskit_experiments/framework/experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,20 +792,39 @@ def add_data(

# Directly add non-job data
with self._result_data.lock:
tmp_exp_data = ExperimentData()
composite_flag = False
experiment_seperator = {}
for datum in data:
if isinstance(datum, dict):
if datum["metadata"].get("composite_metadata"):
tmp_exp_data = ExperimentData()
if "composite_metadata" in datum["metadata"]:
composite_flag = True
marginalized_data = self._marginalized_component_data([datum])
for inner_datum in marginalized_data:
tmp_exp_data.__add_data(inner_datum)
self._set_child_data([tmp_exp_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])
else:
self._result_data.append(datum)
elif isinstance(datum, Result):
self.__add_data(datum)
if datum["metadata"]:
self._set_child_data(datum["metadata"]._metadata())
else:
self._add_result_data(datum)
else:
raise TypeError(f"Invalid data type {type(datum)}.")
if composite_flag:
tmp_exp_data._set_child_data(list(experiment_seperator.values()))
self._set_child_data([tmp_exp_data])
for exp_data in self._child_data.values():
for sub_exp_data in exp_data.child_data():
print(sub_exp_data.data())
print(self.data())


def __add_data(
self,
Expand Down
3 changes: 1 addition & 2 deletions test/framework/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ def test_composite_count_memory_marginalization(self, memory):
sub_data = CompositeAnalysis([], flatten_results=False)._marginalized_component_data(
test_data.data()
)
# print(sub_data)
#print([exp_data.data() for exp_data in test_data.child_data()])
expected = [
[
{
Expand All @@ -671,7 +671,6 @@ def test_composite_count_memory_marginalization(self, memory):
}
],
]

self.assertListEqual(sub_data, expected)

def test_composite_single_kerneled_memory_marginalization(self):
Expand Down

0 comments on commit cc0b89a

Please sign in to comment.