+1 vote
asked by (390 points)

Dear ITensor team,

Given an ITensor G1 with rank r and a certain index structure, for example

G1 =
ITensor r=4: ("S=1/2 51",2,Site|104)' ("S=1/2 50",2,Site|722)' ("S=1/2 50",2,Site|722) ("S=1/2 51",2,Site|104)

and another ITensor with same rank r, but different index structure, for example

G2 =
ITensor r=4: ("S=1/2 101",2,Site|215)' ("S=1/2 99",2,Site|481)' ("S=1/2 99",2,Site|481) ("S=1/2 101",2,Site|215)

what is the easiest way to take their difference or sum or contract them?

Namely I would like to create an ITensor G2-2 with the same numerical content of G2 but same index structure of G1. Is there an easy way (in version 2.1.1) to do it?
Apologises for the quite standard question but I could not find an answer in the online documentation.

Thanks a lot for your time.


commented by (70.1k points)
Hi Jacopo, Matt's answer below is the right one. But I wanted to comment that if you find yourself in this situation, it's worthwhile to look more broadly at the design of your code to think about if there was another way to organize it so this situation wouldn't happen in the first place.

In my experience with ITensor, I almost never have to replace or change indices. So if you even want to post more of your code here, or a summary of it, I'd be happy to take a look and discuss the design with you.

But I do understand that there can be complicated situations where replacing indices becomes necessary. One example would be translation invariant / infinite systems, where at the end of the unit cell you want it to reconnect with the beginning, and might need to change the indices to make the connection work. But even in this case, a superior option can be to keep the indices identical only up to prime levels or tags.

Best regards,
commented by (390 points)
Dear Miles,

thanks a lot for your time. Matt answer indeed works very well!

I need to take the trace of the difference between a 2-site thermal density matrix computed with ancilla method (so on a 2*N sites chain) and the 2-site reduced density matrix computed on a chain with N sites. Since the two ITensor have different index structure I do not see another way to take their difference if not to change the index of one of them. Or maybe you know better ways to proceed?
Thanks a lot!
commented by (70.1k points)
Hi Jacopo, based on your explanation, I would suggest that a good idea would be to set things up so that the ITensors end up having the same index structure.

One way to do this would be to use a site set (of size N) for the physical indices for both states. Then use a separate site set for the ancilla indices of the 2N long state.

You could make the tensors of the ancilla MPS by "hand" at the beginning, using a loop pattern like:

auto psi = MPS(2*N);
for(int n = 1; n <= 2*N; ++n)
auto s = isodd(n) ? phys_sites(n) : anc_sites(n);
// ...
auto A = ITensor(links[n],s,links[n+1]);
// ...

Just an idea. It may be impractical or more trouble than it's worth though.


1 Answer

0 votes
answered by (14.1k points)


An easy way to replace the indices of an ITensor is by multiplying with a series of delta tensors. If the original tensor has indices {i,j,k} and you want it changed to {a,b,c}, you can do:

G2 = G2 * delta(i,a) * delta(j,b) * delta(k,c);

In your case, {i,j,k} would be the indices of G2, and {a,b,c} would be the corresponding indices of G1.


Welcome to ITensor Support Q&A, where you can ask questions and receive answers from other members of the community.

Formatting Tips:
  • To format code, indent by four spaces
  • To format inline LaTeX, surround it by @@ on both sides
  • To format LaTeX on its own line, surround it by $$ above and below
  • For LaTeX, it may be necessary to backslash-escape underscore characters to obtain proper formatting. So for example writing \sum\_i to represent a sum over i.
If you cannot register due to firewall issues (e.g. you cannot see the capcha box) please email Miles Stoudenmire to ask for an account.

To report ITensor bugs, please use the issue tracker.