# Expanding a matrix in a set of matrices

Consider a vector $a=\{a1,a2,a3\}$. I computed

$e^{i a\cdot \sigma}\qquad \sigma: {\rm Pauli\ matrices}$

and then applied the command ExpToTrig.

Now I want to expand the above result in terms of the Pauli matrices and the identity matrix as

$\cos(\sqrt{a1^2+a2^2+a3^2})\begin{pmatrix}1&0\\0&1\end{pmatrix}+\ i\ \frac{\sin{\sqrt{a1^2+a2^2+a3^2}}}{\sqrt{a1^2+a2^2+a3^2}}\Biggl( a1\begin{pmatrix}0&1\\1&0\end{pmatrix}+ a2\begin{pmatrix}0&-i\\i&0\end{pmatrix} + a3 \begin{pmatrix}1&0\\0&-1\end{pmatrix}\Biggr)\,,$

How can I do this in mathematica? Also, is it possible in general to expand a given matrix in a set of basis matrices we assign?

One thing you might want to do is write the vector $\vec{a}$ as some magnitude $\theta$ times a unit vector {a1, a2, a3}. Then calculate $\exp(i \theta \,a\cdot\sigma)$. This will allow you to get rid of those factors of $\sqrt{a_1^2+a_2^2+a_3^2}$, which are just some $\theta$ anyway.

Here it is in Mathematica:

a = \[Theta] {a1, a2, a3};
b = a.Array[PauliMatrix, 3];
c = MatrixExp[I  b];
c//FullForm;
c = c /. Plus[Power[a1, 2], Power[a2, 2], Power[a3, 2]] -> 1;
c // ExpToTrig // MatrixForm

$\left( \begin{array}{cc} \cos (\theta )+\text{a3} i \sin (\theta ) & \text{a2} \sin (\theta )+\text{a1} i \sin (\theta ) \\ i \text{a1} \sin (\theta )-\text{a2} \sin (\theta ) & \cos (\theta )-i \text{a3} \sin (\theta ) \\ \end{array} \right)$

I used FullForm to peek at the matrix c so I would know how to make the substitution that eliminates the magnitude of the unit vector.

To answer your second question, you can expand any 2x2 matrix $M$ in terms of the set $S$ consisting of the identity matrix and the 3 Pauli matrices, but the coefficients in the expansion will only be real numbers if $M$ is Hermitian. It is fairly easy to prove this. You know that the set $T$ of 4 matrices each with a 1 in one position and 0's in the other positions is a basis for any 2x2 matrix. You can write each matrix in $T$ in terms of the matrices in $S$, but some of the coefficients are complex. If $S$ is basis for $T$ and $T$ is a basis for all 2x2 matrices ...

Thanks to your comment, we now seek coefficients $c_0, c_1, c_2, c_3$ such that matrix $\hat{c} = c_0 \hat{1} + i\,c_1\hat{\sigma}_1 +i\,c_2\hat{\sigma}_2 +i\,c_3\hat{\sigma}_3$. Here is one way to do it in Mathematica. First, create a basis set and a list of coefficients. Note that we have $i$ times the coefficients of the Pauli matrices, so we explicitly put the $i$ into the list of coefficients. The dot product of the coefficients and the basis is the matrix $m$, which we want to to be equal to the given matrix $c$. So, we form the equations and solve for the coefficients. A quick way to display the solution gives a "fair" result, but the Pauli matrices are not in the proper order. A "better" display can be had using the Row function. Here is the code that takes the previous result for $c$ from above, finds the expansion coefficients and displays the result:

basis = Join[{IdentityMatrix}, Array[PauliMatrix, 3]];
coeff = {c0, I c1, I c2, I c3};
m = coeff.basis // Expand;
m // MatrixForm;
soln = Solve[eqns, {c0, c1, c2, c3}] // First;
fair = coeff.(MatrixForm /@ basis) /. soln
Table[coeff[[k]] MatrixForm[basis[[k]]], {k, 1, 4}];
better = Row[% /. soln, "+"]

$$\cos (\theta )\left( \begin{array}{cc} 1 & 0 \\ 0 & 1 \\ \end{array} \right) + i \text{a1} \left( \begin{array}{cc} 0 & 1 \\ 1 & 0 \\ \end{array} \right) \sin (\theta )+i \text{a2} \left( \begin{array}{cc} 0 & -i \\ i & 0 \\ \end{array} \right) \sin (\theta )+i \text{a3} \left( \begin{array}{cc} 1 & 0 \\ 0 & -1 \\ \end{array} \right) \sin (\theta )$$

Factoring out the $i$ and the $\sin\theta$ and putting the Pauli matrix terms in parentheses is a bit trickier. I don't have a solution for that bit of finesse.

• I could not make myself understood, sorry. Actually, I know how to use MatrixExp and ExptoTig. What I want to know is the command to rewrite the result, which is expressed as one matrix , as a linear combination of matrices (the id. matrix and Pauli matrices in this case.) That is why I wrote down a sum of four matrices above, which is what I want to get eventually. – UKS Jun 25 '16 at 15:27

