1515
1616from abc import ABC , abstractmethod
1717from typing import List , Tuple
18+ import copy
1819
20+ from qiskit .providers .options import Options
1921from qiskit .exceptions import QiskitError
2022
2123from .experiment_data import ExperimentData , AnalysisResult
@@ -27,23 +29,51 @@ class BaseAnalysis(ABC):
2729 # Expected experiment data container for analysis
2830 __experiment_data__ = ExperimentData
2931
30- def run (
31- self ,
32- experiment_data : ExperimentData ,
33- save : bool = True ,
34- return_figures : bool = False ,
35- ** options ,
36- ):
32+ def __init__ (self , ** options ):
33+ """Initialize a base analysis class
34+
35+ Args:
36+ options: kwarg options for analysis.
37+ """
38+ self ._options = self ._default_options ()
39+ self .set_options (** options )
40+
41+ @classmethod
42+ def _default_options (cls ) -> Options :
43+ return Options ()
44+
45+ def set_options (self , ** fields ):
46+ """Set the analysis options.
47+
48+ Args:
49+ fields: The fields to update the options
50+ """
51+ self ._options .update_options (** fields )
52+
53+ @property
54+ def options (self ) -> Options :
55+ """Return the analysis options.
56+
57+ The options of an analysis class are used to provide kwarg values for
58+ the :meth:`run` method.
59+ """
60+ return self ._options
61+
62+ def run (self ,
63+ experiment_data : ExperimentData ,
64+ save : bool = True ,
65+ return_figures : bool = False ,
66+ ** options ):
3767 """Run analysis and update stored ExperimentData with analysis result.
3868
3969 Args:
4070 experiment_data: the experiment data to analyze.
4171 save: if True save analysis results and figures to the
4272 :class:`ExperimentData`.
43- return_figures: if true return a pair of
44- ``(analysis_results, figures)``,
73+ return_figures: if true return a pair of ``(analysis_results, figures)``,
4574 otherwise return only analysis_results.
46- options: kwarg options for analysis function.
75+ options: additional analysis options. Any values set here will
76+ override the value from :meth:`options` for the current run.
4777
4878 Returns:
4979 AnalysisResult: the output of the analysis that produces a
@@ -63,13 +93,19 @@ def run(
6393 f"Invalid experiment data type, expected { self .__experiment_data__ .__name__ } "
6494 f" but received { type (experiment_data ).__name__ } "
6595 )
96+
97+ # Get runtime analysis options
98+ analysis_options = copy .copy (self .options )
99+ analysis_options .update_options (** options )
100+ analysis_options = analysis_options .__dict__
101+
66102 # Run analysis
67103 # pylint: disable=broad-except
68104 try :
69- analysis_results , figures = self ._run_analysis (experiment_data , ** options )
105+ analysis_results , figures = self ._run_analysis (experiment_data , ** analysis_options )
70106 analysis_results ["success" ] = True
71- except Exception :
72- analysis_results = AnalysisResult (success = False )
107+ except Exception as ex :
108+ analysis_results = AnalysisResult (success = False , error_message = ex )
73109 figures = None
74110
75111 # Save to experiment data
@@ -88,18 +124,19 @@ def run(
88124
89125 @abstractmethod
90126 def _run_analysis (
91- self , data : ExperimentData , ** options
127+ self , experiment_data : ExperimentData , ** options
92128 ) -> Tuple [List [AnalysisResult ], List ["Figure" ]]:
93129 """Run analysis on circuit data.
94130
95131 Args:
96132 experiment_data: the experiment data to analyze.
97- options: kwarg options for analysis function.
133+ options: additional options for analysis. By default the fields and
134+ values in :meth:`options` are used and any provided values
135+ can override these.
98136
99137 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.
138+ A pair ``(analysis_results, figures)`` where ``analysis_results``
139+ may be a single or list of AnalysisResult objects, and ``figures``
140+ is a list of any figures for the experiment.
104141 """
105142 pass
0 commit comments