@@ -3,6 +3,7 @@ This file defines the QuantumObject (Qobj) structure.
33It 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
89export QuantumObject
@@ -167,6 +168,41 @@ SparseArrays.droptol!(A::QuantumObject{<:AbstractSparseArray}, tol::Real) = (dro
167168SparseArrays. dropzeros (A:: QuantumObject{<:AbstractSparseArray} ) = QuantumObject (dropzeros (A. data), A. type, A. dims)
168169SparseArrays. 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
171207Base. Vector (A:: QuantumObject{<:AbstractVector} ) = QuantumObject (Vector (A. data), A. type, A. dims)
172208Base. Vector {T} (A:: QuantumObject{<:AbstractVector} ) where {T<: Number } = QuantumObject (Vector {T} (A. data), A. type, A. dims)
0 commit comments