Skip to content

Commit a1c4c16

Browse files
Minor changes
1 parent 969f665 commit a1c4c16

File tree

9 files changed

+63
-60
lines changed

9 files changed

+63
-60
lines changed

src/qobj/arithmetic_and_attributes.jl

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ end
3737
for op in (:(+), :(-), :(*))
3838
@eval begin
3939
function LinearAlgebra.$op(A::AbstractQuantumObject, B::AbstractQuantumObject)
40-
A.dims != B.dims &&
41-
throw(DimensionMismatch("The two quantum object don't have the same Hilbert dimension."))
40+
check_dims(A, B)
4241
if A isa QuantumObjectEvolution || B isa QuantumObjectEvolution
4342
return QuantumObjectEvolution($(op)(A.data, B.data), A.type, A.dims)
4443
end
@@ -57,56 +56,56 @@ function LinearAlgebra.:(*)(
5756
A::AbstractQuantumObject{DT1,OperatorQuantumObject},
5857
B::QuantumObject{DT2,KetQuantumObject},
5958
) where {DT1,DT2}
60-
A.dims != B.dims && throw(DimensionMismatch("The two quantum object don't have the same Hilbert dimension."))
59+
check_dims(A, B)
6160
return QuantumObject(A.data * B.data, Ket, A.dims)
6261
end
6362
function LinearAlgebra.:(*)(
6463
A::QuantumObject{DT1,BraQuantumObject},
6564
B::AbstractQuantumObject{DT2,OperatorQuantumObject},
6665
) where {DT1,DT2}
67-
A.dims != B.dims && throw(DimensionMismatch("The two quantum object don't have the same Hilbert dimension."))
66+
check_dims(A, B)
6867
return QuantumObject(A.data * B.data, Bra, A.dims)
6968
end
7069
function LinearAlgebra.:(*)(
7170
A::QuantumObject{DT1,KetQuantumObject},
7271
B::QuantumObject{DT2,BraQuantumObject},
7372
) where {DT1,DT2}
74-
A.dims != B.dims && throw(DimensionMismatch("The two quantum object don't have the same Hilbert dimension."))
73+
check_dims(A, B)
7574
return QuantumObject(A.data * B.data, Operator, A.dims)
7675
end
7776
function LinearAlgebra.:(*)(
7877
A::QuantumObject{DT1,BraQuantumObject},
7978
B::QuantumObject{DT2,KetQuantumObject},
8079
) where {DT1,DT2}
81-
A.dims != B.dims && throw(DimensionMismatch("The two quantum object don't have the same Hilbert dimension."))
80+
check_dims(A, B)
8281
return A.data * B.data
8382
end
8483
function LinearAlgebra.:(*)(
8584
A::AbstractQuantumObject{DT1,SuperOperatorQuantumObject},
8685
B::QuantumObject{DT2,OperatorQuantumObject},
8786
) where {DT1,DT2}
88-
A.dims != B.dims && throw(DimensionMismatch("The two quantum object don't have the same Hilbert dimension."))
87+
check_dims(A, B)
8988
return QuantumObject(vec2mat(A.data * mat2vec(B.data)), Operator, A.dims)
9089
end
9190
function LinearAlgebra.:(*)(
9291
A::QuantumObject{DT1,OperatorBraQuantumObject},
9392
B::QuantumObject{DT2,OperatorKetQuantumObject},
9493
) where {DT1,DT2}
95-
A.dims != B.dims && throw(DimensionMismatch("The two quantum object don't have the same Hilbert dimension."))
94+
check_dims(A, B)
9695
return A.data * B.data
9796
end
9897
function LinearAlgebra.:(*)(
9998
A::AbstractQuantumObject{DT1,SuperOperatorQuantumObject},
10099
B::QuantumObject{DT2,OperatorKetQuantumObject},
101100
) where {DT1,DT2}
102-
A.dims != B.dims && throw(DimensionMismatch("The two quantum object don't have the same Hilbert dimension."))
101+
check_dims(A, B)
103102
return QuantumObject(A.data * B.data, OperatorKet, A.dims)
104103
end
105104
function LinearAlgebra.:(*)(
106105
A::QuantumObject{<:AbstractArray{T1},OperatorBraQuantumObject},
107106
B::AbstractQuantumObject{<:AbstractArray{T2},SuperOperatorQuantumObject},
108107
) where {T1,T2}
109-
A.dims != B.dims && throw(DimensionMismatch("The two quantum object don't have the same Hilbert dimension."))
108+
check_dims(A, B)
110109
return QuantumObject(A.data * B.data, OperatorBra, A.dims)
111110
end
112111

@@ -445,8 +444,8 @@ Matrix cosine of [`QuantumObject`](@ref), defined as
445444
Note that this function only supports for [`Operator`](@ref) and [`SuperOperator`](@ref)
446445
"""
447446
LinearAlgebra.cos(
448-
A::QuantumObject{<:AbstractMatrix{T},ObjType},
449-
) where {T,ObjType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} = (exp(1im * A) + exp(-1im * A)) / 2
447+
A::QuantumObject{DT,ObjType},
448+
) where {DT,ObjType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} = (exp(1im * A) + exp(-1im * A)) / 2
450449

451450
@doc raw"""
452451
diag(A::QuantumObject, k::Int=0)

