Skip to content

Commit

Permalink
Add doctest in all docstrings (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
ytdHuang authored Nov 11, 2024
2 parents 0f5a9b1 + c21855b commit ca872f6
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 214 deletions.
2 changes: 1 addition & 1 deletion src/linear_maps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ It is typically represented as a matrix with dimensions given by `size`, and thi
As an example, we now define the linear map used in the [`eigsolve_al`](@ref) function for Arnoldi-Lindblad eigenvalue solver:
```julia-repl
```julia
struct ArnoldiLindbladIntegratorMap{T,TS,TI} <: AbstractLinearMap{T,TS}
elty::Type{T}
size::TS
Expand Down
4 changes: 2 additions & 2 deletions src/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ matrix ``\hat{\rho}``.
# Examples
Pure state:
```
```jldoctest
julia> ψ = fock(2,0)
Quantum Object: type=Ket dims=[2] size=(2,)
2-element Vector{ComplexF64}:
Expand All @@ -37,7 +37,7 @@ julia> entropy_vn(ρ, base=2)
```
Mixed state:
```
```jldoctest
julia> ρ = maximally_mixed_dm(2)
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true
2×2 Diagonal{ComplexF64, Vector{ComplexF64}}:
Expand Down
6 changes: 3 additions & 3 deletions src/negativity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ and ``\Vert \hat{X} \Vert_1=\textrm{Tr}\sqrt{\hat{X}^\dagger \hat{X}}`` is the t
# Examples
```
```jldoctest
julia> Ψ = bell_state(0, 0)
Quantum Object: type=Ket dims=[2, 2] size=(4,)
4-element Vector{ComplexF64}:
Expand All @@ -34,8 +34,8 @@ Quantum Object: type=Operator dims=[2, 2] size=(4, 4) ishermitian=true
0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im
0.5+0.0im 0.0+0.0im 0.0+0.0im 0.5+0.0im
julia> negativity(ρ, 2)
0.4999999999999998
julia> round(negativity(ρ, 2), digits=2)
0.5
```
"""
function negativity::QuantumObject, subsys::Int; logarithmic::Bool = false)
Expand Down
35 changes: 20 additions & 15 deletions src/qobj/arithmetic_and_attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,15 @@ Note that this function only supports for [`Operator`](@ref) and [`SuperOperator
# Examples
```
```jldoctest
julia> a = destroy(20)
Quantum Object: type=Operator dims=[20] size=(20, 20) ishermitian=false
20×20 SparseMatrixCSC{ComplexF64, Int64} with 19 stored entries:
⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠈⠢⡀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠈⠢⡀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠈⠢⡀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠈⠢
⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠈⠢⡀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠈⠢⡀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠈⠢⡀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠈⠢
julia> tr(a' * a)
190.0 + 0.0im
Expand Down Expand Up @@ -257,7 +257,7 @@ Return the standard vector `p`-norm or [Schatten](https://en.wikipedia.org/wiki/
# Examples
```
```jldoctest
julia> ψ = fock(10, 2)
Quantum Object: type=Ket dims=[10] size=(10,)
10-element Vector{ComplexF64}:
Expand Down Expand Up @@ -474,8 +474,9 @@ proj(ψ::QuantumObject{<:AbstractArray{T},BraQuantumObject}) where {T} = ψ' *
Note that this function will always return [`Operator`](@ref). No matter the input [`QuantumObject`](@ref) is a [`Ket`](@ref), [`Bra`](@ref), or [`Operator`](@ref).
# Examples
Two qubits in the state ``\ket{\psi} = \ket{e,g}``:
```
```jldoctest
julia> ψ = kron(fock(2,0), fock(2,1))
Quantum Object: type=Ket dims=[2, 2] size=(4,)
4-element Vector{ComplexF64}:
Expand All @@ -492,7 +493,7 @@ Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true
```
or in an entangled state ``\ket{\psi} = \frac{1}{\sqrt{2}} \left( \ket{e,e} + \ket{g,g} \right)``:
```
```jldoctest
julia> ψ = 1 / √2 * (kron(fock(2,0), fock(2,0)) + kron(fock(2,1), fock(2,1)))
Quantum Object: type=Ket dims=[2, 2] size=(4,)
4-element Vector{ComplexF64}:
Expand Down Expand Up @@ -693,12 +694,16 @@ Note that this method currently works for [`Ket`](@ref), [`Bra`](@ref), and [`Op
If `order = [2, 1, 3]`, the Hilbert space structure will be re-arranged: ``\mathcal{H}_1 \otimes \mathcal{H}_2 \otimes \mathcal{H}_3 \rightarrow \mathcal{H}_2 \otimes \mathcal{H}_1 \otimes \mathcal{H}_3``.
```
julia> ψ1 = fock(2, 0)
julia> ψ2 = fock(3, 1)
julia> ψ3 = fock(4, 2)
julia> ψ_123 = tensor(ψ1, ψ2, ψ3)
julia> permute(ψ_123, [2, 1, 3]) ≈ tensor(ψ2, ψ1, ψ3)
```jldoctest
julia> ψ1 = fock(2, 0);
julia> ψ2 = fock(3, 1);
julia> ψ3 = fock(4, 2);
julia> ψ_123 = tensor(ψ1, ψ2, ψ3);
julia> permute(ψ_123, (2, 1, 3)) ≈ tensor(ψ2, ψ1, ψ3)
true
```
Expand Down
52 changes: 30 additions & 22 deletions src/qobj/eigsolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,37 @@ A struct containing the eigenvalues, the eigenvectors, and some information from
# Examples
One can obtain the eigenvalues and the corresponding [`QuantumObject`](@ref)-type eigenvectors by:
```
julia> result = eigenstates(sigmax());
```jldoctest
julia> result = eigenstates(sigmax())
EigsolveResult: type=Operator dims=[2]
values:
2-element Vector{ComplexF64}:
-1.0 + 0.0im
1.0 + 0.0im
vectors:
2×2 Matrix{ComplexF64}:
-0.707107+0.0im 0.707107+0.0im
0.707107+0.0im 0.707107+0.0im
julia> λ, ψ, T = result;
julia> λ, ψ, U = result;
julia> λ
2-element Vector{ComplexF64}:
-1.0 + 0.0im
1.0 + 0.0im
julia> ψ
2-element Vector{QuantumObject{Vector{ComplexF64}, KetQuantumObject}}:
QuantumObject{Vector{ComplexF64}, KetQuantumObject}(ComplexF64[-0.7071067811865475 + 0.0im, 0.7071067811865475 + 0.0im], KetQuantumObject(), [2])
QuantumObject{Vector{ComplexF64}, KetQuantumObject}(ComplexF64[0.7071067811865475 + 0.0im, 0.7071067811865475 + 0.0im], KetQuantumObject(), [2])
2-element Vector{QuantumObject{Vector{ComplexF64}, KetQuantumObject, 1}}:
Quantum Object: type=Ket dims=[2] size=(2,)
2-element Vector{ComplexF64}:
-0.7071067811865475 + 0.0im
0.7071067811865475 + 0.0im
Quantum Object: type=Ket dims=[2] size=(2,)
2-element Vector{ComplexF64}:
0.7071067811865475 + 0.0im
0.7071067811865475 + 0.0im
julia> T
julia> U
2×2 Matrix{ComplexF64}:
-0.707107+0.0im 0.707107+0.0im
0.707107+0.0im 0.707107+0.0im
Expand Down Expand Up @@ -411,27 +426,20 @@ end
Calculates the eigenvalues and eigenvectors of the [`QuantumObject`](@ref) `A` using
the Julia [LinearAlgebra](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/) package.
```
```jldoctest
julia> a = destroy(5);
julia> H = a + a'
Quantum Object: type=Operator dims=[5] size=(5, 5) ishermitian=true
5×5 SparseMatrixCSC{ComplexF64, Int64} with 8 stored entries:
⋅ 1.0+0.0im ⋅ ⋅ ⋅
1.0+0.0im ⋅ 1.41421+0.0im ⋅ ⋅
⋅ 1.41421+0.0im ⋅ 1.73205+0.0im ⋅
⋅ ⋅ 1.73205+0.0im ⋅ 2.0+0.0im
⋅ ⋅ ⋅ 2.0+0.0im ⋅
julia> H = a + a';
julia> E, ψ, U = eigen(H)
EigsolveResult: type=Operator dims=[5]
values:
5-element Vector{Float64}:
-2.8569700138728
-1.3556261799742608
1.3322676295501878e-15
1.3556261799742677
2.8569700138728056
5-element Vector{ComplexF64}:
-2.8569700138728 + 0.0im
-1.3556261799742608 + 0.0im
1.3322676295501878e-15 + 0.0im
1.3556261799742677 + 0.0im
2.8569700138728056 + 0.0im
vectors:
5×5 Matrix{ComplexF64}:
0.106101+0.0im -0.471249-0.0im … 0.471249-0.0im 0.106101-0.0im
Expand Down
47 changes: 15 additions & 32 deletions src/qobj/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Note that `ψ` can also be given as a list of [`QuantumObject`](@ref), it return
# Examples
```
```jldoctest
julia> ψ = 1 / √2 * (fock(10,2) + fock(10,4));
julia> a = destroy(10);
Expand Down Expand Up @@ -153,40 +153,23 @@ Returns the [Kronecker product](https://en.wikipedia.org/wiki/Kronecker_product)
# Examples
```
```jldoctest
julia> a = destroy(20)
Quantum Object: type=Operator dims=[20] size=(20, 20) ishermitian=false
20×20 SparseMatrixCSC{ComplexF64, Int64} with 19 stored entries:
⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠈⠢⡀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠈⠢⡀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠈⠢⡀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠈⠢
julia> kron(a, a)
Quantum Object: type=Operator dims=[20, 20] size=(400, 400) ishermitian=false
400×400 SparseMatrixCSC{ComplexF64, Int64} with 361 stored entries:
⠀⠀⠘⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠦
⎡⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀⎤
⎢⠀⠀⠈⠢⡀⠀⠀⠀⠀⠀⎥
⎢⠀⠀⠀⠀⠈⠢⡀⠀⠀⠀⎥
⎢⠀⠀⠀⠀⠀⠀⠈⠢⡀⠀⎥
⎣⠀⠀⠀⠀⠀⠀⠀⠀⠈⠢⎦
julia> O = kron(a, a);
julia> size(a), size(O)
((20, 20), (400, 400))
julia> a.dims, O.dims
([20], [20, 20])
```
"""
function LinearAlgebra.kron(
Expand Down
8 changes: 4 additions & 4 deletions src/qobj/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ This operator acts on a fock state as ``\hat{a} \ket{n} = \sqrt{n} \ket{n-1}``.
# Examples
```
```jldoctest
julia> a = destroy(20)
Quantum Object: type=Operator dims=[20] size=(20, 20) ishermitian=false
20×20 SparseMatrixCSC{ComplexF64, Int64} with 19 stored entries:
Expand All @@ -113,7 +113,7 @@ This operator acts on a fock state as ``\hat{a}^\dagger \ket{n} = \sqrt{n+1} \ke
# Examples
```
```jldoctest
julia> a_d = create(20)
Quantum Object: type=Operator dims=[20] size=(20, 20) ishermitian=false
20×20 SparseMatrixCSC{ComplexF64, Int64} with 19 stored entries:
Expand Down Expand Up @@ -242,14 +242,14 @@ The parameter `which` specifies which of the following operator to return.
Note that if the parameter `which` is not specified, returns a set of Spin-`j` operators: ``(\hat{S}_x, \hat{S}_y, \hat{S}_z)``
# Examples
```
```jldoctest
julia> jmat(0.5, :x)
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true
2×2 SparseMatrixCSC{ComplexF64, Int64} with 2 stored entries:
⋅ 0.5+0.0im
0.5+0.0im ⋅
julia> jmat(0.5, :-)
julia> jmat(0.5, Val(:-))
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=false
2×2 SparseMatrixCSC{ComplexF64, Int64} with 1 stored entry:
⋅ ⋅
Expand Down
2 changes: 1 addition & 1 deletion src/qobj/quantum_object.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Julia struct representing any quantum objects.
# Examples
```
```jldoctest
julia> a = destroy(20)
Quantum Object: type=Operator dims=[20] size=(20, 20) ishermitian=false
20×20 SparseMatrixCSC{ComplexF64, Int64} with 19 stored entries:
Expand Down
2 changes: 1 addition & 1 deletion src/qobj/quantum_object_base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export Bra, Ket, Operator, OperatorBra, OperatorKet, SuperOperator
Abstract type for all quantum objects like [`QuantumObject`](@ref) and [`QuantumObjectEvolution`](@ref).
# Example
```
```jldoctest
julia> sigmax() isa AbstractQuantumObject
true
```
Expand Down
Loading

0 comments on commit ca872f6

Please sign in to comment.