Skip to content

Commit

Permalink
Updated for multithreading qiskit-community#1268
Browse files Browse the repository at this point in the history
  • Loading branch information
Musa-Sina-Ertugrul committed May 1, 2024
1 parent a5959a1 commit c92ce3a
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions qiskit_experiments/framework/experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ def add_data(
elif isinstance(datum, Result):
self._add_result_data(datum)
else:
raise TypeError(f"Invalid data type {type(datum)}.")
raise TypeError(f"Invalid data type {type(datum)}.")
self.create_child_data()
self._init_children_data()

Expand All @@ -761,7 +761,7 @@ def __retrive_self_attrs_as_dict(self) -> dict:

def create_child_data(
self,
) -> "ExperimenData": # pylint: disable=inconsistent-return-statements
) -> "ExperimenData":

"""Bootstrap child experiment data containers from result metadata.
Expand All @@ -772,25 +772,24 @@ def create_child_data(
if (component_metadata := self.metadata.get("component_metadata", None)) is None:
return self

while (new_idx := len(self._child_data)) < len(component_metadata):
child_data = ExperimentData(**self.__retrive_self_attrs_as_dict)
# Add automatically generated component experiment metadata
child_data._artifacts = self._artifacts
try:
this_data = component_metadata[new_idx].copy()
child_data.metadata.update(this_data)
except (KeyError, IndexError):
pass
try:
component_type = self.metadata["component_types"][new_idx]
child_data.experiment_type = component_type
except (KeyError, IndexError):
pass
self.add_child_data(child_data)

with self._child_data.lock:
while (new_idx := len(self._child_data)) < len(component_metadata):
child_data = ExperimentData(**self.__retrive_self_attrs_as_dict)
# Add automatically generated component experiment metadata
try:
this_data = component_metadata[new_idx].copy()
child_data.metadata.update(this_data)
except (KeyError, IndexError):
pass
try:
component_type = self.metadata["component_types"][new_idx]
child_data.experiment_type = component_type
except (KeyError, IndexError):
pass
self.add_child_data(child_data)
return self

def _init_children_data(self): # pylint: disable=inconsistent-return-statements
def _init_children_data(self) -> "ExperimenData":

"""Bootstrap Experiment data containers's data
Expand All @@ -806,9 +805,9 @@ def _init_children_data(self): # pylint: disable=inconsistent-return-statements
for idx, sub_data in self._decompose_component_data(data):
# NOTE : These lines for preventing multiple data addition,
# it occurs and I dont know why
if sub_data not in self.child_data(idx).data():
self.child_data(idx).add_data(sub_data)
self.child_data(idx)._artifacts = self._artifacts
with self.child_data(idx)._result_data.lock:
if sub_data not in self.child_data(idx).data():
self.child_data(idx).add_data(sub_data)

return self

Expand Down

0 comments on commit c92ce3a

Please sign in to comment.