Skip to content

Commit

Permalink
add option to normalize identity matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
thchr committed Feb 21, 2022
1 parent 27dab96 commit 5dba575
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
19 changes: 11 additions & 8 deletions src/GellMannMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ function offdiag_gellmann(T::Type{<:AbstractMatrix}, i, j, d)
end

# "diagonal"-like matrices (h_i on Wiki)
function diag_gellmann(T::Type{<:AbstractMatrix}, i, d)
function diag_gellmann(T::Type{<:AbstractMatrix}, i, d; norm_identity::Bool=false)
if i == 1
T(I(d))
return norm_identity ? T(I(d) / sqrt(d/2)) : T(I(d))
elseif i < d
diag_gellmann(T, i, d-1) zero(eltype(T))
return diag_gellmann(T, i, d-1) zero(eltype(T))
elseif i == d
sqrt(2/(d*(d-1))) * (T(I(d-1)) convert(eltype(T), (1-d)))
return sqrt(2/(d*(d-1))) * (T(I(d-1)) convert(eltype(T), (1-d)))
else
throw(DomainError((i,d), "invalid combination of (i,d); must have i ≤ d and d ≥ 1"))
end
Expand All @@ -55,8 +55,10 @@ Return the generalized Gell-Mann matrices in `d` dimensions as a vector of matri
point element type.
## Keyword arguments
- `skip_identity` (default, `false`): if `true`, the identity matrix of dimension `d` is
included as the last element of the returned vector.
- `skip_identity` (default, `true`): if `false`, the identity matrix of dimension `d` is
included as the last element of the returned vector.
- `norm_identity` (default, `false`): if `true`, the elements of the identity are normalized
by `sqrt(D/2)` to ensure the the orthogonality relation `tr(A'*A) = 2`.
## Examples
Compute the Pauli matrices ``σ_i`` (`d=2`) and Gell-Mann matrices ``Λ_i`` (`d=3`):
Expand All @@ -67,7 +69,8 @@ julia> Λᵢ = gellmann(3)
"""
gellmann(d::Integer; kwargs...) = gellmann(Matrix{ComplexF64}, d; kwargs...)

function gellmann(T::Type{<:AbstractMatrix}, d::Integer; skip_identity=true)
function gellmann(T::Type{<:AbstractMatrix}, d::Integer;
skip_identity=true, norm_identity=false)
d > 0 || throw(DomainError(d, "dimension must be a positive integer"))
eltype(T) == complex(eltype(T)) || throw(DomainError(T, "the element type of T must be complex"))
# we collect the matrices in the slightly odd-looking manner below in order to be
Expand All @@ -81,7 +84,7 @@ function gellmann(T::Type{<:AbstractMatrix}, d::Integer; skip_identity=true)
end
ms[k+=1] = diag_gellmann(T, i, d)
end
skip_identity || (ms[k+=1] = diag_gellmann(T, 1, d))
skip_identity || (ms[k+=1] = diag_gellmann(T, 1, d; norm_identity))
return ms
end

Expand Down
6 changes: 4 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ using Test

# hermicity, tracelessness, & orthogonality
for d in 1:3
ms = gellmann(d)
ms = gellmann(d, skip_identity=false, norm_identity=true)
for (i,mᵢ) in enumerate(ms)
@test mᵢ mᵢ'
@test norm(tr(mᵢ)) < 1e-12
if i d*d
@test norm(tr(mᵢ)) < 1e-12
end
for (j,mⱼ) in enumerate(ms)
if i == j
@test abs(tr(mᵢ'*mᵢ)) 2 # Frobenius matrix norm
Expand Down

2 comments on commit 5dba575

@thchr
Copy link
Owner Author

@thchr thchr commented on 5dba575 Feb 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/55220

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" 5dba575b256440b310e49fe3b75cf03303520576
git push origin v0.1.1

Please sign in to comment.