I am working on a code that takes long time to evaluate, So I come to know that use of Compile[] function can speed up significantly. Here is my original code.
Ix = PauliMatrix[1]; Iy = PauliMatrix[2]; Iz = PauliMatrix[3];
\[Rho]sample[\[Beta]_, \[Gamma]_] := Cos[\[Beta]] Cos[\[Gamma]] Iz;
Ud[\[Beta]_, \[Gamma]_] :=
Table[i Cos[\[Beta]] Cos[\[Gamma]] (Ix-Iy+Iz), {i, 1, 500}];
\[Rho]dc[\[Beta]_, \[Gamma]_] :=
Dot[#, \[Rho]sample[\[Beta], \[Gamma]], #\[ConjugateTranspose]] & /@
Ud[\[Beta], \[Gamma]];
FreeEvolve[\[Beta]_, \[Gamma]_] :=
Tr[(Ix - I Iy)\[ConjugateTranspose].#] & /@ \[Rho]dc[\[Beta], \
\[Gamma]];
Selectedangle =
Flatten[Outer[{#1, #2} &, Range[0, \[Pi], \[Pi]/200],
Range[0, 2 \[Pi], \[Pi]/30]], 1];
FID = Total[
ParallelMap[FreeEvolve[#[[1]], #[[2]]] &,
Selectedangle]]; // AbsoluteTiming
Following the code, I tried with Compile[].
compiledRhoDc =
Compile[{{\[Beta], _Real}, {\[Gamma], _Real}},
Block[{ud, rhoSample, rhoDcList, i}, ud = Ud[\[Beta], \[Gamma]];
rhoSample = \[Rho]sample[\[Beta], \[Gamma]];
rhoDcList =
Table[Dot[ud[[i]], rhoSample, ConjugateTranspose[ud[[i]]]], {i,
Length[ud]}];
rhoDcList], RuntimeAttributes -> {Listable},
Parallelization -> True];
compiledFreeEvolve =
Compile[{{\[Beta], _Real}, {\[Gamma], _Real}},
Block[{rhoDcList, CxConj, CyConj, result, i},
CxConj = ConjugateTranspose[Ix];
CyConj = ConjugateTranspose[Iy];
rhoDcList = compiledRhoDc[\[Beta], \[Gamma]];
result =
Table[Tr[(CxConj + I CyConj).rhoDcList[[i]]], {i,
Length[rhoDcList]}];
result], RuntimeAttributes -> {Listable}, Parallelization -> True];
compFID =
Total[ParallelMap[compiledFreeEvolve[#[[1]], #[[2]]] &,
Selectedangle]]; // AbsoluteTiming
When I try to evaluate, it gives some error although it returns same output as that of original code. The errors appear as follows:
*Compile::part: Part specification ud[[i]] cannot be compiled since the argument is not a tensor of sufficient rank. Evaluation will use the uncompiled function.
Compile::cplist: Compile`$15 should be a tensor of type Integer, Real, or Complex; evaluation will use the uncompiled function.*
Any lead will be really helpful to improve the efficiency using Compile[] or other ways. Thanks in advance!
ConstantArray[0., 500]
would be the fasted and most accurate way to compute the result... ;p $\endgroup$