コード例 #1
0
def test_qubo_to_quso_to_qubo():

    qubo = {(0, ): 1, (0, 1): 1, (1, ): -1, (1, 2): .2, (): -2, (2, ): 1}
    assert qubo == quso_to_qubo(qubo_to_quso(qubo))

    # type asserting
    assert type(qubo_to_quso(qubo)) == QUSO
    assert type(qubo_to_quso(QUBOMatrix(qubo))) == QUSOMatrix
    assert type(qubo_to_quso(QUBO(qubo))) == QUSO

    qubo = {
        ('0', ): 1,
        ('0', 1): 1,
        (1, ): -1,
        (1, '2'): .2,
        (): -2,
        ('2', ): 1,
        (0, 0): 1
    }
    # need to reformat qubo so it is sorted with the same hash
    assert QUBO(qubo) == quso_to_qubo(qubo_to_quso(qubo))

    # type asserting
    assert type(qubo_to_quso(qubo)) == QUSO
    assert type(qubo_to_quso(QUBO(qubo))) == QUSO
コード例 #2
0
def test_symbols():

    a, b = Symbol('a'), Symbol('b')
    quso = {
        (0, ): 1.0 * a,
        (0, 1): 1.,
        (1, ): -1.0 * a,
        (1, 2): 1.,
        (): -2. * b,
        (2, ): 1.0 * a
    }
    quso1 = qubo_to_quso(quso_to_qubo(quso))
    quso1.simplify()
    quso = QUSO(quso)
    quso.simplify()
    assert quso == quso1

    a, b = Symbol('a'), Symbol('b')
    qubo = {
        (0, ): 1.0 * a,
        (0, 1): 1.,
        (1, ): -1.0 * a,
        (1, 2): 1.,
        (): -2.0 * b,
        (2, ): 1.0 * a
    }
    qubo1 = quso_to_qubo(qubo_to_quso(qubo))
    qubo1.simplify()
    qubo = QUBO(qubo)
    qubo.simplify()
    assert qubo == qubo1
コード例 #3
0
def test_quso_to_qubo_to_quso():

    quso = {(0, 1): -4, (0, 2): 3, (): -2, (0, ): 1, (2, ): -2}
    assert quso == qubo_to_quso(quso_to_qubo(quso))

    quso = {('0', 1): -4, ('0', '2'): 3, (): -2, ('0', ): 1, ('2', '2'): -2}
    # need to reformat quso so it is sorted with the same hash and squashed key
    assert QUSO(quso) == qubo_to_quso(quso_to_qubo(quso))
コード例 #4
0
def test_anneal_qubo():

    Q = 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, 10), (2, 15)])

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

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

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

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

    # just make sure everything runs
    anneal_qubo(Q, schedule='linear')
    anneal_qubo(Q, initial_state=[1] * 4)

    # check to see if we find the groundstate of a simple but largeish model.
    Q = 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)])
コード例 #5
0
def test_qubosimulation_vs_qusosimulation():

    ising = sum(-spin_var(i) * spin_var(i + 1) for i in range(15))
    qubo = quso_to_qubo(ising)

    schedule = [(T, 20) for T in range(3, 0, -1)]

    spin = QUSOSimulation(ising)
    boolean = QUBOSimulation(qubo)

    assert spin.initial_state == boolean_to_spin(boolean.initial_state)

    spin.schedule_update(schedule, seed=4)
    boolean.schedule_update(schedule, seed=4)
    assert spin.state == boolean_to_spin(boolean.state)

    initial_state = [0] * 8 + [1] * 8
    spin = QUSOSimulation(ising, boolean_to_spin(initial_state))
    boolean = QUBOSimulation(qubo, initial_state)

    assert spin.initial_state == boolean_to_spin(boolean.initial_state)

    spin.schedule_update(schedule, seed=4)
    boolean.schedule_update(schedule, seed=4)
    assert spin.state == boolean_to_spin(boolean.state)
