Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions qiskit_experiments/framework/base_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,7 @@ def run(
This data can then be saved as its own experiment to a database service.
"""
# Make a new copy of experiment data if not updating results
if not replace_results and (
experiment_data._created_in_db
or experiment_data._analysis_results
or experiment_data._figures
or getattr(experiment_data, "_child_data", None)
):
if not replace_results and _requires_copy(experiment_data):
experiment_data = experiment_data.copy()

# Get experiment device components
Expand Down Expand Up @@ -229,3 +224,23 @@ def __json_encode__(self):
@classmethod
def __json_decode__(cls, value):
return cls.from_config(value)


def _requires_copy(experiment_data) -> bool:
"""Return True if a copy of the experiment data should be made."""
# If data is from DB or contains analysis results it should be copied
if (
experiment_data._created_in_db
or experiment_data._analysis_results
or experiment_data._figures
):
return True

# Check child data:
if hasattr(experiment_data, "_child_data"):
for subdata in experiment_data._child_data.values():
if _requires_copy(subdata):
return True

# No Copy required
return False
8 changes: 4 additions & 4 deletions test/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ def test_analysis_replace_results_true(self):

# Additional data not part of composite experiment
exp3 = FakeExperiment([0, 1])
extra_data = exp3.run(FakeBackend())
extra_data = exp3.run(FakeBackend()).block_for_results()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this relevant to copy?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just to be safe that the child fake experiment finishes all its analysis before later re-running analysis with replacement on the data.

data1.add_child_data(extra_data)

# Replace results
data2 = par_exp.analysis.run(data1, replace_results=True)
data2 = par_exp.analysis.run(data1, replace_results=True).block_for_results()
self.assertEqual(data1, data2)
self.assertEqual(len(data1.child_data()), len(data2.child_data()))
for sub1, sub2 in zip(data1.child_data(), data2.child_data()):
Expand All @@ -231,11 +231,11 @@ def test_analysis_replace_results_false(self):

# Additional data not part of composite experiment
exp3 = FakeExperiment([0, 1])
extra_data = exp3.run(FakeBackend())
extra_data = exp3.run(FakeBackend()).block_for_results()
data1.add_child_data(extra_data)

# Replace results
data2 = par_exp.analysis.run(data1, replace_results=False)
data2 = par_exp.analysis.run(data1).block_for_results()
self.assertNotEqual(data1.experiment_id, data2.experiment_id)
self.assertEqual(len(data1.child_data()), len(data2.child_data()))
for sub1, sub2 in zip(data1.child_data(), data2.child_data()):
Expand Down