Adding another looping element with a list

I have code that is looping on to elements and is working shown below

n = 4;
EList = Take[Tuples[{0, 1}, n + 1], 2^(n – 1)];
PosEList =Table[Flatten[Position[EList[[i]], 1] – 1], {i, 1, Length[EList]}];
CList = Tuples[{0, 1}, n];
WeightCList = Table[Count[CList[[i]], 1], {i, 1, Length[CList]}];
g[x_, y_] = (-1)^(Binomial[x, y]);
SetAttributes[g, Listable];
AlmostPsiList =
ReplaceAll[Transpose[
Table[g[i, j], {i, WeightCList}, {j, PosEList}]], {{} -> {1}}];
AlmostPsiList2 =Table[Times @@@ AlmostPsiList[[i]], {i, 1, Length[AlmostPsiList]}];
psiList =Table[Transpose[{AlmostPsiList2[[i]]}], {i, 1,Length[AlmostPsiList2]}];

X = PauliMatrix[1]; Y = PauliMatrix[2]; Z=PauliMatrix[3]; t = \[Pi]/2;
CT = CoordinateTransformData[“Spherical” -> “Cartesian”, “Mapping”, {1, \[Theta], \[Phi]}];
x = CT[[1]]; y = CT[[2]]; z = CT[[3]];
U = Cos[t]*IdentityMatrix[2] + I*Sin[t]*(x*X + y*Y + z*Z);
Uall = KroneckerProduct[U, U, U, U];
Fidelity = Compile[{{\[Theta], _Real}, {\[Phi], _Real}},Evaluate[Abs[Flatten[(1/Sqrt[2.0^n])*
psiList[[1]]].(Uall.Flatten[(1/Sqrt[2.0^n])*psiList[[1]]])]]];
Do[
If[Fidelity[\[Theta], \[Phi]] >= .999,
Print[N[U] // MatrixForm]], {\[Theta], \[Pi]/180, \[Pi], \[Pi]/180.0},
{\[Phi], -\[Pi] + \[Pi]/180, \[Pi], \[Pi]/180.0}]

Now I want to edit Fidelity and Do so that they will run through all the elements of psiList. Here is what I have tried to do so far

Fidelity =
Compile[{\[Theta], \[Phi], {i, _Integer}},
Evaluate[Abs[
Flatten[(1/Sqrt[2.0^n])*psiList[[i]]].(Uall.Flatten[(1/Sqrt[2.0^n])*
psiList[[i]]])]]];
Do[
If[Fidelity[\[Theta], \[Phi], i] >= .999,
Print[N[U] // MatrixForm]], {\[Theta], \[Pi]/180, \[Pi], \[Pi]/
180.0}, {\[Phi], -\[Pi] + \[Pi]/180, \[Pi], \[Pi]/180.0}, {i,Length[psiList]}]

This is not working and I am not sure why. I have also tried using Function instead of Compile.

When I run the code it gives me these errors

Part::pspec: Part specification i is neither a machine-sized integer nor a list of machine-sized integers. >>

Part::pspec: Part specification i is neither a machine-sized integer nor a list of machine-sized integers. >>

CompiledFunction::cflist: Nontensor object generated; proceeding with uncompiled evaluation. >>

Dot::dotsh: Tensors {{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25}} and {{0.999391,-0.0174418+0.000304447 I,-0.0174418+0.000304447 I,<<11>>,-5.30767*10^-6+2.78163*10^-7 I,9.25469*10^-8-6.47151*10^-9 I},<<14>>,{9.25469*10^-8+<<22>> I,<<14>>,<<18>>}} have incompatible shapes. >>

Dot::dotsh: Tensors {{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25}} and {{0.232857 +0.000288784 I},{-0.241129-0.000296478 I},{-0.241129-0.000296478 I},<<11>>,{-0.258566-0.000312417 I},{0.267751 +0.000320667 I}} have incompatible shapes. >>

CompiledFunction::cflist: Nontensor object generated; proceeding with uncompiled evaluation. >>

Dot::dotsh: Tensors {{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{0.25},{-0.25}} and {{0.999391,-0.0174418+0.000304447 I,-0.0174418+0.000304447 I,<<11>>,-5.30767*10^-6+2.78163*10^-7 I,9.25469*10^-8-6.47151*10^-9 I},<<14>>,{9.25469*10^-8+<<22>> I,<<14>>,<<18>>}} have incompatible shapes. >>

General::stop: Further output of Dot::dotsh will be suppressed during this calculation. >>

CompiledFunction::cflist: Nontensor object generated; proceeding with uncompiled evaluation. >>

General::stop: Further output of CompiledFunction::cflist will be suppressed during this calculation. >>

=================

  

 

Could you describe how the code fails? Does it throw an error, or does it execute normally but the result is not what you want? Could you describe what you are trying to achieve with your code?
– MarcoB
Jul 15 ’15 at 15:52

  

 

Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– Dr. belisarius
Jul 15 ’15 at 16:24

  

 

Thanks for the advice. Didn’t know about that. This is my first time using Mathematica so I am in spittle over my head. I am just trying to get all the help I can.
– Onepiece79
Jul 15 ’15 at 16:51

=================

1 Answer
1

=================

In your declaration of the new compiled function, you omitted to include the type of some variables.
The Part::pspec error has actually come up very recently in this forum. I will refer to that Q&A for a full discussion, but the short answer is that you want to “inject” your matrix into the compiled function so that Part can properly operate with it.

Putting those two points together, I would suggest the following form for your new compiled function:

With[
{list = psiList},
Fidelity = Compile[
{{θ, _Real}, {ϕ, _Real}, {i, _Integer}},
Abs[Flatten[(1/Sqrt[2.0^n])*list[[i]]].(Uall.Flatten[(1/Sqrt[2.0^n])*list[[i]]])]
]
];

  

 

Thanks the code is working now. However it is taking about 30 minutes to run, is there any way to speed that up?
– Onepiece79
Jul 15 ’15 at 16:22

  

 

@Onepiece79 You should vote up this answer if it’s useful for you. MarcoB was way too kind in reading and understanding your code …
– Dr. belisarius
Jul 15 ’15 at 16:25

  

 

@Onepiece79 Please read my comment under the question
– Dr. belisarius
Jul 15 ’15 at 16:27

1

 

@Onepiece79 well, you are running through an awful lot of fine-grained values in your loop… If I reduce the resolution to Pi/9 for theta and phi, i.e. by a factor of 20×20=400, the loop takes ca. 4 s on my computer. It therefore stands to reason that it will take 400*4s = 1600s = 27 min to complete the calculation as you wrote it. Do you need that resolution? Is there any symmetry in your problem that you could exploit to reduce the domain to explore?
– MarcoB
Jul 15 ’15 at 17:29

1

 

There is a symmetry of a half turn around the sphere, so I can cut phi in half. I need that resolution because I have no clue where the spots I am looking for happen.
– Onepiece79
Jul 15 ’15 at 17:45