# Reshape and Index (State) Products in Numpy

Consider the following:

I start with a $2\times 2$ matrix $W_{ij}$. I then take this $W$ matrix and make a new tensor, $T$, by doing the following: $$T_{ijkl}=\sum_{a}W_{ai}W_{aj}W_{ak}W_{al}$$ which in my code I am using the (slick) einsum'' tool in numpy:

tt = np.einsum('ai, aj, ak, al', w, w, w, w)


with import numpy as np for np. Next I preform the following: $$M_{XX'y''y'}=\sum_{y}T_{xx'yy'}T_{x''x'''y''y}$$ with $X=x\otimes x''$ and $X'=x'\otimes x'''$. Now when I do a naive reshape in numpy

mtensor = np.einsum('ikma, jlan', tt, tt).reshape(4, 4, 2, 2)


This simply unfolds the array and then recollects in order. However, I would like to have the power to combine certain indices (states) into a single index (like an outer product but for indices). That is, take my two, $2$ state indices, and combine them into a $4$ state index that is all the possible combinations of their states. If someone knows a slick way to do this in numpy I would greatly appreciate some documentation or an answer. Thanks in advance.

## 1 Answer

Turns out if the indices are input in the alphabetical order as they are up above, the reshape actually is the correct product of states into a single index. Sorry, for this question, I honestly didn't know it was already like this.