Skip to content

Commit 3395e7b

Browse files
Make special methods internally
1 parent a31280d commit 3395e7b

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

src/QuantumToolbox.jl

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,6 @@ export permute
7474
# SciMLOperators
7575
export cache_operator, iscached, isconstant
7676

77-
# TODO: To remove when https://github.com/SciML/SciMLOperators.jl/pull/264 is merged
78-
SCALINGNUMBERTYPES = (:AbstractSciMLScalarOperator, :Number, :UniformScaling)
79-
# Special cases for constant scalars. These simplify the structure when applicable
80-
for T in SCALINGNUMBERTYPES[2:end]
81-
@eval function Base.:*::$T, L::ScaledOperator)
82-
isconstant(L.λ) && return ScaledOperator* L.λ, L.L)
83-
return ScaledOperator(L.λ, α * L.L) # Try to propagate the rule
84-
end
85-
@eval function Base.:*(L::ScaledOperator, α::$T)
86-
isconstant(L.λ) && return ScaledOperator* L.λ, L.L)
87-
return ScaledOperator(L.λ, α * L.L) # Try to propagate the rule
88-
end
89-
@eval function Base.:*::$T, L::MatrixOperator)
90-
isconstant(L) && return MatrixOperator* L.A)
91-
return ScaledOperator(α, L) # Going back to the generic case
92-
end
93-
@eval function Base.:*(L::MatrixOperator, α::$T)
94-
isconstant(L) && return MatrixOperator* L.A)
95-
return ScaledOperator(α, L) # Going back to the generic case
96-
end
97-
end
98-
9977
# Utility
10078
include("utilities.jl")
10179
include("versioninfo.jl")

src/qobj/quantum_object_evo.jl

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ function QuantumObjectEvolution(
360360
if α isa Nothing
361361
return QuantumObjectEvolution(op.data, type, op.dimensions)
362362
end
363-
return QuantumObjectEvolution(α * op.data, type, op.dimensions)
363+
return QuantumObjectEvolution(_promote_to_scimloperator(α, op.data), type, op.dimensions)
364364
end
365365

366366
#=
@@ -443,16 +443,33 @@ function _make_SciMLOperator(op_func::Tuple, α)
443443
T = eltype(op_func[1])
444444
update_func = (a, u, p, t) -> op_func[2](p, t)
445445
if α isa Nothing
446-
return ScalarOperator(zero(T), update_func) * MatrixOperator(op_func[1].data)
446+
return ScalarOperator(zero(T), update_func) * _promote_to_scimloperator(op_func[1].data)
447447
end
448-
return ScalarOperator(zero(T), update_func) * MatrixOperator* op_func[1].data)
448+
return ScalarOperator(zero(T), update_func) * _promote_to_scimloperator(α, op_func[1].data)
449449
end
450450

451-
function _make_SciMLOperator(op::QuantumObject, α)
451+
function _make_SciMLOperator(op::AbstractQuantumObject, α)
452452
if α isa Nothing
453-
return MatrixOperator(op.data)
453+
return _promote_to_scimloperator(op.data)
454454
end
455-
return MatrixOperator* op.data)
455+
return _promote_to_scimloperator(α, op.data)
456+
end
457+
458+
_promote_to_scimloperator(data::AbstractMatrix) = MatrixOperator(data)
459+
_promote_to_scimloperator(data::AbstractSciMLOperator) = data
460+
# TODO: The following special cases can be simplified after
461+
# https://github.com/SciML/SciMLOperators.jl/pull/264 is merged
462+
_promote_to_scimloperator::Number, data::AbstractMatrix) = MatrixOperator* data)
463+
function _promote_to_scimloperator::Number, data::MatrixOperator)
464+
isconstant(data) && return MatrixOperator* data.A)
465+
return ScaledOperator(α, data) # Going back to the generic case
466+
end
467+
function _promote_to_scimloperator::Number, data::ScaledOperator)
468+
isconstant(data.λ) && return ScaledOperator* data.λ, data.L)
469+
return ScaledOperator(data.λ, _promote_to_scimloperator(α, data.L)) # Try to propagate the rule
470+
end
471+
function _promote_to_scimloperator::Number, data::AbstractSciMLOperator)
472+
return α * data # Going back to the generic case
456473
end
457474

458475
@doc raw"""

test/core-test/quantum_objects_evo.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@test_throws DimensionMismatch QobjEvo(a, type = SuperOperator)
2020

2121
ψ = fock(10, 3)
22-
@test_throws TypeError QobjEvo(ψ)
22+
@test_throws MethodError QobjEvo(ψ)
2323
end
2424

2525
# unsupported type of dims

0 commit comments

Comments
 (0)