@@ -8,6 +8,8 @@ export proj, ptrace, purity, permute
88export tidyup, tidyup!
99export get_data, get_coherence, remove_coherence, mean_occupation
1010
11+ import Base: _ind2sub
12+
1113# Broadcasting
1214Base. broadcastable (x:: QuantumObject ) = x. data
1315for op in (:(+ ), :(- ), :(* ), :(/ ), :(^ ))
@@ -775,32 +777,25 @@ function mean_occupation(ψ::QuantumObject{T,KetQuantumObject}) where {T}
775777 if length (ψ. dims) == 1
776778 return mapreduce (k -> abs2 (ψ[k]) * (k - 1 ), + , 1 : ρ. dims[1 ])
777779 else
778- R = CartesianIndices ((ψ. dims... ,))
779- off = circshift (ψ. dims, 1 )
780- off[end ] = 1
780+ t = Tuple (ψ. dims)
781781
782- x = sum (R) do j
783- j_tuple = Tuple (j) .- 1
784- J = dot (j_tuple, off) + 1
785- return abs2 (ψ[J]) * prod (j_tuple)
782+ x = 0.0
783+ for J in eachindex (ψ. data)
784+ x += abs2 (ψ[J]) * prod (Base. _ind2sub (t, J) .- 1 )
786785 end
787-
788- return x
786+ return real (x)
789787 end
790788end
791789
792790function mean_occupation (ρ:: QuantumObject{T,OperatorQuantumObject} ) where {T}
793791 if length (ρ. dims) == 1
794792 return real (mapreduce (k -> ρ[k, k] * (k - 1 ), + , 1 : ρ. dims[1 ]))
795793 else
796- R = CartesianIndices ((ρ. dims... ,))
797- off = circshift (ψ. dims, 1 )
798- off[end ] = 1
794+ t = Tuple (ρ. dims)
799795
800- x = sum (R) do j
801- j_tuple = Tuple (j) .- 1
802- J = dot (j_tuple, off) + 1
803- return ρ[J, J] * prod (j_tuple)
796+ x = 0.0im
797+ for J in eachindex (ρ. data[:, 1 ])
798+ x += ρ[J, J] * prod (Base. _ind2sub (t, J) .- 1 )
804799 end
805800
806801 return real (x)
0 commit comments