diff --git a/qiskit_experiments/curve_analysis/guess.py b/qiskit_experiments/curve_analysis/guess.py index ddf2617bba..1d377d2038 100644 --- a/qiskit_experiments/curve_analysis/guess.py +++ b/qiskit_experiments/curve_analysis/guess.py @@ -398,7 +398,9 @@ def rb_decay( y = y[valid_inds] x = x[valid_inds] - if len(x) < 2: + if len(x) == 0: + return 0 + if len(x) == 1: # If number of element is 1, assume y(0) = 1.0 and directly compute alpha. a = 1.0 - b return ((y[0] - b) / a) ** (1 / x[0]) diff --git a/releasenotes/notes/fix-guess-rb-decay-f78e40a7d6d8dd67.yaml b/releasenotes/notes/fix-guess-rb-decay-f78e40a7d6d8dd67.yaml new file mode 100644 index 0000000000..ea9a49cad7 --- /dev/null +++ b/releasenotes/notes/fix-guess-rb-decay-f78e40a7d6d8dd67.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fixed a bug in :func:`~.rb_decay` where it unintentionally raises + an ``IndexError`` if all ``y`` values are below ``b`` value + so that it returns ``0`` for the case. diff --git a/test/curve_analysis/test_guess.py b/test/curve_analysis/test_guess.py index bff8870628..e111e2a7fc 100644 --- a/test/curve_analysis/test_guess.py +++ b/test/curve_analysis/test_guess.py @@ -207,3 +207,10 @@ def test_rb_decay(self, a, b, alpha): alpha_guess = guess.rb_decay(x, y, b=b) self.assertAlmostEqual(alpha, alpha_guess, delta=alpha * 0.1) + + def test_rb_decay_with_very_bad_output(self): + """Test if rb decay guess does not raise an error even for very bad outputs.""" + x = np.array([1, 2, 3]) + y = np.array([0.24, 0.22, 0.23]) # all are below b + out = guess.rb_decay(x=x, y=y, b=0.25) + self.assertEqual(out, 0.0)