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

Introduce variance #187

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ eigsolve_al
```@docs
ket2dm
expect
variance
LinearAlgebra.kron
sparse_to_dense
dense_to_sparse
Expand Down
27 changes: 21 additions & 6 deletions src/qobj/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=#

export ket2dm
export expect
export expect, variance
export sparse_to_dense, dense_to_sparse
export vec2mat, mat2vec

Expand All @@ -19,12 +19,13 @@
@doc raw"""
expect(O::QuantumObject, ψ::QuantumObject)

Expectation value of the operator `O` with the state `ψ`. The latter
can be a [`KetQuantumObject`](@ref), [`BraQuantumObject`](@ref) or a [`OperatorQuantumObject`](@ref).
If `ψ` is a density matrix, the function calculates ``\Tr \left[ \hat{O} \hat{\psi} \right]``, while if `ψ`
is a state, the function calculates ``\mel{\psi}{\hat{O}}{\psi}``.
Expectation value of the [`Operator`](@ref) `O` with the state `ψ`. The state can be a [`Ket`](@ref), [`Bra`](@ref) or [`Operator`](@ref).

The function returns a real number if the operator is hermitian, and returns a complex number otherwise.
If `ψ` is a [`Ket`](@ref) or [`Bra`](@ref), the function calculates ``\langle\psi|\hat{O}|\psi\rangle``.

If `ψ` is a density matrix ([`Operator`](@ref)), the function calculates ``\textrm{Tr} \left[ \hat{O} \hat{\psi} \right]``

The function returns a real number if `O` is hermitian, and returns a complex number otherwise.

# Examples

Expand Down Expand Up @@ -60,6 +61,20 @@
return ishermitian(O) ? real(tr(O * ρ)) : tr(O * ρ)
end

@doc raw"""
variance(O::QuantumObject, ψ::QuantumObject)

Variance of the [`Operator`](@ref) `O`: ``\langle\hat{O}^2\rangle - \langle\hat{O}\rangle^2``,

where ``\langle\hat{O}\rangle`` is the expectation value of `O` with the state `ψ` (see also [`expect`](@ref)), and the state `ψ` can be a [`Ket`](@ref), [`Bra`](@ref) or [`Operator`](@ref).

The function returns a real number if `O` is hermitian, and returns a complex number otherwise.
"""
variance(

Check warning on line 73 in src/qobj/functions.jl

View check run for this annotation

Codecov / codecov/patch

src/qobj/functions.jl#L73

Added line #L73 was not covered by tests
O::QuantumObject{<:AbstractArray{T1},OperatorQuantumObject},
ψ::QuantumObject{<:AbstractArray{T2}},
) where {T1,T2} = expect(O^2, ψ) - expect(O, ψ)^2

@doc raw"""
sparse_to_dense(A::QuantumObject)

Expand Down
7 changes: 5 additions & 2 deletions test/quantum_objects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
@test opstring == "Quantum Object: type=OperatorBra dims=$ψ2_dims size=$ψ2_size\n$datastring"
end

@testset "Matrix Element" begin
@testset "matrix element" begin
H = Qobj([1 2; 3 4])
L = liouvillian(H)
s0 = Qobj(basis(4, 0).data; type = OperatorKet)
Expand Down Expand Up @@ -321,10 +321,13 @@
end

@testset "expectation value" begin
# expect and variance
N = 10
a = destroy(N)
ψ = normalize(fock(N, 3) + 1im * fock(N, 4))
ψ = rand_ket(N)
@test expect(a, ψ) ≈ expect(a, ψ')
@test variance(a, ψ) ≈ expect(a^2, ψ) - expect(a, ψ)^2

ψ = fock(N, 3)
@test norm(ψ' * a) ≈ 2
@test expect(a' * a, ψ' * a) ≈ 16
Expand Down