# How to set the magnetic field in Fermi Hubbard model

**URL:** <https://itensor.discourse.group/t/how-to-set-the-magnetic-field-in-fermi-hubbard-model/1396>\
**Category:** ITensor Julia Questions\
**Created:** [January 8, 2024, 11:16am UTC](https://itensor.discourse.group/t/how-to-set-the-magnetic-field-in-fermi-hubbard-model/1396 "2024-01-08T11:16:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Faridun](https://avatars.discourse-cdn.com/v4/letter/f/45deac/32.png) [@Faridun](https://itensor.discourse.group/u/Faridun)\
**Post date:** [January 8, 2024, 11:16am UTC](https://itensor.discourse.group/t/how-to-set-the-magnetic-field-in-fermi-hubbard-model/1396/1 "2024-01-08T11:16:11Z")

</div>

Dear all,

I am trying to calculate excited states of spinful fermi hubbard model by using DMRG. I need to add magnetic field \sum{}h\_{i}(n\_{i,\uparrow} - n\_{i,\downarrow})/2. The values of magnetic field should be random at each site. Any idea how to implement this?

Thank you for your time.

---

<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:** [January 8, 2024, 3:40pm UTC](https://itensor.discourse.group/t/how-to-set-the-magnetic-field-in-fermi-hubbard-model/1396/2 "2024-01-08T15:40:43Z")

</div>

Our `OpSum` system allows you to add terms such as h n\_{i \uparrow} by doing:

```julia
os = OpSum()
...
os .+= h,"Nup",i

```

You can put this kind of summation into a `for` loop over `i` to sum over different sites. Then if you want the h values to be random, I would suggest making the random values inside an array named `h` then accessing them as `h[i]`.

For example of making an `OpSum` for the Hamiltonian of the Hubbard model, you can see this code example:  
[ITensors.jl/examples/dmrg/1d\_hubbard\_extended.jl at main · ITensor/ITensors.jl · GitHub](https://github.com/ITensor/ITensors.jl/blob/main/examples/dmrg/1d_hubbard_extended.jl)\>  
Note the part where the onsite interaction `U` is added:

```julia
 for i in 1:N
    os += U, "Nupdn", i
  end

```

You can write a similar part to add fields.

---

<div class="post-metadata">

**Author:** ![Faridun](https://avatars.discourse-cdn.com/v4/letter/f/45deac/32.png) [@Faridun](https://itensor.discourse.group/u/Faridun)\
**Post date:** [January 10, 2024, 10:37am UTC](https://itensor.discourse.group/t/how-to-set-the-magnetic-field-in-fermi-hubbard-model/1396/3 "2024-01-10T10:37:13Z")

</div>

Thank you for your reply! It was useful.  
Is it possible to do this with AutoMPO() ? For example:

```julia
ampo = AutoMPO()
...
    for i = 1:N
        ampo .+= h, "Nup", i
    end
    for i = 1:N
        ampo .+= -h, "Ndn", i
    end

```

Is this a same with which was written above? But I am afraid it does not work. How to add term -Ndn?  
For some reason I can not use OpSum(). Getting following error:  
UndefVarError: `OpSum` not defined in `Main`. What can be reason for this error?

---

<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:** [January 11, 2024, 2:32pm UTC](https://itensor.discourse.group/t/how-to-set-the-magnetic-field-in-fermi-hubbard-model/1396/4 "2024-01-11T14:32:19Z")

</div>

Yes, so `OpSum` is the new name for `AutoMPO`. We have updated all of the code examples and documentation to use `OpSum` but I suppose there are still examples on the forum using the older name.

I’m not sure the reason for that error, unfortunately. Are you putting `using ITensors` in your code? That should bring `OpSum` into your set of imported type names. If the error persists, could you show a minimal example to reproduce that error?
