Closed ghost closed 1 year ago
Can you try the following function?
julia> function compute_gradient(params::AbstractVector{T}) where T
c = dispatch(circuit, params)
expect'(HamBlock, zero_state(Complex{T}, nqubits(c))=>c)[2]
end
dispatch
function is much safer in autodiff.zero_state(Complex{T}, nqubits(c))=>c)
.Can you try the following function?
julia> function compute_gradient(params::AbstractVector{T}) where T c = dispatch(circuit, params) expect'(HamBlock, zero_state(Complex{T}, nqubits(c))=>c)[2] end
- use non-inplace version
dispatch
function is much safer in autodiff.- let the register element type be consistent with the parameter type:
zero_state(Complex{T}, nqubits(c))=>c)
.
Many thanks!
I try to apply the above code to compute the hessian of a VQC, which includes a custom gate. However, it throws an error as:
setparams!(x, θ...) is not implemented
it says i did not implement some functions for this custom gate. But i already implement those functions mentioned in the docs, niparams
, getiparams
and setiparams!
.
Did i miss something?
YaoBlocks.setiparams!(x::AbstractBlock, a::Number, xs::Number...)
According to your error message, you function falls back to the above interface. Try to be more concrete than that one? And also please make sure you have imported this function before overloading.
YaoBlocks.setiparams!
Many thanks!
It turns out that i forget to implement setiparams
for my custom gate to use dispatch
. Now i get the desired hessian matrix.
Let's say we want to compute the hessian matrix of a VQC, i implement the following code and find it does not work. It seems that we should implement many base funcitons.
What is the most convenient way to obtain the hessian matrix using Yao.jl?
Thanks for your help!
Results: