Skip to content

Commit c7d0412

Browse files
ytdHuangalbertomercurio
authored andcommitted
re-export some functions in SciMLOperators
1 parent 7a4cc92 commit c7d0412

File tree

5 files changed

+74
-25
lines changed

5 files changed

+74
-25
lines changed

docs/src/api.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ SuperOperatorQuantumObject
2828
SuperOperator
2929
QuantumObject
3030
QuantumObjectEvolution
31-
size
32-
eltype
33-
length
31+
Base.size
32+
Base.eltype
33+
Base.length
34+
SciMLOperators.cache_operator
3435
```
3536

3637
## [Qobj boolean functions](@id doc-API:Qobj-boolean-functions)
@@ -46,7 +47,8 @@ LinearAlgebra.ishermitian
4647
LinearAlgebra.issymmetric
4748
LinearAlgebra.isposdef
4849
isunitary
49-
isconstant
50+
SciMLOperators.iscached
51+
SciMLOperators.isconstant
5052
```
5153

5254
## [Qobj arithmetic and attributes](@id doc-API:Qobj-arithmetic-and-attributes)

src/QuantumToolbox.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@ module QuantumToolbox
33
# Re-export:
44
# 1. StaticArraysCore.SVector for the type of dims
55
# 2. basic functions in LinearAlgebra and SparseArrays
6+
# 3. some functions in SciMLOperators
67
import Reexport: @reexport
78
@reexport import StaticArraysCore: SVector
89
@reexport using LinearAlgebra
910
@reexport using SparseArrays
11+
@reexport import SciMLOperators:
12+
AbstractSciMLOperator,
13+
MatrixOperator,
14+
ScalarOperator,
15+
ScaledOperator,
16+
AddedOperator,
17+
IdentityOperator,
18+
cache_operator,
19+
iscached,
20+
isconstant
1021

1122
# other functions in LinearAlgebra
1223
import LinearAlgebra: BlasReal, BlasInt, BlasFloat, BlasComplex, checksquare
1324
import LinearAlgebra.BLAS: @blasfunc
1425
import LinearAlgebra.LAPACK: hseqr!
1526

16-
# SciML packages (for OrdinaryDiffEq and LinearSolve)
27+
# SciML packages (for QobjEvo, OrdinaryDiffEq, and LinearSolve)
1728
import SciMLBase:
1829
solve,
1930
solve!,
@@ -33,17 +44,7 @@ import SciMLBase:
3344
ContinuousCallback,
3445
DiscreteCallback
3546
import StochasticDiffEq: StochasticDiffEqAlgorithm, SRA1
36-
import SciMLOperators:
37-
AbstractSciMLOperator,
38-
MatrixOperator,
39-
ScalarOperator,
40-
ScaledOperator,
41-
AddedOperator,
42-
IdentityOperator,
43-
cache_operator,
44-
update_coefficients!,
45-
concretize,
46-
isconstant
47+
import SciMLOperators: SciMLOperators, update_coefficients!, concretize
4748
import LinearSolve: LinearProblem, SciMLLinearSolveAlgorithm, KrylovJL_MINRES, KrylovJL_GMRES
4849
import DiffEqBase: get_tstops
4950
import DiffEqCallbacks: PeriodicCallback, PresetTimeCallback, TerminateSteadyState

src/qobj/boolean_functions.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ All boolean functions for checking the data or type in `QuantumObject`
33
=#
44

55
export isket, isbra, isoper, isoperbra, isoperket, issuper
6-
export isunitary, isconstant
6+
export isunitary
77

