Skip to content

Commit

Permalink
Allow time limit stopping criterion
Browse files Browse the repository at this point in the history
  • Loading branch information
iagoleal committed Jan 9, 2025
1 parent b56a6ee commit b0be2fc
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ end
mutable struct ConvergenceObserver <: AbstractObserver
atol :: Float64
rtol :: Float64
time_limit :: Float64
init_time :: Float64
previous_energy :: Float64

ConvergenceObserver(atol, rtol, time_limit = +Inf) = new(atol, rtol, time_limit, time() , +Inf)
end

function ITensorMPS.checkdone!(o::ConvergenceObserver; energy, sweep, kwargs...)
converged = isapprox(energy, o.previous_energy; atol = o.atol, rtol = o.rtol)
o.previous_energy = energy

return converged
return converged || time() - o.init_time > o.time_limit
end


Expand Down Expand Up @@ -145,8 +149,11 @@ See also [`maximize`](@ref).
function minimize( Q :: AbstractMatrix{T}
, l :: Union{AbstractVector{T}, Nothing} = nothing
, c :: T = zero(T)
; cutoff :: Float64 = 1e-8
, iterations :: Int = 10
; cutoff :: Float64 = 1e-8
, atol :: Float64 = cutoff
, rtol :: Float64 = atol
, iterations :: Int = 10
, time_limit :: Float64 = +Inf
, maxdim = [10, 20, 100, 100, 200]
, device :: Function = identity
, kwargs...
Expand All @@ -160,7 +167,7 @@ function minimize( Q :: AbstractMatrix{T}
# Initial product state
# Slight entanglement to help DMRG avoid local minima
psi0 = random_mps(T, sites; linkdims=2)
observer = ConvergenceObserver(atol, rtol, Inf)
observer = ConvergenceObserver(atol, rtol, time_limit)

energy, tn = dmrg(device(H), device(psi0)
; nsweeps = iterations
Expand Down

0 comments on commit b0be2fc

Please sign in to comment.