Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement mean occupation and get coherence functions #343

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
return list of a and n if multiple subsystems and tests
  • Loading branch information
Adria Labay committed Dec 23, 2024
commit 29cae761943e2d38ba00f368497e75210f9f2f47
124 changes: 83 additions & 41 deletions src/qobj/arithmetic_and_attributes.jl
Original file line number Diff line number Diff line change
@@ -697,66 +697,98 @@
get_data(A::AbstractQuantumObject) = A.data

@doc raw"""
get_coherence(ψ::QuantumObject)
get_coherence(ψ::QuantumObject; idx)

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}``.
"""
function get_coherence(ψ::QuantumObject{<:AbstractArray,KetQuantumObject})
if length(ψ.dims) == 1
return mapreduce(n -> sqrt(n - 1) * ψ.data[n] * conj(ψ.data[n-1]), +, 2:ψ.dims[1])
else
off = sum(cumprod(reverse(ψ.dims[2:end]))) + 1
t = Tuple(reverse(ψ.dims))
Returns the coherence value ``\alpha`` by measuring the expectation value of the destruction operator ``\hat{a}`` on a state ``\ket{\psi}``, density matrix ``\hat{\rho}`` or vectorized state ``|\hat{\rho}\rangle\rangle``.

x = 0.0im
for J in off+2:length(ψ.data)
x += ψ[J] * conj(ψ[J-off]) * prod(sqrt.(_ind2sub(t, J) .- 1))
If the state is a composite state, the coherence values of each subsystems are returned as an array of complex numbers. It is possible to specify the index of the sybsystem to get the coherence value by fixing `idx`.

Check warning on line 704 in src/qobj/arithmetic_and_attributes.jl

GitHub Actions / Spell Check with Typos

