Skip to content

Commit 871a29a

Browse files
committed
extended methods for expect and variance
1 parent 842e668 commit 871a29a

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/qobj/functions.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ket2dm(ψ::QuantumObject{<:AbstractArray{T},KetQuantumObject}) where {T} = ψ *
1717
ket2dm::QuantumObject{<:AbstractArray{T},OperatorQuantumObject}) where {T} = ρ
1818

1919
@doc raw"""
20-
expect(O::QuantumObject, ψ::QuantumObject)
20+
expect(O::QuantumObject, ψ::Union{QuantumObject,Vector{QuantumObject}})
2121
2222
Expectation value of the [`Operator`](@ref) `O` with the state `ψ`. The state can be a [`Ket`](@ref), [`Bra`](@ref) or [`Operator`](@ref).
2323
@@ -27,6 +27,8 @@ If `ψ` is a density matrix ([`Operator`](@ref)), the function calculates ``\tex
2727
2828
The function returns a real number if `O` is of `Hermitian` type or `Symmetric` type, and returns a complex number otherwise. You can make an operator `O` hermitian by using `Hermitian(O)`.
2929
30+
Note that `ψ` can also be given as a list of [`QuantumObject`](@ref), it returns a list of expectation values.
31+
3032
# Examples
3133
3234
```
@@ -77,20 +79,33 @@ function expect(
7779
) where {TF<:Number,TR<:Real,T2}
7880
return real(tr(O * ρ))
7981
end
82+
function expect(
83+
O::QuantumObject{<:AbstractArray{T1},OperatorQuantumObject},
84+
ρ::Vector{<:QuantumObject},
85+
) where {T1}
86+
_expect =-> expect(O, _ρ)
87+
return _expect.(ρ)
88+
end
8089

8190
@doc raw"""
82-
variance(O::QuantumObject, ψ::QuantumObject)
91+
variance(O::QuantumObject, ψ::Union{QuantumObject,Vector{QuantumObject}})
8392
8493
Variance of the [`Operator`](@ref) `O`: ``\langle\hat{O}^2\rangle - \langle\hat{O}\rangle^2``,
8594
8695
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).
8796
8897
The function returns a real number if `O` is hermitian, and returns a complex number otherwise.
98+
99+
Note that `ψ` can also be given as a list of [`QuantumObject`](@ref), it returns a list of expectation values.
89100
"""
90101
variance(
91102
O::QuantumObject{<:AbstractArray{T1},OperatorQuantumObject},
92103
ψ::QuantumObject{<:AbstractArray{T2}},
93104
) where {T1,T2} = expect(O^2, ψ) - expect(O, ψ)^2
105+
variance(
106+
O::QuantumObject{<:AbstractArray{T1},OperatorQuantumObject},
107+
ψ::Vector{<:QuantumObject},
108+
) where {T1} = expect(O^2, ψ) .- expect(O, ψ).^2
94109

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

src/qobj/superoperators.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Since the density matrix is vectorized in [`OperatorKet`](@ref) form: ``|\hat{\r
2525
```math
2626
\mathcal{O} \left(\hat{A}\right) \left[ \hat{\rho} \right] = \hat{\mathbb{1}} \otimes \hat{A} ~ |\hat{\rho}\rangle\rangle
2727
```
28+
(see the section in documentation: [Superoperators and Vectorized Operators](@ref doc:Superoperators-and-Vectorized-Operators) for more details)
2829
2930
The optional argument `Id_cache` can be used to pass a precomputed identity matrix. This can be useful when
3031
the same function is applied multiple times with a known Hilbert space dimension.
@@ -42,6 +43,7 @@ Since the density matrix is vectorized in [`OperatorKet`](@ref) form: ``|\hat{\r
4243
```math
4344
\mathcal{O} \left(\hat{B}\right) \left[ \hat{\rho} \right] = \hat{B}^T \otimes \hat{\mathbb{1}} ~ |\hat{\rho}\rangle\rangle
4445
```
46+
(see the section in documentation: [Superoperators and Vectorized Operators](@ref doc:Superoperators-and-Vectorized-Operators) for more details)
4547
4648
The optional argument `Id_cache` can be used to pass a precomputed identity matrix. This can be useful when
4749
the same function is applied multiple times with a known Hilbert space dimension.
@@ -59,6 +61,7 @@ Since the density matrix is vectorized in [`OperatorKet`](@ref) form: ``|\hat{\r
5961
```math
6062
\mathcal{O} \left(\hat{A}, \hat{B}\right) \left[ \hat{\rho} \right] = \hat{B}^T \otimes \hat{A} ~ |\hat{\rho}\rangle\rangle = \textrm{spre}(A) * \textrm{spost}(B) ~ |\hat{\rho}\rangle\rangle
6163
```
64+
(see the section in documentation: [Superoperators and Vectorized Operators](@ref doc:Superoperators-and-Vectorized-Operators) for more details)
6265
6366
See also [`spre`](@ref) and [`spost`](@ref).
6467
"""

test/quantum_objects.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,20 @@
384384
@test expect(a, ρ) tr(a * ρ)
385385
@test variance(a, ρ) tr(a^2 * ρ) - tr(a * ρ)^2
386386

387+
# when input is a vector of states
388+
xlist = [1.0, 1.0im, -1.0, -1.0im]
389+
ψlist = [normalize!(basis(N, 4) + x * basis(N, 3)) for x in xlist]
390+
@test all(expect(a', ψlist) .≈ xlist)
391+
387392
@testset "Type Inference (expect)" begin
388393
@inferred expect(a, ψ)
389394
@inferred expect(a, ψ')
390395
@inferred variance(a, ψ)
391396
@inferred variance(a, ψ')
392397
@inferred expect(a, ρ)
393398
@inferred variance(a, ρ)
399+
@inferred expect(a, ψlist)
400+
@inferred variance(a, ψlist)
394401
end
395402
end
396403

0 commit comments

Comments
 (0)