From 258e33c9092a1b6f6dc5487648f557d4452f08e8 Mon Sep 17 00:00:00 2001 From: Ivana Date: Tue, 7 Jan 2025 15:47:24 +0800 Subject: [PATCH] Support Experiments initialized with real backends 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. https://github.com/qiskit-community/qiskit-experiments/issues/1508 --- qiskit_pulse_control/json.py | 8 ++++++++ qiskit_pulse_control/unified_job.py | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/qiskit_pulse_control/json.py b/qiskit_pulse_control/json.py index d949386..7b6bd2d 100644 --- a/qiskit_pulse_control/json.py +++ b/qiskit_pulse_control/json.py @@ -1,4 +1,5 @@ from collections.abc import Callable +import collections import json from typing import Any @@ -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': diff --git a/qiskit_pulse_control/unified_job.py b/qiskit_pulse_control/unified_job.py index 1438c9c..a4b4db9 100644 --- a/qiskit_pulse_control/unified_job.py +++ b/qiskit_pulse_control/unified_job.py @@ -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` @@ -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