def CONTROLCONTROL(u): u = astensor(u) assert (u.shape[0] == u.shape[1]) # assert(u.shape==(2,2)) Iu = eye(u.shape[0]) return kron(_UP, kron(_UP, Iu)) + kron(_UP, kron(_DOWN, Iu)) + kron( _DOWN, kron(_UP, Iu)) + kron(_DOWN, kron(_DOWN, u))
def __get_normal_order(self, key, op): l1, l2 = key # assert(l1 != l2) if l1 == l2: print('wrong position for two body gate.') if isinstance(op, tuple): # assert(len(op)==2 and op[0].rank==3 and op[1].rank==3) # assert((l1[0] == l2[0] and l1[1]+1==l2[1]) or (l1[0]+1==l2[0] and l1[1]==l2[1])) if not (len(op) == 2 and op[0].rank == 3 and op[1].rank == 3): raise ValueError('wrong input op for two body gate.') if not ((l1[0] == l2[0] and l1[1] + 1 == l2[1]) or (l1[0] + 1 == l2[0] and l1[1] == l2[1])): raise ValueError('wrong position for two body gate.') else: op = astensor(op) # assert(len(l1)==2 and len(l2)==2) if not (len(l1) == 2 and len(l2) == 2): raise ValueError('wrong position for two body gate.') if op.rank == 2: # assert(op.shape==(4,4) or op.shape==(16,16)) if op.shape != (4, 4): raise ValueError('wrong op shape for two body gate.') s = int(ssqrt(op.shape[0])) op = op.reshape((s, s, s, s)) else: # assert(op.rank==4 and (op.shape==(2,2,2,2) or op.shape==(4,4,4,4))) if op.shape != (2, 2, 2, 2): raise ValueError('wrong op shape for two body gate.') if ((l1[0] == l2[0] and l1[1] == l2[1] + 1) or (l1[0] == l2[0] + 1 and l1[1] == l2[1])): op = op.transpose((1, 0, 3, 2)) key = (l2, l1) op = split_bondmpo(op) return key, op
def apply(self, state, maxbonddimension=2000, svdcutoff=1.0e-8, verbose=1): up = astensor([[1., 0.], [0., 0.]]) down = astensor([[0., 0.], [0., 1.]]) up_gate = OneBodyGate(self.key, up) ms = close_peps(state, state, {self.key: up}) s = brute_force_1(ms) tol = 1.0e-6 if s.imag > tol: warnings.warn('the imaginary part of result is larger than' + str(tol)) s = abs(s.real) l = [s, abs(1 - s)] i = _discrete_sample(l) # print('probability is', s, 'istate is', i) if i == 0: state[self.key] = up.contract(state[self.key], ((1, ), (0, ))) state[self.key] /= sqrt(l[i]) else: state[self.key] = down.contract(state[self.key], ((1, ), (0, ))) state[self.key] /= sqrt(l[i]) self.istate = i self.probability = l[i]
def generateProdPEPS(shape, ds, mpsstr): # assert(len(ds) == len(mpsstr)) # assert(len(ds) == shape[0]) if not (len(ds) == len(mpsstr) and len(ds) == shape[0]): raise ValueError('input shape mismatch.') for i in range(shape[0]): # assert(len(ds[i]) == shape[1]) # assert(len(mpsstr[i]) == shape[1]) if not (len(ds[i]) == shape[1] and len(mpsstr[i]) == shape[1]): raise ValueError('input shape mismatch.') peps = PEPS(shape) for i in range(shape[0]): for j in range(shape[1]): d = ds[i][j] peps[(i, j)] = zeros((d, 1, 1, 1, 1)) if hasattr(mpsstr[i][j], '__iter__'): # peps[(i,j)].axis([1,2,3,4],[0,0,0,0]).assign(astensor(mpsstr[i][j])) peps[(i, j)][:, 0, 0, 0, 0] = astensor(mpsstr[i][j]) else: peps[(i, j)][mpsstr[i][j], 0, 0, 0, 0] = 1. return peps
def __setitem__(self, key, value): self.data[self.__sgi(key)] = astensor(value)
def Rz(theta): theta = 2. * pi * theta return astensor([[1., 0.], [0., exp(1j * theta)]])
def CONTROL(u): # assert(u.shape==(2,2)) u = astensor(u) assert (u.shape[0] == u.shape[1]) return kron(_UP, eye(u.shape[0])) + kron(_DOWN, u)
def Ry(theta): theta = pi * theta / 2 return astensor([[cos(theta), -sin(theta)], [sin(theta), cos(theta)]])
def Rx(theta): theta = pi * theta / 2 return astensor([[cos(theta), -1j * sin(theta)], [-1j * sin(theta), cos(theta)]])
def ROTATION(theta): theta = 2. * pi * theta return astensor([[cos(theta), sin(theta)], [sin(theta), -cos(theta)]])
def R(k): return astensor([[1., 0.], [0., exp(pi * 1j / (2.**(k - 1)))]])
# -*- coding: utf-8 -*- # @Author: guochu # @Date: 2018-08-26 15:17:23 # @Last Modified by: guochu # @Last Modified time: 2019-01-05 11:44:29 from numpy import diag, pi, exp, sin, cos, sqrt, kron from rqc.tensor import eye, astensor # one body gates X = astensor([[0., 1.], [1., 0.]]) Y = astensor([[0., -1j], [1j, 0.]]) Z = astensor([[1., 0.], [0., -1.]]) S = astensor([[1., 0.], [0., 1j]]) _I = eye(2) H = (X + Z) / sqrt(2) _UP = astensor([[1., 0.], [0., 0.]]) _DOWN = astensor([[0., 0.], [0., 1.]]) def R(k): return astensor([[1., 0.], [0., exp(pi * 1j / (2.**(k - 1)))]]) def ROTATION(theta): theta = 2. * pi * theta return astensor([[cos(theta), sin(theta)], [sin(theta), -cos(theta)]]) def Rx(theta):