コード例 #6
0
def test_qubo_to_quso_to_qubo():

    qubo = {(0, ): 1, (0, 1): 1, (1, ): -1, (1, 2): .2, (): -2, (2, ): 1}
    assert qubo == quso_to_qubo(qubo_to_quso(qubo))

    qubo = {
        ('0', ): 1,
        ('0', 1): 1,
        (1, ): -1,
        (1, '2'): .2,
        (): -2,
        ('2', ): 1,
        (0, 0): 1
    }
    # need to reformatt qubo so it is sorted with the same hash
    assert QUBO(qubo) == quso_to_qubo(qubo_to_quso(qubo))
コード例 #7
0
def test_quso_to_qubo_to_quso():

    quso = {(0, 1): -4, (0, 2): 3, (): -2, (0, ): 1, (2, ): -2}
    assert quso == qubo_to_quso(quso_to_qubo(quso))

    # type asserting
    assert type(quso_to_qubo(quso)) == QUBO
    assert type(quso_to_qubo(QUSOMatrix(quso))) == QUBOMatrix
    assert type(quso_to_qubo(QUSO(quso))) == QUBO

    quso = {('0', 1): -4, ('0', '2'): 3, (): -2, ('0', ): 1, ('2', '2'): -2}
    # need to reformat quso so it is sorted with the same hash and squashed key
    assert QUSO(quso) == qubo_to_quso(quso_to_qubo(quso))

    # type asserting
    assert type(quso_to_qubo(quso)) == QUBO
    assert type(quso_to_qubo(QUSO(quso))) == QUBO
コード例 #8
0
def test_qubosimulation_set_state():

    ising = quso_to_qubo(sum(-spin_var(i) * spin_var(i + 1) for i in range(9)))

    sim = QUBOSimulation(ising)
    assert sim.state == {i: 0 for i in ising.variables}

    sim = QUBOSimulation(ising, {i: 1 for i in ising.variables})
    assert sim.state == {i: 1 for i in ising.variables}

    with assert_raises(ValueError):
        sim.set_state({i: 3 for i in ising.variables})

    with assert_raises(ValueError):
        QUBOSimulation(ising, {i: -1 for i in ising.variables})

    sim = QUBOSimulation({(0, ): 1})
    with assert_raises(ValueError):
        sim.set_state([-1])

    sim.set_state([1])
    assert sim.state == {0: 1}

    # test the same thing but wiht matrix
    ising = quso_to_qubo(
        QUSOMatrix(sum(-spin_var(i) * spin_var(i + 1) for i in range(9))))

    sim = QUBOSimulation(ising)
    assert sim.state == {i: 0 for i in ising.variables}

    sim = QUBOSimulation(ising, {i: 1 for i in ising.variables})
    assert sim.state == {i: 1 for i in ising.variables}

    with assert_raises(ValueError):
        sim.set_state({i: 3 for i in ising.variables})

    with assert_raises(ValueError):
        QUBOSimulation(ising, {i: -1 for i in ising.variables})

    sim = QUBOSimulation({(0, ): 1})
    with assert_raises(ValueError):
        sim.set_state([-1])

    sim.set_state([1])
    assert sim.state == {0: 1}

    # test the same thing but wiht QUBO
    ising = quso_to_qubo(
        QUBO(sum(-spin_var(i) * spin_var(i + 1) for i in range(9))))

    sim = QUBOSimulation(ising)
    assert sim.state == {i: 0 for i in ising.variables}

    sim = QUBOSimulation(ising, {i: 1 for i in ising.variables})
    assert sim.state == {i: 1 for i in ising.variables}

    with assert_raises(ValueError):
        sim.set_state({i: 3 for i in ising.variables})

    with assert_raises(ValueError):
        QUBOSimulation(ising, {i: -1 for i in ising.variables})

    sim = QUBOSimulation({(0, ): 1})
    with assert_raises(ValueError):
        sim.set_state([-1])

    sim.set_state([1])
    assert sim.state == {0: 1}
コード例 #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
コード例 #10
0
def test_quso_to_qubo_eq_puso_to_pubo():

    quso = {(0, 1): -4, (0, 2): 3, (): -2, (0, ): 1, (2, ): -2}
    assert quso_to_qubo(quso) == puso_to_pubo(quso)