Skip to content

Commit

Permalink
Remove FermionNumber, FermionSpin
Browse files Browse the repository at this point in the history
Refactors Fermion to always mean FermionParity
  • Loading branch information
lkdvos committed May 18, 2023
1 parent 820f092 commit 4ceba8d
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 241 deletions.
93 changes: 36 additions & 57 deletions src/sectors/fermions.jl
Original file line number Diff line number Diff line change
@@ -1,92 +1,71 @@
struct Fermion{P,I<:Sector} <: Sector
sector::I
function Fermion{P,I}(sector::I) where {P, I<:Sector}
@assert BraidingStyle(I) isa Bosonic
return new{P,I}(sector)
end
struct FermionParity <: Sector
sector::Z2Irrep
end
Fermion{P}(sector::I) where {P, I<:Sector} = Fermion{P,I}(sector)
Fermion{P,I}(sector) where {P, I<:Sector} = Fermion{P,I}(convert(I, sector))
Base.convert(::Type{Fermion{P,I}}, a::Fermion{P,I}) where {P, I<:Sector} = a
Base.convert(::Type{Fermion{P,I}}, a) where {P, I<:Sector} = Fermion{P,I}(convert(I, a))
const fℤ₂ = FermionParity
fermionparity(f::FermionParity) = isodd(f.sector.n)

fermionparity(f::Fermion{P}) where P = P(f.sector)
Base.convert(::Type{FermionParity}, a::FermionParity) = a
Base.convert(::Type{FermionParity}, a) = FermionParity(a)

Base.IteratorSize(::Type{SectorValues{Fermion{P,I}}}) where {P, I<:Sector} =
Base.IteratorSize(SectorValues{I})
Base.length(::SectorValues{Fermion{P,I}}) where {P, I<:Sector} = length(values(I))
function Base.iterate(::SectorValues{Fermion{P, I}}) where {P, I<:Sector}
next = iterate(values(I))
Base.IteratorSize(::Type{SectorValues{FermionParity}}) =
Base.IteratorSize(SectorValues{Z2Irrep})
Base.length(::SectorValues{FermionParity}) = length(values(Z2Irrep))
function Base.iterate(::SectorValues{FermionParity})
next = iterate(values(Z2Irrep))
@assert next !== nothing
value, state = next
return Fermion{P}(value), state
return FermionParity(value), state
end
function Base.iterate(::SectorValues{Fermion{P, I}}, state) where {P, I<:Sector}
next = iterate(values(I), state)

function Base.iterate(::SectorValues{FermionParity}, state)
next = iterate(values(Z2Irrep), state)
if next === nothing
return nothing
else
value, state = next
return Fermion{P}(value), state
return FermionParity(value), state
end
end
Base.getindex(::SectorValues{Fermion{P, I}}, i) where {P, I<:Sector} =
Fermion{P}(values(I)[i])
findindex(::SectorValues{Fermion{P, I}}, f::Fermion{P, I}) where {P, I<:Sector} =
findindex(values(I), f.sector)
Base.getindex(::SectorValues{FermionParity}, i) = FermionParity(values(Z2Irrep)[i])
findindex(::SectorValues{FermionParity}, f::FermionParity) = findindex(values(Z2Irrep), f.sector)

Base.one(::Type{Fermion{P, I}}) where {P, I<:Sector} = Fermion{P}(one(I))
Base.conj(f::Fermion{P}) where {P} = Fermion{P}(conj(f.sector))
Base.one(::Type{FermionParity}) = FermionParity(one(Z2Irrep))
Base.conj(f::FermionParity) = FermionParity(conj(f.sector))

dim(f::Fermion) = dim(f.sector)
dim(f::FermionParity) = dim(f.sector)

FusionStyle(::Type{<:Fermion{<:Any,I}}) where {I<:Sector} = FusionStyle(I)
BraidingStyle(::Type{<:Fermion}) = Fermionic()
Base.isreal(::Type{Fermion{<:Any,I}}) where {I<:Sector} = isreal(I)
FusionStyle(::Type{FermionParity}) = FusionStyle(Z2Irrep)
BraidingStyle(::Type{FermionParity}) = Fermionic()
Base.isreal(::Type{FermionParity}) = isreal(Z2Irrep)

(a::F, b::F) where {F<:Fermion} = SectorSet{F}(a.sector b.sector)
(a::FermionParity, b::FermionParity) = SectorSet{FermionParity}(a.sector b.sector)

Nsymbol(a::F, b::F, c::F) where {F<:Fermion} = Nsymbol(a.sector, b.sector, c.sector)
Nsymbol(a::FermionParity, b::FermionParity, c::FermionParity) = Nsymbol(a.sector, b.sector, c.sector)

