Пример #1
0
    def test_correctly_seeds_rng(self):
        preprocessor_1 = add_normal_noise(1e-5, RNGSEED)
        preprocessor_2 = add_normal_noise(1e-5, RNGSEED)

        params = np.linspace(0, np.pi, 10)

        np.testing.assert_array_equal(preprocessor_1(params),
                                      preprocessor_2(params))
Пример #2
0
    def ground_state_cost_function(self, request, factory_type):
        if factory_type == "old":
            return get_ground_state_cost_function(
                **request.param,
                backend=BACKEND,
                estimation_method=ESTIMATION_METHOD,
                estimation_preprocessors=ESTIMATION_PREPROCESSORS,
            )
        elif factory_type == "new":
            estimation_tasks_factory = expectation_value_estimation_tasks_factory(
                request.param["target_operator"],
                request.param["parametrized_circuit"],
                ESTIMATION_PREPROCESSORS,
            )

            parameter_preprocessors = []
            if "fixed_parameters" in request.param:
                fix_params_preprocessor = fix_parameters(
                    request.param["fixed_parameters"])
                parameter_preprocessors.append(fix_params_preprocessor)
            if "parameter_precision" in request.param:
                noise_preprocessor = add_normal_noise(
                    request.param["parameter_precision"],
                    request.param["parameter_precision_seed"],
                )
                parameter_preprocessors.append(noise_preprocessor)

            return create_cost_function(
                BACKEND,
                estimation_tasks_factory,
                ESTIMATION_METHOD,
                parameter_preprocessors,
            )
Пример #3
0
    def test_does_not_mutate_parameters(self):
        preprocessor = add_normal_noise(0.1, RNGSEED)

        params = np.ones(3)

        preprocessor(params)

        np.testing.assert_array_equal(params, np.ones(3))
Пример #4
0
    def test_seeds_rng_during_initialization(self):
        preprocessor = add_normal_noise(1e-4, RNGSEED)
        params = np.array([0.1, 0.2, -0.5])

        # The second call to preprocessor should advance generator if it was
        # seeded only during initialization, hence the second call should produce
        # different result.
        assert not np.array_equal(preprocessor(params), preprocessor(params))
Пример #5
0
    def test_mean_of_added_noise_is_correct(self):
        preprocessor = add_normal_noise(0.001, RNGSEED)
        num_params = 100
        num_repetitions = 10
        params = np.ones(num_params)

        average_diff = sum(
            (params - preprocessor(params)).sum()
            for _ in range(num_repetitions)) / (num_repetitions * num_params)

        np.testing.assert_allclose(average_diff, 0.0, atol=1e-03)
Пример #6
0
    def test_std_of_added_noise_is_correct(self):
        preprocessor = add_normal_noise(0.001, RNGSEED)
        num_params = 100
        num_repetitions = 100
        params = np.ones(num_params)

        sample = [
            diff for diff in (params - preprocessor(params))
            for _ in range(num_repetitions)
        ]

        np.testing.assert_allclose(np.std(sample), 0.001, atol=1e-03)
Пример #7
0
class TestAnsatzBasedCostFunction:
    @pytest.fixture()
    def old_ansatz_based_cost_function(self):
        return AnsatzBasedCostFunction(
            TARGET_OPERATOR,
            ANSATZ,
            BACKEND,
            ESTIMATION_METHOD,
            ESTIMATION_PREPROCESSORS,
        )

    @pytest.fixture(params=[
        {},
        {
            "parameter_preprocessors": [
                add_normal_noise(
                    parameter_precision=1e-4,
                    parameter_precision_seed=RNGSEED,
                )
            ]
        },
        {
            "gradient_function": finite_differences_gradient
        },
    ])
    def ansatz_based_cost_function(self, request):
        estimation_factory = substitution_based_estimation_tasks_factory(
            TARGET_OPERATOR, ANSATZ, ESTIMATION_PREPROCESSORS)

        return create_cost_function(
            BACKEND,
            estimation_factory,
            ESTIMATION_METHOD,
            **request.param,
        )

    @pytest.mark.parametrize("param", [0.0, 0.42, 1.0, np.pi])
    def test_ansatz_based_cost_function_returns_value_between_plus_and_minus_one(
            self, param, ansatz_based_cost_function,
            old_ansatz_based_cost_function):
        params = np.array([param])

        value = ansatz_based_cost_function(params)
        assert -1 <= value <= 1

        value = old_ansatz_based_cost_function(params)
        assert -1 <= value <= 1

    @pytest.fixture()
    def noisy_ansatz_cost_function_with_ansatz(self):
        ansatz = MockAnsatz(number_of_layers=2, problem_size=1)
        estimation_method = mock.Mock(wraps=calculate_exact_expectation_values)
        return (
            AnsatzBasedCostFunction(
                TARGET_OPERATOR,
                ansatz,
                BACKEND,
                estimation_method,
                parameter_precision=1e-4,
                parameter_precision_seed=RNGSEED,
            ),
            ansatz,
        )

    def test_old_ansatz_based_cost_function_adds_noise_to_parameters(
            self, noisy_ansatz_cost_function_with_ansatz):
        noisy_ansatz_cost_function = noisy_ansatz_cost_function_with_ansatz[0]
        ansatz = noisy_ansatz_cost_function_with_ansatz[1]
        generator = np.random.default_rng(RNGSEED)

        # We expect the below to get added to parameters
        noise = generator.normal(0, 1e-4, 2)

        params = np.array([0.1, 2.3])

        # ansatz based cost function may modify params in place
        # and we need original ones - therefore we pass a copy
        noisy_ansatz_cost_function(np.array(params))

        # We only called our function once, therefore the following should be true
        noisy_ansatz_cost_function.estimation_method.assert_called_once()

        # Here, we make the expected executable circuit with the noisy parameters
        noisy_symbols_map = create_symbols_map(
            ansatz.parametrized_circuit.free_symbols, noise + params)
        expected_noisy_circuit = ansatz.parametrized_circuit.bind(
            noisy_symbols_map)

        assert (noisy_ansatz_cost_function.estimation_method.call_args[0][1]
                [0].circuit == expected_noisy_circuit)