@@ -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 (:(+ ), :(- ), :(* ), :(/ ), :(^ ))
@@ -741,32 +743,25 @@ function mean_occupation(ψ::QuantumObject{T,KetQuantumObject}) where {T}
741743 if length (ψ. dims) == 1
742744 return mapreduce (k -> abs2 (ψ[k]) * (k - 1 ), + , 1 : ρ. dims[1 ])
743745 else
744- R = CartesianIndices ((ψ. dims... ,))
745- off = circshift (ψ. dims, 1 )
746- off[end ] = 1
746+ t = Tuple (ψ. dims)
747747
748- x = sum (R) do j
749- j_tuple = Tuple (j) .- 1
750- J = dot (j_tuple, off) + 1
751- return abs2 (ψ[J]) * prod (j_tuple)
748+ x = 0.0
749+ for J in eachindex (ψ. data)
750+ x += abs2 (ψ[J]) * prod (Base. _ind2sub (t, J) .- 1 )
752751 end
753-
754- return x
752+ return real (x)
755753 end
756754end
757755
758756function mean_occupation (ρ:: QuantumObject{T,OperatorQuantumObject} ) where {T}
759757 if length (ρ. dims) == 1
760758 return real (mapreduce (k -> ρ[k, k] * (k - 1 ), + , 1 : ρ. dims[1 ]))
761759 else
762- R = CartesianIndices ((ρ. dims... ,))
763- off = circshift (ψ. dims, 1 )
764- off[end ] = 1
760+ t = Tuple (ρ. dims)
765761
766- x = sum (R) do j
767- j_tuple = Tuple (j) .- 1
768- J = dot (j_tuple, off) + 1
769- return ρ[J, J] * prod (j_tuple)
762+ x = 0.0im
763+ for J in eachindex (ρ. data[:, 1 ])
764+ x += ρ[J, J] * prod (Base. _ind2sub (t, J) .- 1 )
770765 end
771766
772767 return real (x)
0 commit comments