def test_get_name(self): u1 = User.objects.get(pk=1) u2 = User.objects.get(pk=2) self.assertEqual(get_name(u1), 'superuser') self.assertEqual(get_name(u2), 'Jim Bob') self.assertEqual(u1.get_name(), 'superuser') self.assertEqual(u2.get_name(), 'Jim Bob')
def load_mps(alpha,Delta,Vz,mu,U,target_block,nsite,maxN=None,fixtail=True): ''' Load Specific Matrix product state. Parameters: :which: tuple of dmu, target_block. * target_block, int, '+', '-'. Return: eng,mps ''' suf=get_name(Vz,Delta,mu,alpha,nsite,periodic=False,resonance=False) spaceconfig1=SuperSpaceConfig(chorder([1,2,1,1])) suffix0=suf+'_%s'%target_block filename_mps0='data/mps_%s.dat'%suffix0 filename_eng0='data/eng_%s.dat'%suffix0 tblocks=(0,1) def get1(target_blocki): suffix=suf+'_%s'%target_blocki filename_mps='data/mps_%s.dat'%suffix filename_eng='data/eng_%s.dat'%suffix mps=MPS.load(filename_mps) eng=loadtxt(filename_eng) if target_blocki==1 and fixtail: mps=fix_tail(mps,spaceconfig=spaceconfig1,parity=-1) return eng,mps if os.path.isfile(filename_mps0): eng,mps=get1(target_block) if maxN is not None: mps.compress(maxN=maxN) return eng,mps elif isinstance(target_block,str): eng,mps1=get1(tblocks[0]) eng,mps2=get1(tblocks[1]) if maxN is not None: mps1.compress(maxN=int(maxN/sqrt(2))) mps2.compress(maxN=int(maxN/sqrt(2))) if target_block=='+': mps=1./sqrt(2.)*(mps1+mps2) elif target_block=='-': mps=1./sqrt(2.)*(mps1-mps2) elif target_block=='+i': mps=1./sqrt(2.)*(mps1+1j*mps2) elif target_block=='-i': mps=1./sqrt(2.)*(mps1-1j*mps2) else: raise ValueError('') if maxN is None: mps.recanonicalize() else: mps.compress(maxN=maxN) else: raise Exception('File %s do not exist!'%filename_mps0) if not os.path.isfile(filename_mps0): mps.save(filename_mps0) savetxt(filename_eng0,[eng]) return eng,mps
def mf_wave(J,nsite,h=0.): '''Get the wave function for majorana fermion.''' eng_even,ket_even=load_mps(target_block=(0,0),h=h,J=J,nsite=nsite) eng_odd,ket_odd=load_mps(target_block=(1,0),h=h,J=J,nsite=nsite) mjfile='data/mf_%s.npy'%get_name(h=h,J=J,nsite=nsite) spaceconfig1=SpinSpaceConfig([2,1]) pls=smajorana4mps(spaceconfig=spaceconfig1,ket_even=ket_even,ket_odd=ket_odd,\ theta=0,ordering=arange(nsite)[::-1]) if RANK==0: pls=array(pls) save(mjfile,pls) return pls
def check_orthogonality(h,J,nsite,maxN=Inf): mps1=load_mps(h=h,J=J,nsite=nsite,target_block=0,maxN=maxN)[1] mps2=load_mps(h=h,J=J,nsite=nsite,target_block=1,maxN=maxN)[1] overlap=(mps1.tobra(mps1.labels)*mps2)[0,0] print 'Overlap = %s'%overlap print 'Press `C` to orthogonalize!' pdb.set_trace() suf=get_name(h=h,J=J,nsite=nsite) suffix0='%s_%s'%(suf,1) filename_mps0='data/mps_%s.dat'%suffix0 mps3=(mps2-overlap*mps1) mps3.recanonicalize(maxN=maxN) mps3>>mps2.l-mps3.l mps3=mps3/sqrt(1-abs(overlap)**2) mps3.save(filename_mps0)
def scan_nonlocality(J,h,nsite,angles1,angles2,maxN=140,append=False,token=''): ''' Scan the non locality on a block sphere. ''' sites=arange(nsite+1) suffix=get_name(J=J,h=h,nsite=nsite) ffile='data/scan_fidelity_%s_s%s%s.dat'%(suffix,len(angles1),token) def measure1(angles): (theta1,phi1),(theta2,phi2)=angles fs=bulk_edge_fidelity(J=J,h=h,nsite=nsite,target_blocks=({'theta':theta1,'phi':phi1},{'theta':theta2,'phi':phi2}),direction='->',maxN=maxN,append=append) y=fs[nsite/2] return y ml=mpido(measure1,inputlist=zip(angles1,angles2)) print 'Fin. ml = %s'%ml if RANK==0: savetxt(ffile,ml)
def measure_order(which,delta,t,mu,nsite,polarity,siteindices=None,usempi=True,anchor=None,**kwargs): ''' Get order parameter from ground states. Parameters: :which: str, * 'RHO', <N(i)> :nsite: int, the number of sites. :siteindices: list, the measure points. Return: number, the value of order parameter. ''' if anchor is None:anchor=nsite/2 if siteindices is None: siteindices=arange(nsite) siteindices=array(siteindices) spaceconfig=SuperSpaceConfig(chorder([1,1,1])) suffix=get_name(t=t,mu=mu,delta=delta,nsite=nsite) ofile='data/%s_%s_%s.dat'%(which,suffix,polarity) filename='data/mps_%s.dat'%suffix eng,ket=load_mps(t=t,mu=mu,delta=delta,nsite=nsite,polarity=polarity,maxN=200,fixtail=True) ml=[] ordering=arange(nsite)[::-1] def measure1(siteindex): if which=='RHO': op=opunit_N(spaceconfig,index=None,siteindex=siteindex) else: raise NotImplementedError() if isinstance(op,OpCollection): m=sum([get_expect(opi,ket=ket) for opi in op.ops]) else: m=get_expect(op,ket=ket)#,bra=ket.tobra(labels=ket.labels)) print 'Get %s for site %s = %s @RANK: %s'%(which,siteindex,m.item(),RANK) return m if usempi: ml=mpido(measure1,inputlist=siteindices) else: ml=[] for siteindex in siteindices: ml.append(measure1(siteindex)) ml=array(ml) if RANK==0: savetxt(ofile,ml.real) return ml
def measure_order(which,J,h,nsite,target_block,siteindices=None,usempi=True,anchor=None,**kwargs): ''' Get order parameter from ground states. Parameters: :which: str, * 'Sz', <Sz> :nsite: int, the number of sites. :siteindices: list, the measure points. Return: number, the value of order parameter. ''' if anchor is None:anchor=nsite/2 if siteindices is None: siteindices=arange(nsite) siteindices=array(siteindices) spaceconfig=SpinSpaceConfig([2,1]) suffix=get_name(h=h,J=J,nsite=nsite) ofile='data/%s_%s.dat'%(which,suffix) filename='data/mps_%s.dat'%suffix eng,ket=load_mps(h=h,J=J,nsite=nsite,target_block=target_block,maxN=200) ml=[] ordering=arange(nsite)[::-1] def measure1(siteindex): if which=='Sz': op=opunit_S(which='z',siteindex=siteindex,spaceconfig=spaceconfig) else: raise NotImplementedError() if isinstance(op,OpCollection): m=sum([get_expect(opi,ket=ket) for opi in op.ops]) else: m=get_expect(op,ket=ket)#,bra=ket.tobra(labels=ket.labels)) print 'Get %s for site %s = %s @RANK: %s'%(which,siteindex,m.item(),RANK) return m if usempi: ml=mpido(measure1,inputlist=siteindices) else: ml=[] for siteindex in siteindices: ml.append(measure1(siteindex)) ml=array(ml) if RANK==0: savetxt(ofile,ml.real) return ml
def show_mf_wave(J,nsite,h=0.): mjfile='data/mf_%s.npy'%get_name(h=h,J=J,nsite=nsite) pls=load(mjfile) ampl=(pls[:2]).real print 'Magnituede %s'%sum(ampl,axis=1) pdb.set_trace() ion() lw=2 lc='r' nsite=pls.shape[1] for n in xrange(2): pln=ampl[n] ax=subplot(121+n) lc=LineCollection([[(i,0),(i,pln[i].item())] for i in xrange(nsite)]) lc.set_linewidth(lw) ax.add_collection(lc) ax.autoscale() ax.margins(0.1) pdb.set_trace()
def bulk_edge_fidelity(Vz,Delta,mu,alpha,U,nsite,target_blocks,direction='->',maxN=55,append=False,usvmode=False,**kwargs): ''' Measure the fidelity of states from bulk to edge. ''' dr={'->':'r','<-':'l','<->':'m%s%s'%(maxN,'T' if usvmode else 'F')}[direction] suffix=get_name(Vz,Delta,mu,alpha,nsite,periodic=False,resonance=False)+'_%s%s'%(target_blocks[0],target_blocks[1]) ffile='data/fidelity_%s_%s.dat'%(suffix,dr) if append: fs=loadtxt(ffile) else: kets=[load_mps(alpha=alpha,Vz=Vz,U=U,mu=mu,nsite=nsite,target_block=target_block,Delta=Delta,maxN=maxN,fixtail=True)[1] for target_block in target_blocks] if direction=='<->': fs=sweep_fidelity2(kets=kets,maxN=maxN,usvmode=usvmode) else: fs=sweep_fidelity(kets=kets,direction=direction) savetxt(ffile,fs) print 'FIN' if not ONSV: ion() plot(fs) pdb.set_trace()
def bulk_edge_fidelity(t,mu,delta,nsite,polarities,direction='->',maxN=55,append=False,usvmode=False,**kwargs): ''' Measure the fidelity of states from bulk to edge. ''' dr={'->':'r','<-':'l','<->':'m%s%s'%(maxN,'T' if usvmode else 'F')}[direction] suffix=get_name(t=t,mu=mu,delta=delta,nsite=nsite)+'_%s%s'%(polarities[0],polarities[1]) ffile='data/fidelity_%s_%s.dat'%(suffix,dr) print 'Running %s Fidelity evolusion between polarities=%s'%(direction,polarities) if append: fs=loadtxt(ffile) else: kets=[load_mps(t=t,mu=mu,delta=delta,nsite=nsite,polarity=polarity,maxN=maxN,fixtail=True)[1] for polarity in polarities] if direction=='<->': fs=sweep_fidelity2(kets=kets,maxN=maxN,usvmode=usvmode) else: fs=sweep_fidelity(kets=kets,direction=direction) savetxt(ffile,fs) print 'FIN' if not ONSV: ion() plot(fs) pdb.set_trace() return fs
def bulk_edge_fidelity(h,J,nsite,target_blocks,direction='->',maxN=55,append=False,usvmode=False,**kwargs): ''' Measure the fidelity of states from bulk to edge. ''' dr={'->':'r','<-':'l','<->':'m%s%s'%(maxN,'T' if usvmode else 'F')}[direction] suffix=get_name(h=h,J=J,nsite=nsite)+'_%s%s'%(target_blocks[0],target_blocks[1]) ffile='data/fidelity_%s_%s.dat'%(suffix,dr) print 'Running %s Fidelity evolusion between target_blocks=%s'%(direction,target_blocks) if append: fs=loadtxt(ffile) else: kets=[load_mps(h=h,J=J,nsite=nsite,target_block=target_block,maxN=maxN)[1] for target_block in target_blocks] if direction=='<->': fs=sweep_fidelity2(kets=kets,maxN=maxN,usvmode=usvmode) else: fs=sweep_fidelity(kets=kets,direction=direction) savetxt(ffile,fs) print 'FIN' if not ONSV: ion() plot(fs) pdb.set_trace() return fs
def load_mps(t,mu,delta,polarity,nsite,maxN=None,fixtail=True): ''' Load Specific Matrix product state. Parameters: :which: tuple of dmu, polarity. * polarity, int, '+', '-'. Return: eng,mps ''' suf=get_name(t=t,mu=mu,delta=delta,nsite=nsite) spaceconfig1=SuperSpaceConfig(chorder([1,1,1,1])) suffix0='%s_%s'%(suf,polarity) filename_mps0='data/mps_%s.dat'%suffix0 filename_eng0='data/eng_%s.dat'%suffix0 tblocks=('l','r') def get1(polarityi): suffix='%s_%s'%(suf,polarityi) filename_mps='data/mps_%s.dat'%suffix filename_eng='data/eng_%s.dat'%suffix mps=MPS.load(filename_mps) eng=loadtxt(filename_eng) if polarityi==(1,0) and fixtail: mps=fix_tail(mps,spaceconfig=spaceconfig1,parity=-1) return eng,mps if os.path.isfile(filename_mps0): eng,mps=get1(polarity) if maxN is not None: mps.compress(maxN=maxN) return eng,mps elif isinstance(polarity,str): eng,mps1=get1(tblocks[0]) eng,mps2=get1(tblocks[1]) if maxN is not None and maxN is not Inf: mps1.compress(maxN=int(maxN/sqrt(2))) mps2.compress(maxN=int(maxN/sqrt(2))) if polarity=='+i': mps=1./sqrt(2.)*(mps1+1j*mps2) elif polarity=='-i': mps=1./sqrt(2.)*(mps1-1j*mps2) elif polarity=='+': mps=1./sqrt(2.)*(mps1+mps2) elif polarity=='-': mps=1./sqrt(2.)*(mps1-mps2) elif polarity=='l': mps=mps1 elif polarity=='r': mps=mps2 else: raise ValueError('') mps.recanonicalize() if maxN is not None: mps.compress(maxN=maxN) else: raise Exception('File %s do not exist!'%filename_mps0) if not os.path.isfile(filename_mps0): mps.save(filename_mps0) savetxt(filename_eng0,[eng]) return eng,mps
def load_mps(h,J,target_block,nsite,maxN=None): ''' Load Specific Matrix product state. Parameters: :which: tuple of dh, target_block. * target_block, int, '+', '-'. Return: eng,mps ''' spaceconfig1=SpinSpaceConfig([2,1]) suf=get_name(h=h,J=J,nsite=nsite) suffix0='%s_%s'%(suf,target_block) filename_mps0='data/mps_%s.dat'%suffix0 filename_eng0='data/eng_%s.dat'%suffix0 tblocks=('0','1') def get1(target_blocki): suffix='%s_%s'%(suf,target_blocki) filename_mps='data/mps_%s.dat'%suffix filename_eng='data/eng_%s.dat'%suffix mps=MPS.load(filename_mps) eng=loadtxt(filename_eng) return eng,mps if os.path.isfile(filename_mps0): eng,mps=get1(target_block) if maxN is not None: mps.compress(maxN=maxN) return eng,mps elif isinstance(target_block,str): eng,mps1=get1(tblocks[0]) eng,mps2=get1(tblocks[1]) if maxN is not None: mps1.compress(maxN=int(maxN/sqrt(2))) mps2.compress(maxN=int(maxN/sqrt(2))) if target_block=='+i': mps=1./sqrt(2.)*(mps1+1j*mps2) elif target_block=='-i': mps=1./sqrt(2.)*(mps1-1j*mps2) elif target_block=='+': mps=1./sqrt(2.)*(mps1+mps2) elif target_block=='-': mps=1./sqrt(2.)*(mps1-mps2) elif target_block=='0': mps=mps1 elif target_block=='1': mps=mps2 else: raise ValueError('') mps.recanonicalize() if maxN is not None: mps.compress(maxN=maxN) elif isinstance(target_block,dict): eng,mps1=get1(tblocks[0]) eng,mps2=get1(tblocks[1]) if maxN is not None and maxN is not Inf: mps1.compress(maxN=int(maxN/sqrt(2))) mps2.compress(maxN=int(maxN/sqrt(2))) theta,phi=target_block['theta'],target_block['phi'] print theta,pi mps=cos(theta/2.)*mps1+sin(theta/2.)*exp(1j*phi)*mps2 mps.recanonicalize() if maxN is not None: mps.compress(maxN=maxN) else: raise Exception('File %s do not exist!'%filename_mps0) if not os.path.isfile(filename_mps0) and not isinstance(target_block,dict): mps.save(filename_mps0) savetxt(filename_eng0,[eng]) return eng,mps