src/qobj/quantum_object.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ _check_dims(dims::Any) = throw(
232232
),
233233
)
234234

235+
function check_dims(A::AbstractQuantumObject, B::AbstractQuantumObject)
236+
A.dims != B.dims && throw(DimensionMismatch("The two quantum objects don't have the same Hilbert dimension."))
237+
return nothing
238+
end
239+
235240
function _check_QuantumObject(type::KetQuantumObject, dims, m::Int, n::Int)
236241
(n != 1) && throw(DomainError((m, n), "The size of the array is not compatible with Ket"))
237242
(prod(dims) != m) && throw(DimensionMismatch("Ket with dims = $(dims) does not fit the array size = $((m, n))."))

src/qobj/superoperators.jl

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function sprepost(
6969
A::QuantumObject{<:AbstractArray{T1},OperatorQuantumObject},
7070
B::QuantumObject{<:AbstractArray{T2},OperatorQuantumObject},
7171
) where {T1,T2}
72-
A.dims != B.dims && throw(DimensionMismatch("The two quantum objects don't have the same Hilbert dimension."))
72+
check_dims(A, B)
7373

7474
return QuantumObject(_sprepost(A.data, B.data), SuperOperator, A.dims)
7575
end
@@ -90,12 +90,50 @@ the same function is applied multiple times with a known Hilbert space dimension
9090
See also [`spre`](@ref) and [`spost`](@ref).
9191
"""
9292
function lindblad_dissipator(
93-
O::QuantumObject{<:AbstractArray{T},OperatorQuantumObject},
93+
O::QuantumObject{DT,OperatorQuantumObject},
9494
Id_cache = I(size(O, 1)),
95-
) where {T}
95+
) where {DT}
9696
Od_O = O' * O
9797
return sprepost(O, O') - spre(Od_O, Id_cache) / 2 - spost(Od_O, Id_cache) / 2
9898
end
9999

100100
# It is already a SuperOperator
101-
lindblad_dissipator(O::QuantumObject{<:AbstractArray{T},SuperOperatorQuantumObject}, Id_cache) where {T} = O
101+
lindblad_dissipator(O::QuantumObject{DT,SuperOperatorQuantumObject}, Id_cache=nothing) where {DT} = O
102+
103+
@doc raw"""
104+
liouvillian(H::QuantumObject, c_ops::Union{Nothing,AbstractVector,Tuple}=nothing, Id_cache=I(prod(H.dims)))
105+
106+
Construct the Liouvillian [`SuperOperator`](@ref) for a system Hamiltonian ``\hat{H}`` and a set of collapse operators ``\{\hat{C}_n\}_n``:
107+
108+
```math
109+
\mathcal{L} [\cdot] = -i[\hat{H}, \cdot] + \sum_n \mathcal{D}(\hat{C}_n) [\cdot]
110+
```
111+
112+
where
113+
114+
```math
115+
\mathcal{D}(\hat{C}_n) [\cdot] = \hat{C}_n [\cdot] \hat{C}_n^\dagger - \frac{1}{2} \hat{C}_n^\dagger \hat{C}_n [\cdot] - \frac{1}{2} [\cdot] \hat{C}_n^\dagger \hat{C}_n
116+
```
117+
118+
The optional argument `Id_cache` can be used to pass a precomputed identity matrix. This can be useful when the same function is applied multiple times with a known Hilbert space dimension.
119+
120+
See also [`spre`](@ref), [`spost`](@ref), and [`lindblad_dissipator`](@ref).
121+
"""
122+
function liouvillian(
123+
H::QuantumObject{MT1,OpType1},
124+
c_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
125+
Id_cache = I(prod(H.dims)),
126+
) where {MT1<:AbstractMatrix,OpType1<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}}
127+
L = liouvillian(H, Id_cache)
128+
if !(c_ops isa Nothing)
129+
for c_op in c_ops
130+
L += lindblad_dissipator(c_op, Id_cache)
131+
end
132+
end
133+
return L
134+
end
135+
136+
liouvillian(H::QuantumObject{<:AbstractMatrix,OperatorQuantumObject}, Id_cache::Diagonal = I(prod(H.dims))) =
137+
-1im * (spre(H, Id_cache) - spost(H, Id_cache))
138+
139+
liouvillian(H::QuantumObject{<:AbstractMatrix,SuperOperatorQuantumObject}, Id_cache::Diagonal) = H

src/steadystate.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ function steadystate(
234234
HOpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
235235
StateOpType<:Union{KetQuantumObject,OperatorQuantumObject},
236236
}
237-
(H.dims != ψ0.dims) && throw(DimensionMismatch("The two quantum objects don't have the same Hilbert dimension."))
237+
check_dims(H, ψ0)
238238

239239
N = prod(H.dims)
240240
u0 = sparse_to_dense(_CType(ψ0), mat2vec(ket2dm(ψ0).data))

src/time_evolution/mcsolve.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function mcsolveProblem(
202202
jump_callback::TJC = ContinuousLindbladJumpCallback(),
203203
kwargs...,
204204
) where {MT1<:AbstractMatrix,TJC<:LindbladJumpCallbackType}
205-
H.dims != ψ0.dims && throw(DimensionMismatch("The two quantum objects don't have the same Hilbert dimension."))
205+
check_dims(H, ψ0)
206206

207207
haskey(kwargs, :save_idxs) &&
208208
throw(ArgumentError("The keyword argument \"save_idxs\" is not supported in QuantumToolbox."))

src/time_evolution/mesolve.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function mesolveProblem(
114114
HOpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
115115
StateOpType<:Union{KetQuantumObject,OperatorQuantumObject},
116116
}
117-
H.dims != ψ0.dims && throw(DimensionMismatch("The two quantum objects don't have the same Hilbert dimension."))
117+
check_dims(H, ψ0)
118118

119119
haskey(kwargs, :save_idxs) &&
120120
throw(ArgumentError("The keyword argument \"save_idxs\" is not supported in QuantumToolbox."))

src/time_evolution/sesolve.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function sesolveProblem(
9595
progress_bar::Union{Val,Bool} = Val(true),
9696
kwargs...,
9797
) where {MT1<:AbstractMatrix,T2}
98-
H.dims != ψ0.dims && throw(DimensionMismatch("The two quantum objects don't have the same Hilbert dimension."))
98+
check_dims(H, ψ0)
9999

100100
haskey(kwargs, :save_idxs) &&
101101
throw(ArgumentError("The keyword argument \"save_idxs\" is not supported in QuantumToolbox."))

src/time_evolution/ssesolve.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ function ssesolveProblem(
156156
rng::AbstractRNG = default_rng(),
157157
kwargs...,
158158
) where {MT1<:AbstractMatrix,T2}
159-
H.dims != ψ0.dims && throw(DimensionMismatch("The two quantum objects don't have the same Hilbert dimension."))
159+
check_dims(H, ψ0)
160160

161161
haskey(kwargs, :save_idxs) &&
162162
throw(ArgumentError("The keyword argument \"save_idxs\" is not supported in QuantumToolbox."))

src/time_evolution/time_evolution.jl

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -196,45 +196,6 @@ end
196196

197197
#######################################
198198

199-
### LIOUVILLIAN ###
200-
@doc raw"""
201-
liouvillian(H::QuantumObject, c_ops::Union{Nothing,AbstractVector,Tuple}=nothing, Id_cache=I(prod(H.dims)))
202-
203-
Construct the Liouvillian [`SuperOperator`](@ref) for a system Hamiltonian ``\hat{H}`` and a set of collapse operators ``\{\hat{C}_n\}_n``:
204-
205-
```math
206-
\mathcal{L} [\cdot] = -i[\hat{H}, \cdot] + \sum_n \mathcal{D}(\hat{C}_n) [\cdot]
207-
```
208-
209-
where
210-
211-
```math
212-
\mathcal{D}(\hat{C}_n) [\cdot] = \hat{C}_n [\cdot] \hat{C}_n^\dagger - \frac{1}{2} \hat{C}_n^\dagger \hat{C}_n [\cdot] - \frac{1}{2} [\cdot] \hat{C}_n^\dagger \hat{C}_n
213-
```
214-
215-
The optional argument `Id_cache` can be used to pass a precomputed identity matrix. This can be useful when the same function is applied multiple times with a known Hilbert space dimension.
216-
217-
See also [`spre`](@ref), [`spost`](@ref), and [`lindblad_dissipator`](@ref).
218-
"""
219-
function liouvillian(
220-
H::QuantumObject{MT1,OpType1},
221-
c_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
222-
Id_cache = I(prod(H.dims)),
223-
) where {MT1<:AbstractMatrix,OpType1<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}}
224-
L = liouvillian(H, Id_cache)
225-
if !(c_ops isa Nothing)
226-
for c_op in c_ops
227-
L += lindblad_dissipator(c_op, Id_cache)
228-
end
229-
end
230-
return L
231-
end
232-
233-
liouvillian(H::QuantumObject{<:AbstractMatrix,OperatorQuantumObject}, Id_cache::Diagonal = I(prod(H.dims))) =
234-
-1im * (spre(H, Id_cache) - spost(H, Id_cache))
235-
236-
liouvillian(H::QuantumObject{<:AbstractMatrix,SuperOperatorQuantumObject}, Id_cache::Diagonal) = H
237-
238199
function liouvillian_floquet(
239200
L₀::QuantumObject{<:AbstractArray{T1},SuperOperatorQuantumObject},
240201
Lₚ::QuantumObject{<:AbstractArray{T2},SuperOperatorQuantumObject},

0 commit comments

Comments
 (0)