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_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_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, )
def test_with_observable_batched_factory(executor): observable = Observable(PauliString(spec="Z")) circuit = cirq.Circuit(cirq.H.on(cirq.LineQubit(0))) * 20 noisy_value = observable.expectation(circuit, sample_bitstrings) zne_value = execute_with_zne( circuit, executor=functools.partial(executor, noise_model=cirq.depolarize), observable=observable, factory=PolyFactory(scale_factors=[1, 3, 5], order=2), ) true_value = observable.expectation( circuit, functools.partial(compute_density_matrix, noise_level=(0, ))) assert abs(zne_value - true_value) <= abs(noisy_value - true_value)
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_with_observable_two_qubits(executor): observable = Observable(PauliString(spec="XX", coeff=-1.21), PauliString(spec="ZZ", coeff=0.7)) circuit = cirq.Circuit(cirq.H.on(cirq.LineQubit(0)), cirq.CNOT.on(*cirq.LineQubit.range(2))) circuit += [circuit, cirq.inverse(circuit)] * 20 noisy_value = observable.expectation(circuit, sample_bitstrings) zne_value = execute_with_zne( circuit, executor=functools.partial(executor, noise_model=cirq.depolarize), observable=observable, factory=PolyFactory(scale_factors=[1, 3, 5], order=2), ) true_value = observable.expectation( circuit, functools.partial(compute_density_matrix, noise_level=(0, ))) assert abs(zne_value - true_value) <= 3 * abs(noisy_value - true_value)
def test_shot_list_errors(): """Tests errors related to the "shot_lists" argument.""" with raises(IndexError, match=r"must have the same length."): PolyFactory(X_VALS, order=2, shot_list=[1, 2]) with raises(TypeError, match=r"valid iterator of integers"): PolyFactory(X_VALS, order=2, shot_list=[1.0, 2])
def test_order_is_too_high_for_scale_factors(): """Test that a wrong initialization error is raised.""" with raises(ValueError, match=r"The extrapolation order cannot exceed"): _ = PolyFactory(X_VALS, order=10)
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)