-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CUDA patch #449
base: master
Are you sure you want to change the base?
CUDA patch #449
Changes from 4 commits
8a6a7d3
7db5152
8ecd025
e638027
489d3de
b0741ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
function sample(weights::CuVector) | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Test | ||
using Random | ||
using CUDA | ||
using YaoSubspaceArrayReg | ||
using YaoArrayRegister | ||
using StatsBase | ||
using CuYao | ||
using GPUArrays | ||
using Adapt | ||
using BloqadeCUDA | ||
|
||
|
||
struct Sampler{A,B} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we can start with a simple version without these abstractions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I can't seem to figure out how to make the kernel compile without the abstractions. It is complaining about |
||
cum_prob::A | ||
values::B | ||
end | ||
|
||
struct CuWeights{T<:Real} | ||
values::CuVector{T} | ||
sum::T | ||
end | ||
|
||
|
||
function Adapt.adapt_structure(to, sampler::Sampler) | ||
cum_prob = Adapt.adapt_structure(to, sampler.cum_prob) | ||
values = Adapt.adapt_structure(to, sampler.values) | ||
Sampler(cum_prob,values) | ||
end | ||
|
||
|
||
function StatsBase.Weights(values::CuVector{T}) where {T<:Real} | ||
return CuWeights{eltype(values)}(values,sum(values)) | ||
end | ||
|
||
function (sampler::Sampler)(x) | ||
i = searchsortedfirst(sampler.cum_prob, x) | ||
i = clamp(i, firstindex(sampler.values), lastindex(sampler.values)) | ||
@inbounds sampler.values[i] | ||
end | ||
|
||
|
||
function sample(rng::AbstractRNG, subspace_v::CuVector, weights::CuWeights,nshots::Integer) | ||
dices = rand(rng, nshots) | ||
sampler = Sampler(cumsum(weights.values),subspace_v) | ||
|
||
Array(sampler.(dices)) | ||
end | ||
|
||
|
||
|
||
space = Subspace(10, sort(randperm(1 << 10)[1:76] .- 1)) | ||
r = SubspaceArrayReg(randn(ComplexF64, 76), space) | ||
normalize!(r) | ||
dr = cu(r) | ||
|
||
|
||
weights = Weights(abs2.(relaxedvec(dr))) | ||
subspace_v = vec(dr.subspace) | ||
|
||
samples = sample(CURAND.default_rng(),subspace_v,weights,1000) | ||
|
||
# weights = Weights(abs2.(relaxedvec(dr))) | ||
# @which measure(r) | ||
|
||
# measure(dr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
SubspaceArrayReg
already overloadedadapt
thusadapt(CuArray, reg)
should just work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might have missed that