Ejemplo n.º 1
0
def test_pcso_ne_constraint():

    for i in range(1 << 3):
        P = pubo_to_puso(integer_var('a', 3)) - i
        H = PCSO().add_constraint_ne_zero(P, log_trick=False)
        for sol in H.solve_bruteforce(True):
            assert P.value(sol)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
def test_pcso_ge_constraint_logtrick():

    lam = Symbol("lam")

    H = PCSO(
        pubo_to_puso({
            ('a', ): -1,
            ('b', ): 2,
            ('a', 'b'): -3,
            ('b', 'c'): -4,
            (): -2,
            ('d', ): -1
        }))
    H.add_constraint_ge_zero(pubo_to_puso({
        ('a', ): -1,
        ('b', ): -1,
        ('b', 'c'): -1,
        ('d', ): -1,
        (): 3
    }),
                             lam=lam)
    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)
    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)))

    # lam = 0
    H = PCSO(
        pubo_to_puso({
            ('a', ): -1,
            ('b', ): 2,
            ('a', 'b'): -3,
            ('b', 'c'): -4,
            (): -2,
            ('d', ): -1
        }))
    H.add_constraint_ge_zero(pubo_to_puso({
        ('a', ): -1,
        ('b', ): -1,
        ('b', 'c'): -1,
        ('d', ): -1,
        (): 3
    }),
                             lam=0)

    sol = H.remove_ancilla_from_solution(H.solve_bruteforce())
    assert all((H.is_solution_valid(sol), sol == solution))

    e, sol = solve_pubo_bruteforce(H.to_pubo())
    sol = H.convert_solution(sol, spin=False)
    sol = H.remove_ancilla_from_solution(sol)
    assert all(
        (not H.is_solution_valid(sol), sol != solution, not allclose(e, obj)))
Ejemplo n.º 4
0
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)))

    # lam = 0
    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=0)

    sol = H.solve_bruteforce()
    assert all((H.is_solution_valid(sol), sol == solution))

    e, sol = solve_pubo_bruteforce(H.to_pubo())
    sol = H.convert_solution(sol, False)
    assert all(
        (not H.is_solution_valid(sol), sol != solution, not allclose(e, obj)))