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)
def test_flatten(): shape = (2, 3, 4) valence = 'uuu' A = Qu.random(shape, valence) op = A.get_flatop() B = op.do(A) assert B.shape == (2 * 3 * 4, ), B.shape assert B.valence == 'u' shape = (2, 3) valence = 'ud' a = Qu.random(shape, valence) op = a.get_flatop() assert op.do(a).is_close(a) shape = (2, 3, 4, 5) valence = "udud" a = Qu.random(shape, valence) op = a.get_flatop() b = op.do(a) c = op.undo(b) assert c.is_close(a)
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
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
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()
def test_transpose(): a = Qu.random(2, 'u') r = ~a * a assert is_close(r, a.norm()**2) v = a.v[:] v.shape = v.shape + (1, ) A = Qu(v.shape, 'ud', v) r = ~A * A assert is_close(r[0, 0], a.norm()**2)
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()
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)
def main(): t = argv.get("t", 0.02) T = build(""" XZZXI IXZZX XIXZZ ZXIXZ XXXXX """) n = 5 stabs = [ X@Z@Z@X@I, I@X@Z@Z@X, X@I@X@Z@Z, Z@X@I@X@Z] Lx = X@X@X@X@X #Lz = Z@Z@Z@Z@Z Lz = T[n-1] ops = stabs + [Lx] for i in range(n): for j in range(n): c = (T[i]*ops[j] == ops[j]*T[i]) assert c == (i!=j) a = (T[i]*ops[j] == -ops[j]*T[i]) assert a == (i==j) G = mulclose(stabs) assert len(G) == 16 N = len(G) P = (1./N) * sumops(G) assert P*P == P for op in stabs: assert Lx*op == op*Lx assert Lz*op == op*Lz trials = argv.get("trials", 100) for trial in range(trials): if 1: # noise on one qubit U = errop(t) U = U@I@I@I@I else: # noise on all qubits Us = [] for i in range(n): Us.append(errop(t)) U = reduce(matmul, Us) v = Qu.random((2,)*n, "u"*n) v = P*v v.normalize() #print(v.flatstr()) u = U*v u.normalize() for i, op in enumerate(stabs): val, u = measure(op, u) print(fstr(val), end=" ") if val < 0: #print("*") u = T[i]*u #print() #for i, op in enumerate(stabs): #val, v = measure(op, v) #print(fstr(val), end=" ") #print() assert P*u == u assert P*v == v phase = None for uu in [u, Lx*u, Lz*u, Lx*Lz*u]: r = ~uu * v if abs(1. - abs(r)) < EPSILON: #write("+") phase = r #else: #write(".") #write("\n") if phase is not None: print(phase) else: print()