Exemplo n.º 1
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)))
Exemplo n.º 2
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)))