Skip to content

Commit

Permalink
Fix bug with ExperimentData.load (qiskit-community#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseclectic authored and gadial committed Dec 7, 2021
1 parent 82237ed commit f4d333f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from qiskit.result import marginal_counts
from qiskit.exceptions import QiskitError
from qiskit_experiments.framework.experiment_data import ExperimentData
from qiskit_experiments.database_service import DbExperimentDataV1, DatabaseServiceV1
from qiskit_experiments.database_service import DatabaseServiceV1


class CompositeExperimentData(ExperimentData):
Expand Down Expand Up @@ -114,19 +114,16 @@ def save_metadata(self) -> None:

@classmethod
def load(cls, experiment_id: str, service: DatabaseServiceV1) -> "CompositeExperimentData":
expdata = DbExperimentDataV1.load(experiment_id, service)
components = []
expdata = ExperimentData.load(experiment_id, service)
expdata.__class__ = CompositeExperimentData
expdata._components = []
for comp_id, comp_class in zip(
expdata.metadata["component_ids"], expdata.metadata["component_classes"]
):
load_class = globals()[comp_class]
load_func = getattr(load_class, "load")
loaded_comp = load_func(comp_id, service)
components.append(loaded_comp)

expdata.__class__ = CompositeExperimentData
expdata._experiment = None
expdata._components = components
expdata._components.append(loaded_comp)

return expdata

Expand Down
17 changes: 17 additions & 0 deletions qiskit_experiments/framework/experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from datetime import datetime

from qiskit_experiments.database_service import DbExperimentDataV1
from qiskit_experiments.database_service.database_service import DatabaseServiceV1

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -62,6 +63,22 @@ def completion_times(self) -> Dict[str, datetime]:

return job_times

@classmethod
def load(cls, experiment_id: str, service: DatabaseServiceV1) -> "ExperimentData":
"""Load a saved experiment data from a database service.
Args:
experiment_id: Experiment ID.
service: the database service.
Returns:
The loaded experiment data.
"""
expdata = DbExperimentDataV1.load(experiment_id, service)
expdata.__class__ = ExperimentData
expdata._experiment = None
return expdata

def _copy_metadata(self, new_instance: Optional["ExperimentData"] = None) -> "ExperimentData":
"""Make a copy of the experiment metadata.
Expand Down
8 changes: 8 additions & 0 deletions releasenotes/notes/fix-expdata-load-b7c6569bfbe67bec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
Fixes bug where the
:meth:`~qiskit_experiments.framework.ExperimentData.load` method of
:class:`~qiskit_experiments.framework.ExperimentData` would return a
:class:`~qiskit_experiments.database_service.DbExperimentDataV1` object
instead of a :class:`~qiskit_experiments.framework.ExperimentData` object.

0 comments on commit f4d333f

Please sign in to comment.