Index set for ITensor

I am new to Julia and ITensor, so sorry in advance if it is a basic question: Is there a possible way to have an array of indices? My initial example is

using ITensors

function main()
    N = 4
    dims = 2*ones(Int, N)
    U = rand(dims[1], dims[2], dims[3], dims[4])

    i1 = Index(2, "i1")
    i2 = Index(2, "i2")
    i3 = Index(2, "i3")
    i4 = Index(2, "i4")

    A = ITensor(U, i1, i2, i3, i4)
    @show A

end

main()

and I would like to generalize for arbitrary N.

Okay, I found a solution

using ITensors

function main(; N)
    dims = 2*ones(Int, N)
    U = rand(dims...)
    j = [Index(2, "i$k") for k=1:N]

    A = ITensor(U, j...)
    @show A


end

main(N=4)

where dims... unpacks the elements of dims. Then I would expect A to have indices i[1], [2], ..., i[N]. The only part that (obviously) fails is IndexSet(2, N, "i")

1 Like

Glad you have found a good solution. Here are a few other notes:

  • we have a function called randomITensor that you can use instead of having to first make a random Julia tensor then put it into an ITensor. In your code above you could use it like this: A = randomITensor(j...)
  • if you are making an array of general indices with custom properties, like the tags “i1”, “i2”, that you made then your approach is the best one as it gives you the most control. However, if you specifically want to make an array of “site” indices with the tag “Site” as well as other “physics” tags like “S=1/2” or “S=1” or “Electron” or “Boson” then we have a special function for that called siteinds which you can use like:
s = siteinds("S=1/2",N)

and it will return an array of N indices with the tags “Site,S=1/2,n=1” for the first index, with “n=$n” for the nth index.

1 Like