Skip to content

Commit 69ccc2d

Browse files
author
Adria Labay
committed
implement mean occupation and get coherence functions
1 parent 270d3f6 commit 69ccc2d

File tree

2 files changed

+152
-16
lines changed

2 files changed

+152
-16
lines changed

src/qobj/arithmetic_and_attributes.jl

Lines changed: 110 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -697,24 +697,123 @@ get_data(A::AbstractQuantumObject) = A.data
697697
@doc raw"""
698698
get_coherence(ψ::QuantumObject)
699699
700-
Get the coherence value ``\alpha`` by measuring the expectation value of the destruction operator ``\hat{a}`` on a state ``\ket{\psi}`` or a density matrix ``\hat{\rho}``.
701-
702-
It returns both ``\alpha`` and the corresponding state with the coherence removed: ``\ket{\delta_\alpha} = \exp ( \alpha^* \hat{a} - \alpha \hat{a}^\dagger ) \ket{\psi}`` for a pure state, and ``\hat{\rho_\alpha} = \exp ( \alpha^* \hat{a} - \alpha \hat{a}^\dagger ) \hat{\rho} \exp ( -\bar{\alpha} \hat{a} + \alpha \hat{a}^\dagger )`` for a density matrix. These states correspond to the quantum fluctuations around the coherent state ``\ket{\alpha}`` or ``|\alpha\rangle\langle\alpha|``.
700+
Returns the coherence value ``\alpha`` by measuring the expectation value of the destruction operator ``\hat{a}`` on a state ``\ket{\psi}`` or a density matrix ``\hat{\rho}``.
703701
"""
704702
function get_coherence::QuantumObject{<:AbstractArray,KetQuantumObject})
705-
a = destroy(prod.dims))
706-
α = expect(a, ψ)
707-
D = exp* a' - conj(α) * a)
703+
if length.dims) == 1
704+
return mapreduce(n -> sqrt(n - 1) * ψ.data[n] * conj.data[n-1]), +, 2:ψ.dims[1])
705+
else
706+
R = CartesianIndices((ψ.dims...,))
707+
off = circshift.dims, 1)
708+
off[end] = 1
709+
710+
x = sum(R) do j
711+
j_tuple = Tuple(j) .- 1
712+
if 0 in j_tuple
713+
return 0
714+
end
715+
716+
J = dot(j_tuple, off) + 1
717+
J2 = dot(j_tuple .- 1, off) + 1
718+
return prod(sqrt.(j_tuple)) * ψ[J] * conj(ψ[J2])
719+
end
708720

709-
return α, D' * ψ
721+
return x
722+
end
710723
end
711724

712725
function get_coherence::QuantumObject{<:AbstractArray,OperatorQuantumObject})
713-
a = destroy(prod.dims))
714-
α = expect(a, ρ)
715-
D = exp* a' - conj(α) * a)
726+
if length.dims) == 1
727+
return mapreduce(n -> sqrt(n - 1) * ρ.data[n, n-1], +, 2:ρ.dims[1])
728+
else
729+
R = CartesianIndices((ρ.dims...,))
730+
off = circshift.dims, 1)
731+
off[end] = 1
732+
733+
x = sum(R) do j
734+
j_tuple = Tuple(j) .- 1
735+
if 0 in j_tuple
736+
return 0
737+
end
738+
739+
J = dot(j_tuple, off) + 1
740+
J2 = dot(j_tuple .- 1, off) + 1
741+
return prod(sqrt.(j_tuple)) * ρ[J, J2]
742+
end
743+
744+
return x
745+
end
746+
end
747+
748+
@doc raw"""
749+
remove_coherence(ψ::QuantumObject)
750+
751+
Returns the corresponding state with the coherence removed: ``\ket{\delta_\alpha} = \exp ( \alpha^* \hat{a} - \alpha \hat{a}^\dagger ) \ket{\psi}`` for a pure state, and ``\hat{\rho_\alpha} = \exp ( \alpha^* \hat{a} - \alpha \hat{a}^\dagger ) \hat{\rho} \exp ( -\bar{\alpha} \hat{a} + \alpha \hat{a}^\dagger )`` for a density matrix. These states correspond to the quantum fluctuations around the coherent state ``\ket{\alpha}`` or ``|\alpha\rangle\langle\alpha|``.
752+
"""
753+
function remove_coherence::QuantumObject{<:AbstractArray,KetQuantumObject,1})
754+
α = get_coherence(ψ)
755+
D = displace.dims[1], α)
756+
757+
return D' * ψ
758+
end
759+
760+
function remove_coherence::QuantumObject{<:AbstractArray,OperatorQuantumObject,1})
761+
α = get_coherence(ρ)
762+
D = displace.dims[1], α)
763+
764+
return D' * ρ * D
765+
end
766+
767+
@doc raw"""
768+
mean_occupation(ψ::QuantumObject)
769+
770+
Get the mean occupation number ``n`` by measuring the expectation value of the number operator ``\hat{n}`` on a state ``\ket{\psi}``, a density matrix ``\hat{\rho}`` or a vectorized density matrix ``\ket{\hat{\rho}}``.
771+
772+
It returns the expectation value of the number operator.
773+
"""
774+
function mean_occupation::QuantumObject{T,KetQuantumObject}) where {T}
775+
if length.dims) == 1
776+
return mapreduce(k -> abs2(ψ[k]) * (k - 1), +, 1:ρ.dims[1])
777+
else
778+
R = CartesianIndices((ψ.dims...,))
779+
off = circshift.dims, 1)
780+
off[end] = 1
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)
786+
end
787+
788+
return x
789+
end
790+
end
791+
792+
function mean_occupation::QuantumObject{T,OperatorQuantumObject}) where {T}
793+
if length.dims) == 1
794+
return real(mapreduce(k -> ρ[k, k] * (k - 1), +, 1:ρ.dims[1]))
795+
else
796+
R = CartesianIndices((ρ.dims...,))
797+
off = circshift.dims, 1)
798+
off[end] = 1
799+
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)
804+
end
805+
806+
return real(x)
807+
end
808+
end
809+
810+
function mean_occupation::QuantumObject{T,OperatorKetQuantumObject}) where {T}
811+
if length.dims) > 1
812+
throw(ArgumentError("Mean photon number not implemented for composite OPeratorKetQuantumObject"))
813+
end
716814

