Skip to content

Replace invpermute!! with invpermute!#135

Open
LilithHafner wants to merge 2 commits into
Qaintum:masterfrom
LilithHafner:patch-1
Open

Replace invpermute!! with invpermute!#135
LilithHafner wants to merge 2 commits into
Qaintum:masterfrom
LilithHafner:patch-1

Conversation

@LilithHafner
Copy link
Copy Markdown

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

@LilithHafner
Copy link
Copy Markdown
Author

On Julia 1.11.7 this is a strict performance improvement:

fig

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant