I'm considering the best way to implement the data parallelism features of Mathematica for a specific task. To that end I have a minimal code example below which contains the basic features of my actual code. The example code does the following basic task: Consider defining an initial vector of length $2^N$ (as shown only the first entry of the vector is non-zero): $$\begin{pmatrix} ~~~1&\\ ~~~0 &\\ ~~~\cdot &\\ ~~~\cdot &\\ ~~~0 \end{pmatrix}$$ The vector then undergoes a collective rotation given by the operator $\hat{J}_x := \frac{1}{2}\sum^{N}_{i}\hat{\sigma}_{i}^{x}$ (where $\hat{\sigma}_{i}^{x}$ is the Pauli matrix) about the $x$-axis where the rotation angle is probabilistically (uniformly) taken from the interval $[0,2\pi]$. Then I take the absolute value squared of the inner-product of the rotated state with the initial state. Therefore the process yields a real number at the end as a result. Since I am interested in the average of these independent probabilistic trial results this seems like a process which could benefit from data parallelism.
The main code which does the probabilistic rotation is given by (for example $N=4$):
NotebookDirectory[]
Dimension Number
Num = 4;
Identity and Pauli-X Matrix
Id = {{1, 0}, {0, 1}}; SigX = {{0, 1}, {1, 0}};
Collective X-Rotation Operator
A = Table[Id, {i, Num - 1}]; PrependTo[A, SigX];
Pe = Permutations[A];
KP = Table[KroneckerProduct @@ Pe[[i]], {i, Length[A]}];
JX = (1/2)*Sum[KP[[i]], {i, 1, Length[A]}];
Initial Vector
B1 = ConstantArray[{0}, 2^Num - 1]; PrependTo[B1, {1}];
Random Rotation Angle
R = RandomReal[{0, 2 \[Pi]}]
B2 = Dot[MatrixExp[-I (R) *JX], B1];
Flatten[(Abs[ConjugateTranspose[B2] . B1])^2][[1]]
and the separate code which calls the first code and stores the resultant $10$ independent trial results and calculates the mean value:
Mean[Table[NotebookEvaluate["C:the directory of the above file.nb"], 10]]
Can anyone advise on basic parallelism schemes in Mathematica which would improve the efficiency of the outlined task? Thanks.
R
unevaluate and compute your result, e.g.,f[r_] = (Abs[ConjugateTranspose[MatrixExp[-I r JX] . B1] . B1]^2)[[1, 1]]
? $\endgroup$