def test_get_expectation_values_adaptive_factories(factory): num_steps = 8 fac = AdaExpFactory(steps=num_steps, scale_factor=2.0, asymptote=None) executor = apply_seed_to_func(f_exp_up, seed=1) # Expectation values haven't been computed at any scale factors yet assert isinstance(fac.get_expectation_values(), np.ndarray) assert len(fac.get_expectation_values()) == 0 # Compute expectation values at all the scale factors fac.run_classical(executor) assert isinstance(fac.get_scale_factors(), np.ndarray) # Given this seeded executor, the scale factors should be as follows correct_scale_factors = np.array( [ 1.0, 2.0, 4.0, 4.20469548, 4.20310693, 4.2054822, 4.2031916, 4.2052843, ] ) correct_expectation_values = np.array( [executor(scale) for scale in correct_scale_factors] ) assert len(fac.get_expectation_values()) == num_steps assert np.allclose( fac.get_expectation_values(), correct_expectation_values, atol=1e-3 )
def test_ada_exp_factory_no_asympt_more_steps(test_f: Callable[[float], float], ): """Test of the adaptive exponential extrapolator.""" seeded_f = apply_seed_to_func(test_f, SEED) fac = AdaExpFactory(steps=8, scale_factor=2.0, asymptote=None) fac.run_classical(seeded_f) assert np.isclose(fac.reduce(), seeded_f(0, err=0), atol=CLOSE_TOL)
def test_adaptive_factory_max_iteration_warnings(): """Test that the correct warning is raised beyond the iteration limit.""" fac = AdaExpFactory(steps=10) with warns( ConvergenceWarning, match=r"Factory iteration loop stopped before convergence.", ): fac.run_classical(lambda scale_factor: 1.0, max_iterations=3)
def test_ada_exp_factory_with_asympt( test_f: Callable[[float], float], avoid_log: bool ): """Test of the adaptive exponential extrapolator.""" seeded_f = apply_seed_to_func(test_f, SEED) fac = AdaExpFactory( steps=3, scale_factor=2.0, asymptote=A, avoid_log=avoid_log ) # Note: run_classical calls next which calls reduce, so calling # fac.run_classical with an AdaExpFactory sets the optimal parameters as # well. Hence we check that the opt_params are empty before # AdaExpFactory.run_classical is called. assert not fac._opt_params fac.run_classical(seeded_f) assert np.isclose(fac.reduce(), seeded_f(0, err=0), atol=CLOSE_TOL) # There are three parameters to fit for the (adaptive) exponential ansatz assert len(fac._opt_params) == 3