Skip to content

Commit c00a344

Browse files
Apply Yi-Te comments
1 parent 3dcec43 commit c00a344

File tree

7 files changed

+180
-167
lines changed

7 files changed

+180
-167
lines changed

src/QuantumToolbox.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import SciMLBase:
3434
DiscreteCallback
3535
import StochasticDiffEq: StochasticDiffEqAlgorithm, SRA1
3636
import SciMLOperators:
37-
AbstractSciMLOperator, MatrixOperator, ScalarOperator, cache_operator, update_coefficients!, concretize
37+
AbstractSciMLOperator, MatrixOperator, ScalarOperator, cache_operator, update_coefficients!, concretize, isconstant
3838
import LinearSolve: LinearProblem, SciMLLinearSolveAlgorithm, KrylovJL_MINRES, KrylovJL_GMRES
3939
import DiffEqBase: get_tstops
4040
import DiffEqCallbacks: PeriodicCallback, PresetTimeCallback, TerminateSteadyState
@@ -64,6 +64,7 @@ include("progress_bar.jl")
6464
include("linear_maps.jl")
6565

6666
# Quantum Object
67+
include("qobj/quantum_object_base.jl")
6768
include("qobj/quantum_object.jl")
6869
include("qobj/quantum_object_evo.jl")
6970
include("qobj/boolean_functions.jl")

src/qobj/arithmetic_and_attributes.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ for op in (:(+), :(-), :(*))
3838
@eval begin
3939
function LinearAlgebra.$op(A::AbstractQuantumObject, B::AbstractQuantumObject)
4040
check_dims(A, B)
41-
if A isa QuantumObjectEvolution || B isa QuantumObjectEvolution
42-
return QuantumObjectEvolution($(op)(A.data, B.data), A.type, A.dims)
43-
end
44-
return QuantumObject($(op)(A.data, B.data), A.type, A.dims)
41+
QType = promote_type(A, B)
42+
return QType($(op)(A.data, B.data), A.type, A.dims)
4543
end
4644
LinearAlgebra.$op(A::AbstractQuantumObject) = get_typename_wrapper(A)($(op)(A.data), A.type, A.dims)
4745

