我已經使用numpy和scipy庫將一段MatLab代碼轉換爲Python。然而,我堅持以下索引錯誤;索引錯誤 - Python,Numpy,MatLab
IndexError: index 698 is out of bounds for axis 3 with size 2
698是時間表的大小。
它發生在代碼的最後部分,在這條線;
exp_Pes[indx,jndx,j,i]=np.trace(rhotemp[:,:,indx,jndx] * Pe)
其餘的內容是爲了完整而包括在內。
以下是代碼;
import numpy as np
import math
import time
#from scipy.linalg import expm
tic = time.clock()
dt = 1e-2; # time step
t = np.arange(0, 7-dt, dt) # t
gam=1
mt=2
nt=2
delta=1
ensemble_size=6
RKflag=0
itype = 'em'
#========================================================================
def _min(x):
return [np.amin(x), np.argmin(x)]
#========================================================================
###############
#Wavepacket envelope
#rising exponential
sig1 = 1;
xi_t = np.zeros(len(t));
[min_dif, array_pos_start] = _min(abs(t -t[0]));
[min_dif, array_pos_stop ] = _min(abs(t -t[-1]/2.0));
step = t[1]-t[0]
p_len = np.arange(0, t[array_pos_stop] - t[array_pos_start] + step, step)
xi_t[array_pos_start:array_pos_stop+1] = math.sqrt(sig1) * np.exp((sig1/2.0)*p_len);
norm = np.trapz(t, xi_t * np.conj(xi_t));
xi = xi_t/np.sqrt(abs(norm));
#Pauli matrices
#========================
Pe=np.array([[1,0],[0,0]])
Sm=np.array([[0,0],[1,0]])
Sz=np.array([[1,0],[0,- 1]])
Sy=np.array([[0,- 1j],[1j,0]])
Sx=np.array([[0,1],[1,0]])
#S,L,H coefficients
#=========================
S=np.eye(2)
L=np.sqrt(gam) * Sm
H=np.eye(2)
#=========================
psi=np.array([[0],[1]])
rhoi=psi * psi.T
#rho=np.zeros(2,2,2,2)
rho=np.zeros((2,2,2,2))
rhotot=np.copy(rho)
rhoblank=np.copy(rho)
rhos=np.copy(rho)
rhotots=np.copy(rho)
rhon=np.copy(rho)
rhototN=np.copy(rho)
rhov=np.copy(rhoi)
exp_Pes=np.zeros((2,2,2,2))
exp_Pes2=np.zeros((2,2,2,2))
#initial conditions into rho
#==================
rho[:,:,0,0]=rhoi
#rho[:,:,2,2]=rhoi
rhosi=np.copy(rho)
num_bad=0
avg_val=0
num_badRK=0
avg_valRK=0
#np.zeros(len(t));
exp_Sz=np.zeros((2,2,len(t)))
exp_Sy=np.copy(exp_Sz)
exp_Sx=np.copy(exp_Sz)
exp_Pe=np.copy(exp_Sz)
##########################3
#functions
#========================================================================
#D[X]rho = X*rho* ctranspose(X) -0.5*(ctranspose(X)*X*rho + rho*ctranspose(X)*X)
#
# To call write curlyD(X,rho)
def curlyD(X,rho,nargout=1):
y=X * rho * X.conj().T - 0.5 * (X.conj().T * X * rho + rho * X.conj().T * X)
return y
#========================================================================
def curlyC(A,B,nargout=1):
y=A * B - B * A
return y
#========================================================================
def calc_expect(rhotots,Pe,nargout=1):
for indx in (1,2):
for jndx in (1,2):
exp_Pes[indx,jndx]=np.trace(rhotots[:,:,indx,jndx] * Pe)
exp_Pes2[indx,jndx]=np.trace(rhotots[:,:,indx,jndx] * Pe) ** 2
return exp_Pes,exp_Pes2
#========================================================================
#========================================================================
#========================================================================
#========================================================================
#========================================================================
def single_photon_liouvillian(S,L,H,rho,xi,nargout=1):
rhotot[:,:,1,1]=curlyD(L,rho[:,:,1,1]) + xi * curlyC(S * rho[:,:,0,1],L.T) + xi.T * curlyC(L,rho[:,:,1,0] * S.T) + xi.T * xi * (S * rho[:,:,0,0] * S.T - rho[:,:,0,0])
rhotot[:,:,1,0]=curlyD(L,rho[:,:,1,0]) + xi * curlyC(S * rho[:,:,0,0],L.T)
rhotot[:,:,0,1]=curlyD(L,rho[:,:,0,1]) + xi.T * curlyC(L,rho[:,:,0,0] * S.T)
rhotot[:,:,0,0]=curlyD(L,rho[:,:,0,0])
A=np.copy(rhotot)
return A
#========================================================================
def single_photon_stochastic(S,L,H,rho,xi,nargout=1):
K=np.trace((L + L.T) * rho[:,:,1,1]) + np.trace(S * rho[:,:,0,1]) * xi + np.trace(S.T * rho[:,:,1,0]) * xi.T
rhotot[:,:,1,1]=(L * rho[:,:,1,1] + rho[:,:,1,1] * L.T + S * rho[:,:,0,1] * xi + rho[:,:,1,0] * S.T * xi.T - K * rho[:,:,1,1])
rhotot[:,:,1,0]=(L * rho[:,:,1,0] + rho[:,:,1,0] * L.T + S * rho[:,:,0,0] * xi - K * rho[:,:,1,0])
rhotot[:,:,0,1]=(L * rho[:,:,0,1] + rho[:,:,0,1] * L.T + rho[:,:,0,0] * S.T * xi.T - K * rho[:,:,0,1])
rhotot[:,:,0,0]=(L * rho[:,:,0,0] + rho[:,:,0,0] * L.T - K * rho[:,:,0,0])
B=np.copy(rhotot)
return B
#========================================================================
def sde_int_io2_rk(a,b,S,L,H,yn,xi,dt,dW,nargout=1):
Gn=yn + a[S,L,H,yn,xi] * dt + b[S,L,H,yn,xi] * dW
Gnp=yn + a[S,L,H,yn,xi] * dt + b[S,L,H,yn,xi] * np.sqrt(dt)
Gnm=yn + a[S,L,H,yn,xi] * dt - b[S,L,H,yn,xi] * np.sqrt(dt)
ynp1=yn + 0.5 * (a[S,L,H,Gn,xi] + a[S,L,H,yn,xi]) * dt + 0.25 * (b[S,L,H,Gnp,xi] + b[S,L,H,Gnm,xi] + 2 * b[S,L,H,yn,xi]) * dW + 0.25 * (b[S,L,H,Gnp,xi] - b[S,L,H,Gnm,xi]) * (dW ** 2 - dt) * (dt) ** (- 0.5)
return ynp1
#========================================================================
#========================================================================
def sde_int_photon(itype,rhos,S,L,H,Pe,xi,t,nargout=1):
dt=t[1] - t[0]
rhoblank=np.zeros(len(rhos))
Ax=single_photon_liouvillian
Bx=single_photon_stochastic
#if strcmp(itype,'em'):
if itype == 'em':
for i in (1,(len(t)-1)):
if i == 1:
exp_Pes[:,:,i],exp_Pes2[:,:,i]=calc_expect(rhos,Pe,nargout=2)
continue
dW=np.sqrt(dt) * np.randn
rhotots=rhos + dt * single_photon_liouvillian(S,L,H,rhos,xi[i]) + dW * single_photon_stochastic(S,L,H,rhos,xi[i])
exp_Pes[:,:,i],exp_Pes2[:,:,i]=calc_expect(rhotots,Pe,nargout=2)
rhos=np.copy(rhotots)
rhotots=np.copy(rhoblank)
if itype == 'rk':
for i in (1,(len(t)-1)):
if i == 1:
exp_Pes[:,:,i],exp_Pes2[:,:,i]=calc_expect(rhos,Pe,nargout=2)
continue
dW=np.sqrt(dt) * np.randn
rhotots=sde_int_io2_rk(Ax,Bx,S,L,H,rhos,xi[i],dt,dW)
exp_Pes[:,:,i],exp_Pes2[:,:,i]=calc_expect(rhotots,Pe,nargout=2)
rhos=np.copy(rhotots)
rhotots=np.copy(rhoblank)
return exp_Pes,exp_Pes2
#========================================================================
"""
def md_expm(Ain,nargout=1):
Aout=np.zeros(len(Ain))
r,c,d1,d2=len(Ain,nargout=4)
for indx in (1,d1):
for jndx in (1,d2):
Aout[:,:,indx,jndx]=expm(Ain[:,:,indx,jndx])
return Aout
"""
#========================================================================
#========================================================================
Ax=single_photon_liouvillian
Bx=single_photon_stochastic
toc = time.clock()
for indx in (1,range(mt)):
for jndx in (1,range(nt)):
exp_Sz[indx,jndx,1]=np.trace(rho[:,:,indx,jndx] * Sz)
exp_Sy[indx,jndx,1]=np.trace(rho[:,:,indx,jndx] * Sy)
exp_Sx[indx,jndx,1]=np.trace(rho[:,:,indx,jndx] * Sx)
exp_Pe[indx,jndx,1]=np.trace(rho[:,:,indx,jndx] * Pe)
for i in (2,len(t)-1):
#Master equation
rhotot=rho + dt * single_photon_liouvillian(S,L,H,rho,xi[i - 1])
for indx in (1,range(mt)):
for jndx in (1,range(nt)):
exp_Sz[indx,jndx,i]=np.trace(rhotot[:,:,indx,jndx] * Sz)
exp_Sy[indx,jndx,i]=np.trace(rhotot[:,:,indx,jndx] * Sy)
exp_Sx[indx,jndx,i]=np.trace(rhotot[:,:,indx,jndx] * Sx)
exp_Pe[indx,jndx,i]=np.trace(rhotot[:,:,indx,jndx] * Pe)
rho=np.copy(rhotot)
rhotot=np.copy(rhoblank)
for j in (1,range(ensemble_size)):
psi1=np.array([[0],[1]])
rho1=psi1 * psi1.T
rhotemp = np.zeros((2,2,2,2))
rhotemp[:,:,0,0]=rho1
rhotemp[:,:,1,1]=rho1
rhos=np.copy(rhotemp)
for indx in (1,range(2)):
for jndx in (1,range(2)):
exp_Pes[indx,jndx,j,i]=np.trace(rhotemp[:,:,indx,jndx] * Pe)
exp_Pes2[indx,jndx,j,i]=np.trace(rhotemp[:,:,indx,jndx] * Pe) ** 2
for i in (2,(len(t)-1)):
dW=np.sqrt(dt) * np.random.randn()
rhotots=rhos + dt * single_photon_liouvillian(S,L,H,rhos,xi[i - 1]) + dW * single_photon_stochastic(S,L,H,rhos,xi[i - 1])
for indx in (1,range(mt)):
for jndx in (1,range(nt)):
exp_Pes[indx,jndx,j,i]=np.trace(rhotots[:,:,indx,jndx] * Pe)
exp_Pes2[indx,jndx,j,i]=np.trace(rhotots[:,:,indx,jndx] * Pe) ** 2
rhos=np.copy(rhotots)
rhotots=np.copy(rhoblank)
Irow=np.where(np.squeeze(exp_Pes[2,2,j,:]) > 1)
Val=np.squeeze(exp_Pes[2,2,j,Irow])
if Irow:
num_bad=num_bad + 1
avg_val=avg_val + max(Val)
任何幫助將不勝感激,因爲我一直堅持這一段時間。
謝謝!
好吧,所以如果我把我設置爲0,在每個循環之前? – 2015-03-31 21:14:53
如果你不想在'i'上循環,每次循環時都需要設置它,因爲它在循環內部被定義在引起錯誤的那一行之下幾行。但是,只需要執行像'exp_Pes [indx,jndx,j,0] = ...'這樣的操作可能會更清晰一些。 – TheBlackCat 2015-03-31 21:16:32
我把我以前的回答應答到答案中。 – TheBlackCat 2015-03-31 21:20:27