Skip to content

Commit

Permalink
more tests, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsch committed Jan 21, 2025
1 parent 20d8034 commit 58cdfc6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 15 deletions.
19 changes: 12 additions & 7 deletions docs/src/hamiltonians.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ Stoquastic
```

## Observables
`Rimu.jl` offers two other supertypes for operators that are less
restrictive than [`AbstractHamiltonian`](@ref).
`Rimu.jl` offers two other supertypes for operators that are less
restrictive than [`AbstractHamiltonian`](@ref).
[`AbstractObservable`](@ref) and [`AbstractOperator`](@ref)s both
can represent a physical observable. Their expectation values can be sampled during a [`ProjectorMonteCarloProblem`](@ref) simulation by
passing them into a suitable [`ReplicaStrategy`](@ref), e.g.
can represent a physical observable. Their expectation values can be sampled during a [`ProjectorMonteCarloProblem`](@ref) simulation by
passing them into a suitable [`ReplicaStrategy`](@ref), e.g.
[`AllOverlaps`](@ref). Some observables are also [`AbstractHamiltonian`](@ref)s. The full type hierarchy is
```julia
AbstractHamiltonian{T} <: AbstractOperator{T} <: AbstractObservable{T}
Expand All @@ -96,13 +96,18 @@ Lattices in higher dimensions are defined here and can be passed with the keywor
`geometry` to [`HubbardRealSpace`](@ref) and [`G2RealSpace`](@ref).

```@docs
Hamiltonians.Geometry
CubicGrid
Hamiltonians.Directions
Hamiltonians.Displacements
Hamiltonians.neighbor_site
PeriodicBoundaries
HardwallBoundaries
LadderBoundaries
HoneycombLattice
HexagonalLattice
Hamiltonians.Directions
Hamiltonians.Displacements
Hamiltonians.neighbor_site
Hamiltonians.periodic_dimensions
Hamiltonians.num_dimensions
```

## Index
Expand Down
11 changes: 4 additions & 7 deletions src/Hamiltonians/geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Return a [`CubicGrid`](@ref) where the first dimension is dimensions non-periodi
rest are periodic. Equivalent to `CubicGrid(dims, (true, false, ...))`.
"""
function LadderBoundaries(dims::NTuple{D,Int}) where {D}
return CubicGrid(dims, ntuple(>(1), Val(D)))
return CubicGrid(dims, ntuple(i -> dims[i] > 2, Val(D)))
end
LadderBoundaries(dims::Vararg{Int}) = LadderBoundaries(dims)

Expand Down Expand Up @@ -311,15 +311,12 @@ A 4×4 `HoneycombLattice` is indexed as follows.
"""
struct HoneycombLattice{Dims,Fold} <: Geometry{2}
function HoneycombLattice(dims::Tuple{Int,Int}, fold::Tuple{Bool,Bool}=(true, true))
if fold[1] && isodd(dims[1])
throw(ArgumentError("if `fold[1] == true`, the lattice height must be even"))
end
if fold[2] && isodd(dims[2])
throw(ArgumentError("if `fold[2] == true`, the lattice width must be even"))
end
if any(<(2), dims)
throw(ArgumentError("All dimensions must be at least 2 in size"))
end
if fold[1] && isodd(dims[1]) || fold[2] && isodd(dims[2])
throw(ArgumentError("Periodic dimensions must be even in size"))
end
return new{dims,fold}()
end
end
Expand Down
47 changes: 46 additions & 1 deletion test/Hamiltonians.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ using Rimu.Hamiltonians: Directions, Displacements

@test PeriodicBoundaries(3, 3) == CubicGrid(3, 3)
@test HardwallBoundaries(3, 4, 5) == CubicGrid((3, 4, 5), (false, false, false))
@test LadderBoundaries(2, 3, 4) == CubicGrid((2, 3, 4), (false, true, true))
@test LadderBoundaries(3, 2, 4) == CubicGrid((3, 2, 4), (true, false, true))
@test LadderBoundaries(2, 2, 2, 2) == HardwallBoundaries(2, 2, 2, 2)

for (dims, fold) in (
((4,), (false,)), ((2, 5), (true, false)), ((5, 6, 7), (true, true, false))
Expand Down Expand Up @@ -537,6 +538,50 @@ end
@test exact_energy(H1) exact_energy(H2)
end
end
@testset "Cubic, honeycomb, and hexagonal lattices" begin
@testset "HoneycombLattice vs LadderBoundaries" begin
# N × 2 honeycomb is equivalent to LadderBoundaries for bosons
geom1 = HoneycombLattice(4, 2)
geom2 = LadderBoundaries(4, 2)
bose = BoseFS(1, 1, 1, 1, 0, 0, 0, 0)
fermi = FermiFS(1, 1, 1, 1, 0, 0, 0, 0)
H1 = HubbardRealSpace(bose; geometry=geom1)
H2 = HubbardRealSpace(bose; geometry=geom2)
H3 = HubbardRealSpace(fermi; geometry=geom1)
H4 = HubbardRealSpace(fermi; geometry=geom2)

@test sparse(H1; sort=true) == sparse(H2; sort=true)
@test sparse(H3; sort=true) == sparse(H4; sort=true)
end
@testset "noninteracting cases" begin
geom1 = PeriodicBoundaries(4, 4)
geom2 = HoneycombLattice(4, 4)
geom3 = HexagonalLattice(4, 4)

bose = BoseFS(16, 1 => 1, 2 => 1, 3 => 1, 4 => 1)
H1_bose = HubbardRealSpace(bose; t=[1/4], u=[0], geometry=geom1)
H2_bose = HubbardRealSpace(bose; t=[1/3], u=[0], geometry=geom2)
H3_bose = HubbardRealSpace(bose; t=[1/6], u=[0], geometry=geom3)
E1_bose = eigsolve(sparse(H1_bose), 1, :SR)[1][1]
E2_bose = eigsolve(sparse(H2_bose), 1, :SR)[1][1]
E3_bose = eigsolve(sparse(H3_bose), 1, :SR)[1][1]

# The Hamiltonians have the same energy as it only depends on the number of
# neighbors
@test E1_bose E2_bose && E2_bose E3_bose && E1_bose E3_bose

fermi = FermiFS(16, 1 => 1, 2 => 1, 3 => 1, 4 => 1)
H1_fermi = HubbardRealSpace(fermi; t=[1/4], geometry=geom1)
H2_fermi = HubbardRealSpace(fermi; t=[1/3], geometry=geom2)
H3_fermi = HubbardRealSpace(fermi; t=[1/6], geometry=geom3)
E1_fermi = eigsolve(sparse(H1_fermi), 1, :SR)[1][1]
E2_fermi = eigsolve(sparse(H2_fermi), 1, :SR)[1][1]
E3_fermi = eigsolve(sparse(H3_fermi), 1, :SR)[1][1]

# The Hamiltonians have different energies due to symmetry
@test E1_fermi E2_fermi && E2_fermi E3_fermi && E1_fermi E3_fermi
end
end
end

@testset "Importance sampling" begin
Expand Down

0 comments on commit 58cdfc6

Please sign in to comment.