# How can I obtain this matrix quickly in Mathematica?

Is there a way of obtaining the matrix matFinal quickly by avoiding the following long procedure?

    s1 = PauliMatrix[1]; s2 = PauliMatrix[2]; s3 = PauliMatrix[3];
u = {{a^2, 0, 0, 0, 0, 0, 0, a b}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0,
0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0,
0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {a b, 0,
0, 0, 0, 0, 0, b^2}};
m11 = Tr[u.KroneckerProduct[s1, s1, s1]];
m12 = Tr[u.KroneckerProduct[s1, s1, s2]];
m13 = Tr[u.KroneckerProduct[s1, s1, s3]];
m14 = Tr[u.KroneckerProduct[s1, s2, s1]];
m15 = Tr[u.KroneckerProduct[s1, s2, s2]];
m16 = Tr[u.KroneckerProduct[s1, s2, s3]];
m17 = Tr[u.KroneckerProduct[s1, s3, s1]];
m18 = Tr[u.KroneckerProduct[s1, s3, s2]];
m19 = Tr[u.KroneckerProduct[s1, s3, s3]];

m21 = Tr[u.KroneckerProduct[s2, s1, s1]];
m22 = Tr[u.KroneckerProduct[s2, s1, s2]];
m23 = Tr[u.KroneckerProduct[s2, s1, s3]];
m24 = Tr[u.KroneckerProduct[s2, s2, s1]];
m25 = Tr[u.KroneckerProduct[s2, s2, s2]];
m26 = Tr[u.KroneckerProduct[s2, s2, s3]];
m27 = Tr[u.KroneckerProduct[s2, s3, s1]];
m28 = Tr[u.KroneckerProduct[s2, s3, s2]];
m29 = Tr[u.KroneckerProduct[s2, s3, s3]];

m31 = Tr[u.KroneckerProduct[s3, s1, s1]];
m32 = Tr[u.KroneckerProduct[s3, s1, s2]];
m33 = Tr[u.KroneckerProduct[s3, s1, s3]];
m34 = Tr[u.KroneckerProduct[s3, s2, s1]];
m35 = Tr[u.KroneckerProduct[s3, s2, s2]];
m36 = Tr[u.KroneckerProduct[s3, s2, s3]];
m37 = Tr[u.KroneckerProduct[s3, s3, s1]];
m38 = Tr[u.KroneckerProduct[s3, s3, s2]];
m39 = Tr[u.KroneckerProduct[s3, s3, s3]];

matFinal = {{m11, m12, m13, m14, m15, m16, m17, m18,
m19},        {m21, m22, m23, m24, m25, m26, m27, m28, m29}, {m31,
m32, m33, m34, m35, m36, m37, m38, m39}}


Note that mi(jk) = Tr[u.KroneckerProduct[si, sj, sk]].

matFinal2 = Join @@@ Array[Tr[u. KroneckerProduct @@ (PauliMatrix /@ {##})] &, {3, 3, 3}]

{{2 a b, 0, 0, 0, -2 a b, 0, 0, 0, 0},
{0, -2 a b, 0, -2 a b, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, a^2 - b^2}}

TeXForm @ MatrixForm @ matFinal2


$$\left( \begin{array}{ccccccccc} 2 a b & 0 & 0 & 0 & -2 a b & 0 & 0 & 0 & 0 \\ 0 & -2 a b & 0 & -2 a b & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^2-b^2 \\ \end{array} \right)$$

Alternative version without @, @@, @@@, /@, ##:

matFinal3 = Map[Flatten][
Table[Tr[u.KroneckerProduct[PauliMatrix[i], PauliMatrix[j], PauliMatrix[k]]],
{i, 3}, {j, 3}, {k, 3}]];

matFinal3 == matFinal2 == matFinal

True

• Thanks a lot!, but this notation @, @@, @@@, /@, ## is too compact and I am not able to understand what is happening:) Commented Jan 11, 2022 at 20:26
• Could you write in couple of lines on how this works? Commented Jan 11, 2022 at 20:27
• Read the documentation for Prefix, Function, Map, and Apply. In general, in Mathematica highlight a symbol or operator that you don't understand (e.g., @@) and press F1 for help. Commented Jan 11, 2022 at 20:33
• @User101, see if the alternative version using Table is easier to follow.
– kglr
Commented Jan 11, 2022 at 20:35

Clear["Global*"]
`