Qaintum / Qaintessent.jl

Digital quantum circuit simulator written in Julia
Apache License 2.0
30 stars 8 forks source link

Replace invpermute!! with invpermute! #135

Open LilithHafner opened 1 year ago

LilithHafner commented 1 year ago

This stops using the undocumented and therefore internal method Base.invpermute!! which may be removed from Base in 1.10. It also increases performance by up to 10x for large numbers of qbits at the cost of a decrease in performance up to 3x for few qbits.

benchmarking code

using BenchmarkTools, Random, Plots

old(state, perm) = @inbounds Base.invpermute!!(state, perm)
new(state, perm) = invpermute!(state, perm)

function time(func, n)
    s = rand(ComplexF64, n)
    p = randperm(n)
    p_buf = copy(p)
    @belapsed $func($s, copyto!($p_buf, $p)) setup=(randperm!($p); rand!($s))
end

x = 2 .^ (1:25)
y_old = [time(old, n) for n in x]
y_new = [time(new, n) for n in x]

plot(log2.(x), y_old ./ x, label="old")
plot!(log2.(x), y_new ./ x, label="new")
plot!(xlabel="N", yaxis=:log10, legend=:topleft, ylabel="seconds/2^N")

Result

Screenshot 2023-05-15 at 5 22 37 PM