Skip to content

Commit

Permalink
Merge 84b0cdc into 014c86b
Browse files Browse the repository at this point in the history
  • Loading branch information
bastikr committed Jan 11, 2017
2 parents 014c86b + 84b0cdc commit 427a067
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ Fock
.. jl:autofunction:: fock.jl qfunc
.. _section-api-nlevel:

N-level
^^^^^^^

.. jl:autotype:: nlevel.jl NLevelBasis
.. jl:autofunction:: nlevel.jl transition
.. jl:autofunction:: nlevel.jl nlevelstate
.. _section-api-spin:

Spin
Expand Down
1 change: 1 addition & 0 deletions docs/source/bases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The primary purpose of bases in **QuantumOptics.jl** is to specify the dimension

* :ref:`Spin basis <section-spin>`
* :ref:`Fock basis <section-fock>`
* :ref:`N-level basis <section-nlevel>`
* :ref:`Position basis and Momentum basis <section-particle>`
* :ref:`N-particle basis <section-nparticles>`

Expand Down
25 changes: 25 additions & 0 deletions docs/source/nlevel.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.. _section-nlevel:

N-level basis
=============

Systems consisting of discrete states like e.g. an atom represented by a few relevant levels can be treated using the :jl:type:`NLevelBasis`. The only thing it needs to know is the number of states::

N = 3
b = NLevelBasis(N)

Essentially it is defined just as::

type NLevelBasis <: Basis
shape::Vector{Int}
N::Int
end

Defined operators:

* :jl:func:`transition`


States can be created with

* :jl:func:`nlevelstate`
1 change: 1 addition & 0 deletions docs/source/quantumsystems.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ Quantum systems in **QuantumOptics.jl** primarily define one or more appropriate

spin
fock
nlevel
particle
nparticles
3 changes: 3 additions & 0 deletions src/QuantumOptics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export bases, Basis, GenericBasis, CompositeBasis,
subspace, SubspaceBasis, projector,
particle, PositionBasis, MomentumBasis, gaussianstate,
positionoperator, momentumoperator, laplace_x, laplace_p,
nlevel, NLevelBasis, transition, nlevelstate,
nparticlebasis, BosonicNParticleBasis, FermionicNParticleBasis, nparticleoperator_1, nparticleoperator_2,
metrics, tracedistance,
spectralanalysis, operatorspectrum, operatorspectrum_hermitian,
Expand All @@ -42,6 +43,7 @@ include("spin.jl")
include("fock.jl")
include("subspace.jl")
include("particle.jl")
include("nlevel.jl")
include("nparticles.jl")
include("metrics.jl")
include("ode_dopri.jl")
Expand Down Expand Up @@ -71,6 +73,7 @@ using .spin
using .fock
using .subspace
using .particle
using .nlevel
using .nparticles
using .metrics
using .spectralanalysis
Expand Down
68 changes: 68 additions & 0 deletions src/nlevel.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module nlevel

import Base.==

using ..bases, ..states, ..operators, ..operators_sparse

export NLevelBasis, transition, nlevelstate

"""
Basis for a system consisting of N states.
Arguments
---------
N
Number of states.
"""
type NLevelBasis <: Basis
shape::Vector{Int}
N::Int
function NLevelBasis(N::Int)
if N < 1
throw(DimensionMismatch())
end
new([N], N)
end
end

==(b1::NLevelBasis, b2::NLevelBasis) = b1.N == b2.N


"""
Transition operator :math:`|n><m|`.
Arguments
---------
b
NLevelBasis
to
Index of ket state in :math:`|to><from|`
from
Index of bra state in :math:`|to><from|`
"""
function transition(b::NLevelBasis, to::Int, from::Int)
if to < 1 || b.N < to
throw(BoundsError("'to' index has to be between 1 and b.N"))
end
if from < 1 || b.N < from
throw(BoundsError("'from' index has to be between 1 and b.N"))
end
op = SparseOperator(b)
op.data[to, from] = 1.
op
end


"""
State where the system is completely in the n-th level.
"""
function nlevelstate(b::NLevelBasis, n::Int)
if n < 1 || b.N < n
throw(BoundsError("n has to be between 1 and b.N"))
end
basis_ket(b, n)
end

end # module
24 changes: 24 additions & 0 deletions test/test_nlevel.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Base.Test
using QuantumOptics


N = 3
b = NLevelBasis(N)

# Test basis equality
@test b == NLevelBasis(N)
@test b != NLevelBasis(N+1)

# Test transition operator
@test_throws BoundsError transition(b, 0, 1)
@test_throws BoundsError transition(b, N+1, 1)
@test_throws BoundsError transition(b, 1, 0)
@test_throws BoundsError transition(b, 1, N+1)
@test full(transition(b, 2, 1)) == basis_ket(b, 2) basis_bra(b, 1)

# Test nlevel states
@test_throws BoundsError nlevelstate(b, 0)
@test_throws BoundsError nlevelstate(b, N+1)
@test norm(nlevelstate(b, 1)) == 1.
@test norm(dagger(nlevelstate(b, 1))*nlevelstate(b, 2)) == 0.
@test norm(dagger(nlevelstate(b, 1))*transition(b, 1, 2)*nlevelstate(b, 2)) == 1.

0 comments on commit 427a067

Please sign in to comment.