# How to rewrite a tensor as a matrix

I put
{TensorProduct[{1, 0}, {0, 1}, {0, 1}, {0, 1}, {0, 1}]}/Sqrt[2]//MatrixForm and I got $$\left( \begin{array}{cc} \left( \begin{array}{cc} \left( \begin{array}{cc} 0 & 0 \\ 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 0 \\ 0 & 0 \\ \end{array} \right) \\ \left( \begin{array}{cc} 0 & 0 \\ 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 0 \\ 0 & \frac{1}{\sqrt{2}} \\ \end{array} \right) \\ \end{array} \right) & \left( \begin{array}{cc} \left( \begin{array}{cc} 0 & 0 \\ 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 0 \\ 0 & 0 \\ \end{array} \right) \\ \left( \begin{array}{cc} 0 & 0 \\ 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 0 \\ 0 & 0 \\ \end{array} \right) \\ \end{array} \right) \\ \end{array} \right)$$ as a result. I now would like to rewrite this as $$\left( \begin{array}{cc} 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 & \frac{1}{\sqrt{2}} & 0 & 0 & 0 & 0 \end{array} \right)$$ to calculate eigenvalues of this matrix above.

Could you tell me how?

mat = {TensorProduct[{1, 0}, {0, 1}, {0, 1}, {0, 1}, {0, 1}]/Sqrt[2]};

FixedPoint[ArrayFlatten, mat] // MatrixForm


$$\left( \begin{array}{cccccccc} 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 & \frac{1}{\sqrt{2}} & 0 & 0 & 0 & 0 \\ \end{array} \right)$$

mat = {TensorProduct[{1, 0}, {0, 1}, {0, 1}, {0, 1}, {0, 1}]/ Sqrt[2]};

ArrayFlatten[ArrayFlatten /@ mat] // MatrixForm


$$\left( \begin{array}{cccccccc} 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 & \frac{1}{\sqrt{2}} & 0 & 0 & 0 & 0 \\ \end{array} \right)$$

• Yes, it was a typo. Thanks for catching.. Jul 18, 2020 at 3:09
X = {TensorProduct[{1, 0}, {0, 1}, {0, 1}, {0, 1}, {0, 1}]/Sqrt[2]};

Flatten[X, {{1, 3, 5}, {2, 4, 6}}]

(*    {{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, 1/Sqrt[2], 0, 0, 0, 0}}    *)

• Side note: to understand this syntax of Flatten, one may read this answer. Jul 17, 2020 at 12:12

Another option

(m = {TensorProduct[{1, 0}, {0, 1}, {0, 1}, {0, 1}, {0, 1}]}/Sqrt[2]) // MatrixForm


And now

m1 = ArrayFlatten[m[[1, 1]], 2]
m2 = ArrayFlatten[m[[1, 2]], 2]
Join[m1, m2, 2] // MatrixForm