Correct way to define states for a new SiteType which has an alias

Hi there,
I’m having some difficulties when I try to define a new SiteType, with some alias pointing to it.
First of all, I defined a custom SiteType I call vS=1/2; for this type, I defined some states and operators, such as

function ITensors.state(::StateName"Up", ::SiteType"vS=1/2")
    return ...
end

Everything works as intended, so far.
Now, I’d like to define an alias, named HvS=1/2, to this site type.
I added some lines, mimicking the behaviour of some already existing site types:

ITensors.alias(::SiteType"HvS=1/2") = SiteType"vS=1/2"()

ITensors.space(st::SiteType"HvS=1/2") = ITensors.space(ITensors.alias(st))

function ITensors.state(sn::StateName, st::SiteType"HvS=1/2", s::Index; kwargs...)
    return ITensors.state(sn, ITensors.alias(st), s; kwargs...)
end

and so on for the operators.

The problem is, when I define an Index of this second site type, my definitions above for vS=1/2 aren’t reached:

i = Index(4, "HvS=1/2")
state(i, "Up")

returns

ERROR: ArgumentError: Overload of "state" or "state!" functions not found for state name "Up" and Index tags "HvS=1/2"

If i try to follow the functions calls in the REPL with @which I get

@which ITensors.state(StateName("Up"), SiteType("HvS=1/2"), i)
state(sn::StateName, st::SiteType{HvS=1/2}, s::Index; kwargs...) in mypackage at ...

which is okay, this is the function I defined above; but then

@which ITensors.state(StateName("Up"), ITensors.alias(SiteType("HvS=1/2")), i)
state(::StateName, ::SiteType, ::Index; kwargs...) in ITensors at /opt/julia/packages/ITensors/LXBUp/src/physics/sitetype.jl:524

which is the “default” function in ITensors.jl which returns nothing.
Here I thought that my ITensors.state(::StateName"Up", ::SiteType"vS=1/2") above would get called.
What am I missing? Do I need do define something else?

I think the reason for the error you are getting is that the state function shouldn’t be defined to take an Index argument. If you look at the examples here, they only take a StateName and SiteType.

When I remove the Index argument your alias approach worked correctly.

Here is a fully working example using a custom “S=3/2” SiteType as the original site type:

ITensors.space(::SiteType"S=3/2") = 4

ITensors.op(::OpName"Sz",::SiteType"S=3/2") =
  [+3/2   0    0    0
     0  +1/2   0    0
     0    0  -1/2   0
     0    0    0  -3/2]

ITensors.state(::StateName"Up",::SiteType"S=3/2") = [1.0, 0, 0, 0]

#---------------------------------------------

ITensors.alias(::SiteType"HvS=3/2") = SiteType"S=3/2"()

ITensors.space(st::SiteType"HvS=3/2") = ITensors.space(ITensors.alias(st))

function ITensors.state(sn::StateName, st::SiteType"HvS=3/2"; kwargs...)
  return ITensors.state(sn, ITensors.alias(st); kwargs...)
end
1 Like