Skip to content

Commit

Permalink
Support Experiments initialized with real backends
Browse files Browse the repository at this point in the history
The JSON encoder and decoder provided by Qiskit Experiments cannot
properly encode and decode the instances of Experiment initialized with
real backends. To cache and recover the tasks of experiments, we have
to delete the backend entry before encoding the task. The issue below
provides an workaround to add the backend back after decoding.

qiskit-community/qiskit-experiments#1508
  • Loading branch information
IvanaGyro committed Jan 7, 2025
1 parent a609922 commit 258e33c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions qiskit_pulse_control/json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Callable
import collections
import json
from typing import Any

Expand All @@ -21,6 +22,13 @@ def default(self, any_object: Any) -> Any:
if isinstance(any_object, unified_job.Job):
return self._encode_job(any_object)
if isinstance(any_object, unified_job.ExperimentJob):
# fix https://github.com/qiskit-community/qiskit-experiments/issues/1508
copied_experiment = any_object.experiment.copy()
experiment_kwargs = getattr(copied_experiment, '__init_kwargs__',
collections.OrderedDict())
if 'backend' in experiment_kwargs:
del experiment_kwargs['backend']

encoded_value = {
'jobs': [self._encode_job(job) for job in any_object.jobs],
'experiment':
Expand Down
6 changes: 5 additions & 1 deletion qiskit_pulse_control/unified_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ def experiment_data(self) -> framework.ExperimentData:
return self._experiment_data
self._experiment_data = framework.ExperimentData(
experiment=self.experiment)
# TODO: check if the runtime jobs is done
if all(job.status == providers.JobStatus.DONE for job in self.jobs):
for job in self.jobs:
# .add_data() cannot handle PrimitiveResult. The `job_id`
Expand All @@ -124,6 +123,11 @@ def experiment_data(self) -> framework.ExperimentData:
for job in self.jobs:
self._experiment_data.add_jobs(
[job.runtime_job for job in self.jobs])
if (self._experiment_data.status() ==
framework.ExperimentStatus.DONE):
self.experiment.analysis.run(
self._experiment_data, replace_results=True)
self.analysis_result = self._experiment_data.analysis_results()
return self._experiment_data


Expand Down

0 comments on commit 258e33c

Please sign in to comment.