<p>Maybe helpful: Take a look into <a href="https://arxiv.org/abs/1402.0939" rel="nofollow noreferrer">https://arxiv.org/abs/1402.0939</a> which is a description of an efficient framework for the problem of contracting so called tensor networks in a single function <code>ncon(...)</code>. As far as I see implementations of it are directly available for Matlab (can be found within in the link) and for Python3 (<a href="https://github.com/mhauru/ncon" rel="nofollow noreferrer">https://github.com/mhauru/ncon</a>).</p>
Efficient tensor contraction in python
<p>I have a list <code>L</code> of tensors (<code>ndarray</code> objects), with several indices each. I need to contract these indices according to a graph of connections. </p>

<p>The connections are encoded in a list of tuples in the form <code>((m,i),(n,j))</code> signifying "contract the <em>i</em>-th index of the tensor <code>L[m]</code> with the <em>j</em>-th index of the tensor <code>L[n]</code>.</p>

<p>How can I handle non-trivial connectivity graphs? The first problem is that as soon as I contract a pair of indices, the result is a new tensor that does not belong to the list <code>L</code>. But even if I solved this (e.g. by giving a unique identifier to all the indices of all the tensors), there is the issue that one can pick any order to perform the contractions, and some choices yield unnecessarily enormous beasts in mid-computation (even if the final result is small). Suggestions?</p>