Exemple #1
0
def test_exp_factory_with_asympt(test_f: Callable[[float], float],
                                 avoid_log: bool):
    """Test of exponential extrapolator."""
    seeded_f = apply_seed_to_func(test_f, SEED)
    fac = ExpFactory(X_VALS, asymptote=A, avoid_log=avoid_log)
    fac.run_classical(seeded_f)
    assert not fac._opt_params
    zne_value = fac.reduce()
    assert np.isclose(zne_value, seeded_f(0, err=0), atol=CLOSE_TOL)
    # There are three parameters in the exponential ansatz
    assert len(fac._opt_params) == 3
    exp_vals = fac.get_expectation_values()
    assert np.isclose(
        fac.extrapolate(X_VALS, exp_vals, asymptote=A, avoid_log=avoid_log),
        zne_value,
    )
    assert np.isclose(
        fac.extrapolate(
            X_VALS,
            exp_vals,
            asymptote=A,
            avoid_log=avoid_log,
            full_output=True,
        )[0],
        zne_value,
    )
Exemple #2
0
def test_failing_fit_error():
    """Test error handling for a failing fit."""
    fac = ExpFactory(X_VALS, asymptote=None)
    fac._instack = [{"scale_factor": x} for x in X_VALS]
    fac._outstack = [1.0, 2.0, 1.0, 2.0, 1.0]
    with raises(ExtrapolationError,
                match=r"The extrapolation fit failed to converge."):
        fac.reduce()
    # test also the static "extrapolate" method.
    with raises(ExtrapolationError,
                match=r"The extrapolation fit failed to converge."):
        ExpFactory.extrapolate(X_VALS, [1.0, 2.0, 1.0, 2.0, 1.0])