QuantumBFS / Yao.jl

Extensible, Efficient Quantum Algorithm Design for Humans.
https://yaoquantum.org
Other
924 stars 122 forks source link

CuYao compiles depending on value, not depending on type #362

Closed jlbosse closed 2 years ago

jlbosse commented 2 years ago

It seems like CuYao recompiles apply! every time the parameters of a circuit are changed, even though the circuit is still the same. This is in contrast to running a circuit on the CPU and and not on the GPU where compilation happens only once when running a circuit for the first time and then all future runs are fast when changing the parameters. See the following MWE:

using Yao, Yao.AD, Yao.EasyBuild
using CuYao
import CUDA

n = 10
d = 5
circuit = variational_circuit(n, d)

cureg = zero_state(n) |> cu
reg = zero_state(n) 

# every time this block gets run the first `apply!()` takes much longer than the second `appply!()`
begin
    cureg.state .= 0.
    @CUDA.allowscalar cureg.state[1] = 1.
    dispatch!(circuit, :random)
    @time apply!(cureg, circuit)
    @time apply!(cureg, circuit)
end

# only the very first time this block gets run the first `apply!()` invokes a compilation pass, after
# that the first `apply!()` and the second apply always take roughly the same time.
begin
    reg.state .= 0.
    reg.state[1] = 1.
    dispatch!(circuit, :random)
    @time apply!(reg, circuit)
    @time apply!(reg, circuit)
end
GiggleLiu commented 2 years ago

Just confirmed this issue, it was due to the use of closure when writing kernel functions, will fix it later today. Thanks for reporting the bug!

GiggleLiu commented 2 years ago

You issue will be fixed by this PR: https://github.com/QuantumBFS/CuYao.jl/pull/74 Thanks again for the bug report!

jlbosse commented 2 years ago

Great, and thank you for providing a fix that quickly!

GiggleLiu commented 2 years ago

The PR in CuYao has been merged and is being tagged, I will close this issue for now. Feel free to reopen it if the patch does not fix your issue.

jlbosse commented 2 years ago

Worked perfectly, thanks