# Extended function not being seen (Is explicity imported)

**URL:** https://discourse.julialang.org/t/extended-function-not-being-seen-is-explicity-imported/51200
**Category:** General Usage
**Created:** [December 3, 2020, 2:49pm UTC](https://discourse.julialang.org/t/extended-function-not-being-seen-is-explicity-imported/51200 "2020-12-03T14:49:54Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Annie](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Annie](https://discourse.julialang.org/u/Annie)
#### Post date: [December 3, 2020, 2:49pm UTC](https://discourse.julialang.org/t/extended-function-not-being-seen-is-explicity-imported/51200/1 "2020-12-03T14:49:54Z")

</div>

I’ve been looking at extending functions to work with a self-defined data type. Most of them I got working no problem but I am struggling to get one of them working.

I define a type

```julia
mutable struct ImagTimeEvolution{N,Tt,HT<:AbstractBlock{N}} <: PrimitiveBlock{N}
 ....
end

```

and then the extension of the function apply\_back!

```julia
function YaoBase.apply_back!(st, block::ImagTimeEvolution{N}, collector) where {N}
 ....
end

```

When I run it by itself it works as expected but when I run

```julia
_, grad = expect'(heisenberg(10), rand_state(10)=>imag_time_evolve(heisenberg(10), 10))

```

Which calls the function I’m trying to extend I receive the error

```julia
no method matching apply_back(::Tuple{ArrayReg{1,Complex{Float64},Array{Complex{Float64},2}},ArrayReg{1,Complex{Float64},Array{Complex{Float64},2}}}, ::ImagTimeEvolution{10,Int64,Add{10}}, ::Array{Any,1})

```

I’ve had a google and it seems this problem is generally resolved by explicitly importing the function being extended but I believe I’m doing that as I am using YaoBase.apply\_back! . One thought I had is the error message doesn’t include the ! mark but where the error is being called is calling the function with ! so don’t think that’s the issue.

Any help would be greatly appreciated! (Also this is my first time posting here so if the formating is way off or I’m missing some necessary info I apologise, and please let me know. I will then try to correct it)

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [December 3, 2020, 2:56pm UTC](https://discourse.julialang.org/t/extended-function-not-being-seen-is-explicity-imported/51200/2 "2020-12-03T14:56:04Z")

</div>

> [@Annie](#):
>
> One thought I had is the error message doesn’t include the ! mark but where the error is being called is calling the function with ! so don’t think that’s the issue.

It’s a little hard to parse this–can you post the stack trace of the error so that we can see what you mean?

---

<div class="post-metadata">

### Author: ![Annie](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Annie](https://discourse.julialang.org/u/Annie)
#### Post date: [December 3, 2020, 2:57pm UTC](https://discourse.julialang.org/t/extended-function-not-being-seen-is-explicity-imported/51200/3 "2020-12-03T14:57:17Z")

</div>

Of course

```julia
ERROR: LoadError: MethodError: no method matching apply_back(::Tuple{ArrayReg{1,Complex{Float64},Array{Complex{Float64},2}},ArrayReg{1,Complex{Float64},Array{Complex{Float64},2}}}, ::ImagTimeEvolution{10,Int64,Add{10}}, ::Array{Any,1})
Closest candidates are:
  apply_back(::Tuple{var"#s317",var"#s316"} where var"#s316"<:ArrayReg where var"#s317"<:ArrayReg, ::AbstractBlock; kwargs...) at C:\Users\ap877\.julia\packages\YaoBlocks\a3uN9\src\autodiff\apply_back.jl:149
Stacktrace:
 [1] apply_back!(::Tuple{ArrayReg{1,Complex{Float64},Array{Complex{Float64},2}},ArrayReg{1,Complex{Float64},Array{Complex{Float64},2}}}, ::ImagTimeEvolution{10,Int64,Add{10}}, ::Array{Any,1}) at C:\Users\ap877\.julia\packages\YaoBlocks\a3uN9\src\autodiff\apply_back.jl:25
 [2] apply_back(::Tuple{ArrayReg{1,Complex{Float64},Array{Complex{Float64},2}},ArrayReg{1,Complex{Float64},Array{Complex{Float64},2}}}, ::ImagTimeEvolution{10,Int64,Add{10}}; 
kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\ap877\.julia\packages\YaoBlocks\a3uN9\src\autodiff\apply_back.jl:151
 [3] apply_back(::Tuple{ArrayReg{1,Complex{Float64},Array{Complex{Float64},2}},ArrayReg{1,Complex{Float64},Array{Complex{Float64},2}}}, ::ImagTimeEvolution{10,Int64,Add{10}}) 
at C:\Users\ap877\.julia\packages\YaoBlocks\a3uN9\src\autodiff\apply_back.jl:150
 [4] expect_g(::Add{10}, ::Pair{ArrayReg{1,Complex{Float64},Array{Complex{Float64},2}},ImagTimeEvolution{10,Int64,Add{10}}}) at C:\Users\ap877\.julia\packages\YaoBlocks\a3uN9\src\autodiff\specializes.jl:15
 [5] (::Adjoint{Any,typeof(expect)})(::Add{10}, ::Pair{ArrayReg{1,Complex{Float64},Array{Complex{Float64},2}},ImagTimeEvolution{10,Int64,Add{10}}}) at C:\Users\ap877\.julia\packages\YaoBlocks\a3uN9\src\autodiff\specializes.jl:8
 [6] top-level scope at C:\Users\ap877\OneDrive - University of Exeter\Julia\GSEProject\ImagTEBlock.jl:81
 [7] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1088
in expression starting at C:\Users\ap877\OneDrive - University of Exeter\Julia\GSEProject\ImagTEBlock.jl:81

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [December 3, 2020, 3:02pm UTC](https://discourse.julialang.org/t/extended-function-not-being-seen-is-explicity-imported/51200/4 "2020-12-03T15:02:04Z")

</div>

Ah, looks like you ran into this: [https://github.com/QuantumBFS/YaoBlocks.jl/blob/61ef37aad1b09cbb07b188cc1d9678a22ef21c9d/src/autodiff/apply\_back.jl#L17-L27](https://github.com/QuantumBFS/YaoBlocks.jl/blob/61ef37aad1b09cbb07b188cc1d9678a22ef21c9d/src/autodiff/apply_back.jl#L17-L27)

That code looks wrong to me–there clearly _is_ an applicable method (you wrote it!) but that code chooses to throw a `MethodError` anyway, which makes it look like your method doesn’t exist.

I’m not familiar with how that code works, but hopefully someone else here will be. Otherwise you might consider opening an issue on the YaoBlocks.jl repo. In any case, this means that you did extend the function correctly, there’s just some other suble thing that YaoBlocks.jl is expecting.

---

<div class="post-metadata">

### Author: ![Annie](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Annie](https://discourse.julialang.org/u/Annie)
#### Post date: [December 3, 2020, 4:01pm UTC](https://discourse.julialang.org/t/extended-function-not-being-seen-is-explicity-imported/51200/5 "2020-12-03T16:01:35Z")

</div>

Thanks!

After some routing around in source code I figured it out. It seems there is a YaoBase.apply\_back! and a YaoBloacks.AD.apply\_back! I needed to extend the second not the first.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [December 3, 2020, 4:30pm UTC](https://discourse.julialang.org/t/extended-function-not-being-seen-is-explicity-imported/51200/6 "2020-12-03T16:30:22Z")

</div>

Ok, good to know. It still might be worth opening an issue–throwing a `MethodError` without an explanation is pretty confusing (and incorrect anyway), and I’m sure you’re neither the first nor the last person to be bitten by this.

This is quite similar to another questionable behavior in Julia Base: [Cannot find overloaded method - #7 by GunnarFarneback](https://discourse.julialang.org/t/cannot-find-overloaded-method/48633/7)
