Skip to content

Commit b69f2b2

Browse files
authored
Change order of AbstractQuantumObject data type (#371)
2 parents 88bf11b + 5c7445f commit b69f2b2

32 files changed

+469
-601
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Add **GPUArrays** compatibility for `ptrace` function, by using **KernelAbstractions.jl**. ([#350])
1212
- Introduce `Space`, `Dimensions`, `GeneralDimensions` structures to support wider definitions and operations of `Qobj/QobjEvo`, and potential functionalities in the future. ([#271], [#353], [#360])
1313
- Improve lazy tensor warning for `SciMLOperators`. ([#370])
14+
- Change order of `AbstractQuantumObject` data type. For example, from `QuantumObject{DataType,ObjType,DimsType}` to `QuantumObject{ObjType,DimsType,DataType}`. ([#371])
1415

1516
## [v0.24.0]
1617
Release date: 2024-12-13
@@ -77,3 +78,4 @@ Release date: 2024-11-13
7778
[#353]: https://github.com/qutip/QuantumToolbox.jl/issues/353
7879
[#360]: https://github.com/qutip/QuantumToolbox.jl/issues/360
7980
[#370]: https://github.com/qutip/QuantumToolbox.jl/issues/370
81+
[#371]: https://github.com/qutip/QuantumToolbox.jl/issues/371

ext/QuantumToolboxCUDAExt.jl

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module QuantumToolboxCUDAExt
22

33
using QuantumToolbox
4+
using QuantumToolbox: makeVal, getVal
45
import CUDA: cu, CuArray
56
import CUDA.CUSPARSE: CuSparseVector, CuSparseMatrixCSC, CuSparseMatrixCSR
67
import SparseArrays: SparseVector, SparseMatrixCSC
@@ -10,60 +11,56 @@ import SparseArrays: SparseVector, SparseMatrixCSC
1011
1112
If `A.data` is a dense array, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CuArray` for gpu calculations.
1213
"""
13-
CuArray(A::QuantumObject{Tq}) where {Tq<:Union{Vector,Matrix}} = QuantumObject(CuArray(A.data), A.type, A.dimensions)
14+
CuArray(A::QuantumObject) = QuantumObject(CuArray(A.data), A.type, A.dimensions)
1415

1516
@doc raw"""
1617
CuArray{T}(A::QuantumObject)
1718
1819
If `A.data` is a dense array, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CuArray` with element type `T` for gpu calculations.
1920
"""
20-
CuArray{T}(A::QuantumObject{Tq}) where {T,Tq<:Union{Vector,Matrix}} =
21-
QuantumObject(CuArray{T}(A.data), A.type, A.dimensions)
21+
CuArray{T}(A::QuantumObject) where {T} = QuantumObject(CuArray{T}(A.data), A.type, A.dimensions)
2222

2323
@doc raw"""
2424
CuSparseVector(A::QuantumObject)
2525
2626
If `A.data` is a sparse vector, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CUSPARSE.CuSparseVector` for gpu calculations.
2727
"""
28-
CuSparseVector(A::QuantumObject{<:SparseVector}) = QuantumObject(CuSparseVector(A.data), A.type, A.dimensions)
28+
CuSparseVector(A::QuantumObject) = QuantumObject(CuSparseVector(A.data), A.type, A.dimensions)
2929

3030
@doc raw"""
3131
CuSparseVector{T}(A::QuantumObject)
3232
3333
If `A.data` is a sparse vector, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CUSPARSE.CuSparseVector` with element type `T` for gpu calculations.
3434
"""
35-
CuSparseVector{T}(A::QuantumObject{<:SparseVector}) where {T} =
36-
QuantumObject(CuSparseVector{T}(A.data), A.type, A.dimensions)
35+
CuSparseVector{T}(A::QuantumObject) where {T} = QuantumObject(CuSparseVector{T}(A.data), A.type, A.dimensions)
3736

3837
@doc raw"""
3938
CuSparseMatrixCSC(A::QuantumObject)
4039
4140
If `A.data` is in the type of `SparseMatrixCSC`, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CUSPARSE.CuSparseMatrixCSC` for gpu calculations.
4241
"""
43-
CuSparseMatrixCSC(A::QuantumObject{<:SparseMatrixCSC}) = QuantumObject(CuSparseMatrixCSC(A.data), A.type, A.dimensions)
42+
CuSparseMatrixCSC(A::QuantumObject) = QuantumObject(CuSparseMatrixCSC(A.data), A.type, A.dimensions)
4443

4544
@doc raw"""
4645
CuSparseMatrixCSC{T}(A::QuantumObject)
4746
4847
If `A.data` is in the type of `SparseMatrixCSC`, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CUSPARSE.CuSparseMatrixCSC` with element type `T` for gpu calculations.
4948
"""
50-
CuSparseMatrixCSC{T}(A::QuantumObject{<:SparseMatrixCSC}) where {T} =
51-
QuantumObject(CuSparseMatrixCSC{T}(A.data), A.type, A.dimensions)
49+
CuSparseMatrixCSC{T}(A::QuantumObject) where {T} = QuantumObject(CuSparseMatrixCSC{T}(A.data), A.type, A.dimensions)
5250

5351
@doc raw"""
5452
CuSparseMatrixCSR(A::QuantumObject)
5553
5654
If `A.data` is in the type of `SparseMatrixCSC`, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CUSPARSE.CuSparseMatrixCSR` for gpu calculations.
5755
"""
58-
CuSparseMatrixCSR(A::QuantumObject{<:SparseMatrixCSC}) = QuantumObject(CuSparseMatrixCSR(A.data), A.type, A.dimensions)
56+
CuSparseMatrixCSR(A::QuantumObject) = QuantumObject(CuSparseMatrixCSR(A.data), A.type, A.dimensions)
5957

6058
@doc raw"""
6159
CuSparseMatrixCSR(A::QuantumObject)
6260
6361
If `A.data` is in the type of `SparseMatrixCSC`, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CUSPARSE.CuSparseMatrixCSR` with element type `T` for gpu calculations.
6462
"""
65-
CuSparseMatrixCSR{T}(A::QuantumObject{<:SparseMatrixCSC}) where {T} =
66-
QuantumObject(CuSparseMatrixCSR{T}(A.data), A.type, A.dimensions)
63+
CuSparseMatrixCSR{T}(A::QuantumObject) where {T} = QuantumObject(CuSparseMatrixCSR{T}(A.data), A.type, A.dimensions)
6764

6865
@doc raw"""
6966
cu(A::QuantumObject; word_size::Int=64)
@@ -74,15 +71,26 @@ Return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA` arr
7471
- `A::QuantumObject`: The [`QuantumObject`](@ref)
7572
- `word_size::Int`: The word size of the element type of `A`, can be either `32` or `64`. Default to `64`.
7673
"""
77-
cu(A::QuantumObject; word_size::Int = 64) =
78-
((word_size == 64) || (word_size == 32)) ? cu(A, Val(word_size)) :
79-
throw(DomainError(word_size, "The word size should be 32 or 64."))
80-
cu(A::QuantumObject{T}, word_size::TW) where {T<:Union{Vector,Matrix},TW<:Union{Val{32},Val{64}}} =
81-
CuArray{_change_eltype(eltype(A), word_size)}(A)
82-
cu(A::QuantumObject{<:SparseVector}, word_size::TW) where {TW<:Union{Val{32},Val{64}}} =
83-
CuSparseVector{_change_eltype(eltype(A), word_size)}(A)
84-
cu(A::QuantumObject{<:SparseMatrixCSC}, word_size::TW) where {TW<:Union{Val{32},Val{64}}} =
85-
CuSparseMatrixCSC{_change_eltype(eltype(A), word_size)}(A)
74+
function cu(A::QuantumObject; word_size::Union{Val,Int} = Val(64))
75+
_word_size = getVal(makeVal(word_size))
76+
77+
((_word_size == 64) || (_word_size == 32)) || throw(DomainError(_word_size, "The word size should be 32 or 64."))
78+
79+
return cu(A, makeVal(word_size))
80+
end
81+
cu(A::QuantumObject, word_size::Union{Val{32},Val{64}}) = CuArray{_change_eltype(eltype(A), word_size)}(A)
82+
function cu(
83+
A::QuantumObject{ObjType,DimsType,<:SparseVector},
84+
word_size::Union{Val{32},Val{64}},
85+
) where {ObjType<:QuantumObjectType,DimsType<:AbstractDimensions}
86+
return CuSparseVector{_change_eltype(eltype(A), word_size)}(A)
87+
end
88+
function cu(
89+
A::QuantumObject{ObjType,DimsType,<:SparseMatrixCSC},
90+
word_size::Union{Val{32},Val{64}},
91+
) where {ObjType<:QuantumObjectType,DimsType<:AbstractDimensions}
92+
return CuSparseMatrixCSC{_change_eltype(eltype(A), word_size)}(A)
93+
end
8694

8795
_change_eltype(::Type{T}, ::Val{64}) where {T<:Int} = Int64
8896
_change_eltype(::Type{T}, ::Val{32}) where {T<:Int} = Int32

ext/QuantumToolboxCairoMakieExt.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using CairoMakie: Axis, Axis3, Colorbar, Figure, GridLayout, heatmap!, surface!,
66
@doc raw"""
77
plot_wigner(
88
library::Val{:CairoMakie},
9-
state::QuantumObject{DT,OpType};
9+
state::QuantumObject{OpType};
1010
xvec::Union{Nothing,AbstractVector} = nothing,
1111
yvec::Union{Nothing,AbstractVector} = nothing,
1212
g::Real = √2,
@@ -15,7 +15,7 @@ using CairoMakie: Axis, Axis3, Colorbar, Figure, GridLayout, heatmap!, surface!,
1515
location::Union{GridPosition,Nothing} = nothing,
1616
colorbar::Bool = false,
1717
kwargs...
18-
) where {DT,OpType}
18+
) where {OpType}
1919
2020
Plot the [Wigner quasipropability distribution](https://en.wikipedia.org/wiki/Wigner_quasiprobability_distribution) of `state` using the [`CairoMakie`](https://github.com/MakieOrg/Makie.jl/tree/master/CairoMakie) plotting library.
2121
@@ -44,7 +44,7 @@ Plot the [Wigner quasipropability distribution](https://en.wikipedia.org/wiki/Wi
4444
"""
4545
function QuantumToolbox.plot_wigner(
4646
library::Val{:CairoMakie},
47-
state::QuantumObject{DT,OpType};
47+
state::QuantumObject{OpType};
4848
xvec::Union{Nothing,AbstractVector} = LinRange(-7.5, 7.5, 200),
4949
yvec::Union{Nothing,AbstractVector} = LinRange(-7.5, 7.5, 200),
5050
g::Real = 2,
@@ -53,7 +53,7 @@ function QuantumToolbox.plot_wigner(
5353
location::Union{GridPosition,Nothing} = nothing,
5454
colorbar::Bool = false,
5555
kwargs...,
56-
) where {DT,OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
56+
) where {OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
5757
QuantumToolbox.getVal(projection) == :two_dim ||
5858
QuantumToolbox.getVal(projection) == :three_dim ||
5959
throw(ArgumentError("Unsupported projection: $projection"))
@@ -74,7 +74,7 @@ end
7474

7575
function _plot_wigner(
7676
::Val{:CairoMakie},
77-
state::QuantumObject{DT,OpType},
77+
state::QuantumObject{OpType},
7878
xvec::AbstractVector,
7979
yvec::AbstractVector,
8080
projection::Val{:two_dim},
@@ -83,7 +83,7 @@ function _plot_wigner(
8383
location::Union{GridPosition,Nothing},
8484
colorbar::Bool;
8585
kwargs...,
86-
) where {DT,OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
86+
) where {OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
8787
fig, location = _getFigAndLocation(location)
8888

8989
lyt = GridLayout(location)
@@ -107,7 +107,7 @@ end
107107

108108
function _plot_wigner(
109109
::Val{:CairoMakie},
110-
state::QuantumObject{DT,OpType},
110+
state::QuantumObject{OpType},
111111
xvec::AbstractVector,
112112
yvec::AbstractVector,
113113
projection::Val{:three_dim},
@@ -116,7 +116,7 @@ function _plot_wigner(
116116
location::Union{GridPosition,Nothing},
117117
colorbar::Bool;
118118
kwargs...,
119-
) where {DT,OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
119+
) where {OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
120120
fig, location = _getFigAndLocation(location)
121121

122122
lyt = GridLayout(location)

src/correlations.jl

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,16 @@ Returns the two-times correlation function of three operators ``\hat{A}``, ``\ha
2424
If the initial state `ψ0` is given as `nothing`, then the [`steadystate`](@ref) will be used as the initial state. Note that this is only implemented if `H` is constant ([`QuantumObject`](@ref)).
2525
"""
2626
function correlation_3op_2t(
27-
H::AbstractQuantumObject{DataType,HOpType},
28-
ψ0::Union{Nothing,QuantumObject{<:AbstractArray{T1},StateOpType}},
27+
H::AbstractQuantumObject{HOpType},
28+
ψ0::Union{Nothing,QuantumObject{StateOpType}},
2929
tlist::AbstractVector,
3030
τlist::AbstractVector,
3131
c_ops::Union{Nothing,AbstractVector,Tuple},
32-
A::QuantumObject{<:AbstractArray{T2},OperatorQuantumObject},
33-
B::QuantumObject{<:AbstractArray{T3},OperatorQuantumObject},
34-
C::QuantumObject{<:AbstractArray{T4},OperatorQuantumObject};
32+
A::QuantumObject{OperatorQuantumObject},
33+
B::QuantumObject{OperatorQuantumObject},
34+
C::QuantumObject{OperatorQuantumObject};
3535
kwargs...,
3636
) where {
37-
DataType,
38-
T1,
39-
T2,
40-
T3,
41-
T4,
4237
HOpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
4338
StateOpType<:Union{KetQuantumObject,OperatorQuantumObject},
4439
}
@@ -79,20 +74,15 @@ Returns the one-time correlation function of three operators ``\hat{A}``, ``\hat
7974
If the initial state `ψ0` is given as `nothing`, then the [`steadystate`](@ref) will be used as the initial state. Note that this is only implemented if `H` is constant ([`QuantumObject`](@ref)).
8075
"""
8176
function correlation_3op_1t(
82-
H::AbstractQuantumObject{DataType,HOpType},
83-
ψ0::Union{Nothing,QuantumObject{<:AbstractArray{T1},StateOpType}},
77+
H::AbstractQuantumObject{HOpType},
78+
ψ0::Union{Nothing,QuantumObject{StateOpType}},
8479
τlist::AbstractVector,
8580
c_ops::Union{Nothing,AbstractVector,Tuple},
86-
A::QuantumObject{<:AbstractArray{T2},OperatorQuantumObject},
87-
B::QuantumObject{<:AbstractArray{T3},OperatorQuantumObject},
88-
C::QuantumObject{<:AbstractArray{T4},OperatorQuantumObject};
81+
A::QuantumObject{OperatorQuantumObject},
82+
B::QuantumObject{OperatorQuantumObject},
83+
C::QuantumObject{OperatorQuantumObject};
8984
kwargs...,
9085
) where {
91-
DataType,
92-
T1,
93-
T2,
94-
T3,
95-
T4,
9686
HOpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
9787
StateOpType<:Union{KetQuantumObject,OperatorQuantumObject},
9888
}
@@ -119,20 +109,16 @@ If the initial state `ψ0` is given as `nothing`, then the [`steadystate`](@ref)
119109
When `reverse=true`, the correlation function is calculated as ``\left\langle \hat{A}(t) \hat{B}(t + \tau) \right\rangle``.
120110
"""
121111
function correlation_2op_2t(
122-
H::AbstractQuantumObject{DataType,HOpType},
123-
ψ0::Union{Nothing,QuantumObject{<:AbstractArray{T1},StateOpType}},
112+
H::AbstractQuantumObject{HOpType},
113+
ψ0::Union{Nothing,QuantumObject{StateOpType}},
124114
tlist::AbstractVector,
125115
τlist::AbstractVector,
126116
c_ops::Union{Nothing,AbstractVector,Tuple},
127-
A::QuantumObject{<:AbstractArray{T2},OperatorQuantumObject},
128-
B::QuantumObject{<:AbstractArray{T3},OperatorQuantumObject};
117+
A::QuantumObject{OperatorQuantumObject},
118+
B::QuantumObject{OperatorQuantumObject};
129119
reverse::Bool = false,
130120
kwargs...,
131121
) where {
132-
DataType,
133-
T1,
134-
T2,
135-
T3,
136122
HOpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
137123
StateOpType<:Union{KetQuantumObject,OperatorQuantumObject},
138124
}
@@ -163,19 +149,15 @@ If the initial state `ψ0` is given as `nothing`, then the [`steadystate`](@ref)
163149
When `reverse=true`, the correlation function is calculated as ``\left\langle \hat{A}(0) \hat{B}(\tau) \right\rangle``.
164150
"""
165151
function correlation_2op_1t(
166-
H::AbstractQuantumObject{DataType,HOpType},
167-
ψ0::Union{Nothing,QuantumObject{<:AbstractArray{T1},StateOpType}},
152+
H::AbstractQuantumObject{HOpType},
153+
ψ0::Union{Nothing,QuantumObject{StateOpType}},
168154
τlist::AbstractVector,
169155
c_ops::Union{Nothing,AbstractVector,Tuple},
170-
A::QuantumObject{<:AbstractArray{T2},OperatorQuantumObject},
171-
B::QuantumObject{<:AbstractArray{T3},OperatorQuantumObject};
156+
A::QuantumObject{OperatorQuantumObject},
157+
B::QuantumObject{OperatorQuantumObject};
172158
reverse::Bool = false,
173159
kwargs...,
174160
) where {
175-
DataType,
176-
T1,
177-
T2,
178-
T3,
179161
HOpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
180162
StateOpType<:Union{KetQuantumObject,OperatorQuantumObject},
181163
}

0 commit comments

Comments
 (0)