Exemplo n.º 1
0
def _gen_hamiltonian_block(HL0, HR0, hgen_l, hgen_r, interop, blockinfo):
    '''Get the combined hamiltonian for specific block.'''
    ndiml, ndimr = HL0.shape[0], HR0.shape[0]
    bm_tot, pm = blockinfo['bmg'].join_bms(
        [blockinfo['bml'], blockinfo['bmr']]).compact_form()
    pm = ((blockinfo['pml'] * ndimr)[:, newaxis] +
          blockinfo['pmr']).ravel()[pm]
    indices = pm[bm_tot.get_slice(blockinfo['target_block'], uselabel=True)]
    cinds = ind2c(indices, N=[ndiml, ndimr])
    t0 = time.time()
    H1 = fget_subblock_dmrg(hl=HL0.toarray(),
                            hr=identity(ndimr),
                            indices=cinds,
                            is_identity=2)
    H2 = fget_subblock_dmrg(hl=identity(ndiml),
                            hr=HR0.toarray(),
                            indices=cinds,
                            is_identity=1)
    Hc = H1 + H2
    t1 = time.time()
    sb = SuperBlock(hgen_l, hgen_r)
    for op in interop:
        Hc = Hc + sb.get_op(op, indices=cinds)
    t2 = time.time()
    print 'Generate Hamiltonian %s, %s' % (t1 - t0, t2 - t1)
    return sps.csr_matrix(Hc), bm_tot, pm
Exemplo n.º 2
0
def _gen_hamiltonian_full(HL0, HR0, hgen_l, hgen_r, interop):
    '''Get the full hamiltonian.'''
    ndiml, ndimr = HL0.shape[0], HR0.shape[0]
    H1, H2 = kron(HL0, sps.identity(ndimr)), kron(sps.identity(ndiml), HR0)
    H = H1 + H2
    #get the link hamiltonians
    sb = SuperBlock(hgen_l, hgen_r)
    Hin = []
    for op in interop:
        Hin.append(sb.get_op(op))
    H = H + sum(Hin)
    H = _eliminate_zeros(H, ZERO_REF)
    return H
Exemplo n.º 3
0
	def left_sweep(self,m):
		'''sweep one site towards left'''
		self.lblocks.pop(-1)
		psi0_guess=np.reshape(self.sblock.full_psi0,(-1,self.rblocks[-2].D*self.d))
		psi0_guess=psi0_guess.dot(self.rblocks[-1].V.todense())
		psi0_guess=psi0_guess.reshape(self.lblocks[-1].D,-1)
		psi0_guess=self.lblocks[-1].U.todense().dot(psi0_guess)
		psi0_guess=psi0_guess.reshape(-1,1)
		self.lblocks.pop(-1)
		self.lhgen=deepcopy(self.lblocks[-1])
		self.rhgen=deepcopy(self.rblocks[-1])
		self.lhgen.enlarge()
		self.rhgen.enlarge()
		self.sblock=SuperBlock(self.lhgen,self.rhgen)
		E,psi=self.sblock.eigen(psi0_guess)
		print '='*(self.lblocks[-1].l)+'++'+'-'*(self.rblocks[-1].l)
		print 'E=',float(E)/self.L
		
		U=self.sblock.transmat(m);self.lhgen.U=U
		V=self.sblock.rtransmat(m);self.rhgen.V=V	
		self.lhgen.transform(U)
		self.rhgen.transform(V)
		self.lhgen.basis_sector_array=self.sblock.new_sector_array
		self.lhgen.basis_by_sector=self.sblock.new_basis_by_sector
		self.rhgen.basis_sector_array=self.sblock.rnew_sector_array
		self.rhgen.basis_by_sector=self.sblock.rnew_basis_by_sector
		self.lblocks.append(deepcopy(self.lhgen))
		self.rblocks.append(deepcopy(self.rhgen))
Exemplo n.º 4
0
def _gen_hamiltonian_block0(HL0, HR0, hgen_l, hgen_r, interop, blockinfo):
    '''Get the combined hamiltonian for specific block.'''
    ndiml, ndimr = HL0.shape[0], HR0.shape[0]
    bml, bmr, pml, pmr, bmg, target_block = blockinfo['bml'], blockinfo[
        'bmr'], blockinfo['pml'], blockinfo['pmr'], blockinfo[
            'bmg'], blockinfo['target_block']
    bm_tot, pm = bmg.join_bms([bml, bmr]).compact_form()
    pm = ((pml * len(pmr))[:, newaxis] + pmr).ravel()[pm]
    t0 = time.time()
    H1, H2 = kron(HL0, sps.identity(ndimr)), kron(sps.identity(ndiml), HR0)
    t1 = time.time()
    indices = pm[bm_tot.get_slice(target_block, uselabel=True)]
    H1, H2 = H1.tocsr()[indices][:, indices], H2.tocsr()[indices][:, indices]
    Hc = H1 + H2
    sb = SuperBlock(hgen_l, hgen_r)
    for op in interop:
        Hc = Hc + (sb.get_op(op)).tocsr()[indices][:, indices]
    t2 = time.time()
    print 'Generate Hamiltonian %s, %s' % (t1 - t0, t2 - t1)
    return Hc, bm_tot, pm
