# Best way to work with Tensor Networks

I have to write a script in which I have to work with a very simple tensor network but I can't find a nice scalable way to make this work.

The basic object I am working with is a tensor more or less of this form (using the Einstein summation convention) $$A_{i_1....i_n}^{j_1...j_n} = T^{}_{i_1k_1}(T^{})^{k_1j_1}_{i_2k_2}(T^{})^{k_2j_2}_{i_3k_3}...$$ etc., i.e. some contraction of $$\bigotimes_kT^{[k]}$$.

I then need to change the representation of this tensor by collecting all lower and upper indices $$I={i_1,i_2,...,i_n}$$, $$J={j_1,...,j_n}$$ to effectively obtain a matrix $$A_I^J$$.

To achieve the first part I can use the TensorProduct function followed by TensorContract, but then I cannot think of a smart scalable way (i.e. code that doesn't depend on $$n$$) to group indices together. Mathematica has the KroneckerProduct function to represent tensor products of matrices as matrices, but is there some analogue for the cases in which I don't have a tensor product but a more generic tensor? Or is there another smart way to achieve this?

If you look at it in MatrixForm, at the end of the day I only have to remove internal parenthesis. I can't believe that there is no smart automated way to do this :)

• "at the end of the day I only have to remove internal parenthesis" - can you use Flatten with an adequate level specification then? You did not include your actual expression, so I can't try it myself, unfortunately. – MarcoB Jun 17 at 15:39

Probably Transpose and ArrayReshape can do the conversion into a matrix for you.
A = Array[a, {2, 2, 2, 2, 3, 3, 3, 3}];