# How to replace or set Index of a Tensor object?

+1 vote
edited

Sometimes we may feel it's not convenient to copy a Tensor object and use it to contract with other Tensor objects since the indexes of Tensor is unique. When I construct a tensor network, I have to set the indexes of these tensors to fit each other.

Tensor class maybe needs such kind a method, setIndex(oldIndex, newIndex) or replaceIndex(oldIndex, newIndex).

+1 vote

In Itensor there is a kind of tensor named delta tensor. We can use this delta tensor to replace or set Index of a Tensor object(http://itensor.org/docs.cgi?page=classes/diag_itensor).

It looks like
auto T = ITensor(a,b,c);
T *= delta(b,f);

Then we obtain T(a,f,c).

commented by (70.1k points)
Thanks for posting this question & answer Ruizhen
commented by (320 points)
Good point! Maybe a pedantic question as these operations might usually not be a bottleneck of the code:
Is the contraction by a delta tensor handled in a special way? As I guess a contraction as usual might be less efficient than just checking compatibility of indices and then substituting them in the ITensor?
commented by (70.1k points)
You definitely make an important point that "inefficient" code is only really so if it dominates a significant portion of the running time. It's always good to keep an eye on sections of code for possible inefficiencies though. So to answer your question: yes delta tensor contraction is handled in a special way. In fact it launches one of 3 different algorithms depending on the case. For the case of a 2-index delta tensor, the code just replaces one index with another without touching any tensor data. The other two cases deal with whether all of the indices of the delta tensor are contracted or just some. Then it calls special sparse-tensor routines for these cases.