Skip to content

Commit 66b043d

Browse files
Extend methods for Qobj and QobjEvo
1 parent 60a57e1 commit 66b043d

File tree

4 files changed

+64
-32
lines changed

4 files changed

+64
-32
lines changed

src/qobj/operators.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,12 @@ Note that `type` can only be either [`Operator`](@ref) or [`SuperOperator`](@ref
428428
!!! note
429429
`qeye` is a synonym of `eye`.
430430
"""
431-
function eye(N::Int; type::ObjType = Operator, dims = nothing) where {ObjType<:Union{Operator,SuperOperator}}
431+
function eye(
432+
N::Int;
433+
type::Union{ObjType,Type{ObjType}} = Operator,
434+
dims = nothing,
435+
) where {ObjType<:Union{Operator,SuperOperator}}
436+
type = _get_type(type)
432437
if dims isa Nothing
433438
dims = isa(type, Operator) ? N : isqrt(N)
434439
end

src/qobj/quantum_object.jl

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ struct QuantumObject{ObjType<:QuantumObjectType,DimType<:AbstractDimensions,Data
5050
type::ObjType
5151
dimensions::DimType
5252

53-
function QuantumObject(data::DT, type::ObjType, dims) where {DT<:AbstractArray,ObjType<:QuantumObjectType}
53+
function QuantumObject(
54+
data::DT,
55+
type::Union{ObjType,Type{ObjType}},
56+
dims,
57+
) where {DT<:AbstractArray,ObjType<:QuantumObjectType}
5458
dimensions = _gen_dimensions(dims)
5559

60+
type = _get_type(type)
61+
5662
_size = _get_size(data)
5763
_check_QuantumObject(type, dimensions, _size[1], _size[2])
5864

@@ -71,14 +77,16 @@ Generate [`QuantumObject`](@ref) with a given `A::AbstractArray` and specified `
7177
"""
7278
function QuantumObject(
7379
A::AbstractMatrix{T};
74-
type::ObjType = nothing,
80+
type::Union{Nothing,ObjType,Type{ObjType}} = nothing,
7581
dims = nothing,
76-
) where {T,ObjType<:Union{Nothing,QuantumObjectType}}
82+
) where {T,ObjType<:QuantumObjectType}
7783
_size = _get_size(A)
7884

85+
type = _get_type(type)
86+
7987
if type isa Nothing
80-
type = (_size[1] == 1 && _size[2] > 1) ? Bra : Operator # default type
81-
elseif type != Operator && type != SuperOperator && type != Bra && type != OperatorBra
88+
type = (_size[1] == 1 && _size[2] > 1) ? Bra() : Operator() # default type
89+
elseif !(type isa Operator) && !(type isa SuperOperator) && !(type isa Bra) && !(type isa OperatorBra)
8290
throw(
8391
ArgumentError(
8492
"The argument type must be Operator, SuperOperator, Bra or OperatorBra if the input array is a matrix.",
@@ -103,12 +111,12 @@ end
103111

104112
function QuantumObject(
105113
A::AbstractVector{T};
106-
type::ObjType = nothing,
114+
type::Union{Nothing,ObjType,Type{ObjType}} = nothing,
107115
dims = nothing,
108-
) where {T,ObjType<:Union{Nothing,QuantumObjectType}}
116+
) where {T,ObjType<:QuantumObjectType}
109117
if type isa Nothing
110118
type = Ket # default type
111-
elseif type != Ket && type != OperatorKet
119+
elseif !(type isa Ket) && !(type isa OperatorKet)
112120
throw(ArgumentError("The argument type must be Ket or OperatorKet if the input array is a vector."))
113121
end
114122

@@ -126,15 +134,20 @@ end
126134

127135
function QuantumObject(
128136
A::AbstractArray{T,N};
129-
type::ObjType = nothing,
137+
type::Union{Nothing,ObjType,Type{ObjType}} = nothing,
130138
dims = nothing,
131-
) where {T,N,ObjType<:Union{Nothing,QuantumObjectType}}
139+
) where {T,N,ObjType<:QuantumObjectType}
132140
throw(DomainError(size(A), "The size of the array is not compatible with vector or matrix."))
133141
end
134142

