Take the 2-minute tour ×
Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.