How to metapgrogram with custom-defined spins?

Hi everyone!

I just started using ITensor.jl and was trying to define a custom group of sitetype e.g. Q-state Potts; what I was trying to do is as following:

  1. I define the general rules for the Hilbert space for all Q-state potts model, with Q as a hyper-parameter, and write it down as Make-a-Custom-Local-Hilbert-Space indicated
  2. I wrap it as a string and use common Julia meta-prog trick to parse it from a for loop e.g. Q in [2, 3, 4, …]

However, during the 2nd step I found it impossible to parse the script and interpolate Q correctly, it seems that strings like ::SiteType"Q={Q}" can’t be directly treated. May I ask if there’s any existing solutions to my need? Or any other workaround is welcome as well.

Thank you for any help or comment!

Thanks for the question. My recommendation would be to not do this using tricks with strings, but in one of the following ways below:

  1. one way would be to just define your space function to accept an optional dim keyword argument
function ITensors.space(::SiteType"Q"; dim=2)
 ...
end

then make your op overloads like this:

function op(on::OpName"Name", st::SiteType"Q", s::Index)
....
end

so that you have access to the actual site index s in your op function body, and you can get its dimension by just calling dim(s). Note that this version of op is expected to return an ITensor, not a matrix.

  1. use the pattern used for the “Qudit” site type, code here:
    https://github.com/ITensor/ITensors.jl/blob/main/src/physics/site_types/qudit.jl
    The main thing to notice is that calls to op are routed through the function labeled #interface at the end of the file. This passes additional d::Int dimension arguments to the op overloads earlier in the file, which return matrices.
1 Like