135-
function QuantumObject(A::QuantumObject; type::ObjType = A.type, dims = A.dimensions) where {ObjType<:QuantumObjectType}
143+
function QuantumObject(
144+
A::QuantumObject;
145+
type::Union{ObjType,Type{ObjType}} = A.type,
146+
dims = A.dimensions,
147+
) where {ObjType<:QuantumObjectType}
136148
_size = _get_size(A.data)
137149
dimensions = _gen_dimensions(dims)
150+
type = _get_type(type)
138151
_check_QuantumObject(type, dimensions, _size[1], _size[2])
139152
return QuantumObject(copy(A.data), type, dimensions)
140153
end

src/qobj/quantum_object_base.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ function _check_QuantumObject(type::OperatorBra, dimensions::Dimensions, m::Int,
196196
return nothing
197197
end
198198

199+
_get_type(type::QuantumObjectType) = type
200+
_get_type(::Type{ObjType}) where {ObjType<:QuantumObjectType} = ObjType()
201+
_get_type(::Nothing) = nothing
202+
199203
function Base.getproperty(A::AbstractQuantumObject, key::Symbol)
200204
# a comment here to avoid bad render by JuliaFormatter
201205
if key === :dims

src/qobj/quantum_object_evo.jl

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,14 @@ struct QuantumObjectEvolution{
119119

120120
function QuantumObjectEvolution(
121121
data::DT,
122-
type::ObjType,
122+
type::Union{ObjType,Type{ObjType}},
123123
dims,
124124
) where {DT<:AbstractSciMLOperator,ObjType<:QuantumObjectType}
125-
(type == Operator || type == SuperOperator) ||
126-
throw(ArgumentError("The type $type is not supported for QuantumObjectEvolution."))
125+
(ObjType == Operator || ObjType == SuperOperator) ||
126+
throw(ArgumentError("The type $ObjType is not supported for QuantumObjectEvolution."))
127127

128128
dimensions = _gen_dimensions(dims)
129+
type = _get_type(type)
129130

130131
_size = _get_size(data)
131132
_check_QuantumObject(type, dimensions, _size[1], _size[2])
@@ -153,15 +154,20 @@ function Base.show(io::IO, QO::QuantumObjectEvolution)
153154
end
154155

155156
@doc raw"""
156-
QobjEvo(data::AbstractSciMLOperator; type::QuantumObjectType = Operator, dims = nothing)
157-
QuantumObjectEvolution(data::AbstractSciMLOperator; type::QuantumObjectType = Operator, dims = nothing)
157+
QobjEvo(data::AbstractSciMLOperator; type::Union{ObjType, Type{ObjType}} = Operator, dims = nothing)
158+
QuantumObjectEvolution(data::AbstractSciMLOperator; type::Union{ObjType, Type{ObjType}} = Operator, dims = nothing)
158159
159160
Generate a [`QuantumObjectEvolution`](@ref) object from a [`SciMLOperator`](https://github.com/SciML/SciMLOperators.jl), in the same way as [`QuantumObject`](@ref) for `AbstractArray` inputs.
160161
161162
Note that `QobjEvo` is a synonym of `QuantumObjectEvolution`
162163
"""
163-
function QuantumObjectEvolution(data::AbstractSciMLOperator; type::QuantumObjectType = Operator, dims = nothing)
164+
function QuantumObjectEvolution(
165+
data::AbstractSciMLOperator;
166+
type::Union{ObjType,Type{ObjType}} = Operator,
167+
dims = nothing,
168+
) where {ObjType<:QuantumObjectType}
164169
_size = _get_size(data)
170+
type = _get_type(type)
165171

166172
if dims isa Nothing
167173
if type isa Operator
@@ -177,8 +183,8 @@ function QuantumObjectEvolution(data::AbstractSciMLOperator; type::QuantumObject
177183
end
178184

179185
@doc raw"""
180-
QobjEvo(op_func_list::Union{Tuple,AbstractQuantumObject}, α::Union{Nothing,Number}=nothing; type::Union{Nothing, QuantumObjectType}=nothing)
181-
QuantumObjectEvolution(op_func_list::Union{Tuple,AbstractQuantumObject}, α::Union{Nothing,Number}=nothing; type::Union{Nothing, QuantumObjectType}=nothing)
186+
QobjEvo(op_func_list::Union{Tuple,AbstractQuantumObject}, α::Union{Nothing,Number}=nothing; type::Union{Nothing, ObjType, Type{ObjType}}=nothing)
187+
QuantumObjectEvolution(op_func_list::Union{Tuple,AbstractQuantumObject}, α::Union{Nothing,Number}=nothing; type::Union{Nothing, ObjType, Type{ObjType}}=nothing)
182188
183189
Generate [`QuantumObjectEvolution`](@ref).
184190
@@ -272,10 +278,12 @@ Quantum Object: type=Operator dims=[10, 2] size=(20, 20) ishermitian=fal
272278
function QuantumObjectEvolution(
273279
op_func_list::Tuple,
274280
α::Union{Nothing,Number} = nothing;
275-
type::Union{Nothing,QuantumObjectType} = nothing,
276-
)
281+
type::Union{ObjType,Type{ObjType}} = nothing,
282+
) where {ObjType<:QuantumObjectType}
277283
op, data = _QobjEvo_generate_data(op_func_list, α)
278284
dims = op.dimensions
285+
type = _get_type(type)
286+
279287
if type isa Nothing
280288
type = op.type
281289
end
@@ -291,12 +299,12 @@ end
291299
QuantumObjectEvolution(
292300
op_func::Tuple{QuantumObject,Function},
293301
α::Union{Nothing,Number} = nothing;
294-
type::Union{Nothing,QuantumObjectType} = nothing,
295-
) = QuantumObjectEvolution((op_func,), α; type = type)
302+
type::Union{Nothing,ObjType,Type{ObjType}} = nothing,
303+
) where {ObjType<:QuantumObjectType} = QuantumObjectEvolution((op_func,), α; type = type)
296304

297305
@doc raw"""
298-
QuantumObjectEvolution(op::QuantumObject, f::Function, α::Union{Nothing,Number}=nothing; type::Union{Nothing,QuantumObjectType} = nothing)
299-
QobjEvo(op::QuantumObject, f::Function, α::Union{Nothing,Number}=nothing; type::Union{Nothing,QuantumObjectType} = nothing)
306+
QuantumObjectEvolution(op::QuantumObject, f::Function, α::Union{Nothing,Number}=nothing; type::Union{Nothing, ObjType, Type{ObjType}} = nothing)
307+
QobjEvo(op::QuantumObject, f::Function, α::Union{Nothing,Number}=nothing; type::Union{Nothing, ObjType, Type{ObjType}} = nothing)
300308
301309
Generate [`QuantumObjectEvolution`](@ref).
302310
@@ -329,14 +337,15 @@ QuantumObjectEvolution(
329337
op::QuantumObject,
330338
f::Function,
331339
α::Union{Nothing,Number} = nothing;
332-
type::Union{Nothing,QuantumObjectType} = nothing,
333-
) = QuantumObjectEvolution(((op, f),), α; type = type)
340+
type::Union{Nothing,ObjType,Type{ObjType}} = nothing,
341+
) where {ObjType<:QuantumObjectType} = QuantumObjectEvolution(((op, f),), α; type = type)
334342

335343
function QuantumObjectEvolution(
336344
op::QuantumObject,
337345
α::Union{Nothing,Number} = nothing;
338-
type::Union{Nothing,QuantumObjectType} = nothing,
339-
)
346+
type::Union{Nothing,ObjType,Type{ObjType}} = nothing,
347+
) where {ObjType<:QuantumObjectType}
348+
type = _get_type(type)
340349
if type isa Nothing
341350
type = op.type
342351
end
@@ -346,8 +355,9 @@ end
346355
function QuantumObjectEvolution(
347356
op::QuantumObjectEvolution,
348357
α::Union{Nothing,Number} = nothing;
349-
type::Union{Nothing,QuantumObjectType} = nothing,
350-
)
358+
type::Union{Nothing,ObjType,Type{ObjType}} = nothing,
359+
) where {ObjType<:QuantumObjectType}
360+
type = _get_type(type)
351361
if type isa Nothing
352362
type = op.type
353363
elseif type != op.type

0 commit comments

Comments
 (0)