Skip to content

Commit 0a68971

Browse files
Rework experiment options (#39)
1 parent 3fd4813 commit 0a68971

File tree

15 files changed

+402
-316
lines changed

15 files changed

+402
-316
lines changed

qiskit_experiments/analysis/plotting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def plot_curve_fit(
3535
):
3636
"""Generate plot of a curve fit analysis result.
3737
38-
Wraps ``matplotlib.pyplot.plot``.
38+
Wraps :func:`matplotlib.pyplot.plot`.
3939
4040
Args:
4141
func: the fit function for curve_fit.

qiskit_experiments/base_analysis.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,46 @@
1616
from abc import ABC, abstractmethod
1717
from typing import List, Tuple
1818

19+
from qiskit.providers.options import Options
1920
from qiskit.exceptions import QiskitError
2021

21-
from .experiment_data import ExperimentData, AnalysisResult
22+
from qiskit_experiments.experiment_data import ExperimentData, AnalysisResult
23+
24+
# pylint: disable = unused-import
25+
from qiskit_experiments.matplotlib import pyplot
2226

2327

2428
class BaseAnalysis(ABC):
25-
"""Base Analysis class for analyzing Experiment data."""
29+
"""Base Analysis class for analyzing Experiment data.
30+
31+
The data produced by experiments (i.e. subclasses of BaseExperiment)
32+
are analyzed with subclasses of BaseExperiment. The analysis is
33+
typically run after the data has been gathered by the experiment.
34+
For example, an analysis may perform some data processing of the
35+
measured data and a fit to a function to extract a parameter.
36+
37+
When designing Analysis subclasses default values for any kwarg
38+
analysis options of the `run` method should be set by overriding
39+
the `_default_options` class method. When calling `run` these
40+
default values will be combined with all other option kwargs in the
41+
run method and passed to the `_run_analysis` function.
42+
"""
2643

2744
# Expected experiment data container for analysis
2845
__experiment_data__ = ExperimentData
2946

47+
@classmethod
48+
def _default_options(cls) -> Options:
49+
return Options()
50+
3051
def run(
3152
self,
3253
experiment_data: ExperimentData,
3354
save: bool = True,
3455
return_figures: bool = False,
3556
**options,
3657
):
37-
"""Run analysis and update stored ExperimentData with analysis result.
58+
"""Run analysis and update ExperimentData with analysis result.
3859
3960
Args:
4061
experiment_data: the experiment data to analyze.
@@ -43,14 +64,13 @@ def run(
4364
return_figures: if true return a pair of
4465
``(analysis_results, figures)``,
4566
otherwise return only analysis_results.
46-
options: kwarg options for analysis function.
67+
options: additional analysis options. See class documentation for
68+
supported options.
4769
4870
Returns:
49-
AnalysisResult: the output of the analysis that produces a
50-
single result.
5171
List[AnalysisResult]: the output for analysis that produces
5272
multiple results.
53-
tuple: If ``return_figures=True`` the output is a pair
73+
Tuple: If ``return_figures=True`` the output is a pair
5474
``(analysis_results, figures)`` where ``analysis_results``
5575
may be a single or list of :class:`AnalysisResult` objects, and
5676
``figures`` may be None, a single figure, or a list of figures.
@@ -63,10 +83,15 @@ def run(
6383
f"Invalid experiment data type, expected {self.__experiment_data__.__name__}"
6484
f" but received {type(experiment_data).__name__}"
6585
)
86+
# Get analysis options
87+
analysis_options = self._default_options()
88+
analysis_options.update_options(**options)
89+
analysis_options = analysis_options.__dict__
90+
6691
# Run analysis
6792
# pylint: disable=broad-except
6893
try:
69-
analysis_results, figures = self._run_analysis(experiment_data, **options)
94+
analysis_results, figures = self._run_analysis(experiment_data, **analysis_options)
7095
analysis_results["success"] = True
7196
except Exception as ex:
7297
analysis_results = AnalysisResult(success=False, error_message=ex)
@@ -88,18 +113,19 @@ def run(
88113

89114
@abstractmethod
90115
def _run_analysis(
91-
self, data: ExperimentData, **options
92-
) -> Tuple[List[AnalysisResult], List["matplotlib.figure.Figure"]]:
116+
self, experiment_data: ExperimentData, **options
117+
) -> Tuple[List[AnalysisResult], List["pyplot.Figure"]]:
93118
"""Run analysis on circuit data.
94119
95120
Args:
96121
experiment_data: the experiment data to analyze.
97-
options: kwarg options for analysis function.
122+
options: additional options for analysis. By default the fields and
123+
values in :meth:`options` are used and any provided values
124+
can override these.
98125
99126
Returns:
100-
tuple: A pair ``(analysis_results, figures)`` where
101-
``analysis_results`` may be a single or list of
102-
AnalysisResult objects, and ``figures`` is a list of any
103-
figures for the experiment.
127+
A pair ``(analysis_results, figures)`` where ``analysis_results``
128+
may be a single or list of AnalysisResult objects, and ``figures``
129+
is a list of any figures for the experiment.
104130
"""
105131
pass

0 commit comments

Comments
 (0)