Skip to content

Commit

Permalink
rm SparseArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu committed Sep 21, 2024
1 parent a099305 commit f58c086
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 38 deletions.
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ version = "1.4.3"
[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Yao = "5872b779-8223-5990-8dd0-5abbb0748c8c"

[compat]
Expand Down
1 change: 0 additions & 1 deletion src/FLOYao.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ module FLOYao

using LinearAlgebra
using Random
using SparseArrays
using Yao
using Yao.YaoBlocks.LuxurySparse: SparseMatrixCOO

Expand Down
26 changes: 2 additions & 24 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,19 +352,10 @@ random_orthogonal_matrix(n) = random_orthogonal_matrix(Float64, n)
# Utilities for fast sparse matrix operations
# -------------------------------------------
"""
fast_add!(A::AbstractMatrix, B::SparseMatrixCSC)
fast_add!(A::AbstractMatrix, B::AbstractMatrix)
Fast implementation of `A .+= B` for sparse `B`.
"""
function fast_add!(A::AbstractMatrix, B::SparseMatrixCSC)
@assert size(A, 1) == size(B, 1) && size(A, 2) == size(B, 2) "Dimension mismatch"
for j = 1:size(B, 2)
for k in SparseArrays.nzrange(B, j)
@inbounds A[B.rowval[k],j] += B.nzval[k]
end
end
return A
end
function fast_add!(A::AbstractMatrix, B::SparseMatrixCOO)
@assert size(A, 1) == size(B, 1) && size(A, 2) == size(B, 2) "Dimension mismatch"
for (i, j, v) in zip(B.is, B.js, B.vs)
Expand All @@ -378,23 +369,10 @@ function fast_add!(A::AbstractMatrix, B::AbstractMatrix)
end

"""
fast_overlap(y::AbstractVecOrMat, A::SparseMatrixCSC, x::AbstractVecOrMat)
fast_overlap(y::AbstractVecOrMat, A::AbstractMatrix, x::AbstractVecOrMat)
Fast implementation of `tr(y' * A * x)` for sparse `A`.
"""
function fast_overlap(y::AbstractVecOrMat{T1}, A::SparseMatrixCSC{T2}, x::AbstractVecOrMat{T3}) where {T1,T2,T3}
@assert size(x, 1) == size(A, 2) && size(y, 1) == size(A, 1) && size(x, 2) == size(y, 2) "Dimension mismatch"
g = zero(promote_type(T1, T2, T3))
@inbounds for j = 1:size(A, 2)
for k in SparseArrays.nzrange(A, j)
i = A.rowval[k]
for m = 1:size(y, 2)
g += conj(y[i, m]) * A.nzval[k] * x[j, m]
end
end
end
return g
end
function fast_overlap(y::AbstractVecOrMat{T1}, A::SparseMatrixCOO{T2}, x::AbstractVecOrMat{T3}) where {T1,T2,T3}
@assert size(x, 1) == size(A, 2) && size(y, 1) == size(A, 1) && size(x, 2) == size(y, 2) "Dimension mismatch"
g = zero(promote_type(T1, T2, T3))
Expand Down
1 change: 0 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Yao = "5872b779-8223-5990-8dd0-5abbb0748c8c"
Expand Down
15 changes: 4 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,17 @@ end
nq = 4
x = randn(ComplexF64, 10, 10)
y = randn(ComplexF64, 10, 10)
A = FLOYao.sprand(ComplexF64, 10, 10, 0.3)
B = FLOYao.SparseMatrixCOO(Matrix(A))
r = FLOYao.fast_overlap(y, A, x)
dmat = [rand() < 0.4 ? randn() : 0.0 for i=1:10, j=1:10]
B = FLOYao.SparseMatrixCOO(dmat)
r2 = FLOYao.fast_overlap(y, B, x)
@test isapprox(r, tr(y' * A * x), atol=1e-7)
@test isapprox(r, y (A * x), atol=1e-7)
@test isapprox(r2, tr(y' * B * x), atol=1e-7)
@test isapprox(r2, y (B * x), atol=1e-7)
end

@testset "fast_add!" begin
A = randn(10, 10)
B = FLOYao.sprand(10, 10, 0.2)
D = FLOYao.SparseMatrixCOO(Matrix(FLOYao.sprand(10, 10, 0.2)))
C = copy(A)
@test FLOYao.fast_add!(C, B) A + B
C = copy(A)
@test FLOYao.fast_add!(C, Matrix(B)) A + B
dmat = [rand() < 0.4 ? randn() : 0.0 for i=1:10, j=1:10]
D = FLOYao.SparseMatrixCOO(dmat)

C = copy(A)
@test FLOYao.fast_add!(C, D) A + D
Expand Down

0 comments on commit f58c086

Please sign in to comment.