Skip to content

Commit 8c8b705

Browse files
authored
ParticleNumberOperator (#255)
* ParticleNumberOperator * use recent julia version for docs generation --------- Co-authored-by: Joachim Brand <joachim.brand@gmail.com>
1 parent 2751499 commit 8c8b705

File tree

7 files changed

+50
-3
lines changed

7 files changed

+50
-3
lines changed

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- uses: actions/checkout@v4
1616
- uses: julia-actions/setup-julia@v2
1717
with:
18-
version: '1.7'
18+
version: '1'
1919
- uses: julia-actions/cache@v1
2020
- name: Install dependencies
2121
run: |

docs/src/hamiltonians.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Observables are [`AbstractHamiltonian`](@ref)s that represent a physical
102102
observable. Their ground state expectation values can be sampled by passing
103103
them into [`AllOverlaps`](@ref).
104104
```@docs
105+
ParticleNumberOperator
105106
G2MomCorrelator
106107
G2RealCorrelator
107108
SuperfluidCorrelator

src/Hamiltonians/Hamiltonians.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Other
3636
- [`Stoquastic`](@ref)
3737
3838
## [Observables](#Observables)
39+
- [`ParticleNumberOperator`](@ref)
3940
- [`G2MomCorrelator`](@ref)
4041
- [`G2RealCorrelator`](@ref)
4142
- [`DensityMatrixDiagonal`](@ref)
@@ -83,6 +84,7 @@ export Stoquastic
8384
export Transcorrelated1D
8485
export hubbard_dispersion, continuum_dispersion
8586
export FroehlichPolaron
87+
export ParticleNumberOperator
8688

8789
export G2MomCorrelator, G2RealCorrelator, SuperfluidCorrelator, DensityMatrixDiagonal, Momentum
8890
export StringCorrelator
@@ -125,6 +127,7 @@ include("Transcorrelated1D.jl")
125127
include("correlation_functions.jl")
126128
include("DensityMatrixDiagonal.jl")
127129
include("Momentum.jl")
130+
include("particle_number.jl")
128131

129132
include("HOCartesianContactInteractions.jl")
130133
include("HOCartesianEnergyConservedPerDim.jl")

src/Hamiltonians/Momentum.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Momentum(component=0; fold=true) = Momentum{component}(fold)
2727
Base.show(io::IO, mom::Momentum{C}) where {C} = print(io, "Momentum($C; fold=$(mom.fold))")
2828

2929
LOStructure(::Type{<:Momentum}) = IsDiagonal()
30-
num_offdiagonals(ham::Momentum, add) = 0
3130

3231
@inline function _momentum(add::SingleComponentFockAddress, fold)
3332
M = num_modes(add)

src/Hamiltonians/particle_number.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
ParticleNumberOperator([address]) <: AbstractHamiltonian
3+
4+
The number operator in Fock space. This operator is diagonal in the Fock basis and
5+
returns the number of particles in the Fock state. Passing an address is optional.
6+
7+
```jldoctest
8+
julia> h = FroehlichPolaron(fs"|0 0⟩{}"; mode_cutoff=5, v=3); bsr = BasisSetRep(h);
9+
10+
julia> gs = DVec(zip(bsr.basis, eigen(Matrix(bsr)).vectors[:,1])); # ground state
11+
12+
julia> dot(gs, ParticleNumberOperator(), gs) # particle number expectation value
13+
2.8823297252925917
14+
```
15+
16+
See also [`AbstractHamiltonian`](@ref).
17+
"""
18+
struct ParticleNumberOperator{A} <: AbstractHamiltonian{Float64}
19+
address::A
20+
end
21+
ParticleNumberOperator() = ParticleNumberOperator(BoseFS(1,))
22+
23+
function Base.show(io::IO, n::ParticleNumberOperator)
24+
io = IOContext(io, :compact => true)
25+
print(io, "ParticleNumberOperator(")
26+
n.address === BoseFS(1,) || show(io, n.address) # suppress if default
27+
print(io, ")")
28+
end
29+
30+
LOStructure(::Type{<:ParticleNumberOperator}) = IsDiagonal()
31+
starting_address(n::ParticleNumberOperator) = n.address
32+
33+
function diagonal_element(::ParticleNumberOperator, addr::AbstractFockAddress)
34+
return float(num_particles(addr))
35+
end
36+
allowed_address_type(::ParticleNumberOperator) = AbstractFockAddress

src/Interfaces/hamiltonians.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ LOStructure(op) = LOStructure(typeof(op))
252252
LOStructure(::Type) = AdjointUnknown()
253253
LOStructure(::AbstractMatrix) = AdjointKnown()
254254

255+
# diagonal matrices have zero offdiagonal elements
256+
function num_offdiagonals(h::H, addr) where {H<:AbstractHamiltonian}
257+
return num_offdiagonals(LOStructure(H), h, addr)
258+
end
259+
num_offdiagonals(::IsDiagonal, _, _) = 0
260+
255261
"""
256262
has_adjoint(op)
257263

test/Hamiltonians.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ end
196196
HOCartesianCentralImpurity(BoseFS((1,0,0,0,0))),
197197

198198
FroehlichPolaron(OccupationNumberFS(1,1,1)),
199-
FroehlichPolaron(OccupationNumberFS(1,1,1); momentum_cutoff = 10.0)
199+
FroehlichPolaron(OccupationNumberFS(1,1,1); momentum_cutoff = 10.0),
200+
201+
ParticleNumberOperator(OccupationNumberFS(1, 1, 1))
200202
)
201203
test_hamiltonian_interface(H)
202204
end

0 commit comments

Comments
 (0)