@@ -705,21 +705,13 @@ function get_coherence(ψ::QuantumObject{<:AbstractArray,KetQuantumObject})
705705 if length (ψ. dims) == 1
706706 return mapreduce (n -> sqrt (n - 1 ) * ψ. data[n] * conj (ψ. data[n- 1 ]), + , 2 : ψ. dims[1 ])
707707 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))
722710
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
723715 return x
724716 end
725717end
@@ -728,25 +720,26 @@ function get_coherence(ρ::QuantumObject{<:AbstractArray,OperatorQuantumObject})
728720 if length (ρ. dims) == 1
729721 return mapreduce (n -> sqrt (n - 1 ) * ρ. data[n, n- 1 ], + , 2 : ρ. dims[1 ])
730722 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))
745725
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
746730 return x
747731 end
748732end
749733
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+
750743@doc raw """
751744 remove_coherence(ψ::QuantumObject)
752745
@@ -773,42 +766,45 @@ Get the mean occupation number ``n`` by measuring the expectation value of the n
773766
774767It returns the expectation value of the number operator.
775768"""
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))
781772
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
787776 end
777+ reverse! (mean_occ)
778+
779+ return isnothing (idx) ? mean_occ : mean_occ[idx]
788780end
789781
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 ])
795783
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))
800787
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
802792 end
793+ reverse! (mean_occ)
794+
795+ return isnothing (idx) ? real .(mean_occ) : real (mean_occ[idx])
803796end
804797
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
807803 throw (ArgumentError (" Mean photon number not implemented for composite OPeratorKetQuantumObject" ))
808804 end
809805
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))
812808end
813809
814810@doc raw """
0 commit comments