Ejemplo n.º 1
0
def test_annealresults_filter_states():

    states = [
        AnnealResult({
            0: -1,
            1: 1,
            'a': -1
        }, 1, True),
        AnnealResult({
            0: 1,
            1: 1,
            'a': -1
        }, 9, True),
        AnnealResult({
            0: -1,
            1: -1,
            'a': -1
        }, -3, True)
    ]
    filtered_states = [
        AnnealResult({
            0: -1,
            1: 1,
            'a': -1
        }, 1, True),
        AnnealResult({
            0: 1,
            1: 1,
            'a': -1
        }, 9, True)
    ]
    res = AnnealResults(states)
    filtered_res = res.filter_states(lambda x: x[1] == 1)
    assert filtered_res == filtered_states
Ejemplo n.º 2
0
def test_extend_add():

    res0 = AnnealResults(AnnealResult({}, 2, True) for _ in range(4))
    assert res0.best.value == 2
    res1 = AnnealResults(AnnealResult({}, 1, True) for _ in range(3))
    assert res1.best.value == 1

    assert (res0 + res1).best.value == 1
    assert type(res0 + res1) == AnnealResults

    temp = res0.copy()
    temp += res1
    assert temp.best.value == 1
    assert type(temp) == AnnealResults

    temp = res0.copy()
    temp.extend(res1)
    assert temp.best.value == 1
    assert type(temp) == AnnealResults

    assert (res1 + res0).best.value == 1
    assert type(res1 + res0) == AnnealResults

    temp = res1.copy()
    temp += res0
    assert temp.best.value == 1
    assert type(temp) == AnnealResults

    temp = res1.copy()
    temp.extend(res0)
    assert temp.best.value == 1
    assert type(temp) == AnnealResults
Ejemplo n.º 3
0
def test_annealresults_filter():

    states = [
        AnnealResult({
            0: -1,
            1: 1,
            'a': -1
        }, 1, True),
        AnnealResult({
            0: 1,
            1: 1,
            'a': -1
        }, 9, True),
        AnnealResult({
            0: -1,
            1: -1,
            'a': -1
        }, -3, True)
    ]
    filtered_states = [
        AnnealResult({
            0: -1,
            1: 1,
            'a': -1
        }, 1, True),
        AnnealResult({
            0: -1,
            1: -1,
            'a': -1
        }, -3, True)
    ]
    res = AnnealResults.from_list(states, True)
    filtered_res = res.filter(lambda x: x.state[0] == -1)
    assert filtered_res == filtered_states
Ejemplo n.º 4
0
def test_annealresults_convert_states():

    states = [
        AnnealResult({
            0: -1,
            1: 1,
            'a': -1
        }, 1, True),
        AnnealResult({
            0: 1,
            1: 1,
            'a': -1
        }, 9, True),
        AnnealResult({
            0: -1,
            1: -1,
            'a': -1
        }, -3, True)
    ]
    new_states = [
        AnnealResult({
            'b': -1,
            1: 1,
            'a': -1
        }, 1, True),
        AnnealResult({
            'b': 1,
            1: 1,
            'a': -1
        }, 9, True),
        AnnealResult({
            'b': -1,
            1: -1,
            'a': -1
        }, -3, True)
    ]
    res = AnnealResults(states)
    new_res = res.convert_states(
        lambda x: {k if k else 'b': v
                   for k, v in x.items()})
    assert new_res == new_states
Ejemplo n.º 5
0
def test_annealresults_apply_function():

    states = [
        AnnealResult({
            0: -1,
            1: 1,
            'a': -1
        }, 1, True),
        AnnealResult({
            0: 1,
            1: 1,
            'a': -1
        }, 9, True),
        AnnealResult({
            0: -1,
            1: -1,
            'a': -1
        }, -3, True)
    ]
    new_states = [
        AnnealResult({
            0: -1,
            1: 1,
            'a': -1
        }, 1 + 2, True),
        AnnealResult({
            0: 1,
            1: 1,
            'a': -1
        }, 9 + 2, True),
        AnnealResult({
            0: -1,
            1: -1,
            'a': -1
        }, -3 + 2, True)
    ]
    res = AnnealResults(states)
    new_res = res.apply_function(
        lambda x: AnnealResult(x.state, x.value + 2, x.spin))
    assert new_res == new_states
