# How to simplify this contraction?

**URL:** <https://itensor.discourse.group/t/how-to-simplify-this-contraction/2092>\
**Category:** DMRG and Numerical Methods\
**Created:** [October 28, 2024, 4:49pm UTC](https://itensor.discourse.group/t/how-to-simplify-this-contraction/2092 "2024-10-28T16:49:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![funnybear](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/funnybear/32/662_2.png) [@funnybear](https://itensor.discourse.group/u/funnybear)\
**Post date:** [October 28, 2024, 4:49pm UTC](https://itensor.discourse.group/t/how-to-simplify-this-contraction/2092/1 "2024-10-28T16:49:53Z")

</div>

Hello,

I am trying to find the optimal contraction for:

M\_{ij} = \sum\_{prsq} C\_{pi} C\_{qj} T\_{prsq} C\_{ri} C\_{sj} = \sum\_{prsq} T\_{prsq} C\_{pi} C\_{qj} C\_{ri} C\_{sj}

where T\_{prsq} is an N\times N \times N \times N tensor and C\_{ab} is an N\times N tensor…

It seems like the following is possible:

```auto
Complete contraction: pi,qj,prsq,ri,sj->ij
--------------------------------------------------------------------------------
scaling BLAS current remaining
--------------------------------------------------------------------------------
   3 0 ri,pi->rip qj,prsq,sj,rip->ij
   3 0 sj,qj->sjq prsq,rip,sjq->ij
   5 TDOT rip,prsq->isq sjq,isq->ij
   4 TDOT isq,sjq->ij ij->ij
--------------------------------------------------------------------------------

```

However, I was wondering if there is a better way given the fact that the C matrices above are all the same.

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![kmp5](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/kmp5/32/251_2.png) [@kmp5](https://itensor.discourse.group/u/kmp5)\
**Post date:** [October 28, 2024, 9:04pm UTC](https://itensor.discourse.group/t/how-to-simplify-this-contraction/2092/2 "2024-10-28T21:04:24Z")

</div>

Hello,

I have much experience with this kind of problem. I would suggest the sequence you have already suggested. Because of the non-covarient indices (i and j) these kinds of tensor products are relatively memory bound.

One could do something like fix the values i and j which is equivalent to taking a subvector from the C matrix. Then one can perform a set of matrix-vector and vector-vector products  
{}^{ij} M = \sum\_{pqst} T\_{pqrs} {}^iC\_{p} {}^jC\_{q} {}^iC\_{r} {}^jC\_{s}  
In principle this would require no outer products and therefore could be more operationally efficient than the algorithm you suggested. Additionally, each value of i and j are independent so the operation is trivially parallelizable. One can perform this “sliced” operations with blas by creating a loop around the blas calls and carefully choosing the data step size. I have written these kinds of kernels and have found that they can be computed relatively efficiently!

All the best,  
Karl

p.s. I have written a kernel in my own branch of ITensor which can perform the algorithm I suggest however it is not an out-of-the-box feature of ITensors.jl