Exemplo n.º 5
0
    def createSuperBlock(self, fsimg, superBlockOffset, superBlockSize, mode,
                         data):
        superBlock = SuperBlock()

        if mode == 0:  # If we work locally
            superBlock.createFromBytes(fsimg, superBlockOffset, superBlockSize,
                                       mode, None)
            return superBlock
        elif mode == 1:  # If we work remotely
            superBlock.createFromBytes(None, None, None, mode, data)
            print superBlock.inodeCount
Exemplo n.º 6
0
 def __init__(self, disk='foo.img', *args, **kw):
     fuse.Fuse.__init__(self, *args, **kw)
     self.disk = disk
     with open(self.disk, 'rb') as f:
         f.seek(1024)
         data = f.read(SuperBlockSize)
         self.sb = SuperBlock(data)
         first_group_pos = max(2048, self.sb.get_block_size())
         f.seek(first_group_pos)
         data = f.read(BlockGroupSize)
         self.first_bg = BlockGroup(data)
         root_i_pos = (self.sb.get_block_size() * 
                       self.first_bg.get_inode_table() + 
                       InodeSize)
         f.seek(root_i_pos)
         data = f.read(InodeSize)
         self.root_i = Inode(data)
Exemplo n.º 7
0
	def single_step(self,m): #only for infinite
		'''single dmrg step.m:number of kept states'''
		self.lhgen.enlarge()
		self.rhgen.enlarge()
		self.sblock=SuperBlock(self.lhgen,self.rhgen,joint=True)
		E,psi=self.sblock.eigen()
		print '*'*(self.lhgen.l-1)+'++'+'*'*(self.rhgen.l-1)
		print 'E=',float(E)/self.sblock.L

		U=self.sblock.transmat(m);self.lhgen.U=U
		self.lhgen.transform(U)
		V=self.sblock.rtransmat(m);self.rhgen.V=V
		self.rhgen.transform(V)
		self.lhgen.basis_sector_array=self.sblock.new_sector_array
		self.lhgen.basis_by_sector=self.sblock.new_basis_by_sector
		self.rhgen.basis_sector_array=self.sblock.rnew_sector_array
		self.rhgen.basis_by_sector=self.sblock.rnew_basis_by_sector

		self.lblocks.append(deepcopy(self.lhgen))
		self.rblocks.append(deepcopy(self.rhgen))