Ejemplo n.º 6
0
def test_anneal_puso():

    H = {(i, i + 1, i + 2): -1 for i in range(3)}

    with assert_raises(ValueError):
        anneal_puso(H, anneal_duration=-1)

    with assert_raises(ValueError):
        anneal_puso(H, anneal_duration=-2)

    with assert_warns(QUBOVertWarning):
        anneal_puso(H, temperature_range=(1, 2), schedule=[(3, 10), (2, 15)])

    with assert_raises(ValueError):
        anneal_puso(H, temperature_range=(1, 2))

    with assert_raises(ValueError):
        anneal_puso(H, schedule='something')

    empty_result = AnnealResults(True)
    for _ in range(4):
        empty_result.add_state({}, 2)
    assert anneal_puso({(): 2}, num_anneals=4) == empty_result

    assert anneal_puso(H, num_anneals=0) == AnnealResults(True)
    assert anneal_puso(H, num_anneals=-1) == AnnealResults(True)

    # just make sure everything runs
    anneal_puso(H, schedule='linear')
    anneal_puso(H, initial_state=[1] * 5)

    # check to see if we find the groundstate of a simple but largeish model.
    H = {(i, i + 1): -1 for i in range(30)}
    res = anneal_puso(H, num_anneals=4, seed=0)
    assert res.best.state in (dict(enumerate([1] * 31)),
                              dict(enumerate([-1] * 31)))
    assert res.best.value == -30
    assert len([x for x in res]) == 4

    # check to see if we find the groundstate of same but out of order
    res = anneal_puso(H, num_anneals=4, in_order=False, seed=0)
    assert res.best.state in (dict(enumerate([1] * 31)),
                              dict(enumerate([-1] * 31)))
    assert res.best.value == -30
    assert len([x for x in res]) == 4

    # make sure we run branch where an explicit schedule is given and no
    # temperature range is supplied
    anneal_puso(H, schedule=[(3, 10), (2, 15)])
Ejemplo n.º 7
0
def test_annealresults():

    states = [
        ({
            0: -1,
            1: 1,
            'a': -1
        }, 1),
        ({
            0: 1,
            1: 1,
            'a': -1
        }, 9),
        ({
            0: -1,
            1: -1,
            'a': -1
        }, -3),
    ]
    sorted_states = sorted(states, key=lambda x: x[1])
    anneal_states = [AnnealResult(*s, True) for s in states]
    anneal_sorted_states = [AnnealResult(*s, True) for s in sorted_states]

    res, boolean_res = AnnealResults(), AnnealResults()
    for s in states:
        res.add_state(*s, True)
        boolean_res.add_state(spin_to_boolean(s[0]), s[1], False)

    for s in states:
        assert AnnealResult(*s, True) in res
        assert AnnealResult(s[0], s[1] + 1, True) not in res

    assert len(res) == 3
    assert len(boolean_res) == 3
    assert res.best.state == states[2][0]
    assert res.best.value == states[2][1]
    assert res.copy() == res
    assert res.to_spin() == res
    assert res.to_boolean() == boolean_res
    assert boolean_res.to_spin() == res
    assert res == anneal_states
    assert boolean_res == [x.to_boolean() for x in anneal_states]
    str(res)
    str(boolean_res)
    repr(res)
    repr(boolean_res)

    count = 0
    for s in res:
        assert s == AnnealResult(*states[count], True)
        count += 1

    for i in range(3):
        assert res[i] == anneal_states[i]

    assert res[:2] == anneal_states[:2]
    assert isinstance(res[1:3], AnnealResults)

    res.sort()
    count = 0
    for s in res:
        assert s == AnnealResult(*sorted_states[count], True)
        count += 1

    for i in range(3):
        assert res[i] == anneal_sorted_states[i]

    assert res[:2] == anneal_sorted_states[:2]

    boolean_res.sort()
    assert res == anneal_sorted_states
    assert boolean_res == [x.to_boolean() for x in anneal_sorted_states]

    assert type(res * 2) == AnnealResults
    assert type(res + res) == AnnealResults
    res *= 2
    assert type(res) == AnnealResults
    res += res
    assert type(res) == AnnealResults
    res += anneal_states
    assert type(res) == AnnealResults
    res.extend(anneal_sorted_states)
    assert type(res) == AnnealResults

    res.clear()
    assert res.best is None
    assert not res