For the first part, you can use MatrixExp. Since you are interested in trigonometric form, I would suggest using polar coordinates from the beginning.

p0 = PauliMatrix; p1 = PauliMatrix;
p2 = PauliMatrix; p3 = PauliMatrix;

a1 = a Sin[q1] Cos[q2];
a2 = a Sin[q1] Sin[q2];
a3 = a Cos[q1];
m = a1 p1 + a2 p2  + a3 p3;

m1 = MatrixExp[I m] // FullSimplify;
m1 // MatrixForm


$\left( \begin{array}{cc} \cos (a)+i \cos (\text{q1}) \sin (a) & \sin (a) \sin (\text{q1}) (i \cos (\text{q2})+\sin (\text{q2})) \\ i e^{i \text{q2}} \sin (a) \sin (\text{q1}) & \cos (a)-i \cos (\text{q1}) \sin (a) \\ \end{array} \right)$

For the second part, I am going to use the method described in Expressing a matrix in terms of four basis matrices. First we define a basis and then use LinearSolve.

basis = {p0, p1, p2, p3};
LinearSolve[Flatten[basis,{{2,3}}],Flatten[m1]]// FullSimplify


Cos[a]

I Cos[q2] Sin[a] Sin[q1]

I Sin[a] Sin[q1] Sin[q2]

I Cos[q1] Sin[a]

This is basically a compact way of solving equations for all the component at once. You can also proceed in this way. Say m1 = c0 p0 + c1 p1 + c2 p2 + c3 p3 where c0,c1,c2,c3 are the unknown coefficients to determine.

cp = (c0 p0 + c1 p1 + c2 p2 + c3 p3)
{c0, c1, c2, c3} /.Solve[Flatten@Table[cp[[i, j]] == m1[[i, j]],
{i, 2}, {j, 2}], {c0, c1, c2, c3}] // FullSimplify


which will give you the same answer.

• Thanks to your edit, I understood that LinearSolve is helpful for my question. Could you explain how the part ''Flatten[basis,{{2, 3}}],Flatten[m1]'' works? I see that Flatten[m1] lines up all the components of m1. However, Flatten[basis,{{2, 3}}] was not clear to me although I read the link you kindly introduced. – UKS Jun 25 '16 at 17:36
• LinearSolve is not the only way. It is a compact way solving set of linear equations. You can also use Solve. I added it to my answer. Flatten[basis,{{2,3}}] is a way to construct a 4x4 matrix from a 2x2x4 array. You can find more example in document center. – Sumit Jun 25 '16 at 22:04
• "Flatten[basis,{{2,3}}] is a way to construct a 4x4 matrix from a 2x2x4 array." I am trying to understand what this means. I see that 2x2x4 means four 2x2 matrices but how do they turn into 4x4?({{2,3}}??) I would like your further comments. I found a similar example in the document center: u = {{a, b}, {c, d}} Flatten[{{u, 0 u}, {0 u, u}}, {{1, 3}, {2, 4}}] // MatrixForm, where I do not understand {{1,3},{2,4}}. – UKS Jun 26 '16 at 6:11
• An array is a list with curly brackets say {{{a1,b1},{c1,d1}},{{a2,....When I say 2x2x4, that means it has 4 2x2 matrices. What Flatten does is to remove the brackets and bring the elements together. The default order is Infinity, so of you use it without an order it will give {a1,b1,c1,d1,a2,...}. The arguments in Flatten says it where to remove the braces and which element to group. I would suggest to create some arrays with any dimensions an then use Flatten with different order. It will give you some good idea. – Sumit Jun 26 '16 at 7:14
• "The arguments in Flatten says it where to remove the braces and which element to group." That sounds clear. I will try some examples to convinvce myself. – UKS Jun 26 '16 at 8:13

You can directly use the function pauliReduce that I defined in this answer:

a = {a1, a2, a3}
(* ==> {a1, a2, a3} *)

a.{σ, σ, σ}
(* ==> a1 σ + a2 σ + a3 σ *)

pauliReduce[
MatrixExp[I a.{σ, σ, σ}]]


$$\frac{1} {\sqrt{\text{a1}^2+\text{a2}^2+\text{a3}^2}}\left(\hat{1} \sqrt{\text{a1}^2+\text{a2}^2+\text{a3}^2} \cos \left(\sqrt{\text{a1}^2+\text{a2}^2+\text{a3}^2}\right)+ i (\text{a1} \sigma (1)+\text{a2} \sigma (2)+\text{a3} \sigma (3)) \sin \left(\sqrt{\text{a1}^2+\text{a2}^2+\text{a3}^2}\right)\right)$$

• I appreciate your clear and powerful solution. I will learn a lot from your function, so that I can also consider similar cases where other basis matrices are used, not just the Pauli matrices, hopefully. – UKS Jun 26 '16 at 8:20