88
@doc raw"""
99
isbra(A)
@@ -91,8 +91,15 @@ isunitary(U::QuantumObject{<:AbstractArray{T}}; kwargs...) where {T} =
9191
isoper(U) ? isapprox(U.data * U.data', I(size(U, 1)); kwargs...) : false
9292

9393
@doc raw"""
94-
isconstant(A::AbstractQuantumObject)
94+
SciMLOperators.iscached(A::AbstractQuantumObject)
95+
96+
Test whether the [`AbstractQuantumObject`](@ref) `A` has preallocated caches for inplace evaluations.
97+
"""
98+
SciMLOperators.iscached(A::AbstractQuantumObject) = iscached(A.data)
99+
100+
@doc raw"""
101+
SciMLOperators.isconstant(A::AbstractQuantumObject)
95102
96103
Test whether the [`AbstractQuantumObject`](@ref) `A` is constant in time. For a [`QuantumObject`](@ref), this function returns `true`, while for a [`QuantumObjectEvolution`](@ref), this function returns `true` if the operator is contant in time.
97104
"""
98-
isconstant(A::AbstractQuantumObject) = isconstant(A.data)
105+
SciMLOperators.isconstant(A::AbstractQuantumObject) = isconstant(A.data)

src/qobj/quantum_object.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ This file defines the QuantumObject (Qobj) structure.
33
It also implements the fundamental functions in Julia standard library:
44
- Base: show, real, imag, Vector, Matrix
55
- SparseArrays: sparse, nnz, nonzeros, rowvals, droptol!, dropzeros, dropzeros!, SparseVector, SparseMatrixCSC
6+
- SciMLOperators: cache_operator
67
=#
78

89
export QuantumObject
@@ -167,6 +168,41 @@ SparseArrays.droptol!(A::QuantumObject{<:AbstractSparseArray}, tol::Real) = (dro
167168
SparseArrays.dropzeros(A::QuantumObject{<:AbstractSparseArray}) = QuantumObject(dropzeros(A.data), A.type, A.dims)
168169
SparseArrays.dropzeros!(A::QuantumObject{<:AbstractSparseArray}) = (dropzeros!(A.data); return A)
169170

171+
@doc raw"""
172+
SciMLOperators.cached_operator(L::AbstractQuantumObject, u)
173+
174+
Allocate caches for [`AbstractQuantumObject`](@ref) `L` for in-place evaluation with `u`-like input vectors.
175+
176+
Here, `u` can be in either the following types:
177+
- `AbstractVector`
178+
- [`Ket`](@ref)-type [`QuantumObject`](@ref) (if `L` is an [`Operator`](@ref))
179+
- [`OperatorKet`](@ref)-type [`QuantumObject`](@ref) (if `L` is a [`SuperOperator`](@ref))
180+
"""
181+
SciMLOperators.cache_operator(
182+
L::AbstractQuantumObject{DT,OpType},
183+
u::AbstractVector,
184+
) where {DT,OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} =
185+
get_typename_wrapper(L)(cache_operator(L.data, sparse_to_dense(similar(u))), L.type, L.dims)
186+
187+
function SciMLOperators.cache_operator(
188+
L::AbstractQuantumObject{DT1,OpType},
189+
u::QuantumObject{DT2,SType},
190+
) where {
191+
DT1,
192+
DT2,
193+
OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
194+
SType<:Union{KetQuantumObject,OperatorKetQuantumObject},
195+
}
196+
check_dims(L, u)
197+
198+
if isoper(L) && isoperket(u)
199+
throw(ArgumentError("The input state `u` must be a Ket if `L` is an Operator."))
200+
elseif issuper(L) && isket(u)
201+
throw(ArgumentError("The input state `u` must be an OperatorKet if `L` is a SuperOperator."))
202+
end
203+
return cache_operator(L, u.data)
204+
end
205+
170206
# data type conversions
171207
Base.Vector(A::QuantumObject{<:AbstractVector}) = QuantumObject(Vector(A.data), A.type, A.dims)
172208
Base.Vector{T}(A::QuantumObject{<:AbstractVector}) where {T<:Number} = QuantumObject(Vector{T}(A.data), A.type, A.dims)

test/core-test/quantum_objects_evo.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@
174174
H_ti = coef1(p, t) * a + a' * a + coef2(p, t) * a'
175175
ψ = rand_ket(N)
176176
@test H_td(p, t) H_ti
177+
@test iscached(H_td) == true
178+
H_td = cache_operator(H_td, ψ)
179+
@test iscached(H_td) == true
177180
@test H_td(ψ, p, t) H_ti * ψ
178181
@test isconstant(a) == true
179182
@test isconstant(H_td) == false
@@ -195,18 +198,18 @@
195198
L_td = @test_logs (:warn,) (:warn,) liouvillian(H_td, c_ops) # warnings from lazy tensor in `lindblad_dissipator(c_op2)`
196199
ρvec = mat2vec(rand_dm(N))
197200
@test L_td(p, t) L_ti
198-
# TODO: L_td here is ComposedOperator and need to setup cache first for the following test
199-
# TODO: (maybe can support `iscached` and `cache_operator` for QobjEvo in the future)
200-
# @test iscached(L_td) == false
201-
# @test L_td = cache_operator(L_td, ρvec)
202-
# @test iscached(L_td) == true
203-
# @test L_td(ρvec, p, t) ≈ L_ti * ρvec
201+
@test iscached(L_td) == false
202+
L_td = cache_operator(L_td, ρvec)
203+
@test iscached(L_td) == true
204+
@test L_td(ρvec, p, t) L_ti * ρvec
204205
@test isconstant(L_td) == false
205206
@test issuper(L_td) == true
206207

207208
@test_logs (:warn,) (:warn,) liouvillian(H_td * H_td) # warnings from lazy tensor
208209
@test_throws MethodError QobjEvo([[a, coef1], a' * a, [a', coef2]])
209210
@test_throws ArgumentError H_td(ρvec, p, t)
211+
@test_throws ArgumentError cache_operator(H_td, ρvec)
210212
@test_throws ArgumentError L_td(ψ, p, t)
213+
@test_throws ArgumentError cache_operator(L_td, ψ)
211214
end
212215
end

0 commit comments

Comments
 (0)