# How to define a new model in VUMPS?

**URL:** <https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467>\
**Category:** ITensor Julia Questions\
**Tags:** dmrg\
**Created:** [October 14, 2022, 1:48am UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467 "2022-10-14T01:48:41Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meng-Zeng](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/meng-zeng/32/83_2.png) [@Meng-Zeng](https://itensor.discourse.group/u/Meng-Zeng)\
**Post date:** [October 14, 2022, 1:48am UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/1 "2022-10-14T01:48:41Z")

</div>

Hi Matt,

I’m trying to define my own model in VUMPS by following [heisenberg](https://github.com/ITensor/ITensorInfiniteMPS.jl/blob/main/src/models/heisenberg.jl) and [vumps](https://github.com/ITensor/ITensorInfiniteMPS.jl/blob/main/examples/vumps/vumps_heisenberg.jl). In particular, I used the following in vumps code (I only changed the name from “heisenberg” to “mymodel” with everything else untouched)

```julia
function unit_cell_terms(::Model"mymodel")
  opsum = OpSum()
  opsum += 0.5, "S+", 1, "S-", 2
  opsum += 0.5, "S-", 1, "S+", 2
  opsum += "Sz", 1, "Sz", 2
  return opsum
end
model = Model("mymodel")		

```

However, I got “LoadError: MethodError: no method matching unit\_cell\_terms(::Model{:mymodel})”. I feel I misunderstood how this works, but I don’t know where the key issue is.

Thank you for your help.  
-Meng

---

<div class="post-metadata">

**Author:** ![Vladimir](https://avatars.discourse-cdn.com/v4/letter/v/94ad74/32.png) [@Vladimir](https://itensor.discourse.group/u/Vladimir)\
**Post date:** [October 20, 2022, 8:54am UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/2 "2022-10-20T08:54:11Z")

</div>

I’m also trying to define a new model in VUMPS and ran into the same issue. Inserting the new unt\_cell\_terms function into ITensorInfiniteMPS solved it for me.

---

<div class="post-metadata">

**Author:** ![Meng-Zeng](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/meng-zeng/32/83_2.png) [@Meng-Zeng](https://itensor.discourse.group/u/Meng-Zeng)\
**Post date:** [October 20, 2022, 3:51pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/3 "2022-10-20T15:51:47Z")

</div>

Hi Vladimir,

Thanks for your response!  
I actually did the same thing: (everything is copy-paste except that I changed the name “heisenberg” to “mymodel”)

```julia
using ITensors
using ITensorInfiniteMPS

include(
  joinpath(
    pkgdir(ITensorInfiniteMPS), "examples", "vumps", "src", "vumps_subspace_expansion.jl"
  ),
)

#########################################################################
# VUMPS parameters

maxdim = 10 # Maximum bond dimension
cutoff = 1e-8 # Singular value cutoff when increasing the bond dimension
max_vumps_iters = 100 # Maximum number of iterations of the VUMPS algorithm at each bond dimension
vumps_tol = 1e-5
conserve_qns = true
outer_iters = 6 # Number of times to increase the bond dimension

##############################################################################
# CODE BELOW HERE DOES NOT NEED TO BE MODIFIED

N = 2 # Number of sites in the unit cell
initstate(n) = isodd(n) ? "↑" : "↓"
s = infsiteinds("S=1/2", N; conserve_qns, initstate)
println("s ",s)
ψ = InfMPS(s, initstate)

#########################################################
function unit_cell_terms(::Model"mymodel")
  opsum = OpSum()
  opsum += 0.5, "S+", 1, "S-", 2
  opsum += 0.5, "S-", 1, "S+", 2
  opsum += "Sz", 1, "Sz", 2
  return opsum
end
#########################################################
model = Model("mymodel")													
H = InfiniteSum{MPO}(model, s)	

```

I’m wondering how you did it differently that made it work.  
Thank you for your time.  
-Meng

---

<div class="post-metadata">

**Author:** ![Vladimir](https://avatars.discourse-cdn.com/v4/letter/v/94ad74/32.png) [@Vladimir](https://itensor.discourse.group/u/Vladimir)\
**Post date:** [October 20, 2022, 4:13pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/4 "2022-10-20T16:13:35Z")

</div>

Hi Meng,

I meant I put unit\_cell\_terms directly into the ITensorInfiniteMPS.jl file; it seems like it has to be defined before ITensorInfiniteMPS is precompiled.

---

<div class="post-metadata">

**Author:** ![Meng-Zeng](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/meng-zeng/32/83_2.png) [@Meng-Zeng](https://itensor.discourse.group/u/Meng-Zeng)\
**Post date:** [October 20, 2022, 7:37pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/5 "2022-10-20T19:37:52Z")

</div>

Hi Vladimir,

Following your hint, I added mymodel.jl in the same directory as the other models and added the line

```julia
include("models/mymodel.jl")

```

in the ITensorInfiniteMPS.jl file in my directory “.julia/packages/ITensorInfiniteMPS/Ogcvb/src/” and now it works!!!

Thank you so much for your help, I’ve been stuck on this for more than a week.

Best,  
-Meng

---

<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:** [October 20, 2022, 9:02pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/6 "2022-10-20T21:02:40Z")

</div>

You shouldn’t need to add the file for your model to the package directly, instead you can overload the function `unit_cell_terms` as follows, which can be defined anywhere:

```auto
function ITensorInfiniteMPS.unit_cell_terms(::Model"mymodel")
  opsum = OpSum()
  opsum += 0.5, "S+", 1, "S-", 2
  opsum += 0.5, "S-", 1, "S+", 2
  opsum += "Sz", 1, "Sz", 2
  return opsum
end

```

or alternatively you can import the function with `import ITensorInfiniteMPS: unit_cell_terms` before defining your own overload. This extends the `unit_cell_terms` for your own `Model` type.

---

<div class="post-metadata">

**Author:** ![Meng-Zeng](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/meng-zeng/32/83_2.png) [@Meng-Zeng](https://itensor.discourse.group/u/Meng-Zeng)\
**Post date:** [October 21, 2022, 5:29am UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/7 "2022-10-21T05:29:38Z")

</div>

Thanks Matt, this is a much easier solution.

---

<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:** [October 21, 2022, 1:01pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/8 "2022-10-21T13:01:38Z")

</div>

I’m not sure if it is implemented right now, but ideally a user could just pass the `OpSum` directly:

```julia
function mymodel_opsum()
  opsum = OpSum()
  opsum += 0.5, "S+", 1, "S-", 2
  opsum += 0.5, "S-", 1, "S+", 2
  opsum += "Sz", 1, "Sz", 2
  return opsum
end
H = InfiniteSum{MPO}(mymodel_opsum(), s)	

```

The `Model` type may be superfluous at this point since I made some simplifications to the `InfiniteSum` and related constructors.

If you have time please try that out, and if it doesn’t work it would be easy to add support for that.

---

<div class="post-metadata">

**Author:** ![Meng-Zeng](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/meng-zeng/32/83_2.png) [@Meng-Zeng](https://itensor.discourse.group/u/Meng-Zeng)\
**Post date:** [October 21, 2022, 7:58pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/9 "2022-10-21T19:58:03Z")

</div>

Hi Matt, I tried this and got the error message “LoadError: MethodError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer”

---

<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:** [October 28, 2022, 4:15pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/10 "2022-10-28T16:15:31Z")

</div>

Ok thanks, I’ll have to investigate that since it would be nice if that worked. Unfortunately I haven’t had much time to work on the VUMPS code recently.

---

<div class="post-metadata">

**Author:** ![andrewkhardy](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/andrewkhardy/32/439_2.png) [@andrewkhardy](https://itensor.discourse.group/u/andrewkhardy)\
**Post date:** [January 18, 2024, 9:58pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/11 "2024-01-18T21:58:07Z")

</div>

Hi Matt,

I’m assuming the answer is that this has been delayed to the TensorNetworks.jl release, but I’m wondering if you’ve gotten a chance to take a look at this?

---

<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:** [January 19, 2024, 3:42pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/12 "2024-01-19T15:42:46Z")

</div>

Is there a particular issue you are having? I believe everything should work if you define a model, the only thing that seems to not work is directly passing an `OpSum` which just has terms in a minimal unit cell that is smaller than the specified number of sites, but that shouldn’t block you from doing anything (it would just provide a slightly simpler interface).

---

<div class="post-metadata">

**Author:** ![andrewkhardy](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/andrewkhardy/32/439_2.png) [@andrewkhardy](https://itensor.discourse.group/u/andrewkhardy)\
**Post date:** [February 6, 2024, 4:05pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/13 "2024-02-06T16:05:36Z")

</div>

@mtfishman I was just curious if this simplified interface will make its way to the new release with `TensorNetworks.jl` ?

---

<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:** [February 6, 2024, 4:37pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/14 "2024-02-06T16:37:04Z")

</div>

`ITensorNetworks.jl` supports conversion of `OpSum` to finite tree tensor networks (TTN), which naturally includes finite MPS. We plan to support conversion of `OpSum` to infinite tensor networks like infinite MPS but that will require more work.

---

<div class="post-metadata">

**Author:** ![lorenzocarfora2000](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/lorenzocarfora2000/32/471_2.png) [@lorenzocarfora2000](https://itensor.discourse.group/u/lorenzocarfora2000)\
**Post date:** [May 24, 2024, 4:32pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/15 "2024-05-24T16:32:33Z")

</div>

Hello,

I would also like to ask how can the energy density of a newly defined model be extracted; is it just the energy of the considered Cell over the number of sites making up said cell? Or is different?

I am asking as the Extended Hubbard example in the Github contains the line:

```auto
energy_exact = reference(model, Observable("energy"); U=model_params.U / model_params.t)

```

which I understand can only be used for the models defined in the repository - or I am misusing it.

---

<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:** [May 24, 2024, 4:46pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/16 "2024-05-24T16:46:05Z")

</div>

Can you start a new post?

---

<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:** [May 24, 2024, 8:55pm UTC](https://itensor.discourse.group/t/how-to-define-a-new-model-in-vumps/467/17 "2024-05-24T20:55:33Z")

</div>


