コード例 #1
0
ファイル: test_qu.py プロジェクト: punkdit/qupy
def test_flatten_exp():

    n1, n2 = 3, 4

    H_1 = Gate.random_hermitian(n1)
    H_2 = Gate.random_hermitian(n2)

    H = H_1 @ Gate.identity(n2) + Gate.identity(n1) @ H_2

    op = H.get_flatop()
    H_flat = op.do(H)
    H_flat = Gate.promote(H_flat)

    t = 1.
    U_flat = H_flat.evolution_operator(t)

    U_1 = H_1.evolution_operator(t)
    U_2 = H_2.evolution_operator(t)

    U = U_1 @ U_2

    U_unflat = op.undo(U_flat)

    assert U.is_close(U_unflat)

    U_unflat = H.evolution_operator(t)

    assert U.is_close(U_unflat)
コード例 #2
0
ファイル: test_qu.py プロジェクト: punkdit/qupy
def test_random():

    A = Qu.random((2, 2), 'ud')

    A = Gate.random_hermitian(4)
    assert A.is_hermitian()

    A = Gate.random_unitary(4)
    assert A.is_unitary()
コード例 #3
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')
コード例 #4
0
ファイル: test_qu.py プロジェクト: punkdit/qupy
def test_pure():
    v = Qu.random((2, ), 'u')
    v /= v.norm()
    A = v @ ~v
    A = Gate.promote(A)

    assert is_close(A.trace(), 1.)
    assert A.is_pure()

    A = 0.5 * Gate.dyads[0] + 0.5 * Gate.dyads[1]
    A = Gate.promote(A)
    assert is_close(A.trace(), 1.)
    assert not A.is_pure()
コード例 #5
0
ファイル: test_qu.py プロジェクト: punkdit/qupy
def test_adjoint():

    for A in Gate.I, Gate.X, Gate.Y, Gate.Z, Gate.H:
        B = A * ~A
        assert B.is_close(Gate.I)
        B = ~A * A
        assert B.is_close(Gate.I)

    A = Qu.random((2, 3), 'ud')

    assert (~A).shape == (2, 3)
    assert (~A).valence == 'du'

    H = ~A * A
    assert Gate.promote(H).is_hermitian()

    B = Qu.random((4, 2), 'ud')
    E = Qu.random((3, 2, 2), 'udu')
    u = Qu.random(2, 'u')
    v = Qu.random(3, 'd')

    for i in range(100):
        word = []
        for j in range(randint(1, 4)):
            word.append(choice((A, B, E, u, v)))

        lhs = ~reduce(matmul, word)
        rhs = reduce(matmul, [~x for x in word])

        assert lhs.space == rhs.space, (lhs.space, rhs.space)

        assert lhs.is_close(rhs)
コード例 #6
0
def errop(t):

    H = Gate.random_hermitian(2)
    U = H.evolution_operator(t)
    assert U.is_unitary()

    return U
コード例 #7
0
def test_errop(t):
    H = Gate.random_hermitian(2)
    A = -1.j*t*H
    print(A)
    for val, vec in A.eigs():
        print(val, numpy.exp(val))
    print()
    U = A.expm()
    for val, vec in U.eigs():
        print(val)
    print()
    assert U.is_unitary()
    return U
コード例 #8
0
ファイル: test_qu.py プロジェクト: punkdit/qupy
def test_gate_properties():

    A = Gate((2, 2))
    A[0, 1] = 1.
    assert not A.is_unitary()
    assert not A.is_hermitian()

    for A in Gate.I, Gate.X, Gate.Y, Gate.Z, Gate.H:
        assert A.is_unitary()

    for A in Gate.I, Gate.X, Gate.Z, Gate.H:
        assert A.is_hermitian()
コード例 #9
0
ファイル: test_qu.py プロジェクト: punkdit/qupy
def test_evolve():

    t = 1.

    H = Gate.random_hermitian(2)
    U = H.evolution_operator(t)
    W = H.evolution_operator(t / 2)

    #print (W*W).shortstr()

    assert (W * W).is_close(U)

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

    #print
    w = U * v
    #print w.shortstr()

    x = W * v
    x = W * x
    #print x.shortstr()
    assert w.is_close(x)