From ec09c1274047b2c07ca1b79569f6d2b60d21984d Mon Sep 17 00:00:00 2001 From: knzwnao Date: Thu, 4 Nov 2021 02:52:45 +0900 Subject: [PATCH 1/2] handling for very slow decay --- .../curve_analysis/standard_analysis/decay.py | 24 +++++++++++++++---- .../standard_analysis/oscillation.py | 16 ++++++++----- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/qiskit_experiments/curve_analysis/standard_analysis/decay.py b/qiskit_experiments/curve_analysis/standard_analysis/decay.py index 589c516bf0..bce4b43fac 100644 --- a/qiskit_experiments/curve_analysis/standard_analysis/decay.py +++ b/qiskit_experiments/curve_analysis/standard_analysis/decay.py @@ -13,6 +13,8 @@ from typing import List, Union +import numpy as np + import qiskit_experiments.curve_analysis as curve @@ -69,16 +71,28 @@ def _generate_fit_guesses( Returns: List of fit options that are passed to the fitter function. + + Raises: + AnalysisError: When the y data is likely constant. """ curve_data = self._data() user_opt.p0.set_if_empty(base=curve.guess.min_height(curve_data.y)[0]) - user_opt.p0.set_if_empty( - tau=-1 / curve.guess.exp_decay(curve_data.x, curve_data.y), - amp=curve.guess.max_height(curve_data.y)[0] - user_opt.p0["base"], - ) - + alpha = curve.guess.exp_decay(curve_data.x, curve_data.y) + + if alpha != 0.0: + user_opt.p0.set_if_empty( + tau=-1 / alpha, + amp=curve.guess.max_height(curve_data.y)[0] - user_opt.p0["base"], + ) + else: + # Likely there is no slope. Cannot fit constant line with this model. + # Set some large enough number to the scan range. + user_opt.p0.set_if_empty( + tau=100 * np.max(curve_data.x), + amp=curve.guess.max_height(curve_data.y)[0] - user_opt.p0["base"], + ) return user_opt def _evaluate_quality(self, fit_data: curve.FitData) -> Union[str, None]: diff --git a/qiskit_experiments/curve_analysis/standard_analysis/oscillation.py b/qiskit_experiments/curve_analysis/standard_analysis/oscillation.py index b78c90eb48..92a3a27286 100644 --- a/qiskit_experiments/curve_analysis/standard_analysis/oscillation.py +++ b/qiskit_experiments/curve_analysis/standard_analysis/oscillation.py @@ -219,15 +219,19 @@ def _generate_fit_guesses( # Set guess for decay parameter based on estimated frequency if freq_guess > df: - user_opt.p0.set_if_empty( - tau=-1 - / curve.guess.oscillation_exp_decay( - curve_data.x, curve_data.y - user_opt.p0["base"], freq_guess=freq_guess - ) + alpha = curve.guess.oscillation_exp_decay( + curve_data.x, curve_data.y - user_opt.p0["base"], freq_guess=freq_guess ) else: # Very low frequency. Assume standard exponential decay - user_opt.p0.set_if_empty(tau=-1 / curve.guess.exp_decay(curve_data.x, curve_data.y)) + alpha = curve.guess.exp_decay(curve_data.x, curve_data.y) + + if alpha != 0.0: + user_opt.p0.set_if_empty(tau=-1 / alpha) + else: + # Likely there is no slope. Cannot fit constant line with this model. + # Set some large enough number to the scan range. + user_opt.p0.set_if_empty(tau=100 * np.max(curve_data.x)) user_opt.bounds.set_if_empty( amp=[0, 1.5], From 0a2f921321d2d2bf9daee00cafd7648a42cf56a8 Mon Sep 17 00:00:00 2001 From: knzwnao Date: Thu, 4 Nov 2021 03:03:26 +0900 Subject: [PATCH 2/2] fix comment --- qiskit_experiments/curve_analysis/standard_analysis/decay.py | 2 +- .../curve_analysis/standard_analysis/oscillation.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/qiskit_experiments/curve_analysis/standard_analysis/decay.py b/qiskit_experiments/curve_analysis/standard_analysis/decay.py index bce4b43fac..4a09d1eb38 100644 --- a/qiskit_experiments/curve_analysis/standard_analysis/decay.py +++ b/qiskit_experiments/curve_analysis/standard_analysis/decay.py @@ -88,7 +88,7 @@ def _generate_fit_guesses( ) else: # Likely there is no slope. Cannot fit constant line with this model. - # Set some large enough number to the scan range. + # Set some large enough number against to the scan range. user_opt.p0.set_if_empty( tau=100 * np.max(curve_data.x), amp=curve.guess.max_height(curve_data.y)[0] - user_opt.p0["base"], diff --git a/qiskit_experiments/curve_analysis/standard_analysis/oscillation.py b/qiskit_experiments/curve_analysis/standard_analysis/oscillation.py index 92a3a27286..282b3ac0bf 100644 --- a/qiskit_experiments/curve_analysis/standard_analysis/oscillation.py +++ b/qiskit_experiments/curve_analysis/standard_analysis/oscillation.py @@ -230,7 +230,7 @@ def _generate_fit_guesses( user_opt.p0.set_if_empty(tau=-1 / alpha) else: # Likely there is no slope. Cannot fit constant line with this model. - # Set some large enough number to the scan range. + # Set some large enough number against to the scan range. user_opt.p0.set_if_empty(tau=100 * np.max(curve_data.x)) user_opt.bounds.set_if_empty(