Skip to content

Commit 1684fc7

Browse files
authored
Fix type-instability for prod (#363)
2 parents d3977c6 + f18d546 commit 1684fc7

File tree

5 files changed

+10
-14
lines changed

5 files changed

+10
-14
lines changed

src/qobj/dimensions.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ Base.:(==)(dims::AbstractDimensions, vect::AbstractVector{T}) where {T} = vect =
7272

7373
Base.length(::AbstractDimensions{N}) where {N} = N
7474

75+
# need to specify return type `Int` for `_get_space_size`
76+
# otherwise the type of `prod(::Dimensions)` will be unstable
77+
_get_space_size(s::AbstractSpace)::Int = s.size
7578
Base.prod(dims::Dimensions) = prod(dims.to)
79+
Base.prod(spaces::SVector{N,AbstractSpace}) where {N} = prod(_get_space_size, spaces)
7680

7781
LinearAlgebra.transpose(dims::Dimensions) = dims
7882
LinearAlgebra.transpose(dims::CompoundDimensions) = CompoundDimensions(dims.from, dims.to) # switch `to` and `from`

src/qobj/quantum_object_base.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function _check_QuantumObject(type::BraQuantumObject, dims::Dimensions, m::Int,
176176
end
177177

178178
function _check_QuantumObject(type::OperatorQuantumObject, dims::Dimensions, m::Int, n::Int)
179-
L = prod(dims.to)
179+
L = prod(dims)
180180
(L == m == n) || throw(DimensionMismatch("Operator with dims = $(dims) does not fit the array size = $((m, n))."))
181181
return nothing
182182
end

src/qobj/space.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,3 @@ struct Space <: AbstractSpace
2626
end
2727
end
2828
Base.string(s::Space) = string(s.size) # this is only used when printing AbstractDimensions
29-
30-
# for `prod(::Dimensions)`
31-
Base.:(*)(i::Int, s::AbstractSpace) = i * s.size
32-
Base.:(*)(s1::AbstractSpace, s2::AbstractSpace) = s1.size * s2.size
33-
Base.prod(spaces::SVector{1,AbstractSpace}) = spaces[1].size # for `Dimensions.to` has only a single Space

test/core-test/correlations_and_spectrum.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
@test all(spec2 .≈ spec3)
2929

3030
@testset "Type Inference spectrum" begin
31-
# TODO: fix this type inference issue (originally from `steadystate`)
32-
# @inferred correlation_2op_1t(H, nothing, t_l, c_ops, a', a; progress_bar = Val(false))
31+
@inferred correlation_2op_1t(H, nothing, t_l, c_ops, a', a; progress_bar = Val(false))
3332
@inferred spectrum_correlation_fft(t_l, corr1)
3433
@inferred spectrum(H, ω_l2, c_ops, a', a)
3534
@inferred spectrum(H, ω_l2, c_ops, a', a; solver = PseudoInverse())

test/core-test/steady_state.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
@inferred steadystate(H, psi0, t_l[end], c_ops, solver = solver)
3939
@inferred steadystate(L, psi0, t_l[end], solver = solver)
4040

41-
# TODO: fix the following type inference issues
42-
#= solver = SteadyStateDirectSolver()
41+
solver = SteadyStateDirectSolver()
4342
@inferred steadystate(H, c_ops, solver = solver)
4443
@inferred steadystate(L, solver = solver)
4544

@@ -52,7 +51,7 @@
5251
@inferred steadystate(L, solver = solver)
5352

5453
@inferred steadystate(H, c_ops)
55-
@inferred steadystate(L) =#
54+
@inferred steadystate(L)
5655
end
5756

5857
H = a_d * a
@@ -74,15 +73,14 @@
7473
@test abs(sum(sol_me.expect[1, end-100:end]) / 101 - expect(e_ops[1], ρ_ss2)) < 1e-3
7574

7675
@testset "Type Inference (steadystate_floquet)" begin
77-
# TODO: fix the following type inference issues
78-
#= @inferred steadystate_floquet(H, -1im * 0.5 * H_t, 1im * 0.5 * H_t, 1, c_ops, solver = SSFloquetLinearSystem())
76+
@inferred steadystate_floquet(H, -1im * 0.5 * H_t, 1im * 0.5 * H_t, 1, c_ops, solver = SSFloquetLinearSystem())
7977
@inferred steadystate_floquet(
8078
H,
8179
-1im * 0.5 * H_t,
8280
1im * 0.5 * H_t,
8381
1,
8482
c_ops,
8583
solver = SSFloquetEffectiveLiouvillian(),
86-
) =#
84+
)
8785
end
8886
end

0 commit comments

Comments
 (0)