Exemplo n.º 8
0
class Ext2(fuse.Fuse):
    def __init__(self, disk='foo.img', *args, **kw):
        fuse.Fuse.__init__(self, *args, **kw)
        self.disk = disk
        with open(self.disk, 'rb') as f:
            f.seek(1024)
            data = f.read(SuperBlockSize)
            self.sb = SuperBlock(data)
            first_group_pos = max(2048, self.sb.get_block_size())
            f.seek(first_group_pos)
            data = f.read(BlockGroupSize)
            self.first_bg = BlockGroup(data)
            root_i_pos = (self.sb.get_block_size() * 
                          self.first_bg.get_inode_table() + 
                          InodeSize)
            f.seek(root_i_pos)
            data = f.read(InodeSize)
            self.root_i = Inode(data)
    def find_inode(self, path):
        if path == '/':
            return self.root_i
        dirs = path.split('/')[1:]
        with open(self.disk, 'rb') as f:
            # TODO: deal with multiple-blocks directories
            next_de_pos = (self.root_i.get_blocks()[0] * 
                           self.sb.get_block_size())
            while dirs != []:
                f.seek(next_de_pos)
                data = f.read(DirEntrySize)
                de = DirEntry(data)
                if de.get_name_length() == 0:
                    raise NotFound("File %s not found" % path)
                if de.get_name() == dirs[0]:
                    dirs.pop()
                    inode_pos = (self.sb.get_block_size() * 
                                 self.first_bg.get_inode_table() +
                                 (de.get_inode()-1) * InodeSize)
                    f.seek(inode_pos)
                    data = f.read(InodeSize)
                    i = Inode(data)
                    if dirs == []: # we found it
                        return i
                    if i.is_file():
                        raise NotFound("%s is a file, not a directory" % 
                                       de.get_name())
                    # TODO
                    next_de_pos = (i.get_blocks()[0] *
                                   self.sb.get_block_size())
                next_de_pos += de.get_length()
        raise NotFound("Invalid path: %s" % path)

    def getattr(self, path):
        inode = self.find_inode(path)
        stat = fuse.Stat()
        stat.st_mode = inode.get_mode()
        stat.st_atime = inode.get_atime()
        stat.st_mtime = inode.get_mtime()
        stat.st_ctime = inode.get_ctime()
        stat.st_uid = inode.get_uid()
        stat.st_gid = inode.get_gid()
        stat.st_size = inode.get_size()
        stat.st_nlink = inode.get_links_count()
        stat.st_ino = 0 # TODO
        stat.st_dev = 0

        return stat

    def readdir(self, path, offset):
        inode = self.find_inode(path)
        with open(self.disk, 'rb') as f:
            next_de_pos = (inode.get_blocks()[0] *
                           self.sb.get_block_size())
            while True:
                f.seek(next_de_pos)
                data = f.read(DirEntrySize)
                de = DirEntry(data)
                if de.get_name_length() == 0:
                    break
                yield fuse.Direntry(de.get_name())
                next_de_pos += de.get_length()
        return
    def mknod(self, path, mode, dev):
        return 0
  
    def unlink(self, path):
        return 0
  
    def read(self, path, size, offset):
        blocks_read = 0
        inode = self.find_inode(path)
        first_block = offset/self.sb.get_block_size()
        data = ''
        with open(self.disk, 'rb') as f:
            all_blocks = (inode.get_blocks() +
                          inode.get_indirect_blocks(f, self.sb.get_block_size(), 1))
            for block in all_blocks[first_block:]:
                if block != 0:
                    blocks_read += 1
                    if blocks_read >= MaxBlocksRead:
                        # we can't give too much data at once
                        return data
                    block_pos = (block * self.sb.get_block_size())
                    f.seek(block_pos)
                    data += f.read(self.sb.get_block_size())
        return data

    def write(self, path, buf, offset):
        return 0
  
    def release(self, path, flags):
        return 0
  
    def open(self, path, flags):
        return 0
  
    def truncate(self, path, size):
        return 0
  
    def utime(self, path, times):
        return 0
  
    def mkdir(self, path, mode):
        return 0
  
    def rmdir(self, path):
        return 0
  
    def rename(self, pathfrom, pathto):
        return 0
  
    def fsync(self, path, isfsyncfile):
        return 0
