def test_iteration_warnings(): """Test that the correct warning is raised beyond the iteration limit.""" fac = LinearFactory(X_VALS) with warns( ConvergenceWarning, match=r"Factory iteration loop stopped before convergence.", ): fac.iterate(lambda scale_factor: 1.0, max_iterations=3)
def test_linear_extr(): """Tests extrapolation with a LinearFactory.""" seeded_f = apply_seed_to_func(f_lin, SEED) fac = LinearFactory(X_VALS) assert not fac._opt_params fac.run_classical(seeded_f) assert np.isclose(fac.reduce(), seeded_f(0, err=0), atol=CLOSE_TOL) assert np.allclose(fac._opt_params, [B, A], atol=CLOSE_TOL)
def test_full_output_keyword(): """Tests the full_output keyword in extrapolate method.""" zlim = LinearFactory.extrapolate([1, 2], [1, 2]) assert np.isclose(zlim, 0.0) zlim, opt_params = LinearFactory.extrapolate( [1, 2], [1, 2], full_output=True ) assert len(opt_params) == 2 assert np.isclose(zlim, 0.0) assert np.isclose(0.0, opt_params[1]) assert np.isclose(1.0, opt_params[0])
def test_equal_simple(): fac = LinearFactory(scale_factors=[1, 2, 3]) assert fac != 1 copied_fac = copy(fac) assert copied_fac == fac copied_fac._already_reduced = True assert copied_fac != fac fac._instack = [{"scale_factor": 1, "shots": 100}] copied_fac = deepcopy(fac) assert copied_fac == fac copied_fac._instack[0].update({"shots": 101}) assert copied_fac != fac
def test_full_output_keyword(): """Tests the full_output keyword in extrapolate method.""" zne_limit = LinearFactory.extrapolate([1, 2], [1, 2]) assert np.isclose(zne_limit, 0.0) ( zne_limit, zne_std, opt_params, params_cov, zne_curve, ) = LinearFactory.extrapolate([1, 2], [1, 2], 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(1.0, opt_params[0]) assert zne_std is None assert params_cov is None assert np.isclose(zne_curve(0), 0.0) assert np.isclose(zne_curve(2), 2.0)
def test_linear_extr(): """Tests extrapolation with a LinearFactory.""" seeded_f = apply_seed_to_func(f_lin, SEED) fac = LinearFactory(X_VALS) assert not fac._opt_params fac.run_classical(seeded_f) zne_value = fac.reduce() assert np.isclose(zne_value, seeded_f(0, err=0), atol=CLOSE_TOL) assert np.allclose(fac._opt_params, [B, A], atol=CLOSE_TOL) exp_vals = fac.get_expectation_values() assert np.isclose(fac.extrapolate(X_VALS, exp_vals), zne_value) assert np.isclose( fac.extrapolate(X_VALS, exp_vals, full_output=True)[0], zne_value, )
def test_get_methods_of_factories(): """Tests the get methods of a factory""" x_values = [0, 0, 1] y_values = [-1, 1, 0] fac = LinearFactory(x_values) fac._instack = [ {"scale_factor": 0}, {"scale_factor": 0}, {"scale_factor": 1}, ] fac._outstack = y_values zne_reduce = fac.reduce() assert np.allclose(fac.get_expectation_values(), y_values) assert np.allclose(fac.get_extrapolation_curve()(0.0), zne_reduce) assert np.allclose(fac.get_optimal_parameters(), [0.0, 0.0]) assert np.allclose( fac.get_parameters_covariance(), [[3.0, -1.0], [-1.0, 1.0]] ) assert np.allclose(fac.get_scale_factors(), x_values) assert np.allclose(fac.get_zero_noise_limit(), zne_reduce) assert np.allclose(fac.get_zero_noise_limit_error(), 1.0)
def test_push_after_already_reduced_warning(): """Tests a warning is raised if new data is pushed in a factory which was already reduced.""" fac = LinearFactory([1, 2]) fac.push({"scale_factor": 1.0}, 1.0) fac.push({"scale_factor": 2.0}, 2.0) fac.reduce() with warns( ExtrapolationWarning, match=r"You are pushing new data into a factory object", ): fac.push({"scale_factor": 3.0}, 3.0) # Assert no warning is raised when .reset() is used fac.reset() fac.push({"scale_factor": 1.0}, 2.0) fac.push({"scale_factor": 2.0}, 1.0) assert np.isclose(3.0, fac.reduce())
"""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]) @mark.parametrize("fac", [LinearFactory([1, 1, 1]), ExpFactory([1, 1, 1])]) def test_failing_fit_warnings(fac): """Test that the correct warning is raised for an ill-conditioned fit.""" fac._instack = [{"scale_factor": 1.0} for _ in range(4)] fac._outstack = [1, 1, 1, 1] 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.", ): fac.extrapolate([1, 1, 1, 1], [1.0, 1.0, 1.0, 1.0])
) from mitiq.benchmarks.utils import noisy_simulation from mitiq.zne import mitigate_executor SCALE_FUNCTIONS = [ fold_gates_at_random, fold_gates_from_left, fold_gates_from_right, fold_global, ] FACTORIES = [ AdaExpFactory(steps=3, scale_factor=1.5, asymptote=0.25), ExpFactory([1.0, 1.4, 2.1], asymptote=0.25), RichardsonFactory([1.0, 1.4, 2.1]), LinearFactory([1.0, 1.6]), PolyFactory([1.0, 1.4, 2.1], order=2), ] def test_rb_circuits(): depths = range(2, 10, 2) # test single qubit RB for trials in [2, 3]: circuits = rb_circuits(n_qubits=1, num_cliffords=depths, trials=trials) for qc in circuits: # we check the ground state population to ignore any global phase wvf = qc.final_wavefunction() zero_prob = abs(wvf[0]**2) assert np.isclose(zero_prob, 1)
def test_less_than_two_scale_factors_error(): """Test less than 2 scale_factors.""" with raises(ValueError, match=r"At least 2 scale factors are necessary"): _ = LinearFactory([1])