• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

joachimbrand / Rimu.jl / 11565770445

29 Oct 2024 01:46AM UTC coverage: 94.31% (-0.5%) from 94.852%
11565770445

push

github

web-flow
Feature/ExtendedHubbardMom1D (#286)

* Create ExtendedHubbardMom1D.jl

- New model (i.e. ``ExtendedHubbardMom1D`` is added to Rimu

* Update excitations.jl

- ``extended_momentum_transfer_diagonal`` is added for the nearest neighbour term in ``ExtendedHubbardMom1D``

* Update Hamiltonians.jl

- ``ExtendedHubbardMom1D`` is added to all the necessary test sets.

* Update Hamiltonians.jl

* Update hamiltonians.md

* Update Hamiltonians.jl

* Update src/Hamiltonians/ExtendedHubbardMom1D.jl

Co-authored-by: Joachim Brand <joachim.brand@gmail.com>

* Update src/Hamiltonians/ExtendedHubbardMom1D.jl

Co-authored-by: Joachim Brand <joachim.brand@gmail.com>

* Update src/Hamiltonians/excitations.jl

Co-authored-by: Joachim Brand <joachim.brand@gmail.com>

* Update src/Hamiltonians/ExtendedHubbardMom1D.jl

Co-authored-by: Joachim Brand <joachim.brand@gmail.com>

* Update HubbardMom1D.jl

* Update HubbardMom1D.jl

* Update HubbardMom1D.jl

* Update HubbardMom1D.jl

* Update HubbardMom1D.jl

* Update excitations.jl

* Update ExtendedHubbardMom1D.jl

* Update HubbardMom1D.jl

* Update Hamiltonians.jl

* Update Hamiltonians.jl

* Update HubbardMom1D.jl

* Update Hamiltonians.jl

* Update excitations.jl

* Update ExtendedHubbardMom1D.jl

* Update ExtendedHubbardMom1D.jl

* docstring fix for HubbardMom1DEP

* Update HubbardMom1DEP.jl

* Update excitations.jl

* Update ExtendedHubbardMom1D.jl

* Update ExtendedHubbardMom1D.jl

* Update HubbardMom1D.jl

* Update ExtendedHubbardMom1D.jl

* Update Hamiltonians.jl

- Added test for comparison between energies of ``ExtendedHubbardMom1D`` and ``ExtendedHubbardReal1D``

* Update src/Hamiltonians/ExtendedHubbardMom1D.jl

Co-authored-by: mtsch <matijacufar@gmail.com>

* Update src/Hamiltonians/ExtendedHubbardMom1D.jl

Co-authored-by: mtsch <matijacufar@gmail.com>

* Update Hamiltonians.jl

* Update Hamiltonians.jl

* Update Hamiltonian... (continued)

66 of 73 new or added lines in 3 files covered. (90.41%)

40 existing lines in 18 files now uncovered.

6978 of 7399 relevant lines covered (94.31%)

13438492.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.03
/src/Hamiltonians/GuidingVectorSampling.jl
1
"""
2
    GuidingVectorSampling
3

4
Wrapper over any [`AbstractHamiltonian`](@ref) that implements guided vector a.k.a. guided
5
wave function sampling. In this importance sampling scheme the Hamiltonian is modified as
6
follows.
7

8
```math
9
\\tilde{H}_{ij} = v_i H_{ij} v_j^{-1}
10
```
11

12
and where `v` is the guiding vector. `v_i` and `v_j` are also thresholded to avoid dividing
13
by zero (see below).
14

15
# Constructors
16

17
* `GuidingVectorSampling(::AbstractHamiltonian, vector, eps)`
18
* `GuidingVectorSampling(::AbstractHamiltonian; vector, eps)`
19

20
`eps` is a thresholding parameter used to avoid dividing by zero; all values below `eps` are
21
set to `eps`. It is recommended that `eps` is in the same value range as the guiding
22
vector. The default value is set to `eps=norm(v, Inf) * 1e-2`
23

24
After construction, we can access the underlying hamiltonian with `G.hamiltonian`, the
25
`eps` parameter with `G.eps`, and the guiding vector with `G.vector`.
26

27
# Example
28

29
```jldoctest
30
julia> H = HubbardReal1D(BoseFS(1,1,1); u=6.0, t=1.0);
31

32
julia> v = DVec(starting_address(H) => 10; capacity=1);
33

34
julia> G = GuidingVectorSampling(H, v, 0.1);
35

36
julia> get_offdiagonal(H, starting_address(H), 4)
37
(BoseFS{3,3}(2, 0, 1), -1.4142135623730951)
38

39
julia> get_offdiagonal(G, starting_address(G), 4)
40
(BoseFS{3,3}(2, 0, 1), -0.014142135623730952)
41
```
42

43
# Observables
44

45
To calculate observables, pass the transformed Hamiltonian `G` to
46
[`AllOverlaps`](@ref) with keyword argument `transform=G`.
47
"""
48
struct GuidingVectorSampling{A,T,H<:AbstractHamiltonian{T},D,E} <: AbstractHamiltonian{T}
49
    # The A parameter sets whether this is an adjoint or not.
50
    # The E parameter is the epsilon value.
51
    hamiltonian::H
14✔
52
    vector::D
53
end
54

55
function GuidingVectorSampling(h, v::AbstractDVec, eps=1e-2 * norm(v, Inf))
13✔
56
    return GuidingVectorSampling{false,eltype(h),typeof(h),typeof(v),eps}(h, v)
13✔
57
end
58
function GuidingVectorSampling(h; vector, eps=1e-2 * norm(vector, Inf))
2✔
59
    return GuidingVectorSampling(h, vector, eps)
1✔
60
end
61

62
starting_address(h::GuidingVectorSampling) = starting_address(h.hamiltonian)
7✔
63
LOStructure(::Type{<:GuidingVectorSampling{<:Any,<:Any,H}}) where {H} = _lo_str(LOStructure(H))
10✔
64

65
function LinearAlgebra.adjoint(h::GuidingVectorSampling{A,T,<:Any,D,E}) where {A,T,D,E}
1✔
66
    h_adj = h.hamiltonian'
1✔
67
    return GuidingVectorSampling{!A,T,typeof(h_adj),D,E}(h_adj, h.vector)
1✔
68
end
69

70
dimension(h::GuidingVectorSampling, addr) = dimension(h.hamiltonian, addr)
2✔
71

72
Base.getproperty(h::GuidingVectorSampling, s::Symbol) = getproperty(h, Val(s))
4,522✔
73
Base.getproperty(h::GuidingVectorSampling{<:Any,<:Any,<:Any,<:Any,E}, ::Val{:eps}) where E = E
1,443✔
74
Base.getproperty(h::GuidingVectorSampling, ::Val{:hamiltonian}) = getfield(h, :hamiltonian)
170✔
75
Base.getproperty(h::GuidingVectorSampling, ::Val{:vector}) = getfield(h, :vector)
2,909✔
76

77
# Forward some interface functions.
78
num_offdiagonals(h::GuidingVectorSampling, add) = num_offdiagonals(h.hamiltonian, add)
1✔
79
diagonal_element(h::GuidingVectorSampling, add) = diagonal_element(h.hamiltonian, add)
56✔
80

81
_apply_eps(x, eps) = ifelse(iszero(x), eps, ifelse(abs(x) < eps, sign(x) * eps, x))
3,151✔
82

83
function guided_vector_modify(value, is_adjoint, eps, guide1, guide2)
3,577✔
84
    if iszero(guide1) && iszero(guide2)
3,577✔
85
        return value
2,040✔
86
    else
87
        guide1 = _apply_eps(guide1, eps)
1,576✔
88
        guide2 = _apply_eps(guide2, eps)
1,575✔
89
        if is_adjoint
1,537✔
90
            return value * (guide1 / guide2)
1,468✔
91
        else
92
            return value * (guide2 / guide1)
69✔
93
        end
94
    end
95
end
96

97
function get_offdiagonal(h::GuidingVectorSampling{A}, add1, chosen) where A
13✔
98
    add2, matrix_element = get_offdiagonal(h.hamiltonian, add1, chosen)
19✔
99
    guide1 = h.vector[add1]
26✔
100
    guide2 = h.vector[add2]
25✔
101
    return add2, guided_vector_modify(matrix_element, A, h.eps, guide1, guide2)
13✔
102
end
103

104
struct GuidingVectorOffdiagonals{
105
    F,T,A,E,H<:AbstractHamiltonian,D,N<:AbstractOffdiagonals{F,T}
106
}<:AbstractOffdiagonals{F,T}
107
    hamiltonian::H
45✔
108
    vector::D
109
    guide::T
110
    offdiagonals::N
111
end
112

113
function offdiagonals(h::GuidingVectorSampling{A,T,H,D,E}, a) where {A,T,H,D,E}
45✔
114
    hps = offdiagonals(h.hamiltonian, a)
69✔
115
    guide = h.vector[a]
51✔
116
    return GuidingVectorOffdiagonals{typeof(a),T,A,E,H,D,typeof(hps)}(
45✔
117
        h.hamiltonian, h.vector, guide, hps
118
    )
119
end
120

121
function Base.getindex(h::GuidingVectorOffdiagonals{F,T,A,E}, i)::Tuple{F,T} where {F,T,A,E}
2,134✔
122
    add2, matrix_element = h.offdiagonals[i]
2,134✔
123
    guide1 = h.guide
2,134✔
124
    guide2 = h.vector[add2]
3,528✔
125
    return add2, guided_vector_modify(matrix_element, A, E, guide1, guide2)
2,134✔
126
end
127

128
Base.size(h::GuidingVectorOffdiagonals) = size(h.offdiagonals)
45✔
129

130
"""
131
    TransformUndoer(k::GuidingVectorSampling, op::AbstractOperator)
132
    TransformUndoer(k::GuidingVectorSampling)
133

134
For a guiding vector similarity transformation ``\\hat{G} = f \\hat{H} f^{-1}``
135
define the operator ``f^{-1} \\hat{A} f^{-1}``, and special case ``f^{-2}``, in order
136
to calculate observables. Here ``f`` is a diagonal operator whose entries are
137
the components of the guiding vector, i.e.``f_{ii} = v_i``.
138

139
See [`AllOverlaps`](@ref), [`GuidingVectorSampling`](@ref).
140
"""
141
function TransformUndoer(k::GuidingVectorSampling, op::Union{Nothing,AbstractOperator})
46✔
142
    if isnothing(op)
46✔
143
        T = eltype(k)
5✔
144
    else
145
        T = promote_type(eltype(k), eltype(op))
41✔
146
    end
147
    return TransformUndoer{T,typeof(k),typeof(op)}(k, op)
46✔
148
end
149

150
# methods for general operator `f^{-1} A f^{-1}`
151
LOStructure(::Type{<:TransformUndoer{<:Any,<:GuidingVectorSampling,A}}) where {A} = LOStructure(A)
36✔
152

153
function LinearAlgebra.adjoint(s::TransformUndoer{T,<:GuidingVectorSampling,<:AbstractOperator}) where {T}
×
154
    a_adj = adjoint(s.op)
×
155
    return TransformUndoer{T,typeof(s.transform),typeof(a_adj)}(s.transform, a_adj)
×
156
end
157

158
function diagonal_element(s::TransformUndoer{<:Any,<:GuidingVectorSampling,<:AbstractOperator}, add)
34✔
159
    guide = s.transform.vector[add]
68✔
160
    diagA = diagonal_element(s.op, add)
297✔
161
    return guided_vector_modify(diagA, true, s.transform.eps, 1., 2 * guide)
34✔
162
end
163

164
function num_offdiagonals(s::TransformUndoer{<:Any,<:GuidingVectorSampling,<:Any}, add)
34✔
165
    return num_offdiagonals(s.op, add)
60✔
166
end
167

168
function get_offdiagonal(s::TransformUndoer{<:Any,<:GuidingVectorSampling,<:Any}, add1, chosen)
1,362✔
169
    add2, offd = get_offdiagonal(s.op, add1, chosen)
1,368✔
170
    # Guiding vector `v` is represented as a diagonal operator `f`
171
    guide1 = s.transform.vector[add1]
2,724✔
172
    guide2 = s.transform.vector[add2]
2,718✔
173
    return add2, guided_vector_modify(offd, true, s.transform.eps, 1., guide1 + guide2)
1,362✔
174
end
175

176
# methods for special case `f^{-2}`
177
LOStructure(::Type{<:TransformUndoer{<:Any,<:GuidingVectorSampling,Nothing}}) = IsDiagonal()
34✔
178

179
function diagonal_element(s::TransformUndoer{<:Any,<:GuidingVectorSampling,Nothing}, add)
34✔
180
    guide = s.transform.vector[add]
68✔
181
    return guided_vector_modify(1., true, s.transform.eps, 1., 2 * guide)
34✔
182
end
183

UNCOV
184
num_offdiagonals(s::TransformUndoer{<:Any,<:GuidingVectorSampling,Nothing}, add) = 0
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc