From 3e2b18047b4d91ccf8ecfeb95aa26fcb5aae6d38 Mon Sep 17 00:00:00 2001 From: Christopher Wood Date: Fri, 21 May 2021 13:06:28 -0400 Subject: [PATCH] Address review comments * Rename Experiment.options to Experiment.experiment_options. * Add some more comments --- qiskit_experiments/base_analysis.py | 8 +++++++- qiskit_experiments/base_experiment.py | 18 +++++++++--------- .../characterization/t1_experiment.py | 14 +++++++------- .../randomized_benchmarking/rb_experiment.py | 6 +++--- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/qiskit_experiments/base_analysis.py b/qiskit_experiments/base_analysis.py index a534edc185..6105ca7088 100644 --- a/qiskit_experiments/base_analysis.py +++ b/qiskit_experiments/base_analysis.py @@ -28,10 +28,16 @@ class BaseAnalysis(ABC): """Base Analysis class for analyzing Experiment data. + The data produced by experiments (i.e. subclasses of BaseExperiment) + are analyzed with subclasses of BaseExperiment. The analysis is + typically run after the data has been gathered by the experiment. + For example, an analysis may perform some data processing of the + measured data and a fit to a function to extract a parameter. + When designing Analysis subclasses default values for any kwarg analysis options of the `run` method should be set by overriding the `_default_options` class method. When calling `run` these - default values will combined with all other option kwargs in the + default values will be combined with all other option kwargs in the run method and passed to the `_run_analysis` function. """ diff --git a/qiskit_experiments/base_experiment.py b/qiskit_experiments/base_experiment.py index c2d1395f0d..4c9469e468 100644 --- a/qiskit_experiments/base_experiment.py +++ b/qiskit_experiments/base_experiment.py @@ -71,7 +71,7 @@ def __init__(self, qubits: Iterable[int], experiment_type: Optional[str] = None) raise QiskitError("Duplicate qubits in physical qubits list.") # Experiment options - self._options = self._default_options() + self._experiment_options = self._default_experiment_options() self._transpile_options = self._default_transpile_options() self._run_options = self._default_run_options() self._analysis_options = self._default_analysis_options() @@ -194,7 +194,7 @@ def circuits(self, backend: Optional[Backend] = None) -> List[QuantumCircuit]: # generation @classmethod - def _default_options(cls) -> Options: + def _default_experiment_options(cls) -> Options: """Default kwarg options for experiment""" # Experiment subclasses should override this method to return # an `Options` object containing all the supported options for @@ -203,11 +203,11 @@ def _default_options(cls) -> Options: return Options() @property - def options(self) -> Options: + def experiment_options(self) -> Options: """Return the options for the experiment.""" - return self._options + return self._experiment_options - def set_options(self, **fields): + def set_experiment_options(self, **fields): """Set the experiment options. Args: @@ -217,18 +217,18 @@ def set_options(self, **fields): AttributeError: If the field passed in is not a supported options """ for field in fields: - if not hasattr(self._options, field): + if not hasattr(self._experiment_options, field): raise AttributeError( f"Options field {field} is not valid for {type(self).__name__}" ) - self._options.update_options(**fields) + self._experiment_options.update_options(**fields) @classmethod def _default_transpile_options(cls) -> Options: """Default transpiler options for transpilation of circuits""" # Experiment subclasses can override this method if they need - # to set specific transpiler options defaults for running the - # experiment. + # to set specific default transpiler options to transpile the + # experiment circuits. return Options(optimization_level=0) @property diff --git a/qiskit_experiments/characterization/t1_experiment.py b/qiskit_experiments/characterization/t1_experiment.py index 38c8398871..cfabf3018c 100644 --- a/qiskit_experiments/characterization/t1_experiment.py +++ b/qiskit_experiments/characterization/t1_experiment.py @@ -221,7 +221,7 @@ class T1Experiment(BaseExperiment): __analysis_class__ = T1Analysis @classmethod - def _default_options(cls) -> Options: + def _default_experiment_options(cls) -> Options: return Options(delays=None, unit="s") def __init__( @@ -249,7 +249,7 @@ def __init__( super().__init__([qubit]) # Set experiment options - self.set_options(delays=delays, unit=unit) + self.set_experiment_options(delays=delays, unit=unit) def circuits(self, backend: Optional[Backend] = None) -> List[QuantumCircuit]: """ @@ -264,7 +264,7 @@ def circuits(self, backend: Optional[Backend] = None) -> List[QuantumCircuit]: Raises: AttributeError: if unit is dt but dt parameter is missing in the backend configuration """ - if self.options.unit == "dt": + if self.experiment_options.unit == "dt": try: dt_factor = getattr(backend.configuration(), "dt") except AttributeError as no_dt: @@ -272,11 +272,11 @@ def circuits(self, backend: Optional[Backend] = None) -> List[QuantumCircuit]: circuits = [] - for delay in self.options.delays: + for delay in self.experiment_options.delays: circ = QuantumCircuit(1, 1) circ.x(0) circ.barrier(0) - circ.delay(delay, 0, self.options.unit) + circ.delay(delay, 0, self.experiment_options.unit) circ.barrier(0) circ.measure(0, 0) @@ -284,10 +284,10 @@ def circuits(self, backend: Optional[Backend] = None) -> List[QuantumCircuit]: "experiment_type": self._type, "qubit": self.physical_qubits[0], "xval": delay, - "unit": self.options.unit, + "unit": self.experiment_options.unit, } - if self.options.unit == "dt": + if self.experiment_options.unit == "dt": circ.metadata["dt_factor"] = dt_factor circuits.append(circ) diff --git a/qiskit_experiments/randomized_benchmarking/rb_experiment.py b/qiskit_experiments/randomized_benchmarking/rb_experiment.py index c46203deed..e3bcc033d8 100644 --- a/qiskit_experiments/randomized_benchmarking/rb_experiment.py +++ b/qiskit_experiments/randomized_benchmarking/rb_experiment.py @@ -63,7 +63,7 @@ def __init__( super().__init__(qubits) # Set configurable options - self.set_options(lengths=list(lengths), num_samples=num_samples) + self.set_experiment_options(lengths=list(lengths), num_samples=num_samples) # Set fixed options self._full_sampling = full_sampling @@ -85,8 +85,8 @@ def circuits(self, backend: Optional[Backend] = None) -> List[QuantumCircuit]: A list of :class:`QuantumCircuit`. """ circuits = [] - for _ in range(self.options.num_samples): - circuits += self._sample_circuits(self.options.lengths, seed=self._rng) + for _ in range(self.experiment_options.num_samples): + circuits += self._sample_circuits(self.experiment_options.lengths, seed=self._rng) return circuits def _sample_circuits(