Skip to content

Commit bf04bdc

Browse files
authored
Support for new Dimensions structure in QuantumToolbox (#136)
2 parents 5216f8d + 299d471 commit bf04bdc

23 files changed

+248
-153
lines changed

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "HierarchicalEOM"
22
uuid = "a62dbcb7-80f5-4d31-9a88-8b19fd92b128"
3-
authors = ["Yi-Te Huang <[email protected]>"]
4-
version = "2.3.3"
3+
authors = ["Yi-Te Huang"]
4+
version = "2.4.0"
55

66
[deps]
77
DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def"
@@ -37,7 +37,7 @@ LinearSolve = "2.4.2 - 2"
3737
OrdinaryDiffEqCore = "1"
3838
OrdinaryDiffEqLowOrderRK = "1"
3939
Pkg = "1"
40-
QuantumToolbox = "0.22 - 0.24"
40+
QuantumToolbox = "0.25"
4141
Reexport = "1"
4242
SciMLBase = "2"
4343
SciMLOperators = "0.3"

docs/src/ADOs.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ which is usually obtained after solving the [time evolution](@ref doc-Time-Evolu
2121
## Fields
2222
The fields of the structure [`ADOs`](@ref) are as follows:
2323
- `data` : the vectorized auxiliary density operators
24-
- `dims` : the dimension list of the coupling operator (should be equal to the system dims).
24+
- `dimensions` : the dimension list of the coupling operator (should be equal to the system dimensions).
2525
- `N` : the number of auxiliary density operators
2626
- `parity`: the [parity](@ref doc-Parity) label
2727

@@ -31,6 +31,7 @@ One obtain the value of each fields as follows:
3131
ados::ADOs
3232

3333
ados.data
34+
ados.dimensions
3435
ados.dims
3536
ados.N
3637
ados.parity

docs/src/heom_matrix/M_Boson.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ M_odd = M_Boson(Hs, tier, Bath, ODD)
3131
The fields of the structure [`M_Boson`](@ref) are as follows:
3232
- `data` : the sparse matrix of HEOM Liouvillian superoperator
3333
- `tier` : the tier (cutoff level) for the bosonic hierarchy
34-
- `dims` : the dimension list of the coupling operator (should be equal to the system dims).
34+
- `dimensions` : the dimension list of the coupling operator (should be equal to the system dimensions).
3535
- `N` : the number of total [ADOs](@ref doc-ADOs)
3636
- `sup_dim` : the dimension of system superoperator
3737
- `parity` : the [parity](@ref doc-Parity) label of the operator which HEOMLS is acting on.
@@ -44,6 +44,7 @@ M::M_Boson
4444

4545
M.data
4646
M.tier
47+
M.dimensions
4748
M.dims
4849
M.N
4950
M.sup_dim

docs/src/heom_matrix/M_Boson_Fermion.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The fields of the structure [`M_Boson_Fermion`](@ref) are as follows:
3636
- `data` : the sparse matrix of HEOM Liouvillian superoperator
3737
- `Btier` : the tier (cutoff level) for bosonic hierarchy
3838
- `Ftier` : the tier (cutoff level) for fermionic hierarchy
39-
- `dims` : the dimension list of the coupling operator (should be equal to the system dims).
39+
- `dimensions` : the dimension list of the coupling operator (should be equal to the system dimensions).
4040
- `N` : the number of total [ADOs](@ref doc-ADOs)
4141
- `sup_dim` : the dimension of system superoperator
4242
- `parity` : the [parity](@ref doc-Parity) label of the operator which HEOMLS is acting on.
@@ -51,6 +51,7 @@ M::M_Boson_Fermion
5151
M.data
5252
M.Btier
5353
M.Ftier
54+
M.dimensions
5455
M.dims
5556
M.N
5657
M.sup_dim

docs/src/heom_matrix/M_Fermion.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ M_odd = M_Fermion(Hs, tier, Bath, ODD)
3131
The fields of the structure [`M_Fermion`](@ref) are as follows:
3232
- `data` : the sparse matrix of HEOM Liouvillian superoperator
3333
- `tier` : the tier (cutoff level) for the fermionic hierarchy
34-
- `dims` : the dimension list of the coupling operator (should be equal to the system dims).
34+
- `dimensions` : the dimension list of the coupling operator (should be equal to the system dimensions).
3535
- `N` : the number of total [ADOs](@ref doc-ADOs)
3636
- `sup_dim` : the dimension of system superoperator
3737
- `parity` : the [parity](@ref doc-Parity) label of the operator which HEOMLS is acting on.
@@ -44,6 +44,7 @@ M::M_Fermion
4444

4545
M.data
4646
M.tier
47+
M.dimensions
4748
M.dims
4849
M.N
4950
M.sup_dim

docs/src/heom_matrix/schrodinger_eq.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ M_odd = M_S(Hs, ODD)
3131
The fields of the structure [`M_S`](@ref) are as follows:
3232
- `data` : the sparse matrix of HEOM Liouvillian superoperator
3333
- `tier` : the tier (cutoff level) for the hierarchy, which equals to `0` in this case
34-
- `dims` : the dimension list of the coupling operator (should be equal to the system dims).
34+
- `dimensions` : the dimension list of the coupling operator (should be equal to the system dimensions).
3535
- `N` : the number of total [ADOs](@ref doc-ADOs), which equals to `1` (only the reduced density operator) in this case
3636
- `sup_dim` : the dimension of system superoperator
3737
- `parity::AbstractParity` : the [parity](@ref doc-Parity) label of the operator which HEOMLS is acting on.
@@ -42,6 +42,7 @@ M::M_S
4242

4343
M.data
4444
M.tier
45+
M.dimensions
4546
M.dims
4647
M.N
4748
M.sup_dim

docs/src/libraryAPI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ ODD
6262
HEOMSuperOp
6363
HEOMSuperOp(op, opParity::AbstractParity, refHEOMLS::AbstractHEOMLSMatrix)
6464
HEOMSuperOp(op, opParity::AbstractParity, refADOs::ADOs)
65-
HEOMSuperOp(op, opParity::AbstractParity, dims::SVector, N::Int)
65+
HEOMSuperOp(op, opParity::AbstractParity, dims, N::Int)
6666
AbstractHEOMLSMatrix
6767
M_S
6868
M_S(Hsys::QuantumObject, parity::AbstractParity=EVEN; verbose::Bool=true)

ext/HierarchicalEOM_CUDAExt.jl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,28 @@ Return a new HEOMLS-matrix-type object with `M.data` is in the type of `CuSparse
2222
function CuSparseMatrixCSC(M::T) where {T<:AbstractHEOMLSMatrix}
2323
A_gpu = _convert_to_gpu_matrix(M.data)
2424
if T <: M_S
25-
return M_S(A_gpu, M.tier, M.dims, M.N, M.sup_dim, M.parity)
25+
return M_S(A_gpu, M.tier, M.dimensions, M.N, M.sup_dim, M.parity)
2626
elseif T <: M_Boson
27-
return M_Boson(A_gpu, M.tier, M.dims, M.N, M.sup_dim, M.parity, M.bath, M.hierarchy)
27+
return M_Boson(A_gpu, M.tier, M.dimensions, M.N, M.sup_dim, M.parity, M.bath, M.hierarchy)
2828
elseif T <: M_Fermion
29-
return M_Fermion(A_gpu, M.tier, M.dims, M.N, M.sup_dim, M.parity, M.bath, M.hierarchy)
29+
return M_Fermion(A_gpu, M.tier, M.dimensions, M.N, M.sup_dim, M.parity, M.bath, M.hierarchy)
3030
else
31-
return M_Boson_Fermion(A_gpu, M.Btier, M.Ftier, M.dims, M.N, M.sup_dim, M.parity, M.Bbath, M.Fbath, M.hierarchy)
31+
return M_Boson_Fermion(
32+
A_gpu,
33+
M.Btier,
34+
M.Ftier,
35+
M.dimensions,
36+
M.N,
37+
M.sup_dim,
38+
M.parity,
39+
M.Bbath,
40+
M.Fbath,
41+
M.hierarchy,
42+
)
3243
end
3344
end
3445

35-
CuSparseMatrixCSC{ComplexF32}(M::HEOMSuperOp) = HEOMSuperOp(_convert_to_gpu_matrix(M.data), M.dims, M.N, M.parity)
46+
CuSparseMatrixCSC{ComplexF32}(M::HEOMSuperOp) = HEOMSuperOp(_convert_to_gpu_matrix(M.data), M.dimensions, M.N, M.parity)
3647

3748
function _convert_to_gpu_matrix(A::AbstractSparseMatrix)
3849
if A isa CuSparseMatrixCSC{ComplexF32,Int32}
@@ -53,7 +64,7 @@ _convert_to_gpu_matrix(A::MatrixOperator) = MatrixOperator(_convert_to_gpu_matri
5364
_convert_to_gpu_matrix(A::ScaledOperator) = ScaledOperator(A.λ, _convert_to_gpu_matrix(A.L))
5465
_convert_to_gpu_matrix(A::AddedOperator) = AddedOperator(map(op -> _convert_to_gpu_matrix(op), A.ops))
5566

56-
_Tr(M::Type{<:CuSparseMatrixCSC}, dims::SVector, N::Int) = CuSparseVector(_Tr(eltype(M), dims, N))
67+
_Tr(M::Type{<:CuSparseMatrixCSC}, dimensions::Dimensions, N::Int) = CuSparseVector(_Tr(eltype(M), dimensions, N))
5768

5869
# change the type of `ADOs` to match the type of HEOMLS matrix
5970
_HandleVectorType(M::Type{<:CuSparseMatrixCSC}, V::SparseVector) = CuArray{_CType(eltype(M))}(V)

src/ADOs.jl

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ The Auxiliary Density Operators for HEOM model.
77
88
# Fields
99
- `data` : the vectorized auxiliary density operators
10-
- `dims` : the dimension list of the coupling operator (should be equal to the system dims).
10+
- `dimensions` : the dimension list of the coupling operator (should be equal to the system dimensions).
1111
- `N` : the number of auxiliary density operators
1212
- `parity`: the parity label (`EVEN` or `ODD`).
1313
14+
!!! note "`dims` property"
15+
For a given `ados::ADOs`, `ados.dims` or `getproperty(ados, :dims)` returns its `dimensions` in the type of integer-vector.
16+
1417
# Methods
1518
One can obtain the density matrix for specific index (`idx`) by calling : `ados[idx]`.
1619
`HierarchicalEOM.jl` also supports the following calls (methods) :
@@ -26,15 +29,17 @@ end
2629
"""
2730
struct ADOs
2831
data::SparseVector{ComplexF64,Int64}
29-
dims::SVector
32+
dimensions::Dimensions
3033
N::Int
3134
parity::AbstractParity
32-
end
3335

34-
# these functions are for forward compatibility
35-
ADOs(data::SparseVector{ComplexF64,Int64}, dim::Int, N::Int, parity::AbstractParity) = ADOs(data, [dim], N, parity)
36-
ADOs(data::SparseVector{ComplexF64,Int64}, dims::AbstractVector, N::Int, parity::AbstractParity) =
37-
ADOs(data, SVector{length(dims),Int}(dims), N, parity)
36+
function ADOs(data::AbstractVector, dims, N::Int, parity::AbstractParity)
37+
dimensions = _gen_dimensions(dims)
38+
Vsize = size(data, 1)
39+
((Vsize / N) == prod(dimensions)^2) || error("The `dimensions` is not consistent with the ADOs number `N`.")
40+
return new(sparsevec(data), dimensions, N, parity)
41+
end
42+
end
3843

3944
@doc raw"""
4045
ADOs(V, N, parity)
@@ -45,16 +50,7 @@ Generate the object of auxiliary density operators for HEOM model.
4550
- `N::Int` : the number of auxiliary density operators.
4651
- `parity::AbstractParity` : the parity label (`EVEN` or `ODD`). Default to `EVEN`.
4752
"""
48-
function ADOs(V::AbstractVector, N::Int, parity::AbstractParity = EVEN)
49-
# check the dimension of V
50-
d = size(V, 1)
51-
dim = (d / N)
52-
if isinteger(dim)
53-
return ADOs(sparsevec(V), SVector{1,Int}(Int(dim)), N, parity)
54-
else
55-
error("The dimension of vector is not consistent with the ADOs number \"N\".")
56-
end
57-
end
53+
ADOs(V::AbstractVector, N::Int, parity::AbstractParity = EVEN) = ADOs(V, isqrt(Int(size(V, 1) / N)), N, parity)
5854

5955
@doc raw"""
6056
ADOs(ρ, N, parity)
@@ -67,11 +63,20 @@ Generate the object of auxiliary density operators for HEOM model.
6763
"""
6864
function ADOs::QuantumObject, N::Int = 1, parity::AbstractParity = EVEN)
6965
= sparsevec(ket2dm(ρ).data)
70-
return ADOs(sparsevec(_ρ.nzind, _ρ.nzval, N * length(_ρ)), ρ.dims, N, parity)
66+
return ADOs(sparsevec(_ρ.nzind, _ρ.nzval, N * length(_ρ)), ρ.dimensions, N, parity)
7167
end
7268
ADOs(ρ, N::Int = 1, parity::AbstractParity = EVEN) =
7369
error("HierarchicalEOM doesn't support input `ρ` with type : $(typeof(ρ))")
7470

71+
function Base.getproperty(ados::ADOs, key::Symbol)
72+
# a comment here to avoid bad render by JuliaFormatter
73+
if key === :dims
74+
return dimensions_to_dims(getfield(ados, :dimensions))
75+
else
76+
return getfield(ados, key)
77+
end
78+
end
79+
7580
Base.checkbounds(A::ADOs, i::Int) =
7681
((i > A.N) || (i < 1)) ? error("Attempt to access $(A.N)-element ADOs at index [$(i)]") : nothing
7782

@@ -92,31 +97,33 @@ Base.lastindex(A::ADOs) = length(A)
9297
function Base.getindex(A::ADOs, i::Int)
9398
checkbounds(A, i)
9499

95-
D = prod(A.dims)
100+
D = prod(A.dimensions)
96101
sup_dim = D^2
97102
back = sup_dim * i
98-
return QuantumObject(reshape(A.data[(back-sup_dim+1):back], D, D), Operator, A.dims)
103+
return QuantumObject(reshape(A.data[(back-sup_dim+1):back], D, D), Operator, A.dimensions)
99104
end
100105

101106
function Base.getindex(A::ADOs, r::UnitRange{Int})
102107
checkbounds(A, r[1])
103108
checkbounds(A, r[end])
104109

105110
result = []
106-
D = prod(A.dims)
111+
D = prod(A.dimensions)
107112
sup_dim = D^2
108113
for i in r
109114
back = sup_dim * i
110-
push!(result, QuantumObject(reshape(A.data[(back-sup_dim+1):back], D, D), Operator, A.dims))
115+
push!(result, QuantumObject(reshape(A.data[(back-sup_dim+1):back], D, D), Operator, A.dimensions))
111116
end
112117
return result
113118
end
114119
Base.getindex(A::ADOs, ::Colon) = getindex(A, 1:lastindex(A))
115120

116121
Base.iterate(A::ADOs, state::Int = 1) = state > length(A) ? nothing : (A[state], state + 1)
117122

118-
Base.show(io::IO, A::ADOs) =
119-
print(io, "$(A.N) Auxiliary Density Operators with $(A.parity) and (system) dims = $(A.dims)\n")
123+
Base.show(io::IO, A::ADOs) = print(
124+
io,
125+
"$(A.N) Auxiliary Density Operators with $(A.parity) and (system) dims = $(_get_dims_string(A.dimensions))\n",
126+
)
120127
Base.show(io::IO, m::MIME"text/plain", A::ADOs) = show(io, A)
121128

122129
@doc raw"""
@@ -130,8 +137,8 @@ Return the density matrix of the reduced state (system) from a given auxiliary d
130137
- `ρ::QuantumObject` : The density matrix of the reduced state
131138
"""
132139
function getRho(ados::ADOs)
133-
D = prod(ados.dims)
134-
return QuantumObject(reshape(ados.data[1:(D^2)], D, D), Operator, ados.dims)
140+
D = prod(ados.dimensions)
141+
return QuantumObject(reshape(ados.data[1:(D^2)], D, D), Operator, ados.dimensions)
135142
end
136143

137144
@doc raw"""
@@ -168,9 +175,9 @@ where ``O`` is the operator and ``\rho`` is the reduced density operator in the
168175
function QuantumToolbox.expect(op, ados::ADOs; take_real::Bool = true)
169176
if op isa HEOMSuperOp
170177
_check_sys_dim_and_ADOs_num(op, ados)
171-
exp_val = dot(transpose(_Tr(eltype(ados), ados.dims, ados.N)), (SparseMatrixCSC(op) * ados).data)
178+
exp_val = dot(transpose(_Tr(eltype(ados), ados.dimensions, ados.N)), (SparseMatrixCSC(op) * ados).data)
172179
else
173-
_op = HandleMatrixType(op, ados.dims, "op (observable)"; type = Operator)
180+
_op = HandleMatrixType(op, ados.dimensions, "op (observable)"; type = Operator)
174181
exp_val = tr(_op.data * getRho(ados).data)
175182
end
176183

@@ -198,7 +205,7 @@ where ``O`` is the operator and ``\rho`` is the reduced density operator in one
198205
- `exp_val` : The expectation value
199206
"""
200207
function QuantumToolbox.expect(op, ados_list::Vector{ADOs}; take_real::Bool = true)
201-
dims = ados_list[1].dims
208+
dimensions = ados_list[1].dimensions
202209
N = ados_list[1].N
203210
for i in 2:length(ados_list)
204211
_check_sys_dim_and_ADOs_num(ados_list[1], ados_list[i])
@@ -208,9 +215,9 @@ function QuantumToolbox.expect(op, ados_list::Vector{ADOs}; take_real::Bool = tr
208215
_check_sys_dim_and_ADOs_num(op, ados_list[1])
209216
_op = op
210217
else
211-
_op = HEOMSuperOp(spre(op), EVEN, dims, N)
218+
_op = HEOMSuperOp(spre(op), EVEN, dimensions, N)
212219
end
213-
tr_op = transpose(_Tr(eltype(op), dims, N)) * SparseMatrixCSC(_op).data
220+
tr_op = transpose(_Tr(eltype(op), dimensions, N)) * SparseMatrixCSC(_op).data
214221

215222
exp_val = [dot(tr_op, ados.data) for ados in ados_list]
216223

src/HeomBase.jl

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ export AbstractHEOMLSMatrix
22

33
abstract type AbstractHEOMLSMatrix{T} end
44

5+
function Base.getproperty(M::AbstractHEOMLSMatrix, key::Symbol)
6+
# a comment here to avoid bad render by JuliaFormatter
7+
if key === :dims
8+
return dimensions_to_dims(getfield(M, :dimensions))
9+
else
10+
return getfield(M, key)
11+
end
12+
end
13+
514
@doc raw"""
615
(M::AbstractHEOMLSMatrix)(p, t)
716
@@ -32,12 +41,12 @@ _get_SciML_matrix_wrapper(M::AddedOperator) = _get_SciML_matrix_wrapper(M.ops[1]
3241
_get_SciML_matrix_wrapper(M::AbstractHEOMLSMatrix) = _get_SciML_matrix_wrapper(M.data)
3342

3443
# equal to : sparse(vec(system_identity_matrix))
35-
function _Tr(T::Type{<:Number}, dims::SVector, N::Int)
36-
D = prod(dims)
44+
function _Tr(T::Type{<:Number}, dimensions::Dimensions, N::Int)
45+
D = prod(dimensions)
3746
return SparseVector(N * D^2, [1 + n * (D + 1) for n in 0:(D-1)], ones(T, D))
3847
end
39-
_Tr(M::AbstractHEOMLSMatrix) = _Tr(_get_SciML_matrix_wrapper(M), M.dims, M.N)
40-
_Tr(M::Type{<:SparseMatrixCSC}, dims::SVector, N::Int) = _Tr(eltype(M), dims, N)
48+
_Tr(M::AbstractHEOMLSMatrix) = _Tr(_get_SciML_matrix_wrapper(M), M.dimensions, M.N)
49+
_Tr(M::Type{<:SparseMatrixCSC}, dimensions::Dimensions, N::Int) = _Tr(eltype(M), dimensions, N)
4150

4251
function HandleMatrixType(
4352
M::AbstractQuantumObject,
@@ -55,19 +64,19 @@ function HandleMatrixType(
5564
end
5665
function HandleMatrixType(
5766
M::AbstractQuantumObject,
58-
dims::SVector,
67+
dimensions::Dimensions,
5968
MatrixName::String = "";
6069
type::T = nothing,
6170
) where {T<:Union{Nothing,OperatorQuantumObject,SuperOperatorQuantumObject}}
62-
if M.dims == dims
71+
if M.dimensions == dimensions
6372
return HandleMatrixType(M, MatrixName; type = type)
6473
else
65-
error("The dims of $(MatrixName) should be: $(dims)")
74+
error("The dimensions of $(MatrixName) should be: $(_get_dims_string(dimensions))")
6675
end
6776
end
6877
HandleMatrixType(
6978
M,
70-
dims::SVector,
79+
dimensions::Dimensions,
7180
MatrixName::String = "";
7281
type::T = nothing,
7382
) where {T<:Union{Nothing,OperatorQuantumObject,SuperOperatorQuantumObject}} =
@@ -86,7 +95,7 @@ _HandleVectorType(M::Type{<:SparseMatrixCSC}, V::SparseVector) = Vector{_CType(e
8695
function _HandleSteadyStateMatrix(M::AbstractHEOMLSMatrix{<:MatrixOperator})
8796
S = size(M, 1)
8897
ElType = eltype(M)
89-
D = prod(M.dims)
98+
D = prod(M.dimensions)
9099
A = copy(M.data.A)
91100
A[1, 1:S] .= 0
92101

@@ -96,8 +105,8 @@ function _HandleSteadyStateMatrix(M::AbstractHEOMLSMatrix{<:MatrixOperator})
96105
end
97106

98107
function _check_sys_dim_and_ADOs_num(A, B)
99-
if (A.dims != B.dims)
100-
error("Inconsistent system dimension (\"dims\").")
108+
if (A.dimensions != B.dimensions)
109+
error("Inconsistent system dimensions.")
101110
end
102111

103112
if (A.N != B.N)

0 commit comments

Comments
 (0)