Skip to content

Commit 20e8b14

Browse files
committed
fix stack overflow error in QobjEvo multiplication
1 parent f9f8213 commit 20e8b14

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased](https://github.com/qutip/QuantumToolbox.jl/tree/main)
99

1010
- Improve Bloch sphere rendering for animation. ([#520])
11+
- Fix stack overflow error in `QobjEvo` multiplication. ([#524], [#525])
1112

1213
## [v0.34.0]
1314
Release date: 2025-07-29
@@ -295,3 +296,5 @@ Release date: 2024-11-13
295296
[#515]: https://github.com/qutip/QuantumToolbox.jl/issues/515
296297
[#517]: https://github.com/qutip/QuantumToolbox.jl/issues/517
297298
[#520]: https://github.com/qutip/QuantumToolbox.jl/issues/520
299+
[#524]: https://github.com/qutip/QuantumToolbox.jl/issues/524
300+
[#525]: https://github.com/qutip/QuantumToolbox.jl/issues/525

src/qobj/arithmetic_and_attributes.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,22 @@ for op in (:(+), :(-), :(*))
4242
return QType($(op)(A.data, B.data), A.type, A.dimensions)
4343
end
4444
Base.$op(A::AbstractQuantumObject) = get_typename_wrapper(A)($(op)(A.data), A.type, A.dimensions)
45+
end
4546

46-
Base.$op(n::T, A::AbstractQuantumObject) where {T<:Number} =
47-
get_typename_wrapper(A)($(op)(n * I, A.data), A.type, A.dimensions)
48-
Base.$op(A::AbstractQuantumObject, n::T) where {T<:Number} =
49-
get_typename_wrapper(A)($(op)(A.data, n * I), A.type, A.dimensions)
47+
if op == :(*)
48+
@eval begin
49+
Base.$op(n::T, A::AbstractQuantumObject) where {T<:Number} =
50+
get_typename_wrapper(A)($(op)(n, A.data), A.type, A.dimensions)
51+
Base.$op(A::AbstractQuantumObject, n::T) where {T<:Number} =
52+
get_typename_wrapper(A)($(op)(A.data, n), A.type, A.dimensions)
53+
end
54+
else
55+
@eval begin
56+
Base.$op(n::T, A::AbstractQuantumObject) where {T<:Number} =
57+
get_typename_wrapper(A)($(op)(n * I, A.data), A.type, A.dimensions)
58+
Base.$op(A::AbstractQuantumObject, n::T) where {T<:Number} =
59+
get_typename_wrapper(A)($(op)(A.data, n * I), A.type, A.dimensions)
60+
end
5061
end
5162
end
5263

test/core-test/quantum_objects_evo.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,18 @@
7070
end
7171

7272
@testset "arithmetic" begin
73-
a = MatrixOperator(sprand(ComplexF64, 100, 100, 0.1))
73+
m = sprand(ComplexF64, 100, 100, 0.1)
74+
a = MatrixOperator(m)
7475
a2 = QobjEvo(a)
7576
a3 = QobjEvo(a, type = SuperOperator())
77+
a4 = QobjEvo(Qobj(m), (p, t) -> 1)
7678

7779
@test +a2 == a2
7880
@test -(-a2) == a2
7981
@test a2 + 2 == 2 + a2
8082
@test (a2 + 2).data == a2.data + 2 * I
8183
@test a2 * 2 == 2 * a2
84+
@test 1*a*a*1 == 1*(a*a) == (a*a)*1 == 1*(a*a)*1 # to check QobjEvo multiplication with Number is correct
8285

8386
zero_like = zero(a2)
8487
iden_like = one(a3)

0 commit comments

Comments
 (0)