# no method matching

**URL:** <https://itensor.discourse.group/t/no-method-matching/604>\
**Category:** ITensor Julia Questions\
**Tags:** julia\
**Created:** [January 21, 2023, 2:14am UTC](https://itensor.discourse.group/t/no-method-matching/604 "2023-01-21T02:14:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jake](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/jake/32/521_2.png) [@jake](https://itensor.discourse.group/u/jake)\
**Post date:** [January 21, 2023, 2:14am UTC](https://itensor.discourse.group/t/no-method-matching/604/1 "2023-01-21T02:14:16Z")

</div>

Hi everyone,

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.

 ![Capture](https://global.discourse-cdn.com/free1/uploads/itensor/original/1X/d1b89fafb4819ec72e8c4c10d6b77515c6b979bf.jpeg)

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [January 22, 2023, 8:20am UTC](https://itensor.discourse.group/t/no-method-matching/604/2 "2023-01-22T08:20:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jake](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/jake/32/521_2.png) [@jake](https://itensor.discourse.group/u/jake)\
**Post date:** [January 22, 2023, 5:44pm UTC](https://itensor.discourse.group/t/no-method-matching/604/3 "2023-01-22T17:44:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jake](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/jake/32/521_2.png) [@jake](https://itensor.discourse.group/u/jake)\
**Post date:** [January 22, 2023, 5:47pm UTC](https://itensor.discourse.group/t/no-method-matching/604/4 "2023-01-22T17:47:08Z")

</div>

Here is the code verbatim since I cannot upload a .txt file

```auto
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

```

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [January 23, 2023, 9:29am UTC](https://itensor.discourse.group/t/no-method-matching/604/5 "2023-01-23T09:29:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jake](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/jake/32/521_2.png) [@jake](https://itensor.discourse.group/u/jake)\
**Post date:** [January 24, 2023, 3:06am UTC](https://itensor.discourse.group/t/no-method-matching/604/6 "2023-01-24T03:06:57Z")

</div>

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.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [January 24, 2023, 4:02am UTC](https://itensor.discourse.group/t/no-method-matching/604/7 "2023-01-24T04:02:02Z")

</div>

Thanks!

Unfortunately, (or fortunately?) it’s working perfectly on my machine when I run that code exactly as you posted it.

Also I ran it on a different machine I have that is using Julia 1.8.2 and ITensors 0.3.20 and also the code ran perfectly there.

Are you loading any other libraries along with the above code, such as in your .julia/config/startup.jl file?
