How to speed up function evaluation by vectorization

조회 수: 4 (최근 30일)
Frederik Lohof
Frederik Lohof 2015년 10월 7일
편집: Frederik Lohof 2015년 10월 7일
Is there a way to speed up this code significantly? Maybe by more vectorization?
It is the evaluation of a Hesse matrix. The first loop is a "dot product" between basis elements in the form of 16x16 matrices (Lambda) and corresponding coefficients (T). The second (double-) loop is the evaluation of the Hesse matrix itself.
This is a more generic version of the code:
Hesse=eye(256,256);
B=rand(1296,256);
T=rand(256,1);
Lambda=rand(16,16,256);
f=rand(1296,1);
P_int=B*T;
rho_int=zeros(16);
%Loop 1
for j=1:256
rho_int=rho_int+T(j,1)*Lambda(:,:,j)/2^4;
end
rho_int_inv=inv(rho_int);
%Loop 2
for k=2:256
for l=2:256
Hesse(k,l)=dot(f, (B(:,k).*B(:,l))./(P_int.^2))+trace(rho_int_inv*Lambda(:,:,k)*rho_int_inv*Lambda(:,:,l)/4^4);
end
end
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2015년 10월 7일
P_int - what is it?
Frederik Lohof
Frederik Lohof 2015년 10월 7일
편집: Frederik Lohof 2015년 10월 7일
In my setting P_int is a vector of probabilities that is used in the calculation of the Hesse matrix. The original function to the Hesse matrix is a likelihood function which is obtained in the context of quantum state tomography (quantum state estimation). The likelihood function is a measure for the likelihood of a certain quantum state (parametrized by T) given the frequencies f of certain measurement outcomes. These are obtained (in my case simulated) by measuring n identical copies of the same (unknown) quantum state in different measurement bases. P_int is a vector of probabilities of measuring a certain outcome given a certain state (B is a matrix yielding the connection between the parametrization T and the expected probabilities P_int).
Edit: And I made a typo in the initialization: It is P_int=B*T :)

댓글을 달려면 로그인하십시오.

채택된 답변

Kirby Fears
Kirby Fears 2015년 10월 7일
편집: Kirby Fears 2015년 10월 7일
The second loop seems fine. Vectorization of loop 1 below.
% rho_int=zeros(16);
% Loop 1 replaced by vector operation
rho_int=sum(repmat(reshape(T,1,1,numel(T)),...
size(Lambda,1),size(Lambda,2)).*Lambda,3)/2^4;
rho_int_inv=inv(rho_int);
You could speed up the second loop with the parallel computing toolbox.
Hope this helps.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Quantum Mechanics에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by