Ejemplo n.º 8
0
def _anneal_puso(type_):

    H = type_({(i, i+1, i+2): -1 for i in range(3)})

    with assert_raises(ValueError):
        anneal_puso(H, anneal_duration=-1)

    with assert_raises(ValueError):
        anneal_puso(H, anneal_duration=-2)

    with assert_warns(QUBOVertWarning):
        anneal_puso(H, temperature_range=(1, 2), schedule=[3, 2])

    with assert_warns(QUBOVertWarning):
        # a quadratic model warns that you shouldn't use anneal_puso
        anneal_puso({(0, 1): 1})

    with assert_raises(ValueError):
        anneal_puso(H, temperature_range=(1, 2))

    with assert_raises(ValueError):
        anneal_puso(H, schedule='something')

    empty_result = AnnealResults()
    for _ in range(4):
        empty_result.add_state({}, 2, True)
    # less than quadratic model so will warn
    with assert_warns(QUBOVertWarning):
        assert anneal_puso({(): 2}, num_anneals=4) == empty_result

    assert anneal_puso(H, num_anneals=0) == AnnealResults()
    assert anneal_puso(H, num_anneals=-1) == AnnealResults()

    # just make sure everything runs
    anneal_puso(H, schedule='linear')
    res = anneal_puso(H, initial_state=[1] * 5)
    for x in res:
        assert all(i in (1, -1) for i in x.state.values())

    # check to see if we find the groundstate of a simple but largeish model.
    H = type_({(i, i+1): -1 for i in range(30)})
    # quadratic model so will warn
    with assert_warns(QUBOVertWarning):
        res = anneal_puso(H, num_anneals=4, seed=0)
    assert res.best.state in (
        dict(enumerate([1]*31)), dict(enumerate([-1]*31))
    )
    assert res.best.value == -30
    assert len([x for x in res]) == 4

    # check to see if we find the groundstate of same but out of order
    # quadratic so will warn
    with assert_warns(QUBOVertWarning):
        res = anneal_puso(H, num_anneals=4, in_order=False, seed=0)
    assert res.best.state in (
        dict(enumerate([1]*31)), dict(enumerate([-1]*31))
    )
    assert res.best.value == -30
    assert len([x for x in res]) == 4

    # make sure we run branch where an explicit schedule is given and no
    # temperature range is supplied
    # quadratic so will warn
    with assert_warns(QUBOVertWarning):
        anneal_puso(H, schedule=[3, 2])

    # make sure it works with fields
    res = anneal_puso(
        type_({(0, 1, 2): 1, (1,): -1, (): 2}),
        num_anneals=10
    )
    assert len(res) == 10
    res.sort()
    for i in range(9):
        assert res[i].value <= res[i + 1].value

    # bigish ordering
    res = anneal_puso(
        type_(
            {(i, j, j + 1): 1 for i in range(70) for j in range(i+1, 70)}
        ),
        num_anneals=20
    )
    assert len(res) == 20
    res.sort()
    for i in range(19):
        assert res[i].value <= res[i + 1].value
Ejemplo n.º 9
0
def _anneal_qubo(type_):

    Q = type_(quso_to_qubo({(i, i+1): -1 for i in range(3)}))

    with assert_raises(ValueError):
        anneal_qubo(Q, anneal_duration=-1)

    with assert_raises(ValueError):
        anneal_qubo(Q, anneal_duration=-2)

    with assert_warns(QUBOVertWarning):
        anneal_qubo(Q, temperature_range=(1, 2), schedule=[3, 2])

    with assert_raises(ValueError):
        anneal_qubo(Q, temperature_range=(1, 2))

    with assert_raises(ValueError):
        anneal_qubo(Q, schedule='something')

    empty_result = AnnealResults()
    for _ in range(4):
        empty_result.add_state({}, 2, False)
    assert anneal_qubo({(): 2}, num_anneals=4) == empty_result

    assert anneal_qubo(Q, num_anneals=0) == AnnealResults()
    assert anneal_qubo(Q, num_anneals=-1) == AnnealResults()

    # just make sure everything runs
    anneal_qubo(Q, schedule='linear')
    res = anneal_qubo(Q, initial_state=[1] * 5)
    for x in res:
        assert all(i in (0, 1) for i in x.state.values())

    # check to see if we find the groundstate of a simple but largeish model.
    Q = type_(quso_to_qubo({(i, i+1): -1 for i in range(30)}))
    res = anneal_qubo(Q, num_anneals=4, seed=0)
    assert res.best.state in (
        dict(enumerate([0]*31)), dict(enumerate([1]*31))
    )
    assert res.best.value == -30
    assert len([x for x in res]) == 4

    # check to see if we find the groundstate of the same but out of order
    res = anneal_qubo(Q, num_anneals=4, in_order=False, seed=0)
    assert res.best.state in (
        dict(enumerate([0]*31)), dict(enumerate([1]*31))
    )
    assert res.best.value == -30
    assert len([x for x in res]) == 4

    # make sure we run branch where an explicit schedule is given and no
    # temperature range is supplied
    anneal_qubo(Q, schedule=[3] * 10 + [2] * 15)

    # make sure it works with fields
    res = anneal_qubo(type_({(0, 1): 1, (1,): -1, (): 2}), num_anneals=10)
    assert len(res) == 10
    res.sort()
    for i in range(9):
        assert res[i].value <= res[i + 1].value

    # big ordering
    res = anneal_qubo(
        type_({(i, j): 1 for i in range(70) for j in range(i+1, 70)}),
        num_anneals=20
    )
    assert len(res) == 20
    res.sort()
    for i in range(19):
        assert res[i].value <= res[i + 1].value