def test_pcso_ne_constraint_logtrick(): for i in range(1 << 4): P = pubo_to_puso(integer_var('a', 4)) - i H = PCSO().add_constraint_ne_zero(P) for sol in H.solve_bruteforce(True): assert P.value(sol) for i in range(1 << 4): P = pubo_to_puso(integer_var('a', 4)) - i H = PCSO(P).add_constraint_ne_zero(P, lam=0) for sol in H.solve_bruteforce(True): assert P.value(sol) for i in range(1 << 2): P = pubo_to_puso(integer_var('a', 2)) - i H = PCSO().add_constraint_ne_zero(P) for sol in solve_puso_bruteforce(H, True)[1]: assert P.value(sol)
def test_pretty_str(): def equal(expression, string): assert expression.pretty_str() == string z = [PCSO() + {(i, ): 1} for i in range(3)] a, b = Symbol('a'), Symbol('b') equal(z[0] * 0, "0") equal(z[0], "z(0)") equal(-z[0], "-z(0)") equal(z[0] * 0, "0") equal(2 * z[0] * z[1] - 3 * z[2], "2 z(0) z(1) - 3 z(2)") equal(0 * z[0] + 1, "1") equal(0 * z[0] - 1, "-1") equal(0 * z[0] + a, "(a)") equal(0 * z[0] + a * b, "(a*b)") equal((a + b) * (z[0] * z[1] - z[2]), "(a + b) z(0) z(1) + (-a - b) z(2)") equal(2 * z[0] * z[1] - z[2], "2 z(0) z(1) - z(2)") equal(-z[2] + z[0] * z[1], "-z(2) + z(0) z(1)") equal(-2 * z[2] + 2 * z[0] * z[1], "-2 z(2) + 2 z(0) z(1)")
def test_pcso_on_deg_5_puso(): problem = PCSO({ ('a', ): -1, ('b', ): 2, ('a', 'b'): -3, ('b', 'c'): -4, (): -2, (0, 1, 2): 1, (0, ): -1, (1, ): -2, (2, ): 1, ('a', 0, 4, 'b', 'c'): -3, (4, 2, 3, 'a', 'b'): 2, (4, 2, 3, 'b'): -1, ('c', ): 4, (3, ): 1 }) solution = {0: 1, 1: 1, 'c': -1, 2: -1, 4: -1, 3: -1, 'b': -1, 'a': -1} obj = -26 Problem(problem, solution, obj).runtests()
def test_pcso_le_constraint(): lam = Symbol("lam") H = PCSO( pubo_to_puso({ ('a', ): -1, ('b', ): 2, ('a', 'b'): -3, ('b', 'c'): -4, (): -2, ('d', ): -1 })) H.add_constraint_le_zero(pubo_to_puso({ ('a', ): 1, ('b', ): 1, ('b', 'c'): 1, ('d', ): 1, (): -3 }), lam=lam, log_trick=False) solution = boolean_to_spin({'c': 1, 'b': 1, 'a': 1, 'd': 0}) obj = -8 problem = H.subs(lam, .5) sol = problem.remove_ancilla_from_solution(problem.solve_bruteforce()) assert all((problem.is_solution_valid(sol), sol == solution)) e, sol = solve_pubo_bruteforce(problem.to_pubo()) sol = problem.convert_solution(sol, spin=False) sol = problem.remove_ancilla_from_solution(sol) assert all((not problem.is_solution_valid(sol), sol != solution, not allclose(e, obj))) problem = H.subs(lam, 10) sol = problem.solve_bruteforce() sol = problem.remove_ancilla_from_solution(sol) assert all((problem.is_solution_valid(sol), sol == solution)) e, sol = solve_pubo_bruteforce(problem.to_pubo()) sol = problem.convert_solution(sol) sol = problem.remove_ancilla_from_solution(sol) assert all( (problem.is_solution_valid(sol), sol == solution, allclose(e, obj)))
def test_pcso_eq_constraint(): lam = Symbol('lam') H = PCSO( pubo_to_puso({ ('a', ): -1, ('b', ): 2, ('a', 'b'): -3, ('b', 'c'): -4, (): -2 })) H.add_constraint_eq_zero(pubo_to_puso({ ('a', ): 1, ('b', ): 1, ('b', 'c'): -1 }), lam=lam) solution = boolean_to_spin({'c': 1, 'b': 1, 'a': 0}) obj = -4 problem = H.subs(lam, 1) sol = problem.solve_bruteforce() assert all((problem.is_solution_valid(sol), sol == solution)) e, sol = solve_pubo_bruteforce(problem.to_pubo()) sol = problem.convert_solution(sol, False) assert all((not problem.is_solution_valid(sol), sol != solution, not allclose(e, obj))) problem = H.subs(lam, 10) sol = problem.solve_bruteforce() assert all((problem.is_solution_valid(sol), sol == solution)) e, sol = solve_pubo_bruteforce(problem.to_pubo()) sol = problem.convert_solution(sol) assert all( (problem.is_solution_valid(sol), sol == solution, allclose(e, obj)))
def test_to_enumerated(): d = PCSO({('a', 'b'): 1, ('a', ): 2}) dt = d.to_enumerated() assert type(dt) == PUSOMatrix assert dt == d.to_puso()
def test_init_pcso(): H = PCSO().add_constraint_eq_zero({(0, ): 1, (1, ): -2}, bounds=(-1, 1)) d = PCSO(H) assert H.constraints == d.constraints
def test_properties(): temp = PCSO({('0', '0'): 1, ('0', 1): 2}) assert temp.offset == 1 d = PCSO() d[(0, )] += 1 d[(1, )] += 2 assert d == d.to_quso() == {(0, ): 1, (1, ): 2} assert d.mapping == d.reverse_mapping == {0: 0, 1: 1} d.set_mapping({1: 0, 0: 1}) assert d.to_quso() == {(1, ): 1, (0, ): 2} assert d.mapping == d.reverse_mapping == {0: 1, 1: 0} assert d.constraints == {} temp = d.copy() d.add_constraint_eq_zero(temp) assert d.constraints == {'eq': [temp]} # an old bug d = PCSO() d.set_mapping({0: 0}) d[(0, )] += 1 assert d.num_binary_variables == 1 assert d.variables == {0}
def test_quso_multiplication(): temp = PCSO({('0', '0'): 1, ('0', 1): 2}) temp1 = {(): 3, (1, '0'): 6}, {(): 3, ('0', 1): 6} temp2 = {(): .5, (1, '0'): 1}, {(): .5, ('0', 1): 1} temp3 = {(1, '0'): 1}, {('0', 1): 1} # constant d = temp.copy() d += 3 d *= -2 assert d in ({(1, '0'): -4, (): -8}, {('0', 1): -4, (): -8}) # __mul__ d = temp.copy() g = d * 3 assert g in temp1 d = temp.copy() g = d * 0 assert g == {} # __imul__ d = temp.copy() d *= 3 assert d in temp1 d = temp.copy() d *= 0 assert d == {} # __rmul__ d = temp.copy() g = 3 * d assert g in temp1 d = temp.copy() g = 0 * d assert g == {} # __truediv__ d = temp.copy() g = d / 2 assert g in temp2 # __itruediv__ d = temp.copy() d /= 2 assert d in temp2 # __floordiv__ d = temp.copy() g = d // 2 assert g in temp3 # __ifloordiv__ d = temp.copy() d //= 2 assert d in temp3 # __mul__ but with dict d = temp.copy() d *= {(1, ): 2, ('0', '0'): -1} assert d in ({ (1, ): 2, (): -1, ('0', ): 4, ('0', 1): -2 }, { (1, ): 2, (): -1, ('0', ): 4, (1, '0'): -2 }) # __pow__ d = temp.copy() d -= 2 d **= 2 assert d in ({(): 5, ('0', 1): -4}, {(): 5, (1, '0'): -4}) d = temp.copy() assert d**2 == d * d assert d**3 == d * d * d d = PCSO({('0', 1): 1, ('1', 2): -1})**2 assert d**4 == d * d * d * d
def test_num_terms(): d = PCSO({(0, ): 1, (0, 3): 2, (0, 2): -1}) assert d.num_terms == len(d)
def test_quso_num_binary_variables(): d = PCSO({(0, ): 1, (0, 3): 2}) assert d.num_binary_variables == 2 assert d.max_index == 1
def test_quso_reinitialize_dictionary(): d = PCSO({(0, 0): 1, ('1', 0): 2, (2, 0): 0, (0, '1'): 1}) assert d in ({(): 1, ('1', 0): 3}, {(): 1, (0, '1'): 3})
def test_quso_remove_value_when_zero(): d = PCSO() d[(0, 1)] += 1 d[(0, 1)] -= 1 assert d == {}
def test_pcso_checkkey(): with assert_raises(KeyError): PCSO({0: -1})
def test_pcso_constraints_warnings(): with assert_warns(QUBOVertWarning): # qlwayss satisfied PCSO().add_constraint_eq_zero({(): 0}) with assert_warns(QUBOVertWarning): # not satisfiable PCSO().add_constraint_eq_zero({(): 1, (0, ): -.5}) with assert_warns(QUBOVertWarning): # not satisfiable PCSO().add_constraint_eq_zero({(): -1, (0, ): .5}) with assert_warns(QUBOVertWarning): # not satisfiable PCSO().add_constraint_lt_zero({(): 1, (0, ): -.5}) with assert_warns(QUBOVertWarning): # not satisfiable PCSO().add_constraint_lt_zero({(): 1, (0, ): -1}) with assert_warns(QUBOVertWarning): # always satisfied PCSO().add_constraint_lt_zero({(): -1, (0, ): -.5}) with assert_warns(QUBOVertWarning): # not satisfiable PCSO().add_constraint_le_zero({(): 1, (0, ): -.5}) with assert_warns(QUBOVertWarning): # always satisfied PCSO().add_constraint_le_zero({(): -1, (0, ): -.5}) with assert_warns(QUBOVertWarning): # not satisfiable PCSO().add_constraint_gt_zero({(): -1, (0, ): .5}) with assert_warns(QUBOVertWarning): # not satisfiable PCSO().add_constraint_gt_zero({(): -1, (0, ): 1}) with assert_warns(QUBOVertWarning): # always satisfied PCSO().add_constraint_gt_zero({(): 1, (0, ): .5}) with assert_warns(QUBOVertWarning): # not satisfiable PCSO().add_constraint_ge_zero({(): -1, (0, ): .5}) with assert_warns(QUBOVertWarning): # always satisfied PCSO().add_constraint_ge_zero({(): 1, (0, ): .5})