"sybsystem" should be "subsystem".
"""
function get_coherence(ψ::QuantumObject{T,KetQuantumObject,N}; idx::Union{Nothing,Int} = nothing) where {T,N}
dims = reverse(Tuple(ψ.dims))
offsets = cumprod([1; dims[1:end-1]...])

Check warning on line 708 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L706-L708

Added lines #L706 - L708 were not covered by tests

expectation_values = zeros(ComplexF64, N)

Check warning on line 710 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L710

Added line #L710 was not covered by tests

for J in 2:length(ψ.data)
indices = Base._ind2sub(dims, J)
for n in 1:N
if indices[n] > 1
expectation_values[n] += conj(ψ.data[J-offsets[n]]) * sqrt(indices[n] - 1) * ψ.data[J]

Check warning on line 716 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L712-L716

Added lines #L712 - L716 were not covered by tests
end
end
return x
end

Check warning on line 719 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L718-L719

Added lines #L718 - L719 were not covered by tests

reverse!(expectation_values)

Check warning on line 721 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L721

Added line #L721 was not covered by tests

return isnothing(idx) ? expectation_values : expectation_values[idx]

Check warning on line 723 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L723

Added line #L723 was not covered by tests
end

function get_coherence(ρ::QuantumObject{<:AbstractArray,OperatorQuantumObject})
if length(ρ.dims) == 1
return mapreduce(n -> sqrt(n - 1) * ρ.data[n, n-1], +, 2:ρ.dims[1])
else
off = sum(cumprod(reverse(ρ.dims[2:end]))) + 1
t = Tuple(reverse(ρ.dims))
function get_coherence(ρ::QuantumObject{T,OperatorQuantumObject,N}; idx::Union{Int,Nothing} = nothing) where {T,N}
dims = reverse(Tuple(ρ.dims))
offsets = cumprod([1; dims[1:end-1]...])

Check warning on line 728 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L726-L728

Added lines #L726 - L728 were not covered by tests

expectation_values = zeros(ComplexF64, N)

Check warning on line 730 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L730

Added line #L730 was not covered by tests

x = 0.0im
for J in off+2:length(ρ.data[1, :])
x += ρ[J, J-off] * prod(sqrt.(_ind2sub(t, J) .- 1))
@inbounds for J in 2:size(ρ.data, 1)
indices = Base._ind2sub(dims, J)
for n in 1:N
if indices[n] > 1
expectation_values[n] += sqrt(indices[n] - 1) * ρ.data[J, J-offsets[n]]

Check warning on line 736 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L732-L736

Added lines #L732 - L736 were not covered by tests
end
end
return x
end

Check warning on line 739 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L738-L739

Added lines #L738 - L739 were not covered by tests

reverse!(expectation_values)

Check warning on line 741 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L741

Added line #L741 was not covered by tests

return isnothing(idx) ? expectation_values : expectation_values[idx]

Check warning on line 743 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L743

Added line #L743 was not covered by tests
end

function get_coherence(v::QuantumObject{T,OperatorKetQuantumObject}) where {T}
if length(v.dims) > 1
throw(ArgumentError("Mean photon number not implemented for composite OPeratorKetQuantumObject"))
function get_coherence(v::QuantumObject{T,OperatorKetQuantumObject,N}; idx::Union{Int,Nothing} = nothing) where {T,N}
dims = reverse(Tuple(v.dims))

Check warning on line 747 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L746-L747

Added lines #L746 - L747 were not covered by tests

offsets = cumprod([1; dims[1:end-1]...])
D = prod(dims)

Check warning on line 750 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L749-L750

Added lines #L749 - L750 were not covered by tests

expectation_values = zeros(ComplexF64, N)

Check warning on line 752 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L752

Added line #L752 was not covered by tests

@inbounds for J in 2:D
indices = Base._ind2sub(dims, J)
for n in 1:N
if indices[n] > 1
expectation_values[n] += sqrt(indices[n] - 1) * v.data[(J-offsets[n]-1)*D+J]

Check warning on line 758 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L754-L758

Added lines #L754 - L758 were not covered by tests
end
end
end

Check warning on line 761 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L760-L761

Added lines #L760 - L761 were not covered by tests

d = v.dims[1]
return mapreduce(n -> sqrt(n - 1) * v.data[(n-1)*d+n-1], +, 2:d)
reverse!(expectation_values)

Check warning on line 763 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L763

Added line #L763 was not covered by tests

return isnothing(idx) ? expectation_values : expectation_values[idx]

Check warning on line 765 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L765

Added line #L765 was not covered by tests
end

get_coherence(ψ::QuantumObject{T,KetQuantumObject,1}) where {T} =
mapreduce(n -> sqrt(n - 1) * ψ.data[n] * conj(ψ.data[n-1]), +, 2:ψ.dims[1])
get_coherence(ρ::QuantumObject{T,OperatorQuantumObject,1}) where {T} =
mapreduce(n -> sqrt(n - 1) * ρ.data[n, n-1], +, 2:ρ.dims[1])
get_coherence(v::QuantumObject{T,OperatorKetQuantumObject,1}) where {T} =
mapreduce(n -> sqrt(n - 1) * v.data[(n-2)*v.dims[1]+n], +, 2:v.dims[1])

Check warning on line 773 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L768-L773

Added lines #L768 - L773 were not covered by tests

@doc raw"""
remove_coherence(ψ::QuantumObject)

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|``.
"""
function remove_coherence(ψ::QuantumObject{<:AbstractArray,KetQuantumObject,1})
α = get_coherence(ψ)
D = displace(ψ.dims[1], α)
function remove_coherence(ψ::QuantumObject{T,KetQuantumObject}) where {T}
αs = get_coherence(ψ)
D = mapreduce(k -> displace(ψ.dims[k], αs[k]), ⊗, eachindex(ψ.dims))

Check warning on line 782 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L780-L782

Added lines #L780 - L782 were not covered by tests

return D' * ψ

Check warning on line 784 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L784

Added line #L784 was not covered by tests
end

function remove_coherence(ρ::QuantumObject{<:AbstractArray,OperatorQuantumObject,1})
α = get_coherence(ρ)
D = displace(ρ.dims[1], α)
function remove_coherence(ρ::QuantumObject{T,OperatorQuantumObject}) where {T}
αs = get_coherence(ρ)
D = mapreduce(k -> displace(ρ.dims[k], αs[k]), ⊗, eachindex(ρ.dims))

Check warning on line 789 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L787-L789

Added lines #L787 - L789 were not covered by tests

return D' * ρ * D

Check warning on line 791 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L791

Added line #L791 was not covered by tests
end

@doc raw"""
@@ -764,49 +796,59 @@

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}}``.

It returns the expectation value of the number operator.
If the state is a composite state, the mean occupation numbers of each subsystems are returned as an array of real numbers. It is possible to specify the index of the sybsystem to get the mean occupation number by fixing `idx`.

Check warning on line 799 in src/qobj/arithmetic_and_attributes.jl

