Skip to content

Commit 3f9ef9a

Browse files
authored
Small fixes and improvements (#39)
* Add test and fix real operators * Remove method overwrite in tests * formatter
1 parent de22235 commit 3f9ef9a

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

src/operators/spinoperators.jl

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function spinmatrices(s::Union{Rational{Int},Int}, elt=ComplexF64)
1919
N = Int(2 * s)
2020

2121
Sx = zeros(elt, N + 1, N + 1)
22-
Sy = zeros(elt, N + 1, N + 1)
22+
Sy = zeros(complex(elt), N + 1, N + 1)
2323
Sz = zeros(elt, N + 1, N + 1)
2424

2525
for row in 1:(N + 1)
@@ -417,13 +417,13 @@ S_exchange(elt::Type{<:Number}; kwargs...) = S_exchange(elt, Trivial; kwargs...)
417417
function S_exchange(symmetry::Type{<:Sector}; kwargs...)
418418
return S_exchange(ComplexF64, symmetry; kwargs...)
419419
end
420-
421-
function S_exchange(elt::Type{<:Number}, ::Type{Trivial}; spin=1 // 2)
422-
return S_xx(elt, Trivial; spin=spin) +
423-
S_yy(elt, Trivial; spin=spin) +
424-
S_zz(elt, Trivial; spin=spin)
420+
function S_exchange(elt::Type{<:Number}, symmetry::Type{<:Sector}; spin=1 // 2)
421+
elt_complex = complex(elt)
422+
SS = (S_plusmin(elt_complex, symmetry; spin=spin) +
423+
S_minplus(elt_complex, symmetry; spin=spin)) / 2 +
424+
S_zz(elt_complex, symmetry; spin=spin)
425+
return elt <: Real ? real(SS) : SS
425426
end
426-
427427
function S_exchange(elt::Type{<:Number}, ::Type{SU2Irrep}; spin=1 // 2)
428428
pspace = SU2Space(spin => 1)
429429
aspace = SU2Space(1 => 1)
@@ -435,11 +435,6 @@ function S_exchange(elt::Type{<:Number}, ::Type{SU2Irrep}; spin=1 // 2)
435435
return SS
436436
end
437437

438-
function S_exchange(elt::Type{<:Number}, symmetry::Type{<:Sector}; spin=1 // 2)
439-
return (S_plusmin(elt, symmetry; spin=spin) + S_minplus(elt, symmetry; spin=spin)) / 2 +
440-
S_zz(elt, symmetry; spin=spin)
441-
end
442-
443438
const SS = S_exchange
444439

445440
"""Pauli exchange operator."""

test/hubbardoperators.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ implemented_symmetries = [(Trivial, Trivial), (U1Irrep, U1Irrep), (U1Irrep, SU2I
4141
end
4242
end
4343

44-
function hamiltonian(particle_symmetry, spin_symmetry; t, U, mu, L)
44+
function hubbard_hamiltonian(particle_symmetry, spin_symmetry; t, U, mu, L)
4545
hopping = t * (e_plusmin(particle_symmetry, spin_symmetry) +
4646
e_minplus(particle_symmetry, spin_symmetry))
4747
interaction = U * e_number_updown(particle_symmetry, spin_symmetry)
@@ -65,7 +65,7 @@ end
6565
U = randn()
6666
mu = randn()
6767

68-
H_triv = hamiltonian(Trivial, Trivial; t, U, mu, L)
68+
H_triv = hubbard_hamiltonian(Trivial, Trivial; t, U, mu, L)
6969
vals_triv = mapreduce(vcat, eigvals(H_triv)) do (c, v)
7070
return repeat(real.(v), dim(c))
7171
end
@@ -75,7 +75,7 @@ end
7575
if (particle_symmetry, spin_symmetry) == (Trivial, Trivial)
7676
continue
7777
end
78-
H_symm = hamiltonian(particle_symmetry, spin_symmetry; t, U, mu, L)
78+
H_symm = hubbard_hamiltonian(particle_symmetry, spin_symmetry; t, U, mu, L)
7979
vals_symm = mapreduce(vcat, eigvals(H_symm)) do (c, v)
8080
return repeat(real.(v), dim(c))
8181
end

test/spinoperators.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ end
133133
S_xx(U1Irrep; spin=spin) + S_yy(U1Irrep; spin=spin) + S_zz(U1Irrep; spin=spin) rtol = 1e-3
134134
end
135135

136+
@testset "Real eltype" begin
137+
for operator in (S_x, S_z, S_xx, S_zz, S_plusmin, S_minplus, S_exchange),
138+
symmetry in (Trivial, Z2Irrep, U1Irrep)
139+
140+
@test real(operator(ComplexF64, symmetry)) operator(Float64, symmetry)
141+
end
142+
@test real(S_exchange(ComplexF64, SU2Irrep)) S_exchange(Float64, SU2Irrep)
143+
end
144+
136145
# potts_ZZ test?
137146
@testset "non-symmetric Q-state potts operators" for Q in 3:5
138147
# inferrability

test/tjoperators.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ implemented_symmetries = [(Trivial, Trivial),
7474
end
7575
end
7676

77-
function hamiltonian(particle_symmetry, spin_symmetry; t, J, mu, L, slave_fermion)
77+
function tjhamiltonian(particle_symmetry, spin_symmetry; t, J, mu, L, slave_fermion)
7878
num = tJ.e_number(particle_symmetry, spin_symmetry; slave_fermion)
7979
hop_heis = (-t) * (tJ.e_plusmin(particle_symmetry, spin_symmetry; slave_fermion) +
8080
tJ.e_minplus(particle_symmetry, spin_symmetry; slave_fermion)) +
@@ -98,7 +98,7 @@ end
9898
mu = randn()
9999

100100
for slave_fermion in (false, true)
101-
H_triv = hamiltonian(Trivial, Trivial; t, J, mu, L, slave_fermion)
101+
H_triv = tjhamiltonian(Trivial, Trivial; t, J, mu, L, slave_fermion)
102102
vals_triv = mapreduce(vcat, eigvals(H_triv)) do (c, v)
103103
return repeat(real.(v), dim(c))
104104
end
@@ -108,8 +108,8 @@ end
108108
if (particle_symmetry, spin_symmetry) == (Trivial, Trivial)
109109
continue
110110
end
111-
H_symm = hamiltonian(particle_symmetry, spin_symmetry; t, J, mu, L,
112-
slave_fermion)
111+
H_symm = tjhamiltonian(particle_symmetry, spin_symmetry; t, J, mu, L,
112+
slave_fermion)
113113
vals_symm = mapreduce(vcat, eigvals(H_symm)) do (c, v)
114114
return repeat(real.(v), dim(c))
115115
end

0 commit comments

Comments
 (0)