Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Early Stopping Criteria besides number of iterations #11

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Combinatorics: multiset_permutations

using ITensorMPS, ITensors
import ITensorMPS: MPS, MPO, dmrg, OpSum, OpName, SiteType, StateName
import ITensorMPS: AbstractObserver


function issquare(A :: AbstractMatrix)
Expand All @@ -14,6 +15,24 @@ function maybe(f::Function, mx::Union{T, Nothing}; default=nothing) where T
return isnothing(mx) ? default : f(mx)
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 || time() - o.init_time > o.time_limit
end


# Diagonal matrix whose eigenvalues are the ordered feasible values for an integer variable.
# For qubits, this is a projection on |1>. Or equivalently, (I - σ_z) / 2.
# This looks like type piracy,
Expand Down Expand Up @@ -130,8 +149,11 @@ minimize(Q; device = Metal.mtl)
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 @@ -145,9 +167,11 @@ 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, time_limit)

energy, tn = dmrg(device(H), device(psi0)
; nsweeps = iterations
, observer = observer
, maxdim, cutoff, kwargs...)

# The calculated energy has approximation errors compared to the true solution.
Expand Down
Loading