GitHub Actions / Spell Check with Typos

"sybsystem" should be "subsystem".
"""
function mean_occupation(ψ::QuantumObject{T,KetQuantumObject}; idx::Union{Int,Nothing} = nothing) where {T}
t = Tuple(reverse(ψ.dims))
mean_occ = zeros(length(ψ.dims))

Check warning on line 803 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L801-L803

Added lines #L801 - L803 were not covered by tests

for J in eachindex(ψ.data)
sub_indices = _ind2sub(t, J) .- 1
mean_occ .+= abs2(ψ[J]) .* sub_indices
end
reverse!(mean_occ)

Check warning on line 809 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L805-L809

Added lines #L805 - L809 were not covered by tests

return isnothing(idx) ? mean_occ : mean_occ[idx]

Check warning on line 811 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L811

Added line #L811 was not covered by tests
end

mean_occupation(ψ::QuantumObject{T,KetQuantumObject,1}) where {T} = mapreduce(k -> abs2(ψ[k]) * (k - 1), +, 1:ψ.dims[1])

Check warning on line 814 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L814

Added line #L814 was not covered by tests

function mean_occupation(ρ::QuantumObject{T,OperatorQuantumObject}; idx::Union{Int,Nothing} = nothing) where {T}
t = Tuple(reverse(ρ.dims))
mean_occ = zeros(eltype(ρ.data), length(ρ.dims))

Check warning on line 818 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L816-L818

Added lines #L816 - L818 were not covered by tests

x = 0.0im
for J in eachindex(ρ.data[:, 1])
sub_indices = _ind2sub(t, J) .- 1
mean_occ .+= ρ[J, J] .* sub_indices
end
reverse!(mean_occ)

Check warning on line 824 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L820-L824

Added lines #L820 - L824 were not covered by tests

return isnothing(idx) ? real.(mean_occ) : real(mean_occ[idx])

Check warning on line 826 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L826

Added line #L826 was not covered by tests
end

mean_occupation(ρ::QuantumObject{T,OperatorQuantumObject,1}) where {T} =
mapreduce(k -> ρ[k, k] * (k - 1), +, 1:ρ.dims[1])

Check warning on line 830 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L829-L830

Added lines #L829 - L830 were not covered by tests

function mean_occupation(v::QuantumObject{T,OperatorKetQuantumObject}) where {T}
if length(v.dims) > 1
throw(ArgumentError("Mean photon number not implemented for composite OPeratorKetQuantumObject"))
end

function mean_occupation(v::QuantumObject{T,OperatorKetQuantumObject,1}) where {T}
d = v.dims[1]
return real(mapreduce(k -> v[(k-1)*d+k] * (k - 1), +, 1:d))

Check warning on line 834 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L832-L834

Added lines #L832 - L834 were not covered by tests
end

function mean_occupation(v::QuantumObject{T,OperatorKetQuantumObject}; idx::Union{Int,Nothing} = nothing) where {T}
dims = reverse(Tuple(v.dims))
mean_occ = zeros(eltype(v.data), length(v.dims))

Check warning on line 839 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L837-L839

Added lines #L837 - L839 were not covered by tests

offset = prod(dims)

Check warning on line 841 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L841

Added line #L841 was not covered by tests

for J in 1:offset
sub_indices = _ind2sub(dims, J) .- 1
mean_occ .+= v[(J-1)*offset+J] .* sub_indices
end
reverse!(mean_occ)

Check warning on line 847 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L843-L847

Added lines #L843 - L847 were not covered by tests

return isnothing(idx) ? real.(mean_occ) : real(mean_occ[idx])

Check warning on line 849 in src/qobj/arithmetic_and_attributes.jl

Codecov / codecov/patch

src/qobj/arithmetic_and_attributes.jl#L849

Added line #L849 was not covered by tests
end

@doc raw"""
permute(A::QuantumObject, order::Union{AbstractVector{Int},Tuple})

96 changes: 88 additions & 8 deletions test/core-test/quantum_objects.jl
Original file line number Diff line number Diff line change
@@ -451,20 +451,93 @@
end

@testset "get and remove coherence" begin
ψ = coherent(30, 3)
α = get_coherence(ψ)
@test isapprox(abs(α), 3, atol = 1e-5)
α1 = 3.0 + 1.5im
ψ1 = coherent(30, α1)
β1 = get_coherence(ψ1)
@test isapprox(α1, β1, atol = 1e-4)