Fsymbol(a::F, b::F, c::F, d::F, e::F, f::F) where {F<:Fermion} =
Fsymbol(a::F, b::F, c::F, d::F, e::F, f::F) where {F<:FermionParity} =
Fsymbol(a.sector, b.sector, c.sector, d.sector, e.sector, f.sector)

function Rsymbol(a::F, b::F, c::F) where {F<:Fermion}
function Rsymbol(a::F, b::F, c::F) where {F<:FermionParity}
if fermionparity(a) && fermionparity(b)
return -Rsymbol(a.sector, b.sector, c.sector)
else
return +Rsymbol(a.sector, b.sector, c.sector)
end
end

twist(a::Fermion) = ifelse(fermionparity(a), -1, +1)*twist(a.sector)
twist(a::FermionParity) = ifelse(fermionparity(a), -1, +1) * twist(a.sector)

type_repr(::Type{Fermion{P,I}}) where {P, I<:Sector} = "Fermion{$P, " * type_repr(I) * "}"
type_repr(::Type{FermionParity}) = "FermionParity"

function Base.show(io::IO, a::Fermion{P, I}) where {P, I<:Sector}
if get(io, :typeinfo, nothing) !== Fermion{P, I}
function Base.show(io::IO, a::FermionParity)
if get(io, :typeinfo, nothing) !== FermionParity
print(io, type_repr(typeof(a)), "(")
end
print(IOContext(io, :typeinfo => I), a.sector)
if get(io, :typeinfo, nothing) !== Fermion{P, I}
print(IOContext(io, :typeinfo => Z2Irrep), a.sector)
if get(io, :typeinfo, nothing) !== FermionParity
print(io, ")")
end
end

Base.hash(f::Fermion, h::UInt) = hash(f.sector, h)
Base.isless(a::F, b::F) where {F<:Fermion} = isless(a.sector, b.sector)

_fermionparity(a::Z2Irrep) = isodd(a.n)
_fermionnumber(a::U1Irrep) = isodd(convert(Int, a.charge))
_fermionspin(a::SU2Irrep) = isodd(twice(a.j))

const FermionParity = Fermion{_fermionparity, Z2Irrep}
const FermionNumber = Fermion{_fermionnumber, U1Irrep}
const FermionSpin = Fermion{_fermionspin, SU2Irrep}
const fℤ₂ = FermionParity
const fU₁ = FermionNumber
const fSU₂ = FermionSpin

type_repr(::Type{FermionParity}) = "FermionParity"
type_repr(::Type{FermionNumber}) = "FermionNumber"
type_repr(::Type{FermionSpin}) = "FermionSpin"
Base.hash(f::FermionParity, h::UInt) = hash(f.sector, h)
Base.isless(a::FermionParity, b::FermionParity) = isless(a.sector, b.sector)
10 changes: 5 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const TK = TensorKit
Random.seed!(1234)

smallset(::Type{I}) where {I<:Sector} = take(values(I), 5)
smallset(::Type{FermionNumber}) = FermionNumber.((0, +1, -1, +2, -2))
function smallset(::Type{ProductSector{Tuple{I1,I2}}}) where {I1,I2}
iter = product(smallset(I1), smallset(I2))
s = collect(i j for (i,j) in iter if dim(i)*dim(j) <= 6)
Expand Down Expand Up @@ -52,10 +51,11 @@ function hasfusiontensor(I::Type{<:Sector})
end

sectorlist = (Z2Irrep, Z3Irrep, Z4Irrep, U1Irrep, CU1Irrep, SU2Irrep, NewSU2Irrep, SU3Irrep,
FibonacciAnyon, IsingAnyon, FermionParity, FermionNumber, FermionSpin,
FermionParity FermionParity, Z3Irrep Z4Irrep, FermionNumber SU2Irrep,
FermionSpin SU2Irrep, NewSU2Irrep NewSU2Irrep, NewSU2Irrep SU2Irrep,
FermionSpin NewSU2Irrep, Z2Irrep FibonacciAnyon FibonacciAnyon)
FibonacciAnyon, IsingAnyon, FermionParity, FermionParity FermionParity,
Z3Irrep Z4Irrep, FermionParity U1Irrep SU2Irrep,
FermionParity SU2Irrep SU2Irrep, NewSU2Irrep NewSU2Irrep,
NewSU2Irrep SU2Irrep, FermionParity SU2Irrep NewSU2Irrep,
Z2Irrep FibonacciAnyon FibonacciAnyon)

Ti = time()
include("sectors.jl")
Expand Down
Loading

0 comments on commit 4ceba8d

Please sign in to comment.