def test_puso_to_pubo_to_puso(): puso = { (0, 1): -4, (0, 2): 3, (): -2, (0, ): 1, (2, ): -2, (0, 1, 2): 3, (0, 2, 3): -1 } assert puso == pubo_to_puso(puso_to_pubo(puso)) puso = { ('0', 1): -4, ('0', '2'): 3, (): -2, ('0', ): 1, ('2', ): -2, ('0', 1, '2'): 3, ('0', '2', 3): -1, ('2', 0, 0, '1', 0): -2, (0, 1, 1, 0, 3, 0, 1, 1, 3, 2, 3): -8 } # need to reformat qubo so it is sorted with the same hash assert PUSO(puso) == pubo_to_puso(puso_to_pubo(puso))
def test_pubo_to_puso_to_pubo(): pubo = { (0, ): 1, (0, 1): 1, (1, ): -1, (1, 2): .5, (): -2, (2, ): 1, (0, 2, 3): -3, (0, 1, 2): -2 } assert pubo == puso_to_pubo(pubo_to_puso(pubo)) pubo = { ('0', ): 1, ('0', 1): 1, (1, ): -1, (1, '2'): .5, (): -2, ('2', ): 1, ('0', '2', 3): -3, ('0', 1, '2'): -2, ('0', '0', 1, '0', '2', '2'): -9, (4, 2, 4, 0, 2, 0, 0): 3 } # need to reformat pubo so it is sorted with the same hash and squashed key assert PUBO(pubo) == puso_to_pubo(pubo_to_puso(pubo))
def test_anneal_pubo(): P = puso_to_pubo({(i, i + 1, i + 2): -1 for i in range(3)}) with assert_raises(ValueError): anneal_pubo(P, anneal_duration=-1) with assert_raises(ValueError): anneal_pubo(P, anneal_duration=-2) with assert_warns(QUBOVertWarning): anneal_pubo(P, temperature_range=(1, 2), schedule=[(3, 10), (2, 15)]) with assert_raises(ValueError): anneal_pubo(P, temperature_range=(1, 2)) with assert_raises(ValueError): anneal_pubo(P, schedule='something') empty_result = AnnealResults(False) for _ in range(4): empty_result.add_state({}, 2) assert anneal_pubo({(): 2}, num_anneals=4) == empty_result assert anneal_pubo(P, num_anneals=0) == AnnealResults(False) assert anneal_pubo(P, num_anneals=-1) == AnnealResults(False) # just make sure everything runs anneal_pubo(P, schedule='linear') anneal_pubo(P, initial_state=[1] * 5) # check to see if we find the groundstate of a simple but largeish model. P = puso_to_pubo({(i, i + 1): -1 for i in range(30)}) res = anneal_pubo(P, 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 same but out of order res = anneal_pubo(P, 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_pubo(P, schedule=[(3, 10), (2, 15)])
def test_temperature_range(): with assert_raises(ValueError): anneal_temperature_range({}, end_flip_prob=-.3) with assert_raises(ValueError): anneal_temperature_range({}, end_flip_prob=2) with assert_raises(ValueError): anneal_temperature_range({}, end_flip_prob=1) with assert_raises(ValueError): anneal_temperature_range({}, start_flip_prob=-.3) with assert_raises(ValueError): anneal_temperature_range({}, start_flip_prob=2) with assert_raises(ValueError): anneal_temperature_range({}, start_flip_prob=1) with assert_raises(ValueError): anneal_temperature_range({}, .3, .9) assert anneal_temperature_range({}) == (0, 0) assert anneal_temperature_range({(): 3}) == (0, 0) H = {(0, 1, 2): 2, (3, ): -1, (4, 5): 5, (): -2} probs = .1, .25, .57, .7 for i, end_flip_prob in enumerate(probs): for start_flip_prob in probs[i:]: # spin model T0, Tf = anneal_temperature_range(H, start_flip_prob, end_flip_prob, True) assert_allclose(T0, -10 / np.log(start_flip_prob)) assert_allclose(Tf, -2 / np.log(end_flip_prob)) # boolean model assert_allclose( (T0, Tf), anneal_temperature_range(puso_to_pubo(H), start_flip_prob, end_flip_prob, False)) H = { (0, 1): 1, ( 1, 2, ): -2, (1, 2, 3): 6, (): 11 } probs = 0, .16, .56, .98 for i, end_flip_prob in enumerate(probs): for start_flip_prob in probs[i:]: # spin model T0, Tf = anneal_temperature_range(H, start_flip_prob, end_flip_prob, True) assert_allclose( T0, -18 / np.log(start_flip_prob) if start_flip_prob else 0) assert_allclose(Tf, -2 / np.log(end_flip_prob) if end_flip_prob else 0) # boolean model assert_allclose( (T0, Tf), anneal_temperature_range(puso_to_pubo(H), start_flip_prob, end_flip_prob, False))
def _anneal_pubo(type_): P = type_(puso_to_pubo({(i, i+1, i+2): -1 for i in range(3)})) with assert_raises(ValueError): anneal_pubo(P, anneal_duration=-1) with assert_raises(ValueError): anneal_pubo(P, anneal_duration=-2) with assert_warns(QUBOVertWarning): anneal_pubo(P, temperature_range=(1, 2), schedule=[3, 2]) with assert_warns(QUBOVertWarning): # a quadratic model warns that you shouldn't use anneal_pubo anneal_pubo({(0, 1): 1}) with assert_raises(ValueError): anneal_pubo(P, temperature_range=(1, 2)) with assert_raises(ValueError): anneal_pubo(P, schedule='something') empty_result = AnnealResults() for _ in range(4): empty_result.add_state({}, 2, False) # less than quadratic so will warn with assert_warns(QUBOVertWarning): assert anneal_pubo({(): 2}, num_anneals=4) == empty_result assert anneal_pubo(P, num_anneals=0) == AnnealResults() assert anneal_pubo(P, num_anneals=-1) == AnnealResults() # just make sure everything runs anneal_pubo(P, schedule='linear') res = anneal_pubo(P, 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. P = type_(puso_to_pubo({(i, i+1): -1 for i in range(30)})) # quadratic so will warn with assert_warns(QUBOVertWarning): res = anneal_pubo(P, 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 same but out of order # quadratic so will warn with assert_warns(QUBOVertWarning): res = anneal_pubo(P, 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 # quadratic so will warn with assert_warns(QUBOVertWarning): anneal_pubo(P, schedule=[3] * 10 + [2] * 15) # make sure it works with fields res = anneal_pubo(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_pubo( 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
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)