Skip to content

Commit ee30a2e

Browse files
Avoid to forcing the type of eigen result, and use Val to choose between sparse or dense solvers
1 parent 3077816 commit ee30a2e

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

src/qobj/eigsolve.jl

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ julia> U
6464
```
6565
"""
6666
struct EigsolveResult{
67-
T1<:Vector{<:Number},
67+
T1<:AbstractVector{<:Number},
6868
T2<:AbstractMatrix{<:Number},
6969
ObjType<:Union{Nothing,Operator,SuperOperator},
7070
DimType<:Union{Nothing,AbstractDimensions},
@@ -551,11 +551,11 @@ true
551551
```
552552
"""
553553
function LinearAlgebra.eigen(A::QuantumObject{OpType}; kwargs...) where {OpType<:Union{Operator,SuperOperator}}
554-
MT = typeof(A.data)
554+
# This creates a weak Union type on CPU. See https://github.com/JuliaLang/LinearAlgebra.jl/issues/1498
555555
F = eigen(to_dense(A.data); kwargs...)
556-
# This fixes a type inference issue. But doesn't work for GPU arrays
557-
E::mat2vec(to_dense(MT)) = F.values
558-
U::to_dense(MT) = F.vectors
556+
557+
E = F.values
558+
U = F.vectors
559559
settings.auto_tidyup && tidyup!(U)
560560

561561
return EigsolveResult(E, U, A.type, A.dimensions, 0, 0, true)
@@ -570,51 +570,57 @@ LinearAlgebra.eigvals(A::QuantumObject{OpType}; kwargs...) where {OpType<:Union{
570570
eigvals(to_dense(A.data); kwargs...)
571571

572572
@doc raw"""
573-
eigenenergies(A::QuantumObject; sparse::Bool=false, kwargs...)
573+
eigenenergies(A::QuantumObject; sparse::Union{Bool,Val}=Val(false), kwargs...)
574574
575575
Calculate the eigenenergies
576576
577577
# Arguments
578578
- `A::QuantumObject`: the [`QuantumObject`](@ref) to solve eigenvalues
579-
- `sparse::Bool`: if `false` call [`eigvals(A::QuantumObject; kwargs...)`](@ref), otherwise call [`eigsolve`](@ref). Default to `false`.
579+
- `sparse::Union{Bool,Val}`: if `false` call [`eigvals(A::QuantumObject; kwargs...)`](@ref), otherwise call [`eigsolve`](@ref). Default to `Val(false)`.
580580
- `kwargs`: Additional keyword arguments passed to the solver. If `sparse=true`, the keyword arguments are passed to [`eigsolve`](@ref), otherwise to [`eigen`](@ref).
581581
582+
!!! warning "Beware of type-stability!"
583+
If you want to keep type stability, it is recommended to use `eigenenergies(A; sparse=Val(sparse))` instead of `eigenenergies(A; sparse=sparse)`. See the [related Section](@ref doc:Type-Stability) about type stability for more details.
584+
582585
# Returns
583586
- `::Vector{<:Number}`: a list of eigenvalues
584587
"""
585588
function eigenenergies(
586589
A::QuantumObject{OpType};
587-
sparse::Bool = false,
590+
sparse::Union{Bool,Val} = Val(false),
588591
kwargs...,
589592
) where {OpType<:Union{Operator,SuperOperator}}
590-
if !sparse
591-
return eigvals(A; kwargs...)
592-
else
593+
if getVal(sparse)
593594
return eigsolve(A; kwargs...).values
595+
else
596+
return eigvals(A; kwargs...)
594597
end
595598
end
596599

597600
@doc raw"""
598-
eigenstates(A::QuantumObject; sparse::Bool=false, kwargs...)
601+
eigenstates(A::QuantumObject; sparse::Union{Bool,Val}=Val(false), kwargs...)
599602
600603
Calculate the eigenvalues and corresponding eigenvectors
601604
602605
# Arguments
603606
- `A::QuantumObject`: the [`QuantumObject`](@ref) to solve eigenvalues and eigenvectors
604-
- `sparse::Bool`: if `false` call [`eigen(A::QuantumObject; kwargs...)`](@ref), otherwise call [`eigsolve`](@ref). Default to `false`.
607+
- `sparse::Union{Bool,Val}`: if `false` call [`eigen(A::QuantumObject; kwargs...)`](@ref), otherwise call [`eigsolve`](@ref). Default to `Val(false)`.
605608
- `kwargs`: Additional keyword arguments passed to the solver. If `sparse=true`, the keyword arguments are passed to [`eigsolve`](@ref), otherwise to [`eigen`](@ref).
606609
610+
!!! warning "Beware of type-stability!"
611+
If you want to keep type stability, it is recommended to use `eigenstates(A; sparse=Val(sparse))` instead of `eigenstates(A; sparse=sparse)`. See the [related Section](@ref doc:Type-Stability) about type stability for more details.
612+
607613
# Returns
608614
- `::EigsolveResult`: containing the eigenvalues, the eigenvectors, and some information from the solver. see also [`EigsolveResult`](@ref)
609615
"""
610616
function eigenstates(
611617
A::QuantumObject{OpType};
612-
sparse::Bool = false,
618+
sparse::Union{Bool,Val} = Val(false),
613619
kwargs...,
614620
) where {OpType<:Union{Operator,SuperOperator}}
615-
if !sparse
616-
return eigen(A; kwargs...)
617-
else
621+
if getVal(sparse)
618622
return eigsolve(A; kwargs...)
623+
else
624+
return eigen(A; kwargs...)
619625
end
620626
end

test/core-test/eigenvalues_and_operators.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,11 @@
105105
c_ops = [((1 + n_th) * κ) * a, κ * b, (n_th * κ) * a_d]
106106
L = liouvillian(H, c_ops)
107107

108-
@inferred eigenstates(H, sparse = false)
109-
@inferred eigenstates(H, sparse = true)
110-
@inferred eigenstates(L, sparse = true)
108+
UnionType = Union{QuantumToolbox.EigsolveResult{Vector{ComplexF64}, Matrix{ComplexF64}, QuantumToolbox.Operator, QuantumToolbox.Dimensions{2, Tuple{QuantumToolbox.Space, QuantumToolbox.Space}}}, QuantumToolbox.EigsolveResult{Vector{Float64}, Matrix{ComplexF64}, QuantumToolbox.Operator, QuantumToolbox.Dimensions{2, Tuple{QuantumToolbox.Space, QuantumToolbox.Space}}}}
109+
110+
@inferred UnionType eigenstates(H, sparse = Val(false))
111+
@inferred eigenstates(H, sparse = Val(true))
112+
@inferred eigenstates(L, sparse = Val(true))
111113
@inferred eigsolve_al(L, 1 \ (40 * κ), eigvals = 10)
112114
end
113115
end

test/ext-test/gpu/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ CUDSS = "45b445bb-4962-46a0-9369-b4df9d0f772e"
44
LinearSolve = "7ed4a6bd-45f5-4d41-b270-4a48e9bafcae"
55
QuantumToolbox = "6c2fb7c5-b903-41d2-bc5e-5a7c320b9fab"
66

7+
[sources]
8+
QuantumToolbox = {path = "/home/alberto/.julia/dev/QuantumToolbox"}
9+
710
[compat]
811
CUDA = "5"

0 commit comments

Comments
 (0)