コード例 #1
0
def main_pauli():
    n = 1
    space = Space(1)
    for decl1, op1 in space.get_basis():
        for decl2, op2 in space.get_basis():
            print("%s*%s=%s" % (decl1, decl2, space.opstr(op1 * op2)), end=" ")
    print()
コード例 #2
0
 def __init__(self, n, stabs, logops):
     Space.__init__(self, n)
     self.d = 2
     if type(stabs) is str:
         stabs = [parse(decl) for decl in stabs.split()]
     if type(logops) is str:
         logops = [parse(decl) for decl in logops.split()]
     self.stabs = stabs
     self.logops = logops
コード例 #3
0
ファイル: dense.py プロジェクト: punkdit/qupy
 def identity(cls, space):
     if type(space) is int:
         n = space
         space = Space((n, n), 'ud')
     A = cls(space.shape)
     assert space.is_square(), space
     for i in range(space.shape[0]):
         A[i, i] = 1.
     return A
コード例 #4
0
ファイル: dense.py プロジェクト: punkdit/qupy
 def random_hermitian(cls, space):
     if type(space) is int:
         n = space
         space = Space((n, n), 'ud')
     assert space.is_square()
     A = Qu.random(space.shape, space.valence)
     A = A.dag() * A
     A = Gate.promote(A)
     return A
コード例 #5
0
ファイル: dense.py プロジェクト: punkdit/qupy
    def __init__(self, space):
        self.space = space
        self.rank = rank = len(space.shape)
        up_idxs = [idx for idx in range(rank) if space.valence[idx] == 'u']
        dn_idxs = [idx for idx in range(rank) if space.valence[idx] == 'd']
        self.perm = perm = up_idxs + dn_idxs
        self.up_shape = [space.shape[idx] for idx in up_idxs]
        self.dn_shape = [space.shape[idx] for idx in dn_idxs]
        if self.up_shape and self.dn_shape:
            shape = (reduce(mul, self.up_shape), reduce(mul, self.dn_shape))
            valence = 'ud'
        elif self.up_shape:
            shape = (reduce(mul, self.up_shape), )
            valence = 'u'
        elif self.dn_shape:
            shape = (reduce(mul, self.dn_shape), )
            valence = 'd'
        else:
            shape = ()
            valence = ''


#        shape = (reduce(mul, self.up_shape, 1), reduce(mul, self.dn_shape, 1))
#        if shape[0] == 1:
#            valence = 'd'
#            shape = shape[1],
#        elif shape[1] == 1:
#            valence = 'u'
#            shape = shape[0],
#        else:
#            valence = 'ud'
        self.shape = shape
        self.valence = valence
        self.target_space = Space(self.shape, self.valence)
コード例 #6
0
ファイル: dense.py プロジェクト: punkdit/qupy
 def random_unitary(cls, space):
     vs = []
     if type(space) is int:
         n = space
         space = Space((n, n), 'ud')
     assert space.is_square()
     n = space.shape[0]
     U = Gate(space.shape)
     for i in range(n):
         v = Qu.random(n, 'u')
         for u in vs:
             r = u.dag() * v
             v -= r * u
             #assert abs((u.transpose() * v)) < 1e-10
         v.normalize()
         vs.append(v)
         U[i, :] = v
     return U
コード例 #7
0
ファイル: test_qu.py プロジェクト: punkdit/qupy
def test_tensor_product():

    A = Gate((2, 2))
    A[0, 0] = 1.
    A[0, 1] = 2.
    A[1, 0] = 3.
    A[1, 1] = 4.

    B = Gate((2, 2))
    B[0, 0] = 5.
    B[0, 1] = 6.
    B[1, 0] = 7.
    B[1, 1] = 8.

    AB = A @ B
    for i, j, k, l in genidx((2, 2, 2, 2)):
        assert AB[i, j, k, l] == A[i, j] * B[k, l]

    AB = AB.contract(1, 2)
    for i, j in genidx((2, 2)):
        #print i, j, AB[i, j], "=>", sum(A[i, k] * B[k, j] for k in (0, 1))
        assert AB[i, j] == sum(A[i, k] * B[k, j] for k in (0, 1))

    assert on.shape == (2, )

    assert (on @ on).is_close(bits('11'))

    c = on @ off
    assert c.shape == (2, 2)

    c = on @ off @ on @ on
    assert c.shape == (2, ) * 4

    assert c.is_close(bits('1011'))

    for i0 in (0, 1):
        for i1 in (0, 1):
            for i2 in (0, 1):
                for i3 in (0, 1):
                    r = c[i0, i1, i2, i3]
                    assert (r != 0.) == ((i0, i1, i2, i3) == (True, False,
                                                              True, True))

    x = Gate.H.apply(off)
    assert x.space == Space((2, ), 'u'), str(x)

    y = (Gate.H @ off).contract(1, 2)
    assert x.is_close(y)

    A = Gate.H @ Gate.I
    #print A.shape, A.valence

    x = bits('00')
