def test_too_few_points_for_polyfit_warning(): """Test that the correct warning is raised if data is not enough to fit.""" fac = PolyFactory(X_VALS, order=2) fac._instack = [ { "scale_factor": 1.0, "shots": 100 }, { "scale_factor": 2.0, "shots": 100 }, ] fac._outstack = [1.0, 2.0] with warns( ExtrapolationWarning, match=r"The extrapolation fit may be ill-conditioned.", ): fac.reduce() # test also the static "extrapolate" method. with warns( ExtrapolationWarning, match=r"The extrapolation fit may be ill-conditioned.", ): PolyFactory.extrapolate([1.0, 2.0], [1.0, 2.0], order=2)
def test_poly_extr(): """Test of polynomial extrapolator.""" # test (order=1) fac = PolyFactory(X_VALS, order=1) fac.run_classical(f_lin) assert np.isclose(fac.reduce(), f_lin(0, err=0), atol=CLOSE_TOL) # test that, for some non-linear functions, # order=1 is bad while order=2 is better. seeded_f = apply_seed_to_func(f_non_lin, SEED) fac = PolyFactory(X_VALS, order=1) fac.run_classical(seeded_f) assert not np.isclose(fac.reduce(), seeded_f(0, err=0), atol=NOT_CLOSE_TOL) seeded_f = apply_seed_to_func(f_non_lin, SEED) fac = PolyFactory(X_VALS, order=2) fac.run_classical(seeded_f) assert np.isclose(fac.reduce(), seeded_f(0, err=0), atol=CLOSE_TOL)
def test_too_few_points_for_polyfit_error(): """Test that the correct error is raised if data is not enough to fit.""" fac = PolyFactory(X_VALS, order=2) fac._instack = [ { "scale_factor": 1.0, "shots": 100 }, { "scale_factor": 2.0, "shots": 100 }, ] fac._outstack = [1.0, 2.0] with raises(ValueError, match=r"Extrapolation order is too high."): fac.reduce()
def test_opt_params_poly_factory(order): """Tests that optimal parameters are stored after calling the reduce method. """ fac = PolyFactory(scale_factors=np.linspace(1, 10, 10), order=order) assert fac.opt_params == [] fac.iterate(apply_seed_to_func(f_non_lin, seed=SEED)) zne_value = fac.reduce() assert len(fac.opt_params) == order + 1 assert np.isclose(fac.opt_params[-1], zne_value)
def test_poly_extr(): """Test of polynomial extrapolator.""" # test (order=1) fac = PolyFactory(X_VALS, order=1) fac.run_classical(f_lin) assert np.isclose(fac.reduce(), f_lin(0, err=0), atol=CLOSE_TOL) # test that, for some non-linear functions, # order=1 is bad while order=2 is better. seeded_f = apply_seed_to_func(f_non_lin, SEED) fac = PolyFactory(X_VALS, order=1) fac.run_classical(seeded_f) assert not np.isclose(fac.reduce(), seeded_f(0, err=0), atol=NOT_CLOSE_TOL) seeded_f = apply_seed_to_func(f_non_lin, SEED) fac = PolyFactory(X_VALS, order=2) fac.run_classical(seeded_f) zne_value = fac.reduce() assert np.isclose(fac.reduce(), seeded_f(0, err=0), atol=CLOSE_TOL) exp_vals = fac.get_expectation_values() assert np.isclose(fac.extrapolate(X_VALS, exp_vals, order=2), zne_value) assert np.isclose( fac.extrapolate(X_VALS, exp_vals, order=2, full_output=True)[0], zne_value, )