Exemplo n.º 1
0
def test_garbage_3():
    print("garbage 3")
    a = qq.reg(1)

    with qq.garbage("test"):
        x = qq.reg(1)
        x += a

    with qq.garbage("test"):
        y = qq.reg(2)
        z = qq.reg(3)
        y += 3

    with qq.garbage("test-2"):
        y = qq.reg(8)
        y += 2

    with qq.garbage("test"):
        with qq.inv():
            qq.reg(2)
        z.clean(3)

    with qq.garbage("test"):
        with qq.inv():
            qq.reg(5)

    with qq.garbage("test-2"):
        with qq.inv():
            qq.reg(10)
Exemplo n.º 2
0
def test_inv_if():
    print("inv if")
    x, y = qq.reg([0, 1], 0)

    with qq.inv():
        with qq.q_if(x):
            y += 1

    with qq.q_if(y):
        with qq.inv():
            x += 1

    qq.print(x, y)
    x.clean(0)
    y.clean([0, -1])
Exemplo n.º 3
0
def test_garbage_4():
    print("garbage 4")
    x = qq.reg(5)

    with qq.garbage():
        x.assign(3)
        qq.print("assign(3) yields", x)
        with qq.inv():
            x.assign(3)

    with qq.garbage():
        qq.print("before bitset", *[x[i] for i in range(-1, 3)])
        x[-1] = 1
        x[1] = 1
        qq.print("bitset yields", *[x[i] for i in range(-1, 3)])
        with qq.inv():
            x[-1] = 1
            x[1] = 1
Exemplo n.º 4
0
def test_garbage_1():
    print("garbage 1")

    with qq.garbage():
        x = qq.reg(1)
        y = qq.reg(2)
        x += 1

        with qq.inv():
            xp = qq.reg(1)
            yp = qq.reg(2)
            xp += 1
Exemplo n.º 5
0
def test_order():
    print("order")
    n = 5
    x = qq.reg(range(2**n))

    qq.reg((7**x).int() % 15)  # has period 4

    with qq.inv():
        x.qft(2**n)

    vals, probs, _ = qq.distribution(x)
    plt.plot(vals, probs)
    plt.show()
Exemplo n.º 6
0
def grover():
    print("grover")
    n = 8

    # generate a random graph
    import random
    k = 4
    edges = []
    for i in range(n):
        for j in range(i + 1, n):
            if i != j + 1 % n:
                edges.append([i, j])
            # if random.random() > 0.5:

    @qq.garbage("oracle")
    def oracle(x):
        num_bad = qq.reg(0)
        clique_size = qq.reg(0)

        for i in range(n):
            with qq.q_if(x[i]):
                clique_size += 1

            for j in range(i + 1, n):
                if [i, j] not in edges:
                    with qq.q_if(x[i] & x[j]):
                        num_bad += 1

        return (num_bad == 0) & (clique_size >= k)

    x = qq.reg(range(2**n))

    for i in range(1):
        with qq.q_if(oracle(x)):
            qq.phase_pi(1)
        with qq.inv():
            oracle(x)

        for j in range(n):
            x.had(j)
        with qq.q_if(x == 0):
            qq.phase_pi(1)
        for j in range(n):
            x.had(j)

    values, probs, _ = qq.distribution(x)
    plt.bar(values, probs)
    plt.show()
Exemplo n.º 7
0
def test_while():
    print("while")

    x, y, l = qq.reg(1, [10, 15, 16], 0)

    with qq.q_while(x < y, l):
        x += 2
    qq.print(x, y, l)

    with qq.inv():
        with qq.q_while(x < y, l):
            x += 2

    x.clean(1)
    y.clean([10, 15, 16])
    l.clean(0)
Exemplo n.º 8
0
def test_garbage_5():
    print("garbage 5")
    i = qq.reg(0)
    tmp = qq.reg(0)
    with qq.garbage():

        with qq.q_while(i < 4, tmp):
            x = qq.reg(i)
            i += 1

        with qq.inv():
            with qq.q_while(i < 4, tmp):
                x = qq.reg(i)
                i += 1

    i.clean(0)
    tmp.clean(0)
Exemplo n.º 9
0
def test_inv():
    print("inv")
    x, y = qq.reg(0, 1)

    def stuff(x):
        x += 1
        x -= (y + 5) // 2
        x *= y
        # x *= 0 causes IrrevError
        x -= 2
        x //= 1
        # x //= 2 causes IrrevError
        x ^= (y + 1)

    stuff(x)
    qq.print(x)
    with qq.inv():
        stuff(x)

    x.clean(0)
    y.clean(1)
Exemplo n.º 10
0
def test_garbage_2():
    print("garbage 2")

    @qq.garbage("test")
    def messy(x):
        out = qq.reg(x)

        for i in [100, 200, 300]:
            with qq.q_while(x * out < i, qq.reg(0)):
                out += 1

        return out

    x = qq.reg([2, 4, 7, 8])

    out = qq.reg(messy(x))

    with qq.inv():
        messy(x)

    qq.print(x, out, x * out)
Exemplo n.º 11
0
def test_repeated_square():
    print("repeated square")

    @qq.garbage("repsquare")
    def rep_square(b, x, N):
        out = qq.reg(0)
        tmp = qq.reg(b)
        for i in range(5):
            with qq.q_if(x[i]):
                out += tmp
            tmp **= 2
            tmp %= N
        return out % 13

    x = qq.reg(range(16))
    out = qq.reg(0)
    with qq.garbage():
        out += rep_square(7, x, 13)
        with qq.inv():
            rep_square(7, x, 13)
        qq.assert_pile_clean('repsquare')

    qq.print(x, out, 7**x % 13)