diff --git a/CHANGELOG.md b/CHANGELOG.md index 8553614a0..e11ac2e8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/qutip/QuantumToolbox.jl/tree/main) +- Rename `sparse_to_dense` as `to_dense` and `dense_to_sparse` as `to_sparse`. ([#392]) + ## [v0.26.0] Release date: 2025-02-09 @@ -115,3 +117,4 @@ Release date: 2024-11-13 [#386]: https://github.com/qutip/QuantumToolbox.jl/issues/386 [#388]: https://github.com/qutip/QuantumToolbox.jl/issues/388 [#389]: https://github.com/qutip/QuantumToolbox.jl/issues/389 +[#392]: https://github.com/qutip/QuantumToolbox.jl/issues/392 diff --git a/docs/src/resources/api.md b/docs/src/resources/api.md index d3d09c3c1..1a5af52a8 100644 --- a/docs/src/resources/api.md +++ b/docs/src/resources/api.md @@ -103,8 +103,8 @@ ket2dm expect variance LinearAlgebra.kron -sparse_to_dense -dense_to_sparse +to_dense +to_sparse vec2mat mat2vec ``` diff --git a/docs/src/users_guide/QuantumObject/QuantumObject.md b/docs/src/users_guide/QuantumObject/QuantumObject.md index 58377dcd7..1a1aead59 100644 --- a/docs/src/users_guide/QuantumObject/QuantumObject.md +++ b/docs/src/users_guide/QuantumObject/QuantumObject.md @@ -214,14 +214,14 @@ SparseMatrixCSC{Int64}(x_s) Matrix{Float64}(x_s) ``` -To convert between dense and sparse arrays, one can also use [`dense_to_sparse`](@ref) and [`sparse_to_dense`](@ref): +To convert between dense and sparse arrays, one can also use [`to_sparse`](@ref) and [`to_dense`](@ref): ```@example Qobj -x_d = sparse_to_dense(x_s) +x_d = to_dense(x_s) ``` ```@example Qobj -dense_to_sparse(x_d) +to_sparse(x_d) ``` !!! note "Convert to GPU arrays" diff --git a/ext/QuantumToolboxCUDAExt.jl b/ext/QuantumToolboxCUDAExt.jl index 7a90b27ba..70168b2b1 100644 --- a/ext/QuantumToolboxCUDAExt.jl +++ b/ext/QuantumToolboxCUDAExt.jl @@ -101,9 +101,9 @@ _change_eltype(::Type{T}, ::Val{32}) where {T<:AbstractFloat} = Float32 _change_eltype(::Type{Complex{T}}, ::Val{64}) where {T<:Union{Int,AbstractFloat}} = ComplexF64 _change_eltype(::Type{Complex{T}}, ::Val{32}) where {T<:Union{Int,AbstractFloat}} = ComplexF32 -QuantumToolbox.sparse_to_dense(A::MT) where {MT<:AbstractCuSparseArray} = CuArray(A) +QuantumToolbox.to_dense(A::MT) where {MT<:AbstractCuSparseArray} = CuArray(A) -QuantumToolbox.sparse_to_dense(::Type{T1}, A::CuArray{T2}) where {T1<:Number,T2<:Number} = CuArray{T1}(A) -QuantumToolbox.sparse_to_dense(::Type{T}, A::AbstractCuSparseArray) where {T<:Number} = CuArray{T}(A) +QuantumToolbox.to_dense(::Type{T1}, A::CuArray{T2}) where {T1<:Number,T2<:Number} = CuArray{T1}(A) +QuantumToolbox.to_dense(::Type{T}, A::AbstractCuSparseArray) where {T<:Number} = CuArray{T}(A) end diff --git a/src/deprecated.jl b/src/deprecated.jl index b39a392bf..a5d729711 100644 --- a/src/deprecated.jl +++ b/src/deprecated.jl @@ -15,11 +15,19 @@ end =# export FFTCorrelation +export sparse_to_dense, dense_to_sparse FFTCorrelation() = error( "`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.", ) +sparse_to_dense(args...) = error( + "`sparse_to_dense` has been deprecated and will be removed in next major release, please use `to_dense` instead.", +) +dense_to_sparse(args...) = error( + "`dense_to_sparse` has been deprecated and will be removed in next major release, please use `to_sparse` instead.", +) + correlation_3op_2t( H::QuantumObject{HOpType}, ψ0::QuantumObject{StateOpType}, diff --git a/src/qobj/arithmetic_and_attributes.jl b/src/qobj/arithmetic_and_attributes.jl index 15a0f430d..488be7956 100644 --- a/src/qobj/arithmetic_and_attributes.jl +++ b/src/qobj/arithmetic_and_attributes.jl @@ -298,7 +298,7 @@ LinearAlgebra.tr( Return the singular values of a [`QuantumObject`](@ref) in descending order """ -LinearAlgebra.svdvals(A::QuantumObject) = svdvals(sparse_to_dense(A.data)) +LinearAlgebra.svdvals(A::QuantumObject) = svdvals(to_dense(A.data)) @doc raw""" norm(A::QuantumObject, p::Real) @@ -415,7 +415,7 @@ Matrix square root of [`QuantumObject`](@ref) !!! note `√(A)` (where `√` can be typed by tab-completing `\sqrt` in the REPL) is a synonym of `sqrt(A)`. """ -LinearAlgebra.sqrt(A::QuantumObject) = QuantumObject(sqrt(sparse_to_dense(A.data)), A.type, A.dimensions) +LinearAlgebra.sqrt(A::QuantumObject) = QuantumObject(sqrt(to_dense(A.data)), A.type, A.dimensions) @doc raw""" log(A::QuantumObject) @@ -425,7 +425,7 @@ Matrix logarithm of [`QuantumObject`](@ref) Note that this function only supports for [`Operator`](@ref) and [`SuperOperator`](@ref) """ LinearAlgebra.log(A::QuantumObject{ObjType}) where {ObjType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} = - QuantumObject(log(sparse_to_dense(A.data)), A.type, A.dimensions) + QuantumObject(log(to_dense(A.data)), A.type, A.dimensions) @doc raw""" exp(A::QuantumObject) @@ -437,7 +437,7 @@ Note that this function only supports for [`Operator`](@ref) and [`SuperOperator LinearAlgebra.exp( A::QuantumObject{ObjType,DimsType,<:AbstractMatrix}, ) where {ObjType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},DimsType} = - QuantumObject(dense_to_sparse(exp(A.data)), A.type, A.dimensions) + QuantumObject(to_sparse(exp(A.data)), A.type, A.dimensions) LinearAlgebra.exp( A::QuantumObject{ObjType,DimsType,<:AbstractSparseMatrix}, ) where {ObjType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},DimsType} = diff --git a/src/qobj/eigsolve.jl b/src/qobj/eigsolve.jl index f53cec4ee..73db30c7b 100644 --- a/src/qobj/eigsolve.jl +++ b/src/qobj/eigsolve.jl @@ -462,10 +462,10 @@ function LinearAlgebra.eigen( kwargs..., ) where {OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} MT = typeof(A.data) - F = eigen(sparse_to_dense(A.data); kwargs...) + F = eigen(to_dense(A.data); kwargs...) # This fixes a type inference issue. But doesn't work for GPU arrays - E::mat2vec(sparse_to_dense(MT)) = F.values - U::sparse_to_dense(MT) = F.vectors + E::mat2vec(to_dense(MT)) = F.values + U::to_dense(MT) = F.vectors return EigsolveResult(E, U, A.type, A.dimensions, 0, 0, true) end @@ -478,7 +478,7 @@ Same as [`eigen(A::QuantumObject; kwargs...)`](@ref) but for only the eigenvalue LinearAlgebra.eigvals( A::QuantumObject{OpType}; kwargs..., -) where {OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} = eigvals(sparse_to_dense(A.data); kwargs...) +) where {OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} = eigvals(to_dense(A.data); kwargs...) @doc raw""" eigenenergies(A::QuantumObject; sparse::Bool=false, kwargs...) diff --git a/src/qobj/functions.jl b/src/qobj/functions.jl index 8fd805b70..e2c225762 100644 --- a/src/qobj/functions.jl +++ b/src/qobj/functions.jl @@ -4,7 +4,7 @@ Functions which manipulates QuantumObject export ket2dm export expect, variance -export sparse_to_dense, dense_to_sparse +export to_dense, to_sparse export vec2mat, mat2vec @doc raw""" @@ -113,19 +113,19 @@ variance(O::QuantumObject{OperatorQuantumObject}, ψ::QuantumObject) = expect(O^ variance(O::QuantumObject{OperatorQuantumObject}, ψ::Vector{<:QuantumObject}) = expect(O^2, ψ) .- expect(O, ψ) .^ 2 @doc raw""" - sparse_to_dense(A::QuantumObject) + to_dense(A::QuantumObject) Converts a sparse QuantumObject to a dense QuantumObject. """ -sparse_to_dense(A::QuantumObject) = QuantumObject(sparse_to_dense(A.data), A.type, A.dimensions) -sparse_to_dense(A::MT) where {MT<:AbstractSparseArray} = Array(A) -sparse_to_dense(A::MT) where {MT<:AbstractArray} = A +to_dense(A::QuantumObject) = QuantumObject(to_dense(A.data), A.type, A.dimensions) +to_dense(A::MT) where {MT<:AbstractSparseArray} = Array(A) +to_dense(A::MT) where {MT<:AbstractArray} = A -sparse_to_dense(::Type{T}, A::AbstractSparseArray) where {T<:Number} = Array{T}(A) -sparse_to_dense(::Type{T1}, A::AbstractArray{T2}) where {T1<:Number,T2<:Number} = Array{T1}(A) -sparse_to_dense(::Type{T}, A::AbstractArray{T}) where {T<:Number} = A +to_dense(::Type{T}, A::AbstractSparseArray) where {T<:Number} = Array{T}(A) +to_dense(::Type{T1}, A::AbstractArray{T2}) where {T1<:Number,T2<:Number} = Array{T1}(A) +to_dense(::Type{T}, A::AbstractArray{T}) where {T<:Number} = A -function sparse_to_dense(::Type{M}) where {M<:SparseMatrixCSC} +function to_dense(::Type{M}) where {M<:SparseMatrixCSC} T = M par = T.parameters npar = length(par) @@ -133,22 +133,22 @@ function sparse_to_dense(::Type{M}) where {M<:SparseMatrixCSC} return Matrix{par[1]} end -sparse_to_dense(::Type{M}) where {M<:AbstractMatrix} = M +to_dense(::Type{M}) where {M<:AbstractMatrix} = M @doc raw""" - dense_to_sparse(A::QuantumObject) + to_sparse(A::QuantumObject) Converts a dense QuantumObject to a sparse QuantumObject. """ -dense_to_sparse(A::QuantumObject, tol::Real = 1e-10) = QuantumObject(dense_to_sparse(A.data, tol), A.type, A.dimensions) -function dense_to_sparse(A::MT, tol::Real = 1e-10) where {MT<:AbstractMatrix} +to_sparse(A::QuantumObject, tol::Real = 1e-10) = QuantumObject(to_sparse(A.data, tol), A.type, A.dimensions) +function to_sparse(A::MT, tol::Real = 1e-10) where {MT<:AbstractMatrix} idxs = findall(@. abs(A) > tol) row_indices = getindex.(idxs, 1) col_indices = getindex.(idxs, 2) vals = getindex(A, idxs) return sparse(row_indices, col_indices, vals, size(A)...) end -function dense_to_sparse(A::VT, tol::Real = 1e-10) where {VT<:AbstractVector} +function to_sparse(A::VT, tol::Real = 1e-10) where {VT<:AbstractVector} idxs = findall(@. abs(A) > tol) vals = getindex(A, idxs) return sparsevec(idxs, vals, length(A)) diff --git a/src/qobj/operators.jl b/src/qobj/operators.jl index 2c3e05d9f..5445818ef 100644 --- a/src/qobj/operators.jl +++ b/src/qobj/operators.jl @@ -48,7 +48,7 @@ function rand_unitary(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}, : # Because inv(Λ) ⋅ R has real and strictly positive elements, Q · Λ is therefore Haar distributed. Λ = diag(R) # take the diagonal elements of R Λ ./= abs.(Λ) # rescaling the elements - return QuantumObject(sparse_to_dense(Q * Diagonal(Λ)); type = Operator, dims = dimensions) + return QuantumObject(to_dense(Q * Diagonal(Λ)); type = Operator, dims = dimensions) end function rand_unitary(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}, ::Val{:exp}) N = prod(dimensions) @@ -59,7 +59,7 @@ function rand_unitary(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}, : # generate Hermitian matrix H = QuantumObject((Z + Z') / 2; type = Operator, dims = dimensions) - return sparse_to_dense(exp(-1.0im * H)) + return to_dense(exp(-1.0im * H)) end rand_unitary(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}, ::Val{T}) where {T} = throw(ArgumentError("Invalid distribution: $(T)")) diff --git a/src/qobj/quantum_object.jl b/src/qobj/quantum_object.jl index f2c6f5569..9f760c876 100644 --- a/src/qobj/quantum_object.jl +++ b/src/qobj/quantum_object.jl @@ -205,7 +205,7 @@ SciMLOperators.cache_operator( L::AbstractQuantumObject{OpType}, u::AbstractVector, ) where {OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} = - get_typename_wrapper(L)(cache_operator(L.data, sparse_to_dense(similar(u))), L.type, L.dimensions) + get_typename_wrapper(L)(cache_operator(L.data, to_dense(similar(u))), L.type, L.dimensions) function SciMLOperators.cache_operator( L::AbstractQuantumObject{OpType}, diff --git a/src/qobj/quantum_object_evo.jl b/src/qobj/quantum_object_evo.jl index 5cd4d828b..4d97d90d4 100644 --- a/src/qobj/quantum_object_evo.jl +++ b/src/qobj/quantum_object_evo.jl @@ -281,7 +281,7 @@ function QuantumObjectEvolution( end # Preallocate the SciMLOperator cache using a dense vector as a reference - v0 = sparse_to_dense(similar(op.data, size(op, 1))) + v0 = to_dense(similar(op.data, size(op, 1))) data = cache_operator(data, v0) return QuantumObjectEvolution(data, type, dims) diff --git a/src/time_evolution/mesolve.jl b/src/time_evolution/mesolve.jl index a87bc3864..5b640bc25 100644 --- a/src/time_evolution/mesolve.jl +++ b/src/time_evolution/mesolve.jl @@ -76,7 +76,7 @@ function mesolveProblem( check_dimensions(L_evo, ψ0) T = Base.promote_eltype(L_evo, ψ0) - ρ0 = sparse_to_dense(_CType(T), mat2vec(ket2dm(ψ0).data)) # Convert it to dense vector with complex element type + ρ0 = to_dense(_CType(T), mat2vec(ket2dm(ψ0).data)) # Convert it to dense vector with complex element type L = L_evo.data is_empty_e_ops = (e_ops isa Nothing) ? true : isempty(e_ops) diff --git a/src/time_evolution/sesolve.jl b/src/time_evolution/sesolve.jl index 999218b9d..a9996a633 100644 --- a/src/time_evolution/sesolve.jl +++ b/src/time_evolution/sesolve.jl @@ -66,7 +66,7 @@ function sesolveProblem( check_dimensions(H_evo, ψ0) T = Base.promote_eltype(H_evo, ψ0) - ψ0 = sparse_to_dense(_CType(T), get_data(ψ0)) # Convert it to dense vector with complex element type + ψ0 = to_dense(_CType(T), get_data(ψ0)) # Convert it to dense vector with complex element type U = H_evo.data is_empty_e_ops = (e_ops isa Nothing) ? true : isempty(e_ops) diff --git a/src/time_evolution/smesolve.jl b/src/time_evolution/smesolve.jl index 904951d42..6dab420af 100644 --- a/src/time_evolution/smesolve.jl +++ b/src/time_evolution/smesolve.jl @@ -91,7 +91,7 @@ function smesolveProblem( dims = L_evo.dimensions T = Base.promote_eltype(L_evo, ψ0) - ρ0 = sparse_to_dense(_CType(T), mat2vec(ket2dm(ψ0).data)) # Convert it to dense vector with complex element type + ρ0 = to_dense(_CType(T), mat2vec(ket2dm(ψ0).data)) # Convert it to dense vector with complex element type progr = ProgressBar(length(tlist), enable = getVal(progress_bar)) diff --git a/src/time_evolution/ssesolve.jl b/src/time_evolution/ssesolve.jl index 0b8ec2146..22227e939 100644 --- a/src/time_evolution/ssesolve.jl +++ b/src/time_evolution/ssesolve.jl @@ -167,7 +167,7 @@ function ssesolveProblem( check_dimensions(H_eff_evo, ψ0) dims = H_eff_evo.dimensions - ψ0 = sparse_to_dense(_CType(ψ0), get_data(ψ0)) + ψ0 = to_dense(_CType(ψ0), get_data(ψ0)) progr = ProgressBar(length(tlist), enable = getVal(progress_bar)) diff --git a/src/time_evolution/time_evolution.jl b/src/time_evolution/time_evolution.jl index 722463101..a21221e26 100644 --- a/src/time_evolution/time_evolution.jl +++ b/src/time_evolution/time_evolution.jl @@ -397,12 +397,12 @@ function liouvillian_generalized( H_d = QuantumObject(Diagonal(complex(E)), type = Operator, dims = dims) Ω = E' .- E - Ωp = triu(dense_to_sparse(Ω, tol), 1) + Ωp = triu(to_sparse(Ω, tol), 1) # Filter in the Hilbert space σ = isnothing(σ_filter) ? 500 * maximum([norm(field) / length(field) for field in fields]) : σ_filter F1 = QuantumObject(gaussian.(Ω, 0, σ), type = Operator, dims = dims) - F1 = dense_to_sparse(F1, tol) + F1 = to_sparse(F1, tol) # Filter in the Liouville space # M1 = ones(final_size, final_size) @@ -412,13 +412,13 @@ function liouvillian_generalized( Ω2 = kron(M1, Ω) Ωdiff = Ω1 .- Ω2 F2 = QuantumObject(gaussian.(Ωdiff, 0, σ), SuperOperator, dims) - F2 = dense_to_sparse(F2, tol) + F2 = to_sparse(F2, tol) L = liouvillian(H_d) for i in eachindex(fields) # The operator that couples the system to the bath in the eigenbasis - X_op = dense_to_sparse((U'*fields[i]*U).data[1:final_size, 1:final_size], tol) + X_op = to_sparse((U'*fields[i]*U).data[1:final_size, 1:final_size], tol) if ishermitian(fields[i]) X_op = (X_op + X_op') / 2 # Make sure it's hermitian end @@ -452,8 +452,8 @@ function _liouvillian_floquet( L_0 = L₀.data L_p = Lₚ.data L_m = Lₘ.data - L_p_dense = sparse_to_dense(Lₚ.data) - L_m_dense = sparse_to_dense(Lₘ.data) + L_p_dense = to_dense(Lₚ.data) + L_m_dense = to_dense(Lₘ.data) S = -(L_0 - 1im * n_max * ω * I) \ L_p_dense T = -(L_0 + 1im * n_max * ω * I) \ L_m_dense @@ -464,5 +464,5 @@ function _liouvillian_floquet( end tol == 0 && return QuantumObject(L_0 + L_m * S + L_p * T, SuperOperator, L₀.dimensions) - return QuantumObject(dense_to_sparse(L_0 + L_m * S + L_p * T, tol), SuperOperator, L₀.dimensions) + return QuantumObject(to_sparse(L_0 + L_m * S + L_p * T, tol), SuperOperator, L₀.dimensions) end diff --git a/src/wigner.jl b/src/wigner.jl index d85385c63..dec636642 100644 --- a/src/wigner.jl +++ b/src/wigner.jl @@ -60,7 +60,7 @@ julia> wig = wigner(ψ, xvec, xvec); or taking advantage of the parallel computation of the `WignerLaguerre` method ```jldoctest wigner -julia> ρ = ket2dm(ψ) |> dense_to_sparse; +julia> ρ = ket2dm(ψ) |> to_sparse; julia> wig = wigner(ρ, xvec, xvec, method=WignerLaguerre(parallel=true)); diff --git a/test/core-test/eigenvalues_and_operators.jl b/test/core-test/eigenvalues_and_operators.jl index 31d5997af..481aedce0 100644 --- a/test/core-test/eigenvalues_and_operators.jl +++ b/test/core-test/eigenvalues_and_operators.jl @@ -58,7 +58,7 @@ # eigen solve for general matrices vals, _, vecs = eigsolve(L.data, sigma = 0.01, k = 10, krylovdim = 50) - vals2, vecs2 = eigen(sparse_to_dense(L.data)) + vals2, vecs2 = eigen(to_dense(L.data)) vals3, state3, vecs3 = eigsolve_al(L, 1 \ (40 * κ), k = 10, krylovdim = 50) idxs = sortperm(vals2, by = abs) vals2 = vals2[idxs][1:10] diff --git a/test/core-test/generalized_master_equation.jl b/test/core-test/generalized_master_equation.jl index a35114e86..54a9ac8e9 100644 --- a/test/core-test/generalized_master_equation.jl +++ b/test/core-test/generalized_master_equation.jl @@ -15,12 +15,12 @@ Tlist = [0, 0.0] E, U, L1 = liouvillian_generalized(H, fields, Tlist, N_trunc = N_trunc, tol = tol) - Ω = dense_to_sparse((E'.-E)[1:N_trunc, 1:N_trunc], tol) + Ω = to_sparse((E'.-E)[1:N_trunc, 1:N_trunc], tol) - H_d = Qobj(dense_to_sparse((U'*H*U)[1:N_trunc, 1:N_trunc], tol)) - Xp = Qobj(Ω .* dense_to_sparse(triu((U'*(a+a')*U).data[1:N_trunc, 1:N_trunc], 1), tol)) - a2 = Qobj(dense_to_sparse((U'*a*U).data[1:N_trunc, 1:N_trunc], tol)) - sm2 = Qobj(dense_to_sparse((U'*sm*U).data[1:N_trunc, 1:N_trunc], tol)) + H_d = Qobj(to_sparse((U'*H*U)[1:N_trunc, 1:N_trunc], tol)) + Xp = Qobj(Ω .* to_sparse(triu((U'*(a+a')*U).data[1:N_trunc, 1:N_trunc], 1), tol)) + a2 = Qobj(to_sparse((U'*a*U).data[1:N_trunc, 1:N_trunc], tol)) + sm2 = Qobj(to_sparse((U'*sm*U).data[1:N_trunc, 1:N_trunc], tol)) # Standard liouvillian case c_ops = [sqrt(0.01) * a2, sqrt(0.01) * sm2] @@ -33,12 +33,12 @@ Tlist = [0.2, 0.0] E, U, L1 = liouvillian_generalized(H, fields, Tlist, N_trunc = N_trunc, tol = tol) - Ω = dense_to_sparse((E'.-E)[1:N_trunc, 1:N_trunc], tol) + Ω = to_sparse((E'.-E)[1:N_trunc, 1:N_trunc], tol) - H_d = Qobj(dense_to_sparse((U'*H*U)[1:N_trunc, 1:N_trunc], tol)) - Xp = Qobj(Ω .* dense_to_sparse(triu((U'*(a+a')*U).data[1:N_trunc, 1:N_trunc], 1), tol)) - a2 = Qobj(dense_to_sparse((U'*a*U).data[1:N_trunc, 1:N_trunc], tol)) - sm2 = Qobj(dense_to_sparse((U'*sm*U).data[1:N_trunc, 1:N_trunc], tol)) + H_d = Qobj(to_sparse((U'*H*U)[1:N_trunc, 1:N_trunc], tol)) + Xp = Qobj(Ω .* to_sparse(triu((U'*(a+a')*U).data[1:N_trunc, 1:N_trunc], 1), tol)) + a2 = Qobj(to_sparse((U'*a*U).data[1:N_trunc, 1:N_trunc], tol)) + sm2 = Qobj(to_sparse((U'*sm*U).data[1:N_trunc, 1:N_trunc], tol)) @test abs(expect(Xp' * Xp, steadystate(L1)) - n_thermal(1, Tlist[1])) / n_thermal(1, Tlist[1]) < 1e-4 diff --git a/test/core-test/negativity_and_partial_transpose.jl b/test/core-test/negativity_and_partial_transpose.jl index ea1c1580e..475cd343e 100644 --- a/test/core-test/negativity_and_partial_transpose.jl +++ b/test/core-test/negativity_and_partial_transpose.jl @@ -24,7 +24,7 @@ @testset "partial_transpose" begin # A (24 * 24)-matrix which contains number 1 ~ 576 A_dense = Qobj(reshape(1:(24^2), (24, 24)), dims = (2, 3, 4)) - A_sparse = dense_to_sparse(A_dense) + A_sparse = to_sparse(A_dense) PT = (true, false) for s1 in PT for s2 in PT diff --git a/test/core-test/quantum_objects.jl b/test/core-test/quantum_objects.jl index ad09f848e..a03b96de3 100644 --- a/test/core-test/quantum_objects.jl +++ b/test/core-test/quantum_objects.jl @@ -323,9 +323,9 @@ @testset "element type conversion" begin vd = Qobj(Int64[0, 0]) - vs = Qobj(dense_to_sparse(vd)) + vs = Qobj(to_sparse(vd)) Md = Qobj(Int64[0 0; 0 0]) - Ms = Qobj(dense_to_sparse(Md)) + Ms = Qobj(to_sparse(Md)) @test typeof(Vector(vd).data) == Vector{Int64} @test typeof(Vector(vs).data) == Vector{Int64} @test typeof(Vector{ComplexF64}(vd).data) == Vector{ComplexF64} @@ -340,6 +340,11 @@ @test typeof(SparseMatrixCSC(Md).data) == SparseMatrixCSC{Int64,Int64} @test typeof(SparseMatrixCSC(Ms).data) == SparseMatrixCSC{Int64,Int64} @test typeof(SparseMatrixCSC{ComplexF64}(Ms).data) == SparseMatrixCSC{ComplexF64,Int64} + + @testset "Deprecated Errors" begin + @test_throws ErrorException sparse_to_dense(vs) + @test_throws ErrorException dense_to_sparse(vd) + end end @testset "Type Inference (QuantumObject)" begin @@ -564,8 +569,8 @@ @testset "trace distance" begin ψz0 = basis(2, 0) ψz1 = basis(2, 1) - ρz0 = dense_to_sparse(ket2dm(ψz0)) - ρz1 = dense_to_sparse(ket2dm(ψz1)) + ρz0 = to_sparse(ket2dm(ψz0)) + ρz1 = to_sparse(ket2dm(ψz1)) ψx0 = sqrt(0.5) * (basis(2, 0) + basis(2, 1)) @test tracedist(ψz0, ψx0) ≈ sqrt(0.5) @test tracedist(ρz0, ψz1) ≈ 1.0 @@ -586,7 +591,7 @@ ψ1 = Qobj(rand(ComplexF64, 5)) ψ2 = Qobj(rand(ComplexF64, 5)) M1 = ψ1 * ψ1' - @test sqrtm(M0) ≈ sqrtm(sparse_to_dense(M0)) + @test sqrtm(M0) ≈ sqrtm(to_dense(M0)) @test isapprox(fidelity(M0, M1), fidelity(ψ1, M0); atol = 1e-6) @test isapprox(fidelity(ψ1, ψ2), fidelity(ket2dm(ψ1), ket2dm(ψ2)); atol = 1e-6) @@ -601,7 +606,7 @@ @testset "log, exp, sinm, cosm" begin M0 = rand(ComplexF64, 4, 4) Md = Qobj(M0 * M0') - Ms = dense_to_sparse(Md) + Ms = to_sparse(Md) e_p = expm(1im * Md) e_m = expm(-1im * Md) @test logm(expm(Ms)) ≈ expm(logm(Md)) @@ -625,28 +630,28 @@ tol = 0.5 ## Vector{Float64} with in-place tidyup ψ1 = Qobj(rand(Float64, N)) - ψ2 = dense_to_sparse(ψ1) + ψ2 = to_sparse(ψ1) @test tidyup!(ψ2, tol) == ψ2 != ψ1 - @test dense_to_sparse(tidyup!(ψ1, tol)) == ψ2 + @test to_sparse(tidyup!(ψ1, tol)) == ψ2 ## Vector{Float64} with normal tidyup ψ1 = Qobj(rand(Float64, N)) - ψ2 = dense_to_sparse(ψ1) + ψ2 = to_sparse(ψ1) @test tidyup(ψ2, tol) != ψ2 - @test dense_to_sparse(tidyup(ψ1, tol)) == tidyup(ψ2, tol) + @test to_sparse(tidyup(ψ1, tol)) == tidyup(ψ2, tol) ## Matrix{ComplexF64} with in-place tidyup tol = 0.1 ρ1 = rand_dm(N) - ρ2 = dense_to_sparse(ρ1) + ρ2 = to_sparse(ρ1) @test tidyup!(ρ2, tol) == ρ2 != ρ1 - @test dense_to_sparse(tidyup!(ρ1, tol)) == ρ2 + @test to_sparse(tidyup!(ρ1, tol)) == ρ2 ## Matrix{ComplexF64} with normal tidyup ρ1 = rand_dm(N) - ρ2 = dense_to_sparse(ρ1) + ρ2 = to_sparse(ρ1) @test tidyup(ρ2, tol) != ρ2 - @test dense_to_sparse(tidyup(ρ1, tol)) == tidyup(ρ2, tol) + @test to_sparse(tidyup(ρ1, tol)) == tidyup(ρ2, tol) @testset "Type Inference (tidyup)" begin @inferred tidyup(ψ1, tol) diff --git a/test/core-test/states_and_operators.jl b/test/core-test/states_and_operators.jl index 573dedd04..f43b3e2a3 100644 --- a/test/core-test/states_and_operators.jl +++ b/test/core-test/states_and_operators.jl @@ -341,7 +341,7 @@ @testset "superoperators" begin # spre, spost, and sprepost Xs = sigmax() - Xd = sparse_to_dense(Xs) + Xd = to_dense(Xs) A_wrong1 = Qobj(rand(4, 4), dims = 4) A_wrong2 = Qobj(rand(4, 4), dims = (2, 2)) A_wrong3 = Qobj(rand(3, 3)) diff --git a/test/core-test/wigner.jl b/test/core-test/wigner.jl index e12a12c2e..e48b53497 100644 --- a/test/core-test/wigner.jl +++ b/test/core-test/wigner.jl @@ -1,7 +1,7 @@ @testset "Wigner" begin α = 0.5 + 0.8im ψ = coherent(30, α) - ρ = dense_to_sparse(ket2dm(ψ), 1e-6) + ρ = to_sparse(ket2dm(ψ), 1e-6) xvec = LinRange(-3, 3, 300) yvec = LinRange(-3, 3, 300) diff --git a/test/ext-test/gpu/cuda_ext.jl b/test/ext-test/gpu/cuda_ext.jl index 262ae88ed..42cd278cb 100644 --- a/test/ext-test/gpu/cuda_ext.jl +++ b/test/ext-test/gpu/cuda_ext.jl @@ -5,16 +5,16 @@ ψdi = Qobj(Int64[1, 0]) ψdf = Qobj(Float64[1, 0]) ψdc = Qobj(ComplexF64[1, 0]) - ψsi = dense_to_sparse(ψdi) - ψsf = dense_to_sparse(ψdf) - ψsc = dense_to_sparse(ψdc) + ψsi = to_sparse(ψdi) + ψsf = to_sparse(ψdf) + ψsc = to_sparse(ψdc) Xdi = Qobj(Int64[0 1; 1 0]) Xdf = Qobj(Float64[0 1; 1 0]) Xdc = Qobj(ComplexF64[0 1; 1 0]) - Xsi = dense_to_sparse(Xdi) - Xsf = dense_to_sparse(Xdf) - Xsc = dense_to_sparse(Xdc) + Xsi = to_sparse(Xdi) + Xsf = to_sparse(Xdf) + Xsc = to_sparse(Xdc) @test_throws DomainError cu(ψdi; word_size = 16) @@ -67,19 +67,19 @@ @test typeof(CuSparseMatrixCSR{ComplexF32}(Xsc).data) == CuSparseMatrixCSR{ComplexF32,Int32} # Sparse To Dense - # @test sparse_to_dense(cu(ψsi; word_size = 64)).data isa CuVector{Int64} # TODO: Fix this in CUDA.jl - @test sparse_to_dense(cu(ψsf; word_size = 64)).data isa CuVector{Float64} - @test sparse_to_dense(cu(ψsc; word_size = 64)).data isa CuVector{ComplexF64} - # @test sparse_to_dense(cu(Xsi; word_size = 64)).data isa CuMatrix{Int64} # TODO: Fix this in CUDA.jl - @test sparse_to_dense(cu(Xsf; word_size = 64)).data isa CuMatrix{Float64} - @test sparse_to_dense(cu(Xsc; word_size = 64)).data isa CuMatrix{ComplexF64} - - # @test sparse_to_dense(Int32, cu(ψsf; word_size = 64)).data isa CuVector{Int32} # TODO: Fix this in CUDA.jl - # @test sparse_to_dense(Float32, cu(ψsf; word_size = 64)).data isa CuVector{Float32} # TODO: Fix this in CUDA.jl - # @test sparse_to_dense(ComplexF32, cu(ψsf; word_size = 64)).data isa CuVector{ComplexF32} # TODO: Fix this in CUDA.jl - # @test sparse_to_dense(Int64, cu(Xsf; word_size = 32)).data isa CuMatrix{Int64} # TODO: Fix this in CUDA.jl - # @test sparse_to_dense(Float64, cu(Xsf; word_size = 32)).data isa CuMatrix{Float64} # TODO: Fix this in CUDA.jl - # @test sparse_to_dense(ComplexF64, cu(Xsf; word_size = 32)).data isa CuMatrix{ComplexF64} # TODO: Fix this in CUDA.jl + # @test to_dense(cu(ψsi; word_size = 64)).data isa CuVector{Int64} # TODO: Fix this in CUDA.jl + @test to_dense(cu(ψsf; word_size = 64)).data isa CuVector{Float64} + @test to_dense(cu(ψsc; word_size = 64)).data isa CuVector{ComplexF64} + # @test to_dense(cu(Xsi; word_size = 64)).data isa CuMatrix{Int64} # TODO: Fix this in CUDA.jl + @test to_dense(cu(Xsf; word_size = 64)).data isa CuMatrix{Float64} + @test to_dense(cu(Xsc; word_size = 64)).data isa CuMatrix{ComplexF64} + + # @test to_dense(Int32, cu(ψsf; word_size = 64)).data isa CuVector{Int32} # TODO: Fix this in CUDA.jl + # @test to_dense(Float32, cu(ψsf; word_size = 64)).data isa CuVector{Float32} # TODO: Fix this in CUDA.jl + # @test to_dense(ComplexF32, cu(ψsf; word_size = 64)).data isa CuVector{ComplexF32} # TODO: Fix this in CUDA.jl + # @test to_dense(Int64, cu(Xsf; word_size = 32)).data isa CuMatrix{Int64} # TODO: Fix this in CUDA.jl + # @test to_dense(Float64, cu(Xsf; word_size = 32)).data isa CuMatrix{Float64} # TODO: Fix this in CUDA.jl + # @test to_dense(ComplexF64, cu(Xsf; word_size = 32)).data isa CuMatrix{ComplexF64} # TODO: Fix this in CUDA.jl # brief example in README and documentation N = 20