예제 #1
0
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)
예제 #2
0
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)
예제 #3
0
def test_ada_exp_fac_with_asympt_more_steps(
    test_f: Callable[[float], float], avoid_log: bool
):
    """Test of the adaptive exponential extrapolator with more steps."""
    seeded_f = apply_seed_to_func(test_f, SEED)
    fac = AdaExpFactory(
        steps=6, scale_factor=2.0, asymptote=A, avoid_log=avoid_log
    )
    fac.iterate(seeded_f)
    assert np.isclose(fac.reduce(), seeded_f(0, err=0), atol=CLOSE_TOL)
예제 #4
0
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
    )
예제 #5
0
def test_ada_exp_factory_bad_arguments():
    with raises(ValueError, match="must be an integer greater or equal to 3"):
        AdaExpFactory(steps=2.5)

    with raises(ValueError, match="must be strictly larger than one"):
        AdaExpFactory(steps=4, scale_factor=0.5)

    with raises(ValueError, match="must be strictly larger than one"):
        AdaExpFactory(steps=4, max_scale_factor=1)

    with raises(ValueError, match="must be either a float or None"):
        AdaExpFactory(steps=10, asymptote=1j)
예제 #6
0
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: iterate calls next which calls reduce, so calling fac.iterate with
    # an AdaExpFactory sets the optimal parameters as well. Hence we check that
    # the opt_params are empty before AdaExpFactory.iterate is called.
    assert len(fac.opt_params) == 0
    fac.iterate(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
예제 #7
0
def test_with_observable_adaptive_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.amplitude_damp),
        observable=observable,
        factory=AdaExpFactory(steps=4, asymptote=0.5),
    )
    true_value = observable.expectation(
        circuit, functools.partial(compute_density_matrix, noise_level=(0, )))

    assert abs(zne_value - true_value) <= abs(noisy_value - true_value)
    fold_gates_from_left,
    fold_gates_from_right,
    fold_global,
)
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