# Using the val function that takes a customised SiteType argument

**URL:** <https://itensor.discourse.group/t/using-the-val-function-that-takes-a-customised-sitetype-argument/440>\
**Category:** ITensor Julia Questions\
**Created:** [September 30, 2022, 3:57pm UTC](https://itensor.discourse.group/t/using-the-val-function-that-takes-a-customised-sitetype-argument/440 "2022-09-30T15:57:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eliem](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Eliem](https://itensor.discourse.group/u/Eliem)\
**Post date:** [September 30, 2022, 3:57pm UTC](https://itensor.discourse.group/t/using-the-val-function-that-takes-a-customised-sitetype-argument/440/1 "2022-09-30T15:57:41Z")

</div>

Hi,  
I’m trying to write a code including the val function that takes a new customised SiteType argument as parameter. The code below

```julia
using ITensors

mysitetype="testsite"
stype = SiteType(mysitetype)
vname =ValName("testname")
sname = StateName("testname")
@eval ITensors.val(vname,stype) = 1
@eval ITensors.state(sname, stype) = [1]

val(sname, stype)
s=Index(1,mysitetype)
println(val(s,vname))
println(val(s,sname))
println(val(s,"testname"))

```

displays an error in its final line, showing that the val function could not be overloaded if I’m using the site type name “testname” as one of its parameters.

Is there anything in the Julia/ITensor syntax that I’m missing?

Thanks very much  
Elie

---

<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:** [September 30, 2022, 9:34pm UTC](https://itensor.discourse.group/t/using-the-val-function-that-takes-a-customised-sitetype-argument/440/2 "2022-09-30T21:34:24Z")

</div>

Thanks for the question, Elie. Yes what you were missing is that in the line:

```auto
ITensors.val(vname,stype) = 1

```

(you don’t need the @eval so I left that off) the code `vname` and `stype` are just “dummy variables” in the physics/math sense. Their names don’t convey any meaning to the Julia compiler. It’s exactly the same as if you had written:

```auto
ITensors.val(x,y,) = 1

```

The correct way to overload the `val` function is to make an overload which takes only restricted **types**. These types can be conveniently made using a macro that has the syntax:

```auto
ValName"myval"
StateName"testname"

```

for example. Then you can do:

```auto
ITensors.val(::ValName"myval",::StateName"testname") = 1

```

where the double semicolons here are the really crucial thing. They are constraining the first argument to have the type `ValName"myval"` and the second argument to have the type `StateName"testname"`.

Here is a corrected version of your code:

```auto
using ITensors

mysitetype="testsite"
stype = SiteType(mysitetype)
#vname =ValName("testname")
#sname = StateName("testname")
ITensors.val(::ValName"myval",::SiteType"testsite") = 1
ITensors.state(::StateName"testname", ::StateName"testsite") = [1]

val(sname, stype)
s=Index(1,mysitetype)
println(val(s,vname))
println(val(s,sname))
println(val(s,"myval"))

```

---

<div class="post-metadata">

**Author:** ![andyjaj](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/andyjaj/32/157_2.png) [@andyjaj](https://itensor.discourse.group/u/andyjaj)\
**Post date:** [October 1, 2022, 9:59am UTC](https://itensor.discourse.group/t/using-the-val-function-that-takes-a-customised-sitetype-argument/440/3 "2022-10-01T09:59:34Z")

</div>

Hi Miles,  
What we want to do is allow for run time definition of a basis using input from a file or e.g. a command line.  
So is there a way to replace what is in the double quotes with a variable? This is probably just Julia syntax that we don’t understand.  
Cheers,  
A.

---

<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:** [October 1, 2022, 4:40pm UTC](https://itensor.discourse.group/t/using-the-val-function-that-takes-a-customised-sitetype-argument/440/4 "2022-10-01T16:40:57Z")

</div>

I see, thanks. When you “definition of a basis” which aspects of the basis do you want to depend on the input from the file? Do you want the name of the basis (SiteType) and of its Vals (ValType) etc. to depend on the external input? Or more like the numerical aspects of the basis like numerically what operators it has, its Hilbert space size, etc? The more explicit information you can give the more helpful my answer could be.

---

<div class="post-metadata">

**Author:** ![andyjaj](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/andyjaj/32/157_2.png) [@andyjaj](https://itensor.discourse.group/u/andyjaj)\
**Post date:** [October 3, 2022, 8:36am UTC](https://itensor.discourse.group/t/using-the-val-function-that-takes-a-customised-sitetype-argument/440/5 "2022-10-03T08:36:08Z")

</div>

Hi Miles, yes what we want is to read in a file that has a list of states defining a local Hilbert space and the associated quantum numbers and then also read in separate files that define operators acting on that space. The name of the states and the sitetype are unimportant (though it could be based on the file name for example or just “CustomSiteType”).  
Cheers,  
A.

---

<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:** [October 4, 2022, 1:51am UTC](https://itensor.discourse.group/t/using-the-val-function-that-takes-a-customised-sitetype-argument/440/6 "2022-10-04T01:51:51Z")

</div>

I see, that’s helpful.

The easiest route I can think of for doing this would be to define a few global variables that hold the relevant information (e.g. set of quantum numbers) that you would like to be deduced from the external input files. You can set these global variables based on what you read in from the files.

Then you can follow the guide about making custom local Hilbert spaces (SiteType’s) here:  
[https://itensor.github.io/ITensors.jl/stable/examples/Physics.html#Make-a-Custom-Local-Hilbert-Space-with-QNs](https://itensor.github.io/ITensors.jl/stable/examples/Physics.html#Make-a-Custom-Local-Hilbert-Space-with-QNs)  
and inside the `space` function or various `op` functions you can have those functions look at the global variables that you set when they are creating the relevant thing like making the space of each Index (`space`) or making a particular operator (`op`).

Even though global variables are generally frowned upon in programming, I think this could be one case where they are a nice solution to your problem.

The only other idea I would have would be to take advantage of the fact that the functions `space` and `op` can accept arbitrary keyword arguments. So you could pass various keyword arguments to them, with information coming from the files you read in, to customize their behavior. But you’d have to think about if this is a viable approach depending on the degree of customization you’d want to do.

Actually one last idea is to consider making your input files be actual Julia code files. Then you could either define various variables in them to customize things following the patterns mentioned above, or you could even explicitly overload `space` and `op` for your site type and operators in those files.

---

<div class="post-metadata">

**Author:** ![andyjaj](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/andyjaj/32/157_2.png) [@andyjaj](https://itensor.discourse.group/u/andyjaj)\
**Post date:** [October 4, 2022, 8:54am UTC](https://itensor.discourse.group/t/using-the-val-function-that-takes-a-customised-sitetype-argument/440/7 "2022-10-04T08:54:35Z")

</div>

Thanks Miles, we’ll have a go at this.
