Esempio n. 1
0
def test_optimization_algorithm_optimize():
    black_box = ExampleBlackBox()
    algorithm = ExampleAlgorithm()
    result = algorithm.optimize(black_box)

    assert isinstance(result.optimal_value, float)
    assert isinstance(result.optimal_parameters, numpy.ndarray)
Esempio n. 2
0
def test_optimize_trial_result_data_methods():
    result1 = OptimizationResult(optimal_value=5.7,
                                 optimal_parameters=numpy.array([1.3, 8.7]),
                                 num_evaluations=59,
                                 cost_spent=3.1,
                                 seed=60,
                                 status=54,
                                 message='ZibVTBNe8',
                                 time=0.1)
    result2 = OptimizationResult(optimal_value=4.7,
                                 optimal_parameters=numpy.array([1.7, 2.1]),
                                 num_evaluations=57,
                                 cost_spent=9.3,
                                 seed=51,
                                 status=32,
                                 message='cicCZ8iCg0D',
                                 time=0.2)
    trial = OptimizationTrialResult([result1, result2],
                                    params=OptimizationParams(
                                        ExampleAlgorithm()))

    assert trial.repetitions == 2
    assert trial.optimal_value == 4.7
    numpy.testing.assert_allclose(trial.optimal_parameters,
                                  numpy.array([1.7, 2.1]))
    assert trial.num_evaluations_quantile() == 58
    numpy.testing.assert_allclose(trial.optimal_value_quantile(), 5.2)
    numpy.testing.assert_allclose(trial.cost_spent_quantile(), 6.2)
    numpy.testing.assert_allclose(trial.time_spent_quantile(), 0.15)
Esempio n. 3
0
def test_optimize_trial_result_init():
    result1 = OptimizationResult(optimal_value=5.7,
                                 optimal_parameters=numpy.array([1.3, 8.7]),
                                 num_evaluations=59,
                                 cost_spent=3.1,
                                 seed=60,
                                 status=54,
                                 message='ZibVTBNe8')
    result2 = OptimizationResult(optimal_value=4.7,
                                 optimal_parameters=numpy.array([1.7, 2.1]),
                                 num_evaluations=57,
                                 cost_spent=9.3,
                                 seed=51,
                                 status=32,
                                 message='cicCZ8iCg0D')
    trial = OptimizationTrialResult([result1, result2],
                                    params=OptimizationParams(
                                        ExampleAlgorithm()))

    assert all(trial.data_frame['optimal_value'] == [5.7, 4.7])
    numpy.testing.assert_allclose(trial.data_frame['optimal_parameters'][0],
                                  numpy.array([1.3, 8.7]))
    numpy.testing.assert_allclose(trial.data_frame['optimal_parameters'][1],
                                  numpy.array([1.7, 2.1]))
    assert all(trial.data_frame['num_evaluations'] == [59, 57])
    assert all(trial.data_frame['cost_spent'] == [3.1, 9.3])
    assert all(trial.data_frame['seed'] == [60, 51])
    assert all(trial.data_frame['status'] == [54, 32])
    assert all(trial.data_frame['message'] == ['ZibVTBNe8', 'cicCZ8iCg0D'])
Esempio n. 4
0
def test_optimization_trial_result_extend():
    result1 = OptimizationResult(optimal_value=4.7,
                                 optimal_parameters=numpy.array([2.3, 2.7]),
                                 num_evaluations=39,
                                 cost_spent=3.9,
                                 seed=63,
                                 status=44,
                                 message='di382j2f')
    result2 = OptimizationResult(optimal_value=3.7,
                                 optimal_parameters=numpy.array([1.2, 3.1]),
                                 num_evaluations=47,
                                 cost_spent=9.9,
                                 seed=21,
                                 status=22,
                                 message='i328d8ie3')

    trial = OptimizationTrialResult([result1],
                                    params=OptimizationParams(
                                        ExampleAlgorithm()))

    assert len(trial.results) == 1
    assert trial.repetitions == 1

    trial.extend([result2])

    assert len(trial.results) == 2
    assert trial.repetitions == 2
Esempio n. 5
0
        ExampleStudy,
        ExampleStudyNoisy)


test_ansatz = ExampleAnsatz()

a, b = test_ansatz.qubits
preparation_circuit = cirq.Circuit.from_ops(cirq.X(a))
test_study = ExampleStudy('test_study',
                          test_ansatz,
                          preparation_circuit=preparation_circuit)
test_study_noisy = ExampleStudyNoisy('test_study_noisy',
                                     test_ansatz,
                                     preparation_circuit=preparation_circuit)

test_algorithm = ExampleAlgorithm()


def test_variational_study_circuit():
    assert (test_study.circuit.to_text_diagram().strip() == """
0: ───X───X^theta0───@───X^theta0───M('all')───
                     │              │
1: ───────X^theta1───@───X^theta1───M──────────
""".strip())


def test_variational_study_num_params():
    assert test_study.num_params == 2


def test_variational_study_ansatz_properties():
Esempio n. 6
0
def test_optimization_algorithm_options():
    algorithm = ExampleAlgorithm()
    assert algorithm.options == {}
Esempio n. 7
0
def test_optimization_algorithm_name():
    algorithm = ExampleAlgorithm()
    assert algorithm.name == 'ExampleAlgorithm'