+1 vote
asked by (760 points)


I have a couple of questions which relate to indexing in ITensor.

Firstly, I have an MPS in a spin-1/2 1D chain denoted by T. I can get the local 2-D tensors (at each) by doing setting the site-index to 1 or 2. I am pretty sure about this but can you confirm that this would give me the components in the computational (Sz) basis ? Now, I would like to change modify the tensor so that I can get this in the (Sx) basis. This basically amounts to adding and subtracting the local 2-D tensors. However, I am not sure how to put them back in the original tensor without messing up the indexing. Here is some minimal code:

using ITensors
sites = siteinds("S=1/2",N)
T = randomMPS(sites,bond_dim);
for j=1:length(T)
#I would like to assign Tx0 to something like T[j]*setelt(sites[j]=>1) but how?

Finally, I have been recently doing a lot of calculations involving contraction of ITensor objects and I was wondering if there is some in-built function that returns the different index of any ITensor object ( somewhat like how "linkind" works for MPS objects ).

In general, is the handling of ITensor indexing in Julia well documented somewhere?

commented by (70k points)
Hi Arnab,
Regarding ITensor indexing, it is documented here:

Please let us know if that part of the documentation leaves out some information you were looking for.

1 Answer

0 votes
answered by (70k points)
selected by
Best answer

Hi Arnab,
Thanks for the question. I'll give some brief answers and we can discuss more in comments below.

(1) "I can get the local 2-D tensors (at each) by doing setting the site-index to 1 or 2." This is not correct unless the MPS is a product (unentangled) state. I think what you are trying to do is to obtain the probability of finding some site j to be in the up state versus the down state. So you are looking for these two numbers, call them pup and pdn. These numbers are the diagonal elements of the reduced density matrix for site j. This reduced density matrix can be found by (a) first calling orthogonalize!(T,j) to make site j the orthogonality center of your MPS then (b) by creating the density matrix rhoj = T[j] * prime(dag(T[j]),"Site"). This density matrix will be a 2x2 matrix and its diagonal entries will be pup and p_dn. Please check that they add to 1.0.

(2) to obtain the probabilities of up and down states in a different basis, such as the x basis, you can perform a change of basis by acting with a unitary operator on site j. First, make sure your MPS is orthogonalized to site j as in (1a) above. Then obtain the j'th MPS tensor: Aj = T[j]. Now act with the Hadamard gate which will change the basis to the X basis: Ajx = noprime(op("H",sites,j)*Aj). Finally, make the density matrix in the X basis: rhox_j = Ajx * prime(dag(Ajx),"Site"). The diagonal entries of rhox_j will be the probabilities for up and down in the X basis.


commented by (70k points)
Hi, so this issue is fixed as of the latest version of ITensor, version 0.2.3. Could you please update to that version?

Also, in your longer version of `op`, there should not be an ITensor `Op` as the first argument. This version ought to work:

function ITensors.op( ::OpName"YRotfunc", ::SiteType"S=1/2", s::Index)
    Op = ITensor(ComplexF64,s',dag(s))
    Op[s'=>1,s=>1] = 1/sqrt(2)
    Op[s'=>2,s=>1] = -im/sqrt(2)
    Op[s'=>1,s=>2] = im/sqrt(2)
    Op[s'=>2,s=>2] = 1/sqrt(2)
    return Op
commented by (100 points)
I apologize if this is a naive question -- I'm new to Julia and ITensors -- but do you know if version 0.2.3 is compatible with Julia Pro? When I use the normal command:

pkg> update ITensors

no update occurs; the package remains at version 0.1.21.
commented by (70k points)
Based on the article linked below, it sounds like JuliaPro used to block certain package updates but that the latest versions of JuliPro (as of August 2020) now let you use any package versions you want.

However, there can be other reasons a package doesn't update, mainly that you have other packages which themselves aren't updated so will block the update of a certain package. So the thing to try is just doing "update" which should update all packages to their latest versions, if I remember correctly. Please be sure you want this to happen, though, and that it might take some minutes to complete.

commented by (100 points)
Thank you so much for all your help! I had to go from Julia Pro v. 1.5.1 to v. 1.5.4, and now installing ITensors automatically gives v. 2.3. Hopefully everything will fall into place now!
commented by (70k points)
Great! Glad that was all it took
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.