@@ -8,6 +8,8 @@ export proj, ptrace, purity, permute
8
8
export tidyup, tidyup!
9
9
export get_data, get_coherence, remove_coherence, mean_occupation
10
10
11
+ import Base: _ind2sub
12
+
11
13
# Broadcasting
12
14
Base. broadcastable (x:: QuantumObject ) = x. data
13
15
for op in (:(+ ), :(- ), :(* ), :(/ ), :(^ ))
@@ -775,32 +777,25 @@ function mean_occupation(ψ::QuantumObject{T,KetQuantumObject}) where {T}
775
777
if length (ψ. dims) == 1
776
778
return mapreduce (k -> abs2 (ψ[k]) * (k - 1 ), + , 1 : ρ. dims[1 ])
777
779
else
778
- R = CartesianIndices ((ψ. dims... ,))
779
- off = circshift (ψ. dims, 1 )
780
- off[end ] = 1
780
+ t = Tuple (ψ. dims)
781
781
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 )
786
785
end
787
-
788
- return x
786
+ return real (x)
789
787
end
790
788
end
791
789
792
790
function mean_occupation (ρ:: QuantumObject{T,OperatorQuantumObject} ) where {T}
793
791
if length (ρ. dims) == 1
794
792
return real (mapreduce (k -> ρ[k, k] * (k - 1 ), + , 1 : ρ. dims[1 ]))
795
793
else
796
- R = CartesianIndices ((ρ. dims... ,))
797
- off = circshift (ψ. dims, 1 )
798
- off[end ] = 1
794
+ t = Tuple (ρ. dims)
799
795
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 )
804
799
end
805
800
806
801
return real (x)
0 commit comments