From da37b688678b68c922ef3d957ea162422ddaa5ff Mon Sep 17 00:00:00 2001 From: Yi-Te Huang Date: Thu, 19 Sep 2024 13:01:03 +0800 Subject: [PATCH 1/9] fix type conversion of `tlist` in time evolution --- src/time_evolution/mcsolve.jl | 2 +- src/time_evolution/mesolve.jl | 2 +- src/time_evolution/sesolve.jl | 2 +- src/utilities.jl | 10 ++++++++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/time_evolution/mcsolve.jl b/src/time_evolution/mcsolve.jl index bd62bb410..a6f67f074 100644 --- a/src/time_evolution/mcsolve.jl +++ b/src/time_evolution/mcsolve.jl @@ -190,7 +190,7 @@ function mcsolveProblem( c_ops isa Nothing && throw(ArgumentError("The list of collapse operators must be provided. Use sesolveProblem instead.")) - t_l = convert(Vector{Float64}, tlist) # Convert it into Float64 to avoid type instabilities for OrdinaryDiffEq.jl + t_l = _convert_tlist(eltype(H), tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl H_eff = H - 1im * mapreduce(op -> op' * op, +, c_ops) / 2 diff --git a/src/time_evolution/mesolve.jl b/src/time_evolution/mesolve.jl index f83a1bc3b..363e0790f 100644 --- a/src/time_evolution/mesolve.jl +++ b/src/time_evolution/mesolve.jl @@ -122,7 +122,7 @@ function mesolveProblem( is_time_dependent = !(H_t isa Nothing) progress_bar_val = makeVal(progress_bar) - t_l = convert(Vector{Float64}, tlist) # Convert it into Float64 to avoid type instabilities for OrdinaryDiffEq.jl + t_l = _convert_tlist(eltype(H), tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl ρ0 = mat2vec(ket2dm(ψ0).data) diff --git a/src/time_evolution/sesolve.jl b/src/time_evolution/sesolve.jl index 6ef530d65..a550e48aa 100644 --- a/src/time_evolution/sesolve.jl +++ b/src/time_evolution/sesolve.jl @@ -103,7 +103,7 @@ function sesolveProblem( is_time_dependent = !(H_t isa Nothing) progress_bar_val = makeVal(progress_bar) - t_l = convert(Vector{Float64}, tlist) # Convert it into Float64 to avoid type instabilities for OrdinaryDiffEq.jl + t_l = _convert_tlist(eltype(H), tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl ϕ0 = get_data(ψ0) diff --git a/src/utilities.jl b/src/utilities.jl index df93872d4..487624455 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -65,3 +65,13 @@ _non_static_array_warning(argname, arg::AbstractVector{T}) where {T} = @warn "The argument $argname should be a Tuple or a StaticVector for better performance. Try to use `$argname = $(Tuple(arg))` or `$argname = SVector(" * join(arg, ", ") * ")` instead of `$argname = $arg`." maxlog = 1 + +# convert tlist in time evolution +_convert_tlist(::Int32, tlist::AbstractVector) = _convert_tlist(Val(32), tlist) +_convert_tlist(::Float32, tlist::AbstractVector) = _convert_tlist(Val(32), tlist) +_convert_tlist(::ComplexF32, tlist::AbstractVector) = _convert_tlist(Val(32), tlist) +_convert_tlist(::Int64, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) +_convert_tlist(::Float64, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) +_convert_tlist(::ComplexF64, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) +_convert_tlist(::Val{32}, tlist::AbstractVector) = convert(Vector{Float32}, tlist) +_convert_tlist(::Val{64}, tlist::AbstractVector) = convert(Vector{Float64}, tlist) \ No newline at end of file From f888a305f5502a29eb8ab46e744eddd366b18e85 Mon Sep 17 00:00:00 2001 From: Yi-Te Huang Date: Thu, 19 Sep 2024 13:15:18 +0800 Subject: [PATCH 2/9] fix typo --- src/utilities.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/utilities.jl b/src/utilities.jl index 487624455..5e906edc8 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -67,11 +67,11 @@ _non_static_array_warning(argname, arg::AbstractVector{T}) where {T} = ")` instead of `$argname = $arg`." maxlog = 1 # convert tlist in time evolution -_convert_tlist(::Int32, tlist::AbstractVector) = _convert_tlist(Val(32), tlist) -_convert_tlist(::Float32, tlist::AbstractVector) = _convert_tlist(Val(32), tlist) -_convert_tlist(::ComplexF32, tlist::AbstractVector) = _convert_tlist(Val(32), tlist) -_convert_tlist(::Int64, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) -_convert_tlist(::Float64, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) -_convert_tlist(::ComplexF64, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) +_convert_tlist(::Type{Int32}, tlist::AbstractVector) = _convert_tlist(Val(32), tlist) +_convert_tlist(::Type{Float32}, tlist::AbstractVector) = _convert_tlist(Val(32), tlist) +_convert_tlist(::Type{ComplexF32}, tlist::AbstractVector) = _convert_tlist(Val(32), tlist) +_convert_tlist(::Type{Int64}, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) +_convert_tlist(::Type{Float64}, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) +_convert_tlist(::Type{ComplexF64}, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) _convert_tlist(::Val{32}, tlist::AbstractVector) = convert(Vector{Float32}, tlist) _convert_tlist(::Val{64}, tlist::AbstractVector) = convert(Vector{Float64}, tlist) \ No newline at end of file From 9a924d255e251bbaddea1db5b282965e0b94fc3b Mon Sep 17 00:00:00 2001 From: Yi-Te Huang Date: Thu, 19 Sep 2024 13:16:32 +0800 Subject: [PATCH 3/9] format files --- src/utilities.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utilities.jl b/src/utilities.jl index 5e906edc8..fce5d46e4 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -74,4 +74,4 @@ _convert_tlist(::Type{Int64}, tlist::AbstractVector) = _convert_tlist(Val(64), t _convert_tlist(::Type{Float64}, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) _convert_tlist(::Type{ComplexF64}, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) _convert_tlist(::Val{32}, tlist::AbstractVector) = convert(Vector{Float32}, tlist) -_convert_tlist(::Val{64}, tlist::AbstractVector) = convert(Vector{Float64}, tlist) \ No newline at end of file +_convert_tlist(::Val{64}, tlist::AbstractVector) = convert(Vector{Float64}, tlist) From fc5da367c3ca4c8718d64f41e2cf8e651a194c63 Mon Sep 17 00:00:00 2001 From: Yi-Te Huang Date: Thu, 19 Sep 2024 14:05:45 +0800 Subject: [PATCH 4/9] introduce inner function `_convert_u0` --- ext/QuantumToolboxCUDAExt.jl | 4 ++++ src/time_evolution/mcsolve.jl | 2 +- src/time_evolution/mesolve.jl | 4 ++-- src/time_evolution/sesolve.jl | 4 ++-- src/utilities.jl | 11 ++--------- 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/ext/QuantumToolboxCUDAExt.jl b/ext/QuantumToolboxCUDAExt.jl index e1b3418bb..8886a2e35 100644 --- a/ext/QuantumToolboxCUDAExt.jl +++ b/ext/QuantumToolboxCUDAExt.jl @@ -1,6 +1,7 @@ module QuantumToolboxCUDAExt using QuantumToolbox +import QuantumToolbox: _convert_u0 import CUDA: cu, CuArray import CUDA.CUSPARSE: CuSparseVector, CuSparseMatrixCSC, CuSparseMatrixCSR import SparseArrays: SparseVector, SparseMatrixCSC @@ -89,4 +90,7 @@ _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 +# make sure u0 in time evolution is dense vector and has complex element type +_convert_u0(u0::Union{CuArray{T},CuSparseVector{T}}) where {T<:Number} = convert(CuArray{complex(T)}, u0) + end diff --git a/src/time_evolution/mcsolve.jl b/src/time_evolution/mcsolve.jl index a6f67f074..9dbd68da1 100644 --- a/src/time_evolution/mcsolve.jl +++ b/src/time_evolution/mcsolve.jl @@ -190,7 +190,7 @@ function mcsolveProblem( c_ops isa Nothing && throw(ArgumentError("The list of collapse operators must be provided. Use sesolveProblem instead.")) - t_l = _convert_tlist(eltype(H), tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl + t_l = convert(Vector{real(eltype(ψ0))}, tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl H_eff = H - 1im * mapreduce(op -> op' * op, +, c_ops) / 2 diff --git a/src/time_evolution/mesolve.jl b/src/time_evolution/mesolve.jl index 363e0790f..9b829ddcd 100644 --- a/src/time_evolution/mesolve.jl +++ b/src/time_evolution/mesolve.jl @@ -122,9 +122,9 @@ function mesolveProblem( is_time_dependent = !(H_t isa Nothing) progress_bar_val = makeVal(progress_bar) - t_l = _convert_tlist(eltype(H), tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl + ρ0 = _convert_u0(mat2vec(ket2dm(ψ0).data)) - ρ0 = mat2vec(ket2dm(ψ0).data) + t_l = convert(Vector{real(eltype(ρ0))}, tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl L = liouvillian(H, c_ops).data progr = ProgressBar(length(t_l), enable = getVal(progress_bar_val)) diff --git a/src/time_evolution/sesolve.jl b/src/time_evolution/sesolve.jl index a550e48aa..28f39a8d0 100644 --- a/src/time_evolution/sesolve.jl +++ b/src/time_evolution/sesolve.jl @@ -103,9 +103,9 @@ function sesolveProblem( is_time_dependent = !(H_t isa Nothing) progress_bar_val = makeVal(progress_bar) - t_l = _convert_tlist(eltype(H), tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl + ϕ0 = _convert_u0(get_data(ψ0)) - ϕ0 = get_data(ψ0) + t_l = convert(Vector{real(eltype(ϕ0))}, tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl U = -1im * get_data(H) progr = ProgressBar(length(t_l), enable = getVal(progress_bar_val)) diff --git a/src/utilities.jl b/src/utilities.jl index fce5d46e4..7fb8ccbcc 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -66,12 +66,5 @@ _non_static_array_warning(argname, arg::AbstractVector{T}) where {T} = join(arg, ", ") * ")` instead of `$argname = $arg`." maxlog = 1 -# convert tlist in time evolution -_convert_tlist(::Type{Int32}, tlist::AbstractVector) = _convert_tlist(Val(32), tlist) -_convert_tlist(::Type{Float32}, tlist::AbstractVector) = _convert_tlist(Val(32), tlist) -_convert_tlist(::Type{ComplexF32}, tlist::AbstractVector) = _convert_tlist(Val(32), tlist) -_convert_tlist(::Type{Int64}, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) -_convert_tlist(::Type{Float64}, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) -_convert_tlist(::Type{ComplexF64}, tlist::AbstractVector) = _convert_tlist(Val(64), tlist) -_convert_tlist(::Val{32}, tlist::AbstractVector) = convert(Vector{Float32}, tlist) -_convert_tlist(::Val{64}, tlist::AbstractVector) = convert(Vector{Float64}, tlist) +# make sure u0 in time evolution is dense vector and has complex element type +_convert_u0(u0::AbstractVector{T}) where {T<:Number} = convert(Vector{complex(T)}, u0) From 79bc22fcd47475a705d581f1bcd3e29afb8deb3e Mon Sep 17 00:00:00 2001 From: Yi-Te Huang Date: Thu, 19 Sep 2024 14:21:37 +0800 Subject: [PATCH 5/9] fix element type for `steadystate` --- src/steadystate.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/steadystate.jl b/src/steadystate.jl index be06cbf1a..c7f2aac80 100644 --- a/src/steadystate.jl +++ b/src/steadystate.jl @@ -95,10 +95,14 @@ function steadystate( (H.dims != ψ0.dims) && throw(DimensionMismatch("The two quantum objects are not of the same Hilbert dimension.")) N = prod(H.dims) - u0 = mat2vec(ket2dm(ψ0).data) + u0 = _convert_u0(mat2vec(ket2dm(ψ0).data)) + + Ftype = real(eltype(u0)) + Tspan = (convert(Ftype, 0), convert(Ftype, tspan)) + L = MatrixOperator(liouvillian(H, c_ops).data) - prob = ODEProblem{true}(L, u0, (0.0, tspan)) + prob = ODEProblem{true}(L, u0, Tspan) sol = solve( prob, solver.alg; @@ -109,7 +113,6 @@ function steadystate( ) ρss = reshape(sol.u[end], N, N) - ρss = (ρss + ρss') / 2 # Hermitianize return QuantumObject(ρss, Operator, H.dims) end From 1ececdaff26df9392f83509336baff7735daab96 Mon Sep 17 00:00:00 2001 From: Yi-Te Huang <44385685+ytdHuang@users.noreply.github.com> Date: Thu, 19 Sep 2024 17:56:20 +0800 Subject: [PATCH 6/9] add comments --- src/steadystate.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/steadystate.jl b/src/steadystate.jl index c7f2aac80..86c9cc2e0 100644 --- a/src/steadystate.jl +++ b/src/steadystate.jl @@ -98,7 +98,7 @@ function steadystate( u0 = _convert_u0(mat2vec(ket2dm(ψ0).data)) Ftype = real(eltype(u0)) - Tspan = (convert(Ftype, 0), convert(Ftype, tspan)) + Tspan = (convert(Ftype, 0), convert(Ftype, tspan)) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl L = MatrixOperator(liouvillian(H, c_ops).data) From b4bf1a11011d84d22db015cb9545c182e4fe4855 Mon Sep 17 00:00:00 2001 From: Yi-Te Huang Date: Thu, 19 Sep 2024 20:59:53 +0800 Subject: [PATCH 7/9] minor change --- src/steadystate.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/steadystate.jl b/src/steadystate.jl index 86c9cc2e0..f8eec06ee 100644 --- a/src/steadystate.jl +++ b/src/steadystate.jl @@ -97,12 +97,10 @@ function steadystate( N = prod(H.dims) u0 = _convert_u0(mat2vec(ket2dm(ψ0).data)) - Ftype = real(eltype(u0)) - Tspan = (convert(Ftype, 0), convert(Ftype, tspan)) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl - L = MatrixOperator(liouvillian(H, c_ops).data) - prob = ODEProblem{true}(L, u0, Tspan) + Ftype = real(eltype(u0)) + prob = ODEProblem{true}(L, u0, (Ftype(0), Ftype(tspan))) # Convert tspan to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl sol = solve( prob, solver.alg; From 6bca7394fc0efa9b26ebdbf7ee8636bf62a29ca2 Mon Sep 17 00:00:00 2001 From: Yi-Te Huang Date: Fri, 20 Sep 2024 00:09:29 +0800 Subject: [PATCH 8/9] handle type of `u0` with `sparse_to_dense` --- ext/QuantumToolboxCUDAExt.jl | 8 +++++--- src/qobj/functions.jl | 6 +++++- src/qobj/quantum_object.jl | 4 ++++ src/steadystate.jl | 6 +++--- src/time_evolution/mcsolve.jl | 2 +- src/time_evolution/mesolve.jl | 4 ++-- src/time_evolution/sesolve.jl | 4 ++-- src/utilities.jl | 17 +++++++++++++++-- test/core-test/time_evolution.jl | 8 +++++++- test/runtests.jl | 2 +- 10 files changed, 45 insertions(+), 16 deletions(-) diff --git a/ext/QuantumToolboxCUDAExt.jl b/ext/QuantumToolboxCUDAExt.jl index 8886a2e35..7d379c3a1 100644 --- a/ext/QuantumToolboxCUDAExt.jl +++ b/ext/QuantumToolboxCUDAExt.jl @@ -1,7 +1,6 @@ module QuantumToolboxCUDAExt using QuantumToolbox -import QuantumToolbox: _convert_u0 import CUDA: cu, CuArray import CUDA.CUSPARSE: CuSparseVector, CuSparseMatrixCSC, CuSparseMatrixCSR import SparseArrays: SparseVector, SparseMatrixCSC @@ -90,7 +89,10 @@ _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 -# make sure u0 in time evolution is dense vector and has complex element type -_convert_u0(u0::Union{CuArray{T},CuSparseVector{T}}) where {T<:Number} = convert(CuArray{complex(T)}, u0) +sparse_to_dense(::Type{T}, A::CuArray{T}) where {T<:Number} = A +sparse_to_dense(::Type{T1}, A::CuArray{T2}) where {T1<:Number,T2<:Number} = CuArray{T1}(A) +sparse_to_dense(::Type{T}, A::CuSparseVector) where {T<:Number} = CuArray{T}(A) +sparse_to_dense(::Type{T}, A::CuSparseMatrixCSC) where {T<:Number} = CuArray{T}(A) +sparse_to_dense(::Type{T}, A::CuSparseMatrixCSR) where {T<:Number} = CuArray{T}(A) end diff --git a/src/qobj/functions.jl b/src/qobj/functions.jl index a69d8c108..de729d08c 100644 --- a/src/qobj/functions.jl +++ b/src/qobj/functions.jl @@ -108,12 +108,16 @@ variance(O::QuantumObject{<:AbstractArray{T1},OperatorQuantumObject}, ψ::Vector Converts a sparse QuantumObject to a dense QuantumObject. """ sparse_to_dense(A::QuantumObject{<:AbstractVecOrMat}) = QuantumObject(sparse_to_dense(A.data), A.type, A.dims) -sparse_to_dense(A::MT) where {MT<:AbstractSparseMatrix} = Array(A) +sparse_to_dense(A::MT) where {MT<:AbstractSparseArray} = Array(A) for op in (:Transpose, :Adjoint) @eval sparse_to_dense(A::$op{T,<:AbstractSparseMatrix}) where {T<:BlasFloat} = Array(A) end sparse_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 + function sparse_to_dense(::Type{M}) where {M<:SparseMatrixCSC} T = M par = T.parameters diff --git a/src/qobj/quantum_object.jl b/src/qobj/quantum_object.jl index 3a2ce8ed8..f55244b2b 100644 --- a/src/qobj/quantum_object.jl +++ b/src/qobj/quantum_object.jl @@ -368,3 +368,7 @@ SparseArrays.SparseMatrixCSC(A::QuantumObject{<:AbstractMatrix}) = QuantumObject(SparseMatrixCSC(A.data), A.type, A.dims) SparseArrays.SparseMatrixCSC{T}(A::QuantumObject{<:SparseMatrixCSC}) where {T<:Number} = QuantumObject(SparseMatrixCSC{T}(A.data), A.type, A.dims) + +# functions for getting Float or Complex element type +_FType(::QuantumObject{<:AbstractArray{T}}) where {T<:Number} = _FType(T) +_CType(::QuantumObject{<:AbstractArray{T}}) where {T<:Number} = _CType(T) diff --git a/src/steadystate.jl b/src/steadystate.jl index f8eec06ee..080f17281 100644 --- a/src/steadystate.jl +++ b/src/steadystate.jl @@ -95,12 +95,12 @@ function steadystate( (H.dims != ψ0.dims) && throw(DimensionMismatch("The two quantum objects are not of the same Hilbert dimension.")) N = prod(H.dims) - u0 = _convert_u0(mat2vec(ket2dm(ψ0).data)) + u0 = sparse_to_dense(_CType(ψ0), mat2vec(ket2dm(ψ0).data)) L = MatrixOperator(liouvillian(H, c_ops).data) - Ftype = real(eltype(u0)) - prob = ODEProblem{true}(L, u0, (Ftype(0), Ftype(tspan))) # Convert tspan to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl + ftype = _FType(ψ0) + prob = ODEProblem{true}(L, u0, (ftype(0), ftype(tspan))) # Convert tspan to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl sol = solve( prob, solver.alg; diff --git a/src/time_evolution/mcsolve.jl b/src/time_evolution/mcsolve.jl index 9dbd68da1..34a722ba8 100644 --- a/src/time_evolution/mcsolve.jl +++ b/src/time_evolution/mcsolve.jl @@ -190,7 +190,7 @@ function mcsolveProblem( c_ops isa Nothing && throw(ArgumentError("The list of collapse operators must be provided. Use sesolveProblem instead.")) - t_l = convert(Vector{real(eltype(ψ0))}, tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl + t_l = convert(Vector{_FType(ψ0)}, tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl H_eff = H - 1im * mapreduce(op -> op' * op, +, c_ops) / 2 diff --git a/src/time_evolution/mesolve.jl b/src/time_evolution/mesolve.jl index 9b829ddcd..95a591f58 100644 --- a/src/time_evolution/mesolve.jl +++ b/src/time_evolution/mesolve.jl @@ -122,9 +122,9 @@ function mesolveProblem( is_time_dependent = !(H_t isa Nothing) progress_bar_val = makeVal(progress_bar) - ρ0 = _convert_u0(mat2vec(ket2dm(ψ0).data)) + ρ0 = sparse_to_dense(_CType(ψ0), mat2vec(ket2dm(ψ0).data)) # Convert it to dense vector with complex element type - t_l = convert(Vector{real(eltype(ρ0))}, tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl + t_l = convert(Vector{_FType(ψ0)}, tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl L = liouvillian(H, c_ops).data progr = ProgressBar(length(t_l), enable = getVal(progress_bar_val)) diff --git a/src/time_evolution/sesolve.jl b/src/time_evolution/sesolve.jl index 28f39a8d0..264a6527b 100644 --- a/src/time_evolution/sesolve.jl +++ b/src/time_evolution/sesolve.jl @@ -103,9 +103,9 @@ function sesolveProblem( is_time_dependent = !(H_t isa Nothing) progress_bar_val = makeVal(progress_bar) - ϕ0 = _convert_u0(get_data(ψ0)) + ϕ0 = sparse_to_dense(_CType(ψ0), get_data(ψ0)) # Convert it to dense vector with complex element type - t_l = convert(Vector{real(eltype(ϕ0))}, tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl + t_l = convert(Vector{_FType(ψ0)}, tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl U = -1im * get_data(H) progr = ProgressBar(length(t_l), enable = getVal(progress_bar_val)) diff --git a/src/utilities.jl b/src/utilities.jl index 7fb8ccbcc..a2888b70c 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -66,5 +66,18 @@ _non_static_array_warning(argname, arg::AbstractVector{T}) where {T} = join(arg, ", ") * ")` instead of `$argname = $arg`." maxlog = 1 -# make sure u0 in time evolution is dense vector and has complex element type -_convert_u0(u0::AbstractVector{T}) where {T<:Number} = convert(Vector{complex(T)}, u0) +# functions for getting Float or Complex element type +_FType(::AbstractArray{T}) where {T<:Number} = _FType(T) +_FType(::Type{Int32}) = Float32 +_FType(::Type{Int64}) = Float64 +_FType(::Type{Float32}) = Float32 +_FType(::Type{Float64}) = Float64 +_FType(::Type{ComplexF32}) = Float32 +_FType(::Type{ComplexF64}) = Float64 +_CType(::AbstractArray{T}) where {T<:Number} = _CType(T) +_CType(::Type{Int32}) = ComplexF32 +_CType(::Type{Int64}) = ComplexF64 +_CType(::Type{Float32}) = ComplexF32 +_CType(::Type{Float64}) = ComplexF64 +_CType(::Type{ComplexF32}) = ComplexF32 +_CType(::Type{ComplexF64}) = ComplexF64 diff --git a/test/core-test/time_evolution.jl b/test/core-test/time_evolution.jl index b4d097dea..f86d0bfe6 100644 --- a/test/core-test/time_evolution.jl +++ b/test/core-test/time_evolution.jl @@ -33,7 +33,9 @@ "reltol = $(sol.reltol)\n" @testset "Type Inference sesolve" begin - @inferred sesolveProblem(H, psi0, t_l) + @inferred sesolveProblem(H, psi0, t_l, progress_bar = Val(false)) + @inferred sesolveProblem(H, psi0, [0, 10], progress_bar = Val(false)) + @inferred sesolveProblem(H, Qobj(zeros(Int64, N * 2); dims = (N, 2)), t_l, progress_bar = Val(false)) @inferred sesolve(H, psi0, t_l, e_ops = e_ops, progress_bar = Val(false)) @inferred sesolve(H, psi0, t_l, progress_bar = Val(false)) @inferred sesolve(H, psi0, t_l, e_ops = e_ops, saveat = t_l, progress_bar = Val(false)) @@ -91,6 +93,8 @@ @testset "Type Inference mesolve" begin @inferred mesolveProblem(H, psi0, t_l, c_ops, e_ops = e_ops, progress_bar = Val(false)) + @inferred mesolveProblem(H, psi0, [0, 10], c_ops, e_ops = e_ops, progress_bar = Val(false)) + @inferred mesolveProblem(H, Qobj(zeros(Int64, N)), t_l, c_ops, e_ops = e_ops, progress_bar = Val(false)) @inferred mesolve(H, psi0, t_l, c_ops, e_ops = e_ops, progress_bar = Val(false)) @inferred mesolve(H, psi0, t_l, c_ops, progress_bar = Val(false)) @inferred mesolve(H, psi0, t_l, c_ops, e_ops = e_ops, saveat = t_l, progress_bar = Val(false)) @@ -108,6 +112,8 @@ ) @inferred mcsolve(H, psi0, t_l, c_ops, n_traj = 500, e_ops = e_ops, progress_bar = Val(false)) @inferred mcsolve(H, psi0, t_l, c_ops, n_traj = 500, progress_bar = Val(true)) + @inferred mcsolve(H, psi0, [0, 10], c_ops, n_traj = 500, progress_bar = Val(false)) + @inferred mcsolve(H, Qobj(zeros(Int64, N)), t_l, c_ops, n_traj = 500, progress_bar = Val(false)) end end diff --git a/test/runtests.jl b/test/runtests.jl index 70c361cd0..cd69fb2cf 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -39,7 +39,7 @@ if (GROUP == "All") || (GROUP == "Core") end end -if (GROUP == "CUDA_Ext")# || (GROUP == "All") +if (GROUP == "CUDA_Ext") || (GROUP == "All") Pkg.add("CUDA") include(joinpath(testdir, "ext-test", "cuda_ext.jl")) end From 725fa0b3ff220f5523dc482416d72e2d3a13c97b Mon Sep 17 00:00:00 2001 From: Yi-Te Huang <44385685+ytdHuang@users.noreply.github.com> Date: Fri, 20 Sep 2024 00:11:13 +0800 Subject: [PATCH 9/9] fix typo --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index cd69fb2cf..70c361cd0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -39,7 +39,7 @@ if (GROUP == "All") || (GROUP == "Core") end end -if (GROUP == "CUDA_Ext") || (GROUP == "All") +if (GROUP == "CUDA_Ext")# || (GROUP == "All") Pkg.add("CUDA") include(joinpath(testdir, "ext-test", "cuda_ext.jl")) end