From d3cde718550f6dd83d53279a02d66013588f15df Mon Sep 17 00:00:00 2001 From: Naoki Kanazawa Date: Sat, 23 Apr 2022 03:21:15 +0900 Subject: [PATCH] finalize - small docs fix - lint - reno --- .../curve_analysis/base_curve_analysis.py | 13 ++++---- .../curve_analysis/curve_analysis.py | 10 +++--- .../standard_analysis/gaussian.py | 4 +-- .../analysis/cr_hamiltonian_analysis.py | 1 - ...eanup-curve-analysis-96d7ff706cae5b4e.yaml | 31 +++++++++++++++++++ test/curve_analysis/test_baseclass.py | 10 +++--- 6 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 releasenotes/notes/cleanup-curve-analysis-96d7ff706cae5b4e.yaml diff --git a/qiskit_experiments/curve_analysis/base_curve_analysis.py b/qiskit_experiments/curve_analysis/base_curve_analysis.py index ca9e266fc0..5d69171fac 100644 --- a/qiskit_experiments/curve_analysis/base_curve_analysis.py +++ b/qiskit_experiments/curve_analysis/base_curve_analysis.py @@ -135,10 +135,13 @@ def _default_options(cls) -> Options: instance that defines the `self.__call__` method. normalization (bool) : Set ``True`` to normalize y values within range [-1, 1]. Default to ``False``. - p0 (Dict[str, float]): Array-like or dictionary - of initial parameters. - bounds (Dict[str, Tuple[float, float]]): Array-like or dictionary - of (min, max) tuple of fit parameter boundaries. + p0 (Dict[str, float]): Initial guesses for the fit parameters. + The dictionary is keyed on the fit parameter names. + bounds (Dict[str, Tuple[float, float]]): Boundary of fit parameters. + The dictionary is keyed on the fit parameter names and + values are the tuples of (min, max) of each parameter. + curve_fitter_options (Dict[str, Any]) Options that are passed to the + scipy curve fit which performs the least square fitting on the experiment results. x_key (str): Circuit metadata key representing a scanned value. result_parameters (List[Union[str, ParameterRepr]): Parameters reported in the database as a dedicated entry. This is a list of parameter representation @@ -149,8 +152,6 @@ def _default_options(cls) -> Options: Representation should be printable in standard output, i.e. no latex syntax. extra (Dict[str, Any]): A dictionary that is appended to all database entries as extra information. - curve_fitter_options (Dict[str, Any]) Options that are passed to the - specified curve fitting function. fixed_parameters (Dict[str, Any]): Fitting model parameters that are fixed during the curve fitting. This should be provided with default value keyed on one of the parameter names in the series definition. diff --git a/qiskit_experiments/curve_analysis/curve_analysis.py b/qiskit_experiments/curve_analysis/curve_analysis.py index c61993e06e..8a7844b023 100644 --- a/qiskit_experiments/curve_analysis/curve_analysis.py +++ b/qiskit_experiments/curve_analysis/curve_analysis.py @@ -59,9 +59,6 @@ def __init__(self): #: List[CurveData]: Processed experiment data set. For backward compatibility. self.__processed_data_set = {} - #: List[int]: Index of physical qubits - self._physical_qubits = None - @classmethod def _fit_params(cls) -> List[str]: """Return a list of fitting parameters. @@ -200,9 +197,7 @@ def _run_analysis( quality = self._evaluate_quality(fit_data) # Create analysis results - analysis_results.extend( - self._create_analysis_results(fit_data, quality, **metadata) - ) + analysis_results.extend(self._create_analysis_results(fit_data, quality, **metadata)) # calling old extra entry method for backward compatibility if hasattr(self, "_extra_database_entry"): warnings.warn( @@ -331,6 +326,9 @@ def analysis_result_to_repr(result: AnalysisResultData) -> str: Returns: String representation of the data. + + Raises: + AnalysisError: When the result data is not likely fit parameter. """ if not isinstance(result.value, (float, UFloat)): raise AnalysisError(f"Result data {result.name} is not a valid fit parameter data type.") diff --git a/qiskit_experiments/curve_analysis/standard_analysis/gaussian.py b/qiskit_experiments/curve_analysis/standard_analysis/gaussian.py index b50e9508a5..26880e0f0e 100644 --- a/qiskit_experiments/curve_analysis/standard_analysis/gaussian.py +++ b/qiskit_experiments/curve_analysis/standard_analysis/gaussian.py @@ -127,10 +127,10 @@ def _evaluate_quality(self, fit_data: curve.FitData) -> Union[str, None]: fit_a = fit_data.fitval("a") fit_b = fit_data.fitval("b") fit_freq = fit_data.fitval("freq") - fit_kappa = fit_data.fitval("kappa") + fit_sigma = fit_data.fitval("sigma") snr = abs(fit_a.n) / np.sqrt(abs(np.median(fit_data.y_data) - fit_b.n)) - fit_width_ratio = fit_kappa.n / np.ptp(fit_data.x_data) + fit_width_ratio = fit_sigma.n / np.ptp(fit_data.x_data) criteria = [ fit_data.x_range[0] <= fit_freq.n <= fit_data.x_range[1], diff --git a/qiskit_experiments/library/characterization/analysis/cr_hamiltonian_analysis.py b/qiskit_experiments/library/characterization/analysis/cr_hamiltonian_analysis.py index 1182df2376..33315e15e4 100644 --- a/qiskit_experiments/library/characterization/analysis/cr_hamiltonian_analysis.py +++ b/qiskit_experiments/library/characterization/analysis/cr_hamiltonian_analysis.py @@ -21,7 +21,6 @@ import qiskit_experiments.curve_analysis as curve import qiskit_experiments.data_processing as dp -from qiskit_experiments.database_service.device_component import Qubit from qiskit_experiments.framework import AnalysisResultData diff --git a/releasenotes/notes/cleanup-curve-analysis-96d7ff706cae5b4e.yaml b/releasenotes/notes/cleanup-curve-analysis-96d7ff706cae5b4e.yaml new file mode 100644 index 0000000000..5c063ed860 --- /dev/null +++ b/releasenotes/notes/cleanup-curve-analysis-96d7ff706cae5b4e.yaml @@ -0,0 +1,31 @@ +--- +developer: + - | + :class:`CurveAnalysis` is now a subclass of :class:`BaseCurveAnalysis`. + This change has been made to introduce multi-grouped curve analysis, + see qiskit-experiments/#737 for details. + + According to this update, several protected methods that are + originally designed to be overridden by subclass have been deprecated or upgraded. + Check the following list of changes you may need to upgrade your analysis subclass. + + - :meth:`CurveAnalysis._generate_fit_guesses` + + The signature of method has been upgraded. Now this method should be called with + ``curve_data`` and calling ``self._data()`` to get curve data has been deprecated. + + - :meth:`CurveAnalysis._data` + + This method has been deprecated. + + - :meth:`CurveAnalysis._extra_database_entry` + + This method has been deprecated. You can directly override + new method :meth:`CurveAnalysis._create_analysis_results`. + + - :class:`FitData` + + Fit data dataclass is now created with :attr:`x_data` and :attr:`y_data` + so that you can retrieve the data used for the fitting along with the outcome parameters. + + See :ref:`curve_analysis_overview` for the overview of new curve analysis base class. diff --git a/test/curve_analysis/test_baseclass.py b/test/curve_analysis/test_baseclass.py index f911c8ba87..13eefc06f0 100644 --- a/test/curve_analysis/test_baseclass.py +++ b/test/curve_analysis/test_baseclass.py @@ -81,6 +81,7 @@ class TestCurveAnalysisUnit(QiskitExperimentsTestCase): class TestAnalysis(CurveAnalysis): """Fake analysis class for unittest.""" + __series__ = [ SeriesDef( name="curve1", @@ -136,9 +137,7 @@ def test_data_extraction(self): xvalues = np.linspace(1.0, 5.0, 10) analysis = self.TestAnalysis() - analysis.set_options( - data_processor=DataProcessor("counts", [Probability("1")]) - ) + analysis.set_options(data_processor=DataProcessor("counts", [Probability("1")])) # data to analyze test_data0 = simulate_output_data( @@ -171,9 +170,7 @@ def test_data_extraction_with_subset(self): xvalues = np.linspace(1.0, 5.0, 10) analysis = self.TestAnalysis() - analysis.set_options( - data_processor=DataProcessor("counts", [Probability("1")]) - ) + analysis.set_options(data_processor=DataProcessor("counts", [Probability("1")])) # data to analyze test_data0 = simulate_output_data( @@ -257,6 +254,7 @@ def test_invalid_options(self): class InvalidClass: """Dummy class.""" + pass with self.assertRaises(TypeError):