def test_avoid_log_keyword(): """Test that avoid_log=True and avoid_log=False give different results.""" fac = ExpFactory(X_VALS, asymptote=A, avoid_log=False) fac.run_classical(f_exp_down) znl_with_log = fac.reduce() fac.avoid_log = True znl_without_log = fac.reduce() assert not znl_with_log == znl_without_log
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()
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, )
def test_exp_factory_no_asympt(test_f: Callable[[float], float]): """Test of exponential extrapolator.""" seeded_f = apply_seed_to_func(test_f, SEED) fac = ExpFactory(X_VALS, asymptote=None) fac.run_classical(seeded_f) assert not fac._opt_params assert np.isclose(fac.reduce(), seeded_f(0, err=0), atol=CLOSE_TOL) # There are three parameters to fit in the exponential ansatz assert len(fac._opt_params) == 3
def test_run_factory_with_number_of_shots(): """Tests "run" method of an ExpFactory with shot_list.""" qp = random_one_qubit_identity_circuit(num_cliffords=TEST_DEPTH) qp = measure(qp, 0) fac = ExpFactory([1.0, 2.0, 3.0], shot_list=[10 ** 4, 10 ** 5, 10 ** 6]) fac.run(qp, basic_executor, scale_noise=scale_noise) result = fac.reduce() assert np.isclose(result, 1.0, atol=1.0e-1) assert fac._instack[0] == {"scale_factor": 1.0, "shots": 10 ** 4} assert fac._instack[1] == {"scale_factor": 2.0, "shots": 10 ** 5} assert fac._instack[2] == {"scale_factor": 3.0, "shots": 10 ** 6}
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=True) fac.iterate(seeded_f) assert len(fac.opt_params) == 0 assert np.isclose(fac.reduce(), seeded_f(0, err=0), atol=CLOSE_TOL) # There are three parameters to fit in the exponential ansatz assert len(fac.opt_params) == 3