@@ -132,7 +130,7 @@ end
132130
@doc raw"""
133131
dot(i::QuantumObject, A::AbstractQuantumObject j::QuantumObject)
134132
135-
Compute the generalized dot product `dot(i, A*j)` between three [`AbstractQuantumObject`](@ref): ``\langle i | \hat{A} | j \rangle``
133+
Compute the generalized dot product `dot(i, A*j)` between a [`AbstractQuantumObject`](@ref) and two [`QuantumObject`](@ref) (`i` and `j`), namely ``\langle i | \hat{A} | j \rangle``.
136134
137135
Supports the following inputs:
138136
- `A` is in the type of [`Operator`](@ref), with `i` and `j` are both [`Ket`](@ref).

src/qobj/boolean_functions.jl

Lines changed: 8 additions & 1 deletion
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
6+
export isunitary, isconstant
77

88
@doc raw"""
99
isbra(A)
@@ -89,3 +89,10 @@ Note that all the keyword arguments will be passed to `Base.isapprox`.
8989
"""
9090
isunitary(U::QuantumObject{<:AbstractArray{T}}; kwargs...) where {T} =
9191
isoper(U) ? isapprox(U.data * U.data', I(size(U, 1)); kwargs...) : false
92+
93+
@doc raw"""
94+
isconstant(A::AbstractQuantumObject)
95+
96+
Test whether the [`AbstractQuantumObject`](@ref) `A` is constant in time. For a [`QuantumObject`](@ref), this function returns `false`, while for a [`QuantumObjectEvolution`](@ref), this function returns `true` if the operator is contant in time.
97+
"""
98+
isconstant(A::AbstractQuantumObject) = isconstant(A.data)

src/qobj/functions.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,8 @@ function LinearAlgebra.kron(
193193
A::AbstractQuantumObject{DT1,OpType},
194194
B::AbstractQuantumObject{DT2,OpType},
195195
) where {DT1,DT2,OpType<:Union{KetQuantumObject,BraQuantumObject,OperatorQuantumObject}}
196-
if A isa QuantumObjectEvolution || B isa QuantumObjectEvolution
197-
return QuantumObjectEvolution(kron(A.data, B.data), A.type, vcat(A.dims, B.dims))
198-
end
199-
return QuantumObject(kron(A.data, B.data), A.type, vcat(A.dims, B.dims))
196+
QType = promote_type(A, B)
197+
return QType(kron(A.data, B.data), A.type, vcat(A.dims, B.dims))
200198
end
201199
LinearAlgebra.kron(A::AbstractQuantumObject) = A
202200
function LinearAlgebra.kron(A::Vector{<:AbstractQuantumObject})

src/qobj/quantum_object.jl

Lines changed: 1 addition & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -7,114 +7,7 @@ Also support for fundamental functions in Julia standard library:
77
- SparseArrays: sparse, nnz, nonzeros, rowvals, droptol!, dropzeros, dropzeros!, SparseVector, SparseMatrixCSC
88
=#
99

10-
export AbstractQuantumObject, QuantumObject
11-
export QuantumObjectType,
12-
BraQuantumObject,
13-
KetQuantumObject,
14-
OperatorQuantumObject,
15-
OperatorBraQuantumObject,
16-
OperatorKetQuantumObject,
17-
SuperOperatorQuantumObject
18-
export Bra, Ket, Operator, OperatorBra, OperatorKet, SuperOperator
19-
20-
@doc raw"""
21-
AbstractQuantumObject{DataType,ObjType,N}
22-
23-
Abstract type for all quantum objects like [`QuantumObject`](@ref) and [`QuantumObjectEvolution`](@ref).
24-
"""
25-
abstract type AbstractQuantumObject{DataType,ObjType,N} end
26-
27-
abstract type QuantumObjectType end
28-
29-
@doc raw"""
30-
BraQuantumObject <: QuantumObjectType
31-
32-
Constructor representing a bra state ``\langle\psi|``.
33-
"""
34-
struct BraQuantumObject <: QuantumObjectType end
35-
Base.show(io::IO, ::BraQuantumObject) = print(io, "Bra")
36-
37-
@doc raw"""
38-
const Bra = BraQuantumObject()
39-
40-
A constant representing the type of [`BraQuantumObject`](@ref): a bra state ``\langle\psi|``
41-
"""
42-
const Bra = BraQuantumObject()
43-
44-
@doc raw"""
45-
KetQuantumObject <: QuantumObjectType
46-
47-
Constructor representing a ket state ``|\psi\rangle``.
48-
"""
49-
struct KetQuantumObject <: QuantumObjectType end
50-
Base.show(io::IO, ::KetQuantumObject) = print(io, "Ket")
51-
52-
@doc raw"""
53-
const Ket = KetQuantumObject()
54-
55-
A constant representing the type of [`KetQuantumObject`](@ref): a ket state ``|\psi\rangle``
56-
"""
57-
const Ket = KetQuantumObject()
58-
59-
@doc raw"""
60-
OperatorQuantumObject <: QuantumObjectType
61-
62-
Constructor representing an operator ``\hat{O}``.
63-
"""
64-
struct OperatorQuantumObject <: QuantumObjectType end
65-
Base.show(io::IO, ::OperatorQuantumObject) = print(io, "Operator")
66-
67-
@doc raw"""
68-
const Operator = OperatorQuantumObject()
69-
70-
A constant representing the type of [`OperatorQuantumObject`](@ref): an operator ``\hat{O}``
71-
"""
72-
const Operator = OperatorQuantumObject()
73-
74-
@doc raw"""
75-
SuperOperatorQuantumObject <: QuantumObjectType
76-
77-
Constructor representing a super-operator ``\hat{\mathcal{O}}`` acting on vectorized density operator matrices.
78-
"""
79-
struct SuperOperatorQuantumObject <: QuantumObjectType end
80-
Base.show(io::IO, ::SuperOperatorQuantumObject) = print(io, "SuperOperator")
81-
82-
@doc raw"""
83-
const SuperOperator = SuperOperatorQuantumObject()
84-
85-
A constant representing the type of [`SuperOperatorQuantumObject`](@ref): a super-operator ``\hat{\mathcal{O}}`` acting on vectorized density operator matrices
86-
"""
87-
const SuperOperator = SuperOperatorQuantumObject()
88-
89-
@doc raw"""
90-
OperatorBraQuantumObject <: QuantumObjectType
91-
92-
Constructor representing a bra state in the [`SuperOperator`](@ref) formalism ``\langle\langle\rho|``.
93-
"""
94-
struct OperatorBraQuantumObject <: QuantumObjectType end
95-
Base.show(io::IO, ::OperatorBraQuantumObject) = print(io, "OperatorBra")
96-
97-
@doc raw"""
98-
const OperatorBra = OperatorBraQuantumObject()
99-
100-
A constant representing the type of [`OperatorBraQuantumObject`](@ref): a bra state in the [`SuperOperator`](@ref) formalism ``\langle\langle\rho|``.
101-
"""
102-
const OperatorBra = OperatorBraQuantumObject()
103-
104-
@doc raw"""
105-
OperatorKetQuantumObject <: QuantumObjectType
106-
107-
Constructor representing a ket state in the [`SuperOperator`](@ref) formalism ``|\rho\rangle\rangle``.
108-
"""
109-
struct OperatorKetQuantumObject <: QuantumObjectType end
110-
Base.show(io::IO, ::OperatorKetQuantumObject) = print(io, "OperatorKet")
111-
112-
@doc raw"""
113-
const OperatorKet = OperatorKetQuantumObject()
114-
115-
A constant representing the type of [`OperatorKetQuantumObject`](@ref): a ket state in the [`SuperOperator`](@ref) formalism ``|\rho\rangle\rangle``
116-
"""
117-
const OperatorKet = OperatorKetQuantumObject()
10+
export QuantumObject
11811

11912
@doc raw"""
12013
struct QuantumObject{MT<:AbstractArray,ObjType<:QuantumObjectType,N}
@@ -232,11 +125,6 @@ _check_dims(dims::Any) = throw(
232125
),
233126
)
234127

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-
240128
function _check_QuantumObject(type::KetQuantumObject, dims, m::Int, n::Int)
241129
(n != 1) && throw(DomainError((m, n), "The size of the array is not compatible with Ket"))
242130
(prod(dims) != m) && throw(DimensionMismatch("Ket with dims = $(dims) does not fit the array size = $((m, n))."))
@@ -321,41 +209,6 @@ function Base.show(io::IO, QO::AbstractQuantumObject)
321209
return show(io, MIME("text/plain"), op_data)
322210
end
323211

