I’m working on scalar field theory and had the code working but today I am getting an error “no method matching”. This code was working fine and I was extracting expectation values a couple of days ago. I have copied and pasted parts of the code necessary to make the code concise and still isolate the problem. My operators defined corresponding to the field does not seem to work. However, this seems to be the correct way to define custom operators. I’m sorry if I’m missing something trivial, but as I said this was working last time I had this open. Any help would be appreciated. Thank you very much.
Thanks for the question as working with the “Boson” site type can be tricky.
Here are some thoughts so far, and we can discuss further:
I wasn’t able to reproduce the error. When I run the code above it works well for me. So could you please post the code as text so I can copy it verbatim? Also what version of ITensors.jl and Julia are you using?
Relatedly, can you please post the code as text, not a screenshot? Then I can cut and paste it. Please use triple backticks (```) around the beginning and ending of the code block to format it.
The preferred way to “spell” the OpName for a custom operator is like OpName"phi"
Lastly, it may work better to define your custom op function in global scope (top of the file) rather than inside your let...end block
Thank you very much for your reply. Here is a text file with the code from above but with your suggestions incorporated. It still has the error. I am using Julia 1.8.2 and ITensor 0.3.23. Thank you again.
Here is the code verbatim since I cannot upload a .txt file
using ITensors
function ITensors.op(::OpName"phi",::SiteType"Boson", d::Int)
mat = zeros(d,d)
for i in 1:d
for j in 1:d
mat[i ,j] = (1/ sqrt(2))*( kd(i-1, j)*sqrt(j+1)+ kd(i, j-1)*sqrt(i+1) )
end
end
return mat
end
let
d= 10
N= 40
sites = siteinds("Boson", N, dim=d , conserve_qns= false )
op = OpSum()
for l in 1: N-1
op += (-1),"phi",l,"phi",l+1
end
H = MPO( op, sites)
inis= randomMPS(sites)
sweeps = Sweeps(5)
setmaxdim!( sweeps, 10,50,100,150,2000)
setcutoff!(sweeps,1E-9)
#@show sweeps
energy, state= dmrg(H, inis, sweeps)
#println(" Final energy = $energy")
nothing
end
Thanks. I find that I can run it without getting that error, using Julia 1.9.0-beta2 and ITensors 0.3.23.
I do get a different error that “kd” is not defined, however. So are you running the code above exactly as you posted and as a standalone script? Or are you loading it into the REPL (Julia console) with other variables previously defined, or some other way?
I updated to 1.8.5. My apologize for for not including the function definition. I had it defined in the notebook. Here is the complete code. I’m guessing that without the function definition you’ll get that error before the method matching error.
using ITensors
function kd(x,y)
if x == y
return 1
else
return 0
end
end
function ITensors.op(::OpName"phi",::SiteType"Boson", d::Int)
mat = zeros(d,d)
for i in 1:d
for j in 1:d
mat[i ,j] = (1/ sqrt(2))*( kd(i-1, j)*sqrt(j+1)+ kd(i, j-1)*sqrt(i+1) )
end
end
return mat
end
let
d= 10
N= 40
sites = siteinds("Boson", N, dim=d , conserve_qns= false )
op = OpSum()
for l in 1: N-1
op += (-1),"phi",l,"phi",l+1
end
H = MPO( op, sites)
inis= randomMPS(sites)
sweeps = Sweeps(5)
setmaxdim!( sweeps, 10,50,100,150,2000)
setcutoff!(sweeps,1E-9)
#@show sweeps
energy, state= dmrg(H, inis, sweeps)
#println(" Final energy = $energy")
nothing
end