Example #1
0
def test_bits():

    assert bits('000').decode() == '000'
    assert bits('001').decode() == '001'
    assert bits('010').decode() == '010'
    assert bits('100').decode() == '100'
    assert bits('110').decode() == '110'
Example #2
0
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')
Example #3
0
def test_bell_basis():

    raise test.Skip

    A = (Gate.H @ Gate.I) * Gate.CN

    print()
    xs = Qu.bell_basis(2)
    for x in xs:
        print(x.shortstr())

    print()
    for i, word in enumerate(['00', '01', '10', '11']):
        x = bits(word)
        print((A * x).shortstr())
Example #4
0
def test_bitflip_code():

    x0 = bits('000')
    x1 = bits('111')

    A = Qu((2, ) * 4, "uduu")

    A[:, 0, :, :] = x0
    A[:, 1, :, :] = x1

    assert (A * off).is_close(x0)
    assert (A * on).is_close(x1)

    B = Gate.CN
    B = B * (Gate.I @ off)
    B = B @ off

    C = Gate.CN @ Gate.I
    assert (C * bits('000')).decode() == '000'
    assert (C * bits('100')).decode() == '110'
    assert (C * bits('111')).decode() == '101'
    C = C.swap((2, 4), (3, 5))  # yuck...
    assert (C * bits('000')).decode() == '000'
    assert (C * bits('100')).decode() == '101'
    assert (C * bits('111')).decode() == '110'

    B = C * B

    assert B.is_close(A)

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

    y = A * x  # encode

    # now entangle with environment...

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

    z = y @ env

    space = z.space @ ~z.space
    U = Qu.random_unitary(space)

    z1 = U * z
Example #5
0
def test_parse_shor_encoder():

    I, X, Y, Z, H = Gate.I, Gate.X, Gate.Y, Gate.Z, Gate.H

    shor = """
    --.--.--H--.--.---
      |  |     |  |   
    -----------+------
      |  |        |   
    --------------+---
      |  |            
    --+-----H--.--.---
         |     |  |   
    -----------+------
         |        |   
    --------------+---
         |            
    -----+--H--.--.---
               |  |   
    -----------+------
                  |   
    --------------+---
    """

    SHOR = parse(shor)
    SHOR = SHOR * (I @ off @ off @ off @ off @ off @ off @ off @ off)

    # phase-flip encoder
    P = parse("""
    --H--.--.--
         |  |  
    -----+-----
            |  
    --------+--
    """)

    P = P * (I @ off @ off)  # ancilla bits

    # bit-flip encoder
    B = parse("""
    --.--.--
      |  |  
    --+-----
         |  
    -----+--
    """)
    B = B * (I @ off @ off)  # ancilla

    if 0:
        # Boo.. P has three outputs not one :-(
        S = parse(
            """
        --.--.--P--
          |  |  
        --+-----P--
             |  
        -----+--P--
        """, locals())

    S = P @ P @ P * B

    r2 = math.sqrt(2)
    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'))

    assert S * off == x0
    assert S * on == x1

    assert S == SHOR
Example #6
0
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
Example #7
0
def test_setitem():

    A = Qu((2, ) * 3, 'uuu')
    A[:] = bits('010')
Example #8
0
def test_swap():

    assert bits('0100').swap((1, 2)).decode() == '0010'
    assert bits('0101').swap((1, 2), (0, 3)).decode() == '1010'
Example #9
0
def test_gate_truth():

    # FLIP truth table (NOT gate)
    A = Gate.X
    assert (A * bits('0')).is_close(bits('1'))
    assert (A * bits('1')).is_close(bits('0'))

    # Identity @ FLIP truth table
    A = Gate.I @ Gate.X
    assert (A * bits('00')).is_close(bits('01'))
    assert (A * bits('01')).is_close(bits('00'))
    assert (A * bits('10')).is_close(bits('11'))
    assert (A * bits('11')).is_close(bits('10'))

    # Controlled-NOT truth table
    A = Gate.CN
    assert (A * bits('00')).is_close(bits('00'))
    assert (A * bits('01')).is_close(bits('01'))
    assert (A * bits('10')).is_close(bits('11'))
    assert (A * bits('11')).is_close(bits('10'))

    A = Gate.SWAP
    assert (A * bits('00')).is_close(bits('00'))
    assert (A * bits('01')).is_close(bits('10'))
    assert (A * bits('10')).is_close(bits('01'))
    assert (A * bits('11')).is_close(bits('11'))