Custom OpName with int converted to string

Hi, I am new to julia and itensor and got a problem in defining custom operators. I want to define a sequence of custom ITensors.op like

function ITensors.op(::OpName"M1", ::SiteType"Boson", d::Int)
function ITensors.op(::OpName"M2", ::SiteType"Boson", d::Int)
function ITensors.op(::OpName"M3", ::SiteType"Boson", d::Int)
function ITensors.op(::OpName"M4", ::SiteType"Boson", d::Int)

and so on. I tried to do this

for i in 1:4
function ITensors.op(::OpName"M"*string(i), ::SiteType"Boson", d::Int)
end

But then got an error saying:

syntax: “(::ITensors.SiteTypes.OpName{:M} * string(i))” is not a valid function argument name

How could I convert an int to a string and then use the string in the OpName?

I am using julia 1.12 and ITensor 0.9.15. Thanks.

You can do that with @eval:

for i in 1:4
    @eval begin
        function ITensors.op(::OpName{Symbol("M", $i)}, ::SiteType"Boson", d::Int)
            # ...
        end
    end
end
1 Like

Thanks! This is very helpful

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.