717-
return α, D' * ρ * D
815+
d = ρ.dims[1]
816+
return real(mapreduce(k -> ρ[(k-1)*r+k] * (k - 1), +, 1:d))
718817
end
719818

720819
@doc raw"""

test/core-test/quantum_objects.jl

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,17 +450,54 @@
450450
end
451451
end
452452

453-
@testset "get coherence" begin
453+
@testset "get and remove coherence" begin
454454
ψ = coherent(30, 3)
455-
α, δψ = get_coherence(ψ)
456-
@test isapprox(abs(α), 3, atol = 1e-5) && abs2(δψ[1]) > 0.999
455+
α = get_coherence(ψ)
456+
@test isapprox(abs(α), 3, atol = 1e-5)
457+
δψ = remove_coherence(ψ)
458+
@test abs2(δψ[1]) > 0.999
457459
ρ = ket2dm(ψ)
458-
α, δρ = get_coherence(ρ)
459-
@test isapprox(abs(α), 3, atol = 1e-5) && abs2(δρ[1, 1]) > 0.999
460+
α = get_coherence(ρ)
461+
@test isapprox(abs(α), 3, atol = 1e-5)
462+
δρ = remove_coherence(ρ)
463+
@test abs2(δρ[1, 1]) > 0.999
460464

461465
@testset "Type Inference (get_coherence)" begin
462466
@inferred get_coherence(ψ)
463467
@inferred get_coherence(ρ)
468+
@inferred remove_coherence(ψ)
469+
@inferred remove_coherence(ρ)
470+
end
471+
end
472+
473+
@testset "mean occupation" begin
474+
N1 = 9.0
475+
ψ1 = coherent(50, 3.0)
476+
ρ1 = ket2dm(ψ1)
477+
v1 = mat2vec(ρ1)
478+
479+
@test mean_occupation(ψ) N1
480+
@test mean_occupation(ρ) N1
481+
@test mean_occupation(v) N1
482+
483+
N2 = 4.0
484+
Nc = N1 * N2
485+
ψ2 = coherent(30, 2.0)
486+
ψc = ψ1 ψ2
487+
ρc = ket2dm(ψc)
488+
vc = mat2vec(ρc)
489+
490+
@test mean_occupation(ψ2) N2
491+
@test mean_occupation(ρ2) N2
492+
@test mean_occupation(v2) N2
493+
494+
@test mean_occupation(ψc) Nc
495+
@test mean_occupation(ρc) Nc
496+
497+
@testset "Type Inference (mean_occupation)" begin
498+
@inferred mean_occupation(ψ)
499+
@inferred mean_occupation(ρ)
500+
@inferred mean_occupation(v)
464501
end
465502
end
466503

0 commit comments

Comments
 (0)