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_params_cov_and_zne_std(): """Tests the variance of the parametes and of the zne are produced.""" x_values = [0, 0, 1] y_values = [-1, 1, 0] zne_limit = PolyFactory.extrapolate(x_values, y_values, order=1) assert np.isclose(zne_limit, 0.0, atol=1.0e-4) ( zne_limit, zne_std, opt_params, params_cov, zne_curve, ) = PolyFactory.extrapolate(x_values, y_values, order=1, full_output=True) assert len(opt_params) == 2 assert np.isclose(zne_limit, 0.0) assert np.isclose(0.0, opt_params[1]) assert np.isclose(0.0, opt_params[0]) assert np.allclose(params_cov, [[3.0, -1.0], [-1.0, 1.0]]) assert np.isclose(zne_std, 1.0) assert np.isclose(zne_curve(0), 0.0) assert np.isclose(zne_curve(0.5), 0.0)
def test_full_output_keyword_cov_std(): """Tests the full_output keyword in extrapolate method.""" zne_limit = PolyFactory.extrapolate([1, 2, 3], [1, 4, 9], order=2) assert np.isclose(zne_limit, 0.0) ( zne_limit, zne_std, opt_params, params_cov, zne_curve, ) = PolyFactory.extrapolate( [1, 2, 3], [1, 4, 9], order=2, full_output=True ) assert len(opt_params) == 3 assert np.isclose(zne_limit, 0.0) assert np.isclose(0.0, opt_params[1]) assert np.isclose(1.0, opt_params[0]) assert params_cov is None assert zne_std is None assert np.isclose(zne_curve(0), 0.0) assert np.isclose(zne_curve(2), 4.0) assert np.isclose(zne_curve(3), 9.0)
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, )