Skip to content

Commit

Permalink
Rework option handling in base classes
Browse files Browse the repository at this point in the history
* Reworks how experiment, transpiler, backend/run, and analysis options are handled in the base classes to make it similar to how options are used by qiskit backends.

* Removes transpiled_circuits method. Transpiled circuits can now be obtained via `transpile(expr.circuits(backend), backend, **expr.transpile_options)`
  • Loading branch information
chriseclectic committed May 5, 2021
1 parent 38f1b33 commit ac3681a
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 111 deletions.
47 changes: 44 additions & 3 deletions qiskit_experiments/base_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"""

from abc import ABC, abstractmethod
import copy

from qiskit.providers.options import Options
from qiskit.exceptions import QiskitError
from .experiment_data import ExperimentData, AnalysisResult

Expand All @@ -25,6 +27,36 @@ class BaseAnalysis(ABC):
# Expected experiment data container for analysis
__experiment_data__ = ExperimentData

def __init__(self, **options):
"""Initialize a base analysis class
Args:
options: kwarg options for analysis.
"""
self._options = self._default_options()
self.set_options(**options)

@classmethod
def _default_options(cls):
return Options()

def set_options(self, **fields):
"""Set the analysis options.
Args:
fields: The fields to update the options
"""
self._options.update_options(**fields)

@property
def options(self):
"""Return the analysis options.
The options of an analysis class are used to provide kwarg values for
the :meth:`run` method.
"""
return self._options

def run(self, experiment_data, save=True, return_figures=False, **options):
"""Run analysis and update stored ExperimentData with analysis result.
Expand All @@ -35,7 +67,8 @@ def run(self, experiment_data, save=True, return_figures=False, **options):
return_figures (bool): if true return a pair of
``(analysis_results, figures)``,
otherwise return only analysis_results.
options: kwarg options for analysis function.
options: additional analysis options. Any values set here will
override the value from :meth:`options` for the current run.
Returns:
AnalysisResult: the output of the analysis that produces a
Expand All @@ -59,8 +92,14 @@ def run(self, experiment_data, save=True, return_figures=False, **options):
# Wait for experiment job to finish
# experiment_data.block_for_result()

# Get runtime analysis options
analysis_options = copy.copy(self.options)
analysis_options.update_options(**options)
analysis_options = analysis_options.__dict__

# Run analysis
analysis_results, figures = self._run_analysis(experiment_data, **options)
analysis_results, figures = self._run_analysis(experiment_data, **analysis_options)

# Save to experiment data
if save:
if isinstance(analysis_results, AnalysisResult):
Expand All @@ -81,7 +120,9 @@ def _run_analysis(self, experiment_data, **options):
Args:
experiment_data (ExperimentData): the experiment data to analyze.
options: kwarg options for analysis function.
options: additional options for analysis. By default the fields and
values in :meth:`options` are used and any provided values
can override these.
Returns:
tuple: A pair ``(analysis_results, figures)`` where
Expand Down
Loading

0 comments on commit ac3681a

Please sign in to comment.