Skip to content

Commit 162c76b

Browse files
authored
Rename function sparse_to_dense as to_dense and dense_to_sparse as to_sparse (#392)
1 parent 8c728f0 commit 162c76b

24 files changed

+109
-93
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased](https://github.com/qutip/QuantumToolbox.jl/tree/main)
99

10+
- Rename `sparse_to_dense` as `to_dense` and `dense_to_sparse` as `to_sparse`. ([#392])
11+
1012
## [v0.26.0]
1113
Release date: 2025-02-09
1214

@@ -115,3 +117,4 @@ Release date: 2024-11-13
115117
[#386]: https://github.com/qutip/QuantumToolbox.jl/issues/386
116118
[#388]: https://github.com/qutip/QuantumToolbox.jl/issues/388
117119
[#389]: https://github.com/qutip/QuantumToolbox.jl/issues/389
120+
[#392]: https://github.com/qutip/QuantumToolbox.jl/issues/392

docs/src/resources/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ ket2dm
103103
expect
104104
variance
105105
LinearAlgebra.kron
106-
sparse_to_dense
107-
dense_to_sparse
106+
to_dense
107+
to_sparse
108108
vec2mat
109109
mat2vec
110110
```

docs/src/users_guide/QuantumObject/QuantumObject.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,14 @@ SparseMatrixCSC{Int64}(x_s)
214214
Matrix{Float64}(x_s)
215215
```
216216

217-
To convert between dense and sparse arrays, one can also use [`dense_to_sparse`](@ref) and [`sparse_to_dense`](@ref):
217+
To convert between dense and sparse arrays, one can also use [`to_sparse`](@ref) and [`to_dense`](@ref):
218218

219219
```@example Qobj
220-
x_d = sparse_to_dense(x_s)
220+
x_d = to_dense(x_s)
221221
```
222222

223223
```@example Qobj
224-
dense_to_sparse(x_d)
224+
to_sparse(x_d)
225225
```
226226

227227
!!! note "Convert to GPU arrays"

ext/QuantumToolboxCUDAExt.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ _change_eltype(::Type{T}, ::Val{32}) where {T<:AbstractFloat} = Float32
101101
_change_eltype(::Type{Complex{T}}, ::Val{64}) where {T<:Union{Int,AbstractFloat}} = ComplexF64
102102
_change_eltype(::Type{Complex{T}}, ::Val{32}) where {T<:Union{Int,AbstractFloat}} = ComplexF32
103103

104-
QuantumToolbox.sparse_to_dense(A::MT) where {MT<:AbstractCuSparseArray} = CuArray(A)
104+
QuantumToolbox.to_dense(A::MT) where {MT<:AbstractCuSparseArray} = CuArray(A)
105105

106-
QuantumToolbox.sparse_to_dense(::Type{T1}, A::CuArray{T2}) where {T1<:Number,T2<:Number} = CuArray{T1}(A)
107-
QuantumToolbox.sparse_to_dense(::Type{T}, A::AbstractCuSparseArray) where {T<:Number} = CuArray{T}(A)
106+
QuantumToolbox.to_dense(::Type{T1}, A::CuArray{T2}) where {T1<:Number,T2<:Number} = CuArray{T1}(A)
107+
QuantumToolbox.to_dense(::Type{T}, A::AbstractCuSparseArray) where {T<:Number} = CuArray{T}(A)
108108

109109
end

src/deprecated.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ end
1515
=#
1616

1717
export FFTCorrelation
18+
export sparse_to_dense, dense_to_sparse
1819

1920
FFTCorrelation() = error(
2021
"`FFTCorrelation` has been deprecated and will be removed in next major release, please use `spectrum_correlation_fft` to calculate the spectrum with FFT method instead.",
2122
)
2223

24+
sparse_to_dense(args...) = error(
25+
"`sparse_to_dense` has been deprecated and will be removed in next major release, please use `to_dense` instead.",
26+
)
27+
dense_to_sparse(args...) = error(
28+
"`dense_to_sparse` has been deprecated and will be removed in next major release, please use `to_sparse` instead.",
29+
)
30+
2331
correlation_3op_2t(
2432
H::QuantumObject{HOpType},
2533
ψ0::QuantumObject{StateOpType},

src/qobj/arithmetic_and_attributes.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ LinearAlgebra.tr(
298298
299299
Return the singular values of a [`QuantumObject`](@ref) in descending order
300300
"""
301-
LinearAlgebra.svdvals(A::QuantumObject) = svdvals(sparse_to_dense(A.data))
301+
LinearAlgebra.svdvals(A::QuantumObject) = svdvals(to_dense(A.data))
302302

303303
@doc raw"""
304304
norm(A::QuantumObject, p::Real)
@@ -415,7 +415,7 @@ Matrix square root of [`QuantumObject`](@ref)
415415
!!! note
416416
`√(A)` (where `√` can be typed by tab-completing `\sqrt` in the REPL) is a synonym of `sqrt(A)`.
417417
"""
418-
LinearAlgebra.sqrt(A::QuantumObject) = QuantumObject(sqrt(sparse_to_dense(A.data)), A.type, A.dimensions)
418+
LinearAlgebra.sqrt(A::QuantumObject) = QuantumObject(sqrt(to_dense(A.data)), A.type, A.dimensions)
419419

420420
@doc raw"""
421421
log(A::QuantumObject)
@@ -425,7 +425,7 @@ Matrix logarithm of [`QuantumObject`](@ref)
425425
Note that this function only supports for [`Operator`](@ref) and [`SuperOperator`](@ref)
426426
"""
427427
LinearAlgebra.log(A::QuantumObject{ObjType}) where {ObjType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} =
428-
QuantumObject(log(sparse_to_dense(A.data)), A.type, A.dimensions)
428+
QuantumObject(log(to_dense(A.data)), A.type, A.dimensions)
429429

430430
@doc raw"""
431431
exp(A::QuantumObject)
@@ -437,7 +437,7 @@ Note that this function only supports for [`Operator`](@ref) and [`SuperOperator
437437
LinearAlgebra.exp(
438438
A::QuantumObject{ObjType,DimsType,<:AbstractMatrix},
439439
) where {ObjType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},DimsType} =
440-
QuantumObject(dense_to_sparse(exp(A.data)), A.type, A.dimensions)
440+
QuantumObject(to_sparse(exp(A.data)), A.type, A.dimensions)
441441
LinearAlgebra.exp(
442442
A::QuantumObject{ObjType,DimsType,<:AbstractSparseMatrix},
443443
) where {ObjType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},DimsType} =

src/qobj/eigsolve.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,10 @@ function LinearAlgebra.eigen(
462462
kwargs...,
463463
) where {OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}}
464464
MT = typeof(A.data)
465-
F = eigen(sparse_to_dense(A.data); kwargs...)
465+
F = eigen(to_dense(A.data); kwargs...)
466466
# This fixes a type inference issue. But doesn't work for GPU arrays
467-
E::mat2vec(sparse_to_dense(MT)) = F.values
468-
U::sparse_to_dense(MT) = F.vectors
467+
E::mat2vec(to_dense(MT)) = F.values
468+
U::to_dense(MT) = F.vectors
469469

470470
return EigsolveResult(E, U, A.type, A.dimensions, 0, 0, true)
471471
end
@@ -478,7 +478,7 @@ Same as [`eigen(A::QuantumObject; kwargs...)`](@ref) but for only the eigenvalue
478478
LinearAlgebra.eigvals(
479479
A::QuantumObject{OpType};
480480
kwargs...,
481-
) where {OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} = eigvals(sparse_to_dense(A.data); kwargs...)
481+
) where {OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} = eigvals(to_dense(A.data); kwargs...)
482482

483483
@doc raw"""
484484
eigenenergies(A::QuantumObject; sparse::Bool=false, kwargs...)

src/qobj/functions.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Functions which manipulates QuantumObject
44

55
export ket2dm
66
export expect, variance
7-
export sparse_to_dense, dense_to_sparse
7+
export to_dense, to_sparse
88
export vec2mat, mat2vec
99

1010
@doc raw"""
@@ -113,42 +113,42 @@ variance(O::QuantumObject{OperatorQuantumObject}, ψ::QuantumObject) = expect(O^
113113
variance(O::QuantumObject{OperatorQuantumObject}, ψ::Vector{<:QuantumObject}) = expect(O^2, ψ) .- expect(O, ψ) .^ 2
114114

115115
@doc raw"""
116-
sparse_to_dense(A::QuantumObject)
116+
to_dense(A::QuantumObject)
117117
118118
Converts a sparse QuantumObject to a dense QuantumObject.
119119
"""
120-
sparse_to_dense(A::QuantumObject) = QuantumObject(sparse_to_dense(A.data), A.type, A.dimensions)
121-
sparse_to_dense(A::MT) where {MT<:AbstractSparseArray} = Array(A)
122-
sparse_to_dense(A::MT) where {MT<:AbstractArray} = A
120+
to_dense(A::QuantumObject) = QuantumObject(to_dense(A.data), A.type, A.dimensions)
121+
to_dense(A::MT) where {MT<:AbstractSparseArray} = Array(A)
122+
to_dense(A::MT) where {MT<:AbstractArray} = A
123123

124-
sparse_to_dense(::Type{T}, A::AbstractSparseArray) where {T<:Number} = Array{T}(A)
125-
sparse_to_dense(::Type{T1}, A::AbstractArray{T2}) where {T1<:Number,T2<:Number} = Array{T1}(A)
126-
sparse_to_dense(::Type{T}, A::AbstractArray{T}) where {T<:Number} = A
124+
to_dense(::Type{T}, A::AbstractSparseArray) where {T<:Number} = Array{T}(A)
125+
to_dense(::Type{T1}, A::AbstractArray{T2}) where {T1<:Number,T2<:Number} = Array{T1}(A)
126+
to_dense(::Type{T}, A::AbstractArray{T}) where {T<:Number} = A
127127

128-
function sparse_to_dense(::Type{M}) where {M<:SparseMatrixCSC}
128+
function to_dense(::Type{M}) where {M<:SparseMatrixCSC}
129129
T = M
130130
par = T.parameters
131131
npar = length(par)
132132
(2 == npar) || error("Type $M is not supported.")
133133
return Matrix{par[1]}
134134
end
135135

136-
sparse_to_dense(::Type{M}) where {M<:AbstractMatrix} = M
136+
to_dense(::Type{M}) where {M<:AbstractMatrix} = M
137137

138138
@doc raw"""
139-
dense_to_sparse(A::QuantumObject)
139+
to_sparse(A::QuantumObject)
140140
141141
Converts a dense QuantumObject to a sparse QuantumObject.
142142
"""
143-
dense_to_sparse(A::QuantumObject, tol::Real = 1e-10) = QuantumObject(dense_to_sparse(A.data, tol), A.type, A.dimensions)
144-
function dense_to_sparse(A::MT, tol::Real = 1e-10) where {MT<:AbstractMatrix}
143+
to_sparse(A::QuantumObject, tol::Real = 1e-10) = QuantumObject(to_sparse(A.data, tol), A.type, A.dimensions)
144+
function to_sparse(A::MT, tol::Real = 1e-10) where {MT<:AbstractMatrix}
145145
idxs = findall(@. abs(A) > tol)
146146
row_indices = getindex.(idxs, 1)
147147
col_indices = getindex.(idxs, 2)
148148
vals = getindex(A, idxs)
149149
return sparse(row_indices, col_indices, vals, size(A)...)
150150
end
151-
function dense_to_sparse(A::VT, tol::Real = 1e-10) where {VT<:AbstractVector}
151+
function to_sparse(A::VT, tol::Real = 1e-10) where {VT<:AbstractVector}
152152
idxs = findall(@. abs(A) > tol)
153153
vals = getindex(A, idxs)
154154
return sparsevec(idxs, vals, length(A))

src/qobj/operators.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function rand_unitary(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}, :
4848
# Because inv(Λ) ⋅ R has real and strictly positive elements, Q · Λ is therefore Haar distributed.
4949
Λ = diag(R) # take the diagonal elements of R
5050
Λ ./= abs.(Λ) # rescaling the elements
51-
return QuantumObject(sparse_to_dense(Q * Diagonal(Λ)); type = Operator, dims = dimensions)
51+
return QuantumObject(to_dense(Q * Diagonal(Λ)); type = Operator, dims = dimensions)
5252
end
5353
function rand_unitary(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}, ::Val{:exp})
5454
N = prod(dimensions)
@@ -59,7 +59,7 @@ function rand_unitary(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}, :
5959
# generate Hermitian matrix
6060
H = QuantumObject((Z + Z') / 2; type = Operator, dims = dimensions)
6161

62-
return sparse_to_dense(exp(-1.0im * H))
62+
return to_dense(exp(-1.0im * H))
6363
end
6464
rand_unitary(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}, ::Val{T}) where {T} =
6565
throw(ArgumentError("Invalid distribution: $(T)"))

src/qobj/quantum_object.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ SciMLOperators.cache_operator(
205205
L::AbstractQuantumObject{OpType},
206206
u::AbstractVector,
207207
) where {OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} =
208-
get_typename_wrapper(L)(cache_operator(L.data, sparse_to_dense(similar(u))), L.type, L.dimensions)
208+
get_typename_wrapper(L)(cache_operator(L.data, to_dense(similar(u))), L.type, L.dimensions)
209209

210210
function SciMLOperators.cache_operator(
211211
L::AbstractQuantumObject{OpType},

0 commit comments

Comments
 (0)