示例#1
0
    def test_save_parameter_grid_evaluation(self):
        # Given
        ansatz = MockAnsatz(2, 2)
        grid = build_uniform_param_grid(1, 2, 0, np.pi, np.pi / 10)
        backend = create_object({
            "module_name": "zquantum.core.interfaces.mock_objects",
            "function_name": "MockQuantumSimulator",
        })
        op = QubitOperator("0.5 [] + 0.5 [Z1]")
        (
            parameter_grid_evaluation,
            optimal_parameters,
        ) = evaluate_operator_for_parameter_grid(ansatz, grid, backend, op)
        # When
        save_parameter_grid_evaluation(parameter_grid_evaluation,
                                       "parameter-grid-evaluation.json")
        save_circuit_template_params(optimal_parameters,
                                     "optimal-parameters.json")
        # Then
        # TODO

        files_to_remove = ("parameter-grid-evaluation.json",
                           "optimal-parameters.json")
        failed_to_remove = []

        for path in files_to_remove:
            try:
                os.remove(path)
            except OSError:
                failed_to_remove.append(path)

        if failed_to_remove:
            raise RuntimeError(f"Failed to remove files: {failed_to_remove}")
示例#2
0
def test_noisy_ground_state_cost_function_adds_noise_to_parameters():
    target_operator = QubitOperator("Z0")
    parametrized_circuit = MockAnsatz(
        number_of_layers=2, problem_size=1
    ).parametrized_circuit
    parametrized_circuit.evaluate = mock.Mock(wraps=parametrized_circuit.evaluate)
    backend = MockQuantumSimulator()
    estimation_method = estimate_expectation_values_by_averaging
    estimation_preprocessors = [partial(allocate_shots_uniformly, number_of_shots=1)]
    noisy_ground_state_cost_function = get_ground_state_cost_function(
        target_operator,
        parametrized_circuit,
        backend,
        estimation_method=estimation_method,
        estimation_preprocessors=estimation_preprocessors,
        parameter_precision=1e-4,
        parameter_precision_seed=RNGSEED,
    )

    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], dtype=float)

    expected_symbols_map = [
        (Symbol("theta_0"), noise[0] + params[0]),
        (Symbol("theta_1"), noise[1] + params[1]),
    ]

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

    # We only called our function once, therefore the following should be true
    parametrized_circuit.evaluate.assert_called_with(expected_symbols_map)

    # and if only everything went right, this only call should be of the form
    # noisy_ansatz.ansatz.get_executable_circuit(params+noise)
    # Therefore, we extract the single argument and compare it to the
    # expected one.
    assert np.array_equal(
        parametrized_circuit.evaluate.call_args[0][0], expected_symbols_map
    )
示例#3
0
def ansatz_based_cost_function():
    target_operator = QubitOperator("Z0")
    ansatz = MockAnsatz(number_of_layers=1, problem_size=1)
    backend = MockQuantumSimulator()
    estimation_method = estimate_expectation_values_by_averaging
    estimation_preprocessors = [partial(allocate_shots_uniformly, number_of_shots=1)]
    return AnsatzBasedCostFunction(
        target_operator,
        ansatz,
        backend,
        estimation_method=estimation_method,
        estimation_preprocessors=estimation_preprocessors,
    )
示例#4
0
def noisy_ansatz_cost_function_with_ansatz():
    target_operator = QubitOperator("Z0")
    ansatz = MockAnsatz(number_of_layers=2, problem_size=1)
    backend = MockQuantumSimulator()
    estimation_method = mock.Mock(wraps=calculate_exact_expectation_values)
    return (
        AnsatzBasedCostFunction(
            target_operator,
            ansatz,
            backend,
            estimation_method=estimation_method,
            parameter_precision=1e-4,
            parameter_precision_seed=RNGSEED,
        ),
        ansatz,
    )
示例#5
0
 def test_save_parameter_grid_evaluation(self):
     # Given
     ansatz = MockAnsatz(2, 2)
     grid = build_uniform_param_grid(1, 2, 0, np.pi, np.pi / 10)
     backend = create_object({
         "module_name": "zquantum.core.interfaces.mock_objects",
         "function_name": "MockQuantumSimulator",
     })
     op = QubitOperator("0.5 [] + 0.5 [Z1]")
     (
         parameter_grid_evaluation,
         optimal_parameters,
     ) = evaluate_operator_for_parameter_grid(ansatz, grid, backend, op)
     # When
     save_parameter_grid_evaluation(parameter_grid_evaluation,
                                    "parameter-grid-evaluation.json")
     save_circuit_template_params(optimal_parameters,
                                  "optimal-parameters.json")
示例#6
0
def ansatz():
    return MockAnsatz(1, 5)
示例#7
0
from zquantum.core.utils import create_symbols_map
from zquantum.core.interfaces.mock_objects import (
    MockAnsatz,
    MockQuantumSimulator,
)
from zquantum.core.measurement import ExpectationValues

RNGSEED = 1234


@pytest.fixture(
    params=[
        {
            "target_operator": QubitOperator("Z0"),
            "parametrized_circuit": MockAnsatz(
                number_of_layers=1, problem_size=1
            ).parametrized_circuit,
            "backend": MockQuantumSimulator(),
            "estimation_method": estimate_expectation_values_by_averaging,
            "estimation_preprocessors": [
                partial(allocate_shots_uniformly, number_of_shots=1)
            ],
        },
        {
            "target_operator": QubitOperator("Z0 Z1"),
            "parametrized_circuit": MockAnsatz(
                number_of_layers=1, problem_size=2
            ).parametrized_circuit,
            "backend": MockQuantumSimulator(),
            "estimation_method": estimate_expectation_values_by_averaging,
            "estimation_preprocessors": [