Skip to content

Commit 8b93242

Browse files
author
Adria Labay
committed
implement mean occupation and get coherence functions
1 parent e4ccef5 commit 8b93242

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
@@ -663,24 +663,123 @@ get_data(A::AbstractQuantumObject) = A.data
663663
@doc raw"""
664664
get_coherence(ψ::QuantumObject)
665665
666-
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}``.
667-
668-
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|``.
666+
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}``.
669667
"""
670668
function get_coherence::QuantumObject{<:AbstractArray,KetQuantumObject})
671-
a = destroy(prod.dims))
672-
α = expect(a, ψ)
673-
D = exp* a' - conj(α) * a)
669+
if length.dims) == 1
670+
return mapreduce(n -> sqrt(n - 1) * ψ.data[n] * conj.data[n-1]), +, 2:ψ.dims[1])
671+
else
672+
R = CartesianIndices((ψ.dims...,))
673+
off = circshift.dims, 1)
674+
off[end] = 1
675+
676+
x = sum(R) do j
677+
j_tuple = Tuple(j) .- 1
678+
if 0 in j_tuple
679+
return 0
680+
end
681+
682+
J = dot(j_tuple, off) + 1
683+
J2 = dot(j_tuple .- 1, off) + 1
684+
return prod(sqrt.(j_tuple)) * ψ[J] * conj(ψ[J2])
685+
end
674686

675-
return α, D' * ψ
687+
return x
688+
end
676689
end
677690

678691
function get_coherence::QuantumObject{<:AbstractArray,OperatorQuantumObject})
679-
a = destroy(prod.dims))
680-
α = expect(a, ρ)
681-
D = exp* a' - conj(α) * a)
692+
if length.dims) == 1
693+
return mapreduce(n -> sqrt(n - 1) * ρ.data[n, n-1], +, 2:ρ.dims[1])
694+
else
695+
R = CartesianIndices((ρ.dims...,))
696+
off = circshift.dims, 1)
697+
off[end] = 1
698+
699+
x = sum(R) do j
700+
j_tuple = Tuple(j) .- 1
701+
if 0 in j_tuple
702+
return 0
703+
end
704+
705+
J = dot(j_tuple, off) + 1
706+
J2 = dot(j_tuple .- 1, off) + 1
707+
return prod(sqrt.(j_tuple)) * ρ[J, J2]
708+
end
709+
710+
return x
711+
end
712+
end
713+
714+
@doc raw"""
715+
remove_coherence(ψ::QuantumObject)
716+
717+
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|``.
718+
"""
719+
function remove_coherence::QuantumObject{<:AbstractArray,KetQuantumObject,1})
720+
α = get_coherence(ψ)
721+
D = displace.dims[1], α)
722+
723+
return D' * ψ
724+
end
725+
726+
function remove_coherence::QuantumObject{<:AbstractArray,OperatorQuantumObject,1})
727+
α = get_coherence(ρ)
728+
D = displace.dims[1], α)
729+
730+
return D' * ρ * D
731+
end
732+
733+
@doc raw"""
734+
mean_occupation(ψ::QuantumObject)
735+
736+
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}}``.
737+
738+
It returns the expectation value of the number operator.
739+
"""
740+
function mean_occupation::QuantumObject{T,KetQuantumObject}) where {T}
741+
if length.dims) == 1
742+
return mapreduce(k -> abs2(ψ[k]) * (k - 1), +, 1:ρ.dims[1])
743+
else
744+
R = CartesianIndices((ψ.dims...,))
745+
off = circshift.dims, 1)
746+
off[end] = 1
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)
752+
end
753+
754+
return x
755+
end
756+
end
757+
758+
function mean_occupation::QuantumObject{T,OperatorQuantumObject}) where {T}
759+
if length.dims) == 1
760+
return real(mapreduce(k -> ρ[k, k] * (k - 1), +, 1:ρ.dims[1]))
761+
else
762+
R = CartesianIndices((ρ.dims...,))
763+
off = circshift.dims, 1)
764+
off[end] = 1
765+
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)
770+
end
771+
772+
return real(x)
773+
end
774+
end
775+
776+
function mean_occupation::QuantumObject{T,OperatorKetQuantumObject}) where {T}
777+
if length.dims) > 1
778+
throw(ArgumentError("Mean photon number not implemented for composite OPeratorKetQuantumObject"))
779+
end
682780

683-
return α, D' * ρ * D
781+
d = ρ.dims[1]
782+
return real(mapreduce(k -> ρ[(k-1)*r+k] * (k - 1), +, 1:d))
684783
end
685784

686785
@doc raw"""

test/core-test/quantum_objects.jl

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,17 +442,54 @@
442442
end
443443
end
444444

445-
@testset "get coherence" begin
445+
@testset "get and remove coherence" begin
446446
ψ = coherent(30, 3)
447-
α, δψ = get_coherence(ψ)
448-
@test isapprox(abs(α), 3, atol = 1e-5) && abs2(δψ[1]) > 0.999
447+
α = get_coherence(ψ)
448+
@test isapprox(abs(α), 3, atol = 1e-5)
449+
δψ = remove_coherence(ψ)
450+
@test abs2(δψ[1]) > 0.999
449451
ρ = ket2dm(ψ)
450-
α, δρ = get_coherence(ρ)
451-
@test isapprox(abs(α), 3, atol = 1e-5) && abs2(δρ[1, 1]) > 0.999
452+
α = get_coherence(ρ)
453+
@test isapprox(abs(α), 3, atol = 1e-5)
454+
δρ = remove_coherence(ρ)
455+
@test abs2(δρ[1, 1]) > 0.999
452456

453457
@testset "Type Inference (get_coherence)" begin
454458
@inferred get_coherence(ψ)
455459
@inferred get_coherence(ρ)
460+
@inferred remove_coherence(ψ)
461+
@inferred remove_coherence(ρ)
462+
end
463+
end
464+
465+
@testset "mean occupation" begin
466+
N1 = 9.0
467+
ψ1 = coherent(50, 3.0)
468+
ρ1 = ket2dm(ψ1)
469+
v1 = mat2vec(ρ1)
470+
471+
@test mean_occupation(ψ) N1
472+
@test mean_occupation(ρ) N1
473+
@test mean_occupation(v) N1
474+
475+
N2 = 4.0
476+
Nc = N1 * N2
477+
ψ2 = coherent(30, 2.0)
478+
ψc = ψ1 ψ2
479+
ρc = ket2dm(ψc)
480+
vc = mat2vec(ρc)
481+
482+
@test mean_occupation(ψ2) N2
483+
@test mean_occupation(ρ2) N2
484+
@test mean_occupation(v2) N2
485+
486+
@test mean_occupation(ψc) Nc
487+
@test mean_occupation(ρc) Nc
488+
489+
@testset "Type Inference (mean_occupation)" begin
490+
@inferred mean_occupation(ψ)
491+
@inferred mean_occupation(ρ)
492+
@inferred mean_occupation(v)
456493
end
457494
end
458495

0 commit comments

Comments
 (0)