Exemplo n.º 1
0
def reduce_order_interaction():

    # A function f defined over binary variables represented as a
    # vector stored in decimal order
    #
    # f(x4, x3, x2, x1) =   a
    #                     + b * x1
    #                     + c * x2
    #                     + d * x3
    #                     + e * x4
    #                     + g * x1 * x2
    #                     + h * x1 * x3
    #                     + i * x1 * x4
    #                     + j * x2 * x3
    #                     + k * x2 * x4
    #                     + l * x3 * x4
    #                     + m * x1 * x2 * x3
    #                     + n * x1 * x2 * x4
    #                     + o * x1 * x3 * x4
    #                     + p * x2 * x3 * x4
    #                     + q * x1 * x2 * x3 * x4
    #
    # f(0000) means when x4 = 0, x3 = 0, x2 = 0, x1 = 0, so f(0000) = a
    # f(0001) means when x4 = 0, x3 = 0, x2 = 0, x1 = 1, so f(0001) = a + b
    # f(0010) means when x4 = 0, x3 = 0, x2 = 1, x1 = 0, so f(0010) = a + c
    # etc.
    f = [0, -3, 0, 2, 1, -4, 0, 5, 1, 2, 0, 2, 1, 0, -3, -1]

    # new_terms means terms after using ancillary variables
    # vars_rep means ancillary variables
    (Q, new_terms, vars_rep) = make_quadratic(f)
    print "Q: ", Q
    print "new_terms: ", new_terms
    print "vars_rep: ", vars_rep

    # f =   x1 * x2
    #     + x3 * x4
    terms = [[1, 2], [3, 4]]
    (new_terms, vars_rep) = reduce_degree(terms)
    print "new_terms: ", new_terms
    print "vars_rep", vars_rep

    # f =   x2 * x3 * x4 * x5 * x8
    #     + x3 * x6 * x8
    #     + x1 * x6 * x7 * x8
    #     + x2 * x3 * x5 * x6 * x7
    #     + x1 * x3 * x6
    #     + x1 * x6 * x8 * x10 * x12
    terms = [[2, 3, 4, 5, 8], [3, 6, 8], [1, 6, 7, 8], [2, 3, 5, 6, 7],
             [1, 3, 6], [1, 6, 8, 10, 12]]
    (new_terms, vars_rep) = reduce_degree(terms)
    print "new_terms: ", new_terms
    print "vars_rep", vars_rep
Exemplo n.º 2
0
def test(f):
    q, new_terms, mapping = make_quadratic(f)
    assert q_vals(q, int(math.log(len(f), 2))) == f
    assert all(len(t) <= 2 for t in new_terms)
Exemplo n.º 3
0
def test_trivial():
    assert make_quadratic([0]) == ({}, [], [])
Exemplo n.º 4
0
def test_bad_f_zero():
    with pytest.raises(ValueError):
        make_quadratic([1])
Exemplo n.º 5
0
def test_bad_f_size():
    with pytest.raises(ValueError):
        make_quadratic([])
    with pytest.raises(ValueError):
        make_quadratic([0, 1, 1])