dwavesystems / dwave-samplers

Classical algorithms for solving binary quadratic models
Apache License 2.0
8 stars 12 forks source link

[Feature Request] Run to time limit for Neal and Greedy #27

Open kevinchern opened 2 years ago

kevinchern commented 2 years ago

Would be useful to have a time_limit argument for Neal and Greedy. Both Neal (for a fixed num_sweeps) and Greedy can implement a run-to-time-limit feature by adding timing calls per read, i.e., variable number of reads depending on time constraint. Example from RandomSampler.

CatherineCMcGeoch commented 2 years ago

Pau has implemented a smart runtime estimator that self-adjusts its convergence rate if necessary. It works pretty well, I believe, might want to ask him about it when he gets back. (Or maybe he has put it in github I dunno)

jackraymond commented 1 year ago

If you wish to evaluate a criteria only after each complete anneal this feature already exists through the interrupt function:

import time
t0 = time.perf_counter_ns()
def interrupt_function():
    time_limit = 0 #Any value in nanoseconds
    if time.perf_counter_ns() - t0 > delay:
        return True
    else:
        return False
from dwave.samplers.sa import SimulatedAnnealingSampler
num_reads = 1000 #example 
resp  = SimulatedAnnealingSampler().sample_ising({i: 1}, {}, interrupt_function, num_reads = num_reads)
assert len(resp.record) == 1  # because time_limit of 0 nanoseconds is immediately exceeded.

For greater flexibility the code could be rewritten so that interrupt function takes sample information (in order to do things like time to energy/diversity criteria). Another possibility would be to evaluate the interrupt function before the anneal completes, but I think there wouldn't be many use cases for that.