@@ -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 (:(+ ), :(- ), :(* ), :(/ ), :(^ ))
@@ -741,32 +743,25 @@ function mean_occupation(ψ::QuantumObject{T,KetQuantumObject}) where {T}
741
743
if length (ψ. dims) == 1
742
744
return mapreduce (k -> abs2 (ψ[k]) * (k - 1 ), + , 1 : ρ. dims[1 ])
743
745
else
744
- R = CartesianIndices ((ψ. dims... ,))
745
- off = circshift (ψ. dims, 1 )
746
- off[end ] = 1
746
+ t = Tuple (ψ. dims)
747
747
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 )
752
751
end
753
-
754
- return x
752
+ return real (x)
755
753
end
756
754
end
757
755
758
756
function mean_occupation (ρ:: QuantumObject{T,OperatorQuantumObject} ) where {T}
759
757
if length (ρ. dims) == 1
760
758
return real (mapreduce (k -> ρ[k, k] * (k - 1 ), + , 1 : ρ. dims[1 ]))
761
759
else
762
- R = CartesianIndices ((ρ. dims... ,))
763
- off = circshift (ψ. dims, 1 )
764
- off[end ] = 1
760
+ t = Tuple (ρ. dims)
765
761
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 )
770
765
end
771
766
772
767
return real (x)
0 commit comments