コード例 #8
0
ファイル: test_qu.py プロジェクト: punkdit/qupy
def test_shor_code():

    x0 = (1./(2*r2)) * (bits('000') + bits('111'))\
        @ (bits('000') + bits('111')) \
        @ (bits('000') + bits('111'))
    x1 = (1./(2*r2)) * (bits('000') - bits('111')) \
        @ (bits('000') - bits('111')) \
        @ (bits('000') - bits('111'))

    A = Qu((2, ) * 10, 'u' * 9 + 'd')

    A[(slice(None), ) * 9 + (0, )] = x0
    A[(slice(None), ) * 9 + (1, )] = x1

    x = Qu.random(2, 'u')
    x.normalize()

    y = A * x  # encode

    # now entangle with environment...

    env = Qu.random(2, 'u')

    z = y @ env

    return

    # this takes a while...

    rank = z.rank
    space = Space(2**rank, 'u')
    H = Qu.random_hermitian(space @ ~space)
    print("H:", H.space)
    U = H.evolution_operator(1.)
    print("U:", U.space, "rank:", U.rank)

    op = z.get_flatop()
    z = op.do(z)

    z1 = U * z

    if 0:
        space = z.space @ ~z.space
        #    U = Qu.random_unitary(space)
        H = Qu.random_hermitian(space)
        print("H:", H.space)
        U = H.evolution_operator(1.)
        print("U:", U.space, "rank:", U.rank)

        z1 = U * z
コード例 #9
0
ファイル: test_qu.py プロジェクト: punkdit/qupy
def test_space_pipe():

    a = Space((2, 2, 2, 2), 'udud')
    b = Space((2, 2, 2, 3), 'uddu')
    perm = a.unify(b)
    assert perm is None

    a = Space((2, 2, 2, 2), 'udud')
    b = Space((2, 2, 2, 2), 'uddu')
    perm = a.unify(b)
    assert perm == [0, 1, 3, 2]

    a = Space(2, 'u')
    b = Space(2, 'd')
    assert b * a == Space((), '')

    a = Space((2, 3), 'ud')
    b = Space((3, 4), 'ud')
    assert a * b == Space((2, 4), 'ud')

    a = Space((2, 2, 2, 2), 'udud')
    assert a * a == a, a * a

    for i in range(100):
        n = randint(0, 5)
        valence = list('u' * n + 'd' * n)
        shuffle(valence)
        valence = ''.join(valence)
        a = Space((2, ) * (2 * n), valence)
        assert a * a == a, (a * a, a)
コード例 #10
0
def main_action():
    def show(space, action):
        for decl, op in space.get_basis():
            op1 = action(op)
            print(decl, "-->", space.opstr(op1))

    n = 1
    space = Space(n)

    if argv.S:
        print("S =", space.opstr(S))
        show(space, lambda op: ~S * op * S)

    if argv.T:
        print("T =", space.opstr(T))
        show(space, lambda op: ~T * op * T)

    n = 2
    space = Space(n)
    CZ = Z.control(1, 0)
    assert CZ == Z.control(0, 1)
    if argv.CZ:
        print("CZ =", space.opstr(CZ))
        show(space, lambda op: CZ * op * ~CZ)

    CS = S.control(1, 0)
    assert CS == S.control(0, 1)
    if argv.CS:
        print("CS =", space.opstr(CS))
        show(space, lambda op: CS * op * ~CS)
    TT = T @ T
    if argv.TT:
        show(space, lambda op: TT * op * ~TT)

    n = 3
    space = Space(n)
    CCZ = Z.control(2, 0, 1)
    assert CCZ == Z.control(0, 1, 2)

    act = lambda op: CCZ * op * ~CCZ

    lhs = act(I @ I @ X)
    rhs = 0.5 * I @ I @ X + 0.5 * I @ Z @ X + 0.5 * Z @ I @ X - 0.5 * Z @ Z @ X
    #print(lhs.shortstr())
    #print(rhs.shortstr())
    assert (lhs == rhs)

    if argv.CCZ:
        print("CCZ =", space.opstr(CCZ))
        show(space, act)