@@ -705,21 +705,13 @@ function get_coherence(ψ::QuantumObject{<:AbstractArray,KetQuantumObject})
705
705
if length (ψ. dims) == 1
706
706
return mapreduce (n -> sqrt (n - 1 ) * ψ. data[n] * conj (ψ. data[n- 1 ]), + , 2 : ψ. dims[1 ])
707
707
else
708
- R = CartesianIndices ((ψ. dims... ,))
709
- off = circshift (ψ. dims, 1 )
710
- off[end ] = 1
711
-
712
- x = sum (R) do j
713
- j_tuple = Tuple (j) .- 1
714
- if 0 in j_tuple
715
- return 0
716
- end
717
-
718
- J = dot (j_tuple, off) + 1
719
- J2 = dot (j_tuple .- 1 , off) + 1
720
- return prod (sqrt .(j_tuple)) * ψ[J] * conj (ψ[J2])
721
- end
708
+ off = sum (cumprod (reverse (ψ. dims[2 : end ]))) + 1
709
+ t = Tuple (reverse (ψ. dims))
722
710
711
+ x = 0.0im
712
+ for J in off+ 2 : length (ψ. data)
713
+ x += ψ[J] * conj (ψ[J- off]) * prod (sqrt .(_ind2sub (t, J) .- 1 ))
714
+ end
723
715
return x
724
716
end
725
717
end
@@ -728,25 +720,26 @@ function get_coherence(ρ::QuantumObject{<:AbstractArray,OperatorQuantumObject})
728
720
if length (ρ. dims) == 1
729
721
return mapreduce (n -> sqrt (n - 1 ) * ρ. data[n, n- 1 ], + , 2 : ρ. dims[1 ])
730
722
else
731
- R = CartesianIndices ((ρ. dims... ,))
732
- off = circshift (ρ. dims, 1 )
733
- off[end ] = 1
734
-
735
- x = sum (R) do j
736
- j_tuple = Tuple (j) .- 1
737
- if 0 in j_tuple
738
- return 0
739
- end
740
-
741
- J = dot (j_tuple, off) + 1
742
- J2 = dot (j_tuple .- 1 , off) + 1
743
- return prod (sqrt .(j_tuple)) * ρ[J, J2]
744
- end
723
+ off = sum (cumprod (reverse (ρ. dims[2 : end ]))) + 1
724
+ t = Tuple (reverse (ρ. dims))
745
725
726
+ x = 0.0im
727
+ for J in off+ 2 : length (ρ. data[1 , :])
728
+ x += ρ[J, J- off] * prod (sqrt .(_ind2sub (t, J) .- 1 ))
729
+ end
746
730
return x
747
731
end
748
732
end
749
733
734
+ function get_coherence (v:: QuantumObject{T,OperatorKetQuantumObject} ) where {T}
735
+ if length (v. dims) > 1
736
+ throw (ArgumentError (" Mean photon number not implemented for composite OPeratorKetQuantumObject" ))
737
+ end
738
+
739
+ d = v. dims[1 ]
740
+ return mapreduce (n -> sqrt (n - 1 ) * v. data[(n- 1 )* d+ n- 1 ], + , 2 : d)
741
+ end
742
+
750
743
@doc raw """
751
744
remove_coherence(ψ::QuantumObject)
752
745
@@ -773,42 +766,45 @@ Get the mean occupation number ``n`` by measuring the expectation value of the n
773
766
774
767
It returns the expectation value of the number operator.
775
768
"""
776
- function mean_occupation (ψ:: QuantumObject{T,KetQuantumObject} ) where {T}
777
- if length (ψ. dims) == 1
778
- return mapreduce (k -> abs2 (ψ[k]) * (k - 1 ), + , 1 : ρ. dims[1 ])
779
- else
780
- t = Tuple (ψ. dims)
769
+ function mean_occupation (ψ:: QuantumObject{T,KetQuantumObject} ; idx:: Union{Int,Nothing} = nothing ) where {T}
770
+ t = Tuple (reverse (ψ. dims))
771
+ mean_occ = zeros (length (ψ. dims))
781
772
782
- x = 0.0
783
- for J in eachindex (ψ. data)
784
- x += abs2 (ψ[J]) * prod (Base. _ind2sub (t, J) .- 1 )
785
- end
786
- return real (x)
773
+ for J in eachindex (ψ. data)
774
+ sub_indices = _ind2sub (t, J) .- 1
775
+ mean_occ .+ = abs2 (ψ[J]) .* sub_indices
787
776
end
777
+ reverse! (mean_occ)
778
+
779
+ return isnothing (idx) ? mean_occ : mean_occ[idx]
788
780
end
789
781
790
- function mean_occupation (ρ:: QuantumObject{T,OperatorQuantumObject} ) where {T}
791
- if length (ρ. dims) == 1
792
- return real (mapreduce (k -> ρ[k, k] * (k - 1 ), + , 1 : ρ. dims[1 ]))
793
- else
794
- t = Tuple (ρ. dims)
782
+ mean_occupation (ψ:: QuantumObject{T,KetQuantumObject,1} ) where {T} = mapreduce (k -> abs2 (ψ[k]) * (k - 1 ), + , 1 : ψ. dims[1 ])
795
783
796
- x = 0.0im
797
- for J in eachindex (ρ. data[:, 1 ])
798
- x += ρ[J, J] * prod (Base. _ind2sub (t, J) .- 1 )
799
- end
784
+ function mean_occupation (ρ:: QuantumObject{T,OperatorQuantumObject} ; idx:: Union{Int,Nothing} = nothing ) where {T}
785
+ t = Tuple (reverse (ρ. dims))
786
+ mean_occ = zeros (eltype (ρ. data), length (ρ. dims))
800
787
801
- return real (x)
788
+ x = 0.0im
789
+ for J in eachindex (ρ. data[:, 1 ])
790
+ sub_indices = _ind2sub (t, J) .- 1
791
+ mean_occ .+ = ρ[J, J] .* sub_indices
802
792
end
793
+ reverse! (mean_occ)
794
+
795
+ return isnothing (idx) ? real .(mean_occ) : real (mean_occ[idx])
803
796
end
804
797
805
- function mean_occupation (ρ:: QuantumObject{T,OperatorKetQuantumObject} ) where {T}
806
- if length (ρ. dims) > 1
798
+ mean_occupation (ρ:: QuantumObject{T,OperatorQuantumObject,1} ) where {T} =
799
+ mapreduce (k -> ρ[k, k] * (k - 1 ), + , 1 : ρ. dims[1 ])
800
+
801
+ function mean_occupation (v:: QuantumObject{T,OperatorKetQuantumObject} ) where {T}
802
+ if length (v. dims) > 1
807
803
throw (ArgumentError (" Mean photon number not implemented for composite OPeratorKetQuantumObject" ))
808
804
end
809
805
810
- d = ρ . dims[1 ]
811
- return real (mapreduce (k -> ρ [(k- 1 )* r + k] * (k - 1 ), + , 1 : d))
806
+ d = v . dims[1 ]
807
+ return real (mapreduce (k -> v [(k- 1 )* d + k] * (k - 1 ), + , 1 : d))
812
808
end
813
809
814
810
@doc raw """
0 commit comments