Skip to content

Commit

Permalink
Merge pull request #157 from ytdHuang/fix/states
Browse files Browse the repository at this point in the history
Fix incorrect inputs for functions that generate states
  • Loading branch information
ytdHuang authored Jun 4, 2024
2 parents c6e8b56 + 0825a97 commit 219c907
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/qobj/states.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,49 @@ export rand_dm
@doc raw"""
fock(N::Int, pos::Int=0; dims::Vector{Int}=[N], sparse::Bool=false)
Generates a fock state ``\ket{\psi}`` of dimension `N`. It is also possible
to specify the list of dimensions `dims` if different subsystems are present.
Generates a fock state ``\ket{\psi}`` of dimension `N`.
It is also possible to specify the list of dimensions `dims` if different subsystems are present.
"""
function fock(N::Int, pos::Int = 0; dims::Vector{Int} = [N], sparse::Bool = false)
if sparse
return QuantumObject(sparsevec([pos + 1], [1.0 + 0im], N), Ket, dims)
array = sparsevec([pos + 1], [1.0 + 0im], N)
else
array = zeros(ComplexF64, N)
array[pos+1] = 1
return QuantumObject(array, Ket, dims)
end
return QuantumObject(array; type = Ket, dims = dims)
end

"""
@doc raw"""
basis(N::Int, pos::Int = 0; dims::Vector{Int}=[N])
Generates a fock state like [`fock`](@ref).
It is also possible to specify the list of dimensions `dims` if different subsystems are present.
"""
basis(N::Int, pos::Int = 0; dims::Vector{Int} = [N]) = fock(N, pos, dims = dims)

@doc raw"""
coherent(N::Real, α::T)
coherent(N::Real, α::Number)
Generates a coherent state ``\ket{\alpha}``, which is defined as an eigenvector of the
bosonic annihilation operator ``\hat{a} \ket{\alpha} = \alpha \ket{\alpha}``.
Generates a coherent state ``\ket{\alpha}``, which is defined as an eigenvector of the bosonic annihilation operator ``\hat{a} \ket{\alpha} = \alpha \ket{\alpha}``.
"""
function coherent(N::Real, α::T) where {T<:Number}
a = destroy(N)
return exp* a' - α' * a) * fock(N, 0)
end

@doc raw"""
rand_dm(N::Integer; kwargs...)
rand_dm(N::Integer; dims::Vector{Int}=[N])
Generates a random density matrix ``\hat{\rho}``, with the property to be positive semi-definite and ``\textrm{Tr} \left[ \hat{\rho} \right] = 1``.
Generates a random density matrix ``\hat{\rho}``, with the property to be positive definite,
and that ``\Tr \left[ \hat{\rho} \right] = 1``.
It is also possible to specify the list of dimensions `dims` if different subsystems are present.
"""
function rand_dm(N::Integer; kwargs...)
function rand_dm(N::Integer; dims::Vector{Int} = [N])
ρ = rand(ComplexF64, N, N)
ρ *= ρ'
ρ /= tr(ρ)
return QuantumObject(ρ; kwargs...)
return QuantumObject(ρ; type = Operator, dims = dims)
end
19 changes: 19 additions & 0 deletions test/states_and_operators.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
@testset "States and Operators" begin
# fock, basis
@test fock(4; dims = [2, 2], sparse = true) basis(4; dims = [2, 2])
@test_throws DimensionMismatch fock(4; dims = [2])

# rand_dm
ρ_AB = rand_dm(4, dims = [2, 2])
ρ_A = ptrace(ρ_AB, 1)
ρ_B = ptrace(ρ_AB, 2)
@test tr(ρ_AB) 1.0
@test tr(ρ_A) 1.0
@test tr(ρ_B) 1.0
@test ishermitian(ρ_AB) == true
@test ishermitian(ρ_A) == true
@test ishermitian(ρ_B) == true
@test all(eigenenergies(ρ_AB) .>= 0)
@test all(eigenenergies(ρ_A) .>= 0)
@test all(eigenenergies(ρ_B) .>= 0)
@test_throws DimensionMismatch rand_dm(4, dims = [2])

# test commutation relations for fermionic creation and annihilation operators
sites = 4
SIZE = 2^sites
Expand Down

0 comments on commit 219c907

Please sign in to comment.