Using the val function that takes a customised SiteType argument

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

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

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

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:

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:

ValName"myval"
StateName"testname"

for example. Then you can do:

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:

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"))

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.

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.

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.

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
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.

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