Setting a specific element of an ITensor

I’m currently trying to create an ITensor element by element and I’m having a bit of trouble. I have a a vector of site indices (s=siteinds(“Fermion”,N)) and I want to create T=ITensor(s,s’). I know I can set an element of this by writing T[s[1]=>1,s[2]=>1,…,s[1]‘=>1,s[2]’=>1,…]=value, but for large N this is very inconvenient. What I want to do is something like T[s=>x,s’=>y] where x and y are vectors of integers length(N). Is there a way to do this?

Hi David,
It’s a good idea for a feature to have an interface like that. But we don’t currently offer that. So here is another approach that should work well, in terms of being convenient to code.

# Define your vectors of values, x's and  y's
values = Float64[ ... ] # not literal code,  just showing the types
x = Int[ ... ]  # not literal code
y = Int[ ... ] # not literal code

for (n,m,val) in zip(x,y,values)
  T[s=>n, s'=>m] = val
end

Basically I’m just suggesting to use a for loop to go over all the elements of x and y and the values you want to set. Will that work for you?

Isn’t s a vector of indices here with n and m being integers so s=>n can’t be done?

I see, I didn’t look closely enough at the first part of your question where you defined s as an array of indices (return value of siteinds).

So a good way to do what you want here is to do some Julia array manipulations and use “splatting”. For example, if you have an array of indices x ::Vector{Int} of the same size as your s array of indices you can do the following:

s = siteinds("S=1/2",4)
x = [1,2,1,2]
T = ITensor(s)
v = [s[j]=>x[j] for j=1:N]
T[v...] = value

where in the last line there I’ve “splatted” the vector v into the brackets of T which is equivalent to writing T[s[1]=>x[1], s[2]=>x[2], s[3]=>x[3], s[4]=>x[4]] = value except that this code will work for any N (assuming I define x to have length N of course).

Ahh ok, that makes sense thankyou!

Hi Miles, I have an associated problem when using this code. The reason I want to create the ITensor element-wise is to reduce the peak RAM usage as this is quite a large object. The problem with this is that the ITensor only has a well defined QN flux once all the elements are calculated, so will throw an error when the tensor is being built. Is there a way to temporarily turn the quantum number conservation off to prevent this happening?

Once you set the first element of the tensor, it uses that element to define the flux of the entire tensor. So if you are getting an error that the element you are trying to set isn’t consistent with the QN flux of the ITensor, it indicates that the tensor you are trying to create doesn’t have a well defined flux, perhaps you are trying to set the wrong elements of the tensor.