324-
@doc raw"""
325-
size(A::AbstractQuantumObject)
326-
size(A::AbstractQuantumObject, idx::Int)
327-
328-
Returns a tuple containing each dimensions of the array in the [`AbstractQuantumObject`](@ref).
329-
330-
Optionally, you can specify an index (`idx`) to just get the corresponding dimension of the array.
331-
"""
332-
Base.size(A::AbstractQuantumObject) = size(A.data)
333-
Base.size(A::AbstractQuantumObject, idx::Int) = size(A.data, idx)
334-
335-
Base.getindex(A::AbstractQuantumObject, inds...) = getindex(A.data, inds...)
336-
Base.setindex!(A::AbstractQuantumObject, val, inds...) = setindex!(A.data, val, inds...)
337-
338-
@doc raw"""
339-
eltype(A::AbstractQuantumObject)
340-
341-
Returns the elements type of the matrix or vector corresponding to the [`AbstractQuantumObject`](@ref) `A`.
342-
"""
343-
Base.eltype(A::AbstractQuantumObject) = eltype(A.data)
344-
345-
@doc raw"""
346-
length(A::AbstractQuantumObject)
347-
348-
Returns the length of the matrix or vector corresponding to the [`AbstractQuantumObject`](@ref) `A`.
349-
"""
350-
Base.length(A::AbstractQuantumObject) = length(A.data)
351-
352-
Base.isequal(A::AbstractQuantumObject, B::AbstractQuantumObject) =
353-
isequal(A.type, B.type) && isequal(A.dims, B.dims) && isequal(A.data, B.data)
354-
Base.isapprox(A::AbstractQuantumObject, B::AbstractQuantumObject; kwargs...) =
355-
isequal(A.type, B.type) && isequal(A.dims, B.dims) && isapprox(A.data, B.data; kwargs...)
356-
Base.:(==)(A::AbstractQuantumObject, B::AbstractQuantumObject) =
357-
(A.type == B.type) && (A.dims == B.dims) && (A.data == B.data)
358-
359212
Base.real(x::QuantumObject) = QuantumObject(real(x.data), x.type, x.dims)
360213
Base.imag(x::QuantumObject) = QuantumObject(imag(x.data), x.type, x.dims)
361214

@@ -379,9 +232,3 @@ SparseArrays.SparseMatrixCSC(A::QuantumObject{<:AbstractMatrix}) =
379232
QuantumObject(SparseMatrixCSC(A.data), A.type, A.dims)
380233
SparseArrays.SparseMatrixCSC{T}(A::QuantumObject{<:SparseMatrixCSC}) where {T<:Number} =
381234
QuantumObject(SparseMatrixCSC{T}(A.data), A.type, A.dims)
382-
383-
get_typename_wrapper(A::AbstractQuantumObject) = Base.typename(typeof(A)).wrapper
384-
385-
# functions for getting Float or Complex element type
386-
_FType(::QuantumObject{<:AbstractArray{T}}) where {T<:Number} = _FType(T)
387-
_CType(::QuantumObject{<:AbstractArray{T}}) where {T<:Number} = _CType(T)

0 commit comments

Comments
 (0)