Exemplo n.º 9
0
class DMRGEngine(object):
	'''
	dmrg engine
	construct:DMRGEngine(lhgen,rhgen)
	
	attributes:
	lhgen:left hamiltonian generator
	rhgen:right hamiltonian generator
	N:length of whole chain
	lblocks:a list to store left generators
	rblocks:a list to store right generators
	sblock:super block	
	'''
	def __init__(self,lhgen,rhgen):
		self.lhgen=lhgen
		self.rhgen=rhgen
		self.L=self.rhgen.L
		self.lblocks=[deepcopy(self.lhgen)]
		self.rblocks=[deepcopy(self.rhgen)]
		self.d=self.lhgen.d
	
	def single_step(self,m): #only for infinite
		'''single dmrg step.m:number of kept states'''
		self.lhgen.enlarge()
		self.rhgen.enlarge()
		self.sblock=SuperBlock(self.lhgen,self.rhgen,joint=True)
		E,psi=self.sblock.eigen()
		print '*'*(self.lhgen.l-1)+'++'+'*'*(self.rhgen.l-1)
		print 'E=',float(E)/self.sblock.L

		U=self.sblock.transmat(m);self.lhgen.U=U
		self.lhgen.transform(U)
		V=self.sblock.rtransmat(m);self.rhgen.V=V
		self.rhgen.transform(V)
		self.lhgen.basis_sector_array=self.sblock.new_sector_array
		self.lhgen.basis_by_sector=self.sblock.new_basis_by_sector
		self.rhgen.basis_sector_array=self.sblock.rnew_sector_array
		self.rhgen.basis_by_sector=self.sblock.rnew_basis_by_sector

		self.lblocks.append(deepcopy(self.lhgen))
		self.rblocks.append(deepcopy(self.rhgen))

	def infinite(self,m):
		'''infinite algorithm'''
		for i in range(self.L/2-1):
			self.single_step(m)
	
	def right_sweep(self,m):
		'''sweep one site towards right'''
		self.rblocks.pop(-1)
		psi0_guess=np.reshape(self.sblock.full_psi0,(self.lblocks[-2].D*self.d,-1))
		psi0_guess=self.lblocks[-1].U.conjugate().transpose().todense().dot(psi0_guess)
		psi0_guess=psi0_guess.reshape(-1,self.rblocks[-1].D)
		psi0_guess=psi0_guess.dot(self.rblocks[-1].V.conjugate().transpose().todense())
		psi0_guess=psi0_guess.reshape(-1,1)
		self.rblocks.pop(-1)
		self.lhgen=deepcopy(self.lblocks[-1])
		self.rhgen=deepcopy(self.rblocks[-1])
		self.lhgen.enlarge();self.rhgen.enlarge()
		self.sblock=SuperBlock(self.lhgen,self.rhgen)
		E,psi=self.sblock.eigen(psi0_guess)
		print '-'*(self.lblocks[-1].l)+'++'+'='*(self.rblocks[-1].l)
		print 'E=',float(E)/self.L
		
		U=self.sblock.transmat(m);self.lhgen.U=U
		V=self.sblock.rtransmat(m);self.rhgen.V=V
		self.lhgen.transform(U)
		self.rhgen.transform(V)
		self.lhgen.basis_sector_array=self.sblock.new_sector_array
		self.lhgen.basis_by_sector=self.sblock.new_basis_by_sector
		self.rhgen.basis_sector_array=self.sblock.rnew_sector_array
		self.rhgen.basis_by_sector=self.sblock.rnew_basis_by_sector
		self.lblocks.append(deepcopy(self.lhgen))
		self.rblocks.append(deepcopy(self.rhgen))

	def left_sweep(self,m):
		'''sweep one site towards left'''
		self.lblocks.pop(-1)
		psi0_guess=np.reshape(self.sblock.full_psi0,(-1,self.rblocks[-2].D*self.d))
		psi0_guess=psi0_guess.dot(self.rblocks[-1].V.todense())
		psi0_guess=psi0_guess.reshape(self.lblocks[-1].D,-1)
		psi0_guess=self.lblocks[-1].U.todense().dot(psi0_guess)
		psi0_guess=psi0_guess.reshape(-1,1)
		self.lblocks.pop(-1)
		self.lhgen=deepcopy(self.lblocks[-1])
		self.rhgen=deepcopy(self.rblocks[-1])
		self.lhgen.enlarge()
		self.rhgen.enlarge()
		self.sblock=SuperBlock(self.lhgen,self.rhgen)
		E,psi=self.sblock.eigen(psi0_guess)
		print '='*(self.lblocks[-1].l)+'++'+'-'*(self.rblocks[-1].l)
		print 'E=',float(E)/self.L
		
		U=self.sblock.transmat(m);self.lhgen.U=U
		V=self.sblock.rtransmat(m);self.rhgen.V=V	
		self.lhgen.transform(U)
		self.rhgen.transform(V)
		self.lhgen.basis_sector_array=self.sblock.new_sector_array
		self.lhgen.basis_by_sector=self.sblock.new_basis_by_sector
		self.rhgen.basis_sector_array=self.sblock.rnew_sector_array
		self.rhgen.basis_by_sector=self.sblock.rnew_basis_by_sector
		self.lblocks.append(deepcopy(self.lhgen))
		self.rblocks.append(deepcopy(self.rhgen))

	def finite(self,mwarmup,mlist):
		'''finite algorithm'''
		self.infinite(mwarmup) #mind the initialize problem
		for m in mlist:
			for i in range(self.L/2,self.L-2):
				self.right_sweep(m)
			for i in range(1,self.L-3):
				self.left_sweep(m)
			for i in range(1,self.L/2-1):
				self.right_sweep(m)
		
	def tomps(self):
		As=[np.array([[1.,0],[0,1.]])]
		As[0]=As[0].reshape(-1,2,As[0].shape[-1])
		Bs=[np.array([[1.,0],[0,1.]])]
		Bs[0]=Bs[0].reshape(Bs[0].shape[0],2,-1)
		for lhgen in self.lblocks[1:]:
			A=copy(lhgen.U)	
			A=A.toarray()
			A=A.reshape(-1,2,A.shape[-1])
			As.append(A)
		for rhgen in self.rblocks[1:]:
			B=copy(rhgen.V)
			B=B.toarray()
			B=B.conjugate().transpose()
			B=B.reshape(B.shape[0],2,-1)
			Bs.append(B)
		Bs.reverse()
		S=self.sblock.s
		
		return MPS(self.d,self.L,As,Bs,S)