16
16
from abc import ABC , abstractmethod
17
17
from typing import List , Tuple
18
18
19
+ from qiskit .providers .options import Options
19
20
from qiskit .exceptions import QiskitError
20
21
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
22
26
23
27
24
28
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
+ """
26
43
27
44
# Expected experiment data container for analysis
28
45
__experiment_data__ = ExperimentData
29
46
47
+ @classmethod
48
+ def _default_options (cls ) -> Options :
49
+ return Options ()
50
+
30
51
def run (
31
52
self ,
32
53
experiment_data : ExperimentData ,
33
54
save : bool = True ,
34
55
return_figures : bool = False ,
35
56
** options ,
36
57
):
37
- """Run analysis and update stored ExperimentData with analysis result.
58
+ """Run analysis and update ExperimentData with analysis result.
38
59
39
60
Args:
40
61
experiment_data: the experiment data to analyze.
@@ -43,14 +64,13 @@ def run(
43
64
return_figures: if true return a pair of
44
65
``(analysis_results, figures)``,
45
66
otherwise return only analysis_results.
46
- options: kwarg options for analysis function.
67
+ options: additional analysis options. See class documentation for
68
+ supported options.
47
69
48
70
Returns:
49
- AnalysisResult: the output of the analysis that produces a
50
- single result.
51
71
List[AnalysisResult]: the output for analysis that produces
52
72
multiple results.
53
- tuple : If ``return_figures=True`` the output is a pair
73
+ Tuple : If ``return_figures=True`` the output is a pair
54
74
``(analysis_results, figures)`` where ``analysis_results``
55
75
may be a single or list of :class:`AnalysisResult` objects, and
56
76
``figures`` may be None, a single figure, or a list of figures.
@@ -63,10 +83,15 @@ def run(
63
83
f"Invalid experiment data type, expected { self .__experiment_data__ .__name__ } "
64
84
f" but received { type (experiment_data ).__name__ } "
65
85
)
86
+ # Get analysis options
87
+ analysis_options = self ._default_options ()
88
+ analysis_options .update_options (** options )
89
+ analysis_options = analysis_options .__dict__
90
+
66
91
# Run analysis
67
92
# pylint: disable=broad-except
68
93
try :
69
- analysis_results , figures = self ._run_analysis (experiment_data , ** options )
94
+ analysis_results , figures = self ._run_analysis (experiment_data , ** analysis_options )
70
95
analysis_results ["success" ] = True
71
96
except Exception as ex :
72
97
analysis_results = AnalysisResult (success = False , error_message = ex )
@@ -88,18 +113,19 @@ def run(
88
113
89
114
@abstractmethod
90
115
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" ]]:
93
118
"""Run analysis on circuit data.
94
119
95
120
Args:
96
121
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.
98
125
99
126
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.
104
130
"""
105
131
pass
0 commit comments