0

Does anyone knows why it's telling me list index out of range? I'm trying to "translate" a Matlab file and I created lists instead of cell arrays, the main problem now lies in the index, it seems correct to me and I don't know how there is an issue

#Projection operator Pi
piOperator = np.zeros((N*N, N*N)) 

#Psi0 state
Psi0 = np.zeros((N*N, 1))
for i in xrange(0 , N-1 ):
    aux = np.zeros((N,1)) #Auxiliary vector 
    aux[i]= 1
    A =  np.sqrt(G[:,i])
    psi = []
    P = [] 
    psi.append(np.tensordot(aux, A))
    P.append(np.dot(psi[i],np.transpose(psi[i])))
    piOperator = piOperator + P[i]
    Psi0 = Psi0 + psi[i] 


Psi0 = 1/np.sqrt(N)*Psi0 


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call    last)
<ipython-input-450-0816498ad018> in <module>()
 11     P = []
 12     psi.append(np.tensordot(aux, A))
 ---> 13     P.append(np.dot(psi[i],np.transpose(psi[i])))
 14     piOperator = piOperator + P[i]
 15     Psi0 = Psi0 + psi[i]

 IndexError: list index out of range 

The original Matlab code was:

Pi = zeros(n^2,n^2);
Psi0 = zeros(n^2,1);
for k=1:n
    aux = zeros(n,1);
    aux(k) = 1;
    psi{k} = kron(aux,sqrt(G(:,k)));
    P{k} = psi{k} * psi{k}';
    Pi = Pi + P{k};
    Psi0 = Psi0 + psi{k};
end
Psi0 = 1/sqrt(n)*Psi0;
1
  • 1
    Can you please post a complete code snippet? G is missing, for example. Commented Jun 16, 2017 at 8:55

1 Answer 1

2

You are clearing your psi variable every iteration. Just initialize it before the loop:

#Projection operator Pi
piOperator = np.zeros((N*N, N*N)) 

#Psi0 state
Psi0 = np.zeros((N*N, 1))
psi = []
P = [] 
for i in xrange(0 , N-1 ):
    aux = np.zeros((N,1)) #Auxiliary vector 
    aux[i]= 1
    A =  np.sqrt(G[:,i])
    psi.append(np.tensordot(aux, A))
    P.append(np.dot(psi[i],np.transpose(psi[i])))
    piOperator = piOperator + P[i]
    Psi0 = Psi0 + psi[i] 


Psi0 = 1/np.sqrt(N)*Psi0 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I'm such a fool :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.