Example #1
0
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,
    )
Example #2
0
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
Example #3
0
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()
Example #4
0
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}
Example #6
0
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
Example #7
0
def test_exp_factory_bad_asympt():
    with raises(ValueError, match="must be either a float or None"):
        ExpFactory(X_VALS, asymptote=1j)
Example #8
0
    """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])
    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
            wvf = qc.final_wavefunction()