# Defining states with parameters as keyword arguments

**URL:** <https://itensor.discourse.group/t/defining-states-with-parameters-as-keyword-arguments/343>\
**Category:** ITensor Julia Questions\
**Created:** [August 14, 2022, 5:31pm UTC](https://itensor.discourse.group/t/defining-states-with-parameters-as-keyword-arguments/343 "2022-08-14T17:31:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phx](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/phx/32/673_2.png) [@phx](https://itensor.discourse.group/u/phx)\
**Post date:** [August 14, 2022, 5:31pm UTC](https://itensor.discourse.group/t/defining-states-with-parameters-as-keyword-arguments/343/1 "2022-08-14T17:31:48Z")

</div>

Hi everyone!

I defined a custom SiteType `vecOsc` and I’m trying to define some states and operator associated with it, by overloading the `ITensors.state` and `ITensors.op` functions.  
The construction of a certain state “Therm” requires additional arguments, say a temperature `T`, so I figured I could overload `state` like

```julia
ITensors.state(::StateName"Therm", ::SiteType"vecOsc"; T::Real)

```

which returns a vector. When I want to create such a state on an Index `s` with the tag `vecOsc`, I would call

```julia
ITensors.state(s, "Therm"; T=t)

```

If I understand correctly, the first thing that happens (looking at `src/physics/sitetype.jl:551`) is that the library calls

```julia
state(StateName("Therm"), SiteType(st); kwargs...)

```

trying each `st` from the tags of `s`. If the result is `nothing`, then it skips the tag and tries with the next one.

Here is the issue: if I create “vecOsc” sites through `siteinds` they will have the tags “Site” and “n=N” (for some `N`) too, and there is no

```julia
state(::StateName"Therm", ::SiteType"Site"; kwargs...)

```

defined. As a consequence, a MethodError exception is thrown, saying that there is no method matching my call.  
Only the following functions

```julia
state(::StateName, ::SiteType) = nothing
state(::StateName, ::SiteType, ::Index) = nothing
state!(::ITensor, ::StateName, ::SiteType, ::Index) = nothing

```

are defined as default, and none of them has keyword arguments, so they don’t match `state(::StateName"Therm", ::SiteType"Site"; kwargs...)` above.  
To solve this, I added in my script the following function

```julia
ITensors.state(::StateName, ::SiteType, ::Index; kwargs...) = nothing

```

and I got no more errors.

Now, I would also like to create a Julia package from my scripts, and these “states with parameters” are in it. When I load my package in the REPL, however, it warns me that the method definition `state(ITensors.StateName{Name} where Name, ITensors.SiteType{T} where T, ITensors.Index{T} where T)` in module ITensors at `.../src/physics/sitetype.jl:516` is overwritten by my own definition I added (the last one), and that as a consequence “incremental compilation may be fatally broken for this module”.

This obviously doesn’t feel right, so I ask you, is my workaround the right way to allow my states to have keyword arguments? Or are there better alternatives to create states with parameters?

---

<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:** [August 15, 2022, 8:11pm UTC](https://itensor.discourse.group/t/defining-states-with-parameters-as-keyword-arguments/343/2 "2022-08-15T20:11:16Z")

</div>

I think it was just an oversight that we don’t forward the keyword arguments through the `ITensors.state` function, it would be nice to add support for that so the code you are trying above will work (we have that supported for `ITensors.op` but I think we just didn’t have examples of state definitions with parameters so far). Would you mind making a pull request with the change you made above? Then we can include it in the next version of ITensors.jl.

---

<div class="post-metadata">

**Author:** ![phx](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/phx/32/673_2.png) [@phx](https://itensor.discourse.group/u/phx)\
**Post date:** [August 15, 2022, 9:13pm UTC](https://itensor.discourse.group/t/defining-states-with-parameters-as-keyword-arguments/343/3 "2022-08-15T21:13:11Z")

</div>

Sure thing! I always wondered why the code was there for `op` but not for `state`. I should just add the `; kwargs...` bit to the signatures of the three “default” functions, then, right? Or do I need to overload them separately?

---

<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:** [August 15, 2022, 9:18pm UTC](https://itensor.discourse.group/t/defining-states-with-parameters-as-keyword-arguments/343/4 "2022-08-15T21:18:48Z")

</div>

It looks like `; kwargs...` should be added to these lines:

> <https://github.com/ITensor/ITensors.jl/blob/1f47c7397abc2e1124a5df313cebaa088667924b/src/physics/sitetype.jl#L515-L517>

> <https://github.com/ITensor/ITensors.jl/blob/1f47c7397abc2e1124a5df313cebaa088667924b/src/physics/sitetype.jl#L520>

> <https://github.com/ITensor/ITensors.jl/blob/1f47c7397abc2e1124a5df313cebaa088667924b/src/physics/sitetype.jl#L571>

> <https://github.com/ITensor/ITensors.jl/blob/1f47c7397abc2e1124a5df313cebaa088667924b/src/physics/sitetype.jl#L581>