ρ1 = ket2dm(ψ1)
β1 = get_coherence(ρ1)
@test isapprox(α1, β1, atol = 1e-4)

v1 = mat2vec(ρ1)
β1 = get_coherence(v1)
@test isapprox(α1, β1, atol = 1e-4)

α2 = 1.2 + 0.3im
ψ2 = coherent(15, α2)
β2 = get_coherence(ψ2)
@test isapprox(α2, β2, atol = 1e-4)

ρ2 = ket2dm(ψ2)
β2 = get_coherence(ρ2)
@test isapprox(α2, β2, atol = 1e-4)

v2 = mat2vec(ρ2)
β2 = get_coherence(v2)
@test isapprox(α2, β2, atol = 1e-4)

ψ = ψ1 ⊗ ψ2
βs = get_coherence(ψ)
@test length(βs) == 2
@test isapprox(βs[1], α1, atol = 1e-4)
@test isapprox(βs[2], α2, atol = 1e-4)

ρ = ρ1 ⊗ ρ2
βs = get_coherence(ρ)
@test length(βs) == 2
@test isapprox(βs[1], α1, atol = 1e-4)
@test isapprox(βs[2], α2, atol = 1e-4)

v = mat2vec(ρ)
βs = get_coherence(v)
@test length(βs) == 2
@test isapprox(βs[1], α1, atol = 1e-4)
@test isapprox(βs[2], α2, atol = 1e-4)

αs = [α1, α2]
for idx in 1:2
β = get_coherence(ψ, idx = idx)
@test isapprox(β, αs[idx], atol = 1e-4)
β = get_coherence(ρ, idx = idx)
@test isapprox(β, αs[idx], atol = 1e-4)
β = get_coherence(v, idx = idx)
@test isapprox(β, αs[idx], atol = 1e-4)
end

@testset "Type Inference (get_coherence)" begin
@inferred get_coherence(ψ1)
@inferred get_coherence(ρ1)
@inferred get_coherence(v1)
@inferred get_coherence(ψ)
@inferred get_coherence(ρ)
@inferred get_coherence(v)
end
end

@testset "remove coherence" begin
α1 = 3.0 + 1.5im
ψ1 = coherent(30, α1)
ψ2 = coherent(15, 1.2 + 0.3im)

δψ1 = remove_coherence(ψ1)
@test abs2(δψ1[1]) > 0.999

ρ1 = ket2dm(ψ1)
δρ1 = remove_coherence(ρ1)
@test abs2(δρ1[1, 1]) > 0.999

ψ = ψ1 ⊗ ψ2
δψ = remove_coherence(ψ)
@test abs2(δψ[1]) > 0.999

ρ = ket2dm(ψ)
α = get_coherence(ρ)
@test isapprox(abs(α), 3, atol = 1e-5)
δρ = remove_coherence(ρ)
@test abs2(δρ[1, 1]) > 0.999

@testset "Type Inference (get_coherence)" begin
@inferred get_coherence(ψ)
@inferred get_coherence(ρ)
@testset "Type Inference (remove_coherence)" begin
@inferred remove_coherence(ψ1)
@inferred remove_coherence(ρ1)
@inferred remove_coherence(ψ)
@inferred remove_coherence(ρ)
end
@@ -497,17 +570,24 @@

@test mean_occupation(ψc) ≈ Ns
@test mean_occupation(ρc) ≈ Ns
@test mean_occupation(vc) ≈ Ns
@test prod(mean_occupation(ψc)) ≈ Nc
@test prod(mean_occupation(ρc)) ≈ Nc
@test prod(mean_occupation(vc)) ≈ Nc
@test mean_occupation(ψc, idx = 1) ≈ N1
@test mean_occupation(ρc, idx = 1) ≈ N1
@test mean_occupation(ψc, idx = 2) ≈ N2
@test mean_occupation(ρc, idx = 2) ≈ N2
@test mean_occupation(vc, idx = 1) ≈ N1
@test mean_occupation(vc, idx = 2) ≈ N2

@testset "Type Inference (mean_occupation)" begin
@inferred mean_occupation(ψ1)
@inferred mean_occupation(ρ1)
@inferred mean_occupation(v1)
@inferred mean_occupation(ψc)
@inferred mean_occupation(ρc)
@inferred mean_occupation(vc)
end
end

Loading