# Setting a specific element of an ITensor

**URL:** <https://itensor.discourse.group/t/setting-a-specific-element-of-an-itensor/721>\
**Category:** ITensor Julia Questions\
**Created:** [March 23, 2023, 3:59pm UTC](https://itensor.discourse.group/t/setting-a-specific-element-of-an-itensor/721 "2023-03-23T15:59:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidStrachan1](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/davidstrachan1/32/165_2.png) [@DavidStrachan1](https://itensor.discourse.group/u/DavidStrachan1)\
**Post date:** [March 23, 2023, 3:59pm UTC](https://itensor.discourse.group/t/setting-a-specific-element-of-an-itensor/721/1 "2023-03-23T15:59:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [March 23, 2023, 8:01pm UTC](https://itensor.discourse.group/t/setting-a-specific-element-of-an-itensor/721/2 "2023-03-23T20:01:16Z")

</div>

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.

```julia
# 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?

---

<div class="post-metadata">

**Author:** ![DavidStrachan1](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/davidstrachan1/32/165_2.png) [@DavidStrachan1](https://itensor.discourse.group/u/DavidStrachan1)\
**Post date:** [March 24, 2023, 10:40am UTC](https://itensor.discourse.group/t/setting-a-specific-element-of-an-itensor/721/3 "2023-03-24T10:40:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [March 31, 2023, 3:07pm UTC](https://itensor.discourse.group/t/setting-a-specific-element-of-an-itensor/721/4 "2023-03-31T15:07:55Z")

</div>

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:

```julia
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).

---

<div class="post-metadata">

**Author:** ![DavidStrachan1](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/davidstrachan1/32/165_2.png) [@DavidStrachan1](https://itensor.discourse.group/u/DavidStrachan1)\
**Post date:** [April 9, 2023, 12:25pm UTC](https://itensor.discourse.group/t/setting-a-specific-element-of-an-itensor/721/5 "2023-04-09T12:25:59Z")

</div>

Ahh ok, that makes sense thankyou!

---

<div class="post-metadata">

**Author:** ![DavidStrachan1](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/davidstrachan1/32/165_2.png) [@DavidStrachan1](https://itensor.discourse.group/u/DavidStrachan1)\
**Post date:** [June 5, 2023, 2:55pm UTC](https://itensor.discourse.group/t/setting-a-specific-element-of-an-itensor/721/6 "2023-06-05T14:55:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [June 6, 2023, 6:07pm UTC](https://itensor.discourse.group/t/setting-a-specific-element-of-an-itensor/721/7 "2023-06-06T18:07:47Z")

</div>

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.
