Skip to content

Consider creating a mechanism for submitting multiple times to the same hybrid sampler #551

Description

@arcondello

For example, if you wanted to submit multiple times to the hybrid NL solver without reuploading the model between runs, you could do so with something like

import numpy as np

from dwave.cloud.computation import Future
from dwave.optimization.generators import knapsack
from dwave.system import LeapHybridNLSampler


model = knapsack(range(100), range(100), capacity=100)
x, = model.iter_decisions()

sampler = LeapHybridNLSampler()

# Get the timelimit for the problem
time_limit = sampler.estimated_min_time_limit(model)

# We can upload at most one state
problem_data_id = sampler.solver.upload_nlm(model, max_num_states=1).result()

def hook(model, future):
    model.states.from_file(future.answer_data, check_header=False)

# 10 submissions
futures = []
for _ in range(10):
    futures.append(sampler.solver.sample_nlm(problem_data_id, time_limit=time_limit))

states = []
for completed in Future.as_completed(futures):
    model.states.from_future(completed, hook)

    # This needs to be saved "off" the model! It will be overwritten in the
    # next loop
    print(x.state())

    states.extend(np.asarray(x.state(i)) for i in range(len(model.states)))

print(states)

This seems to be a semi-common use case so bundling it into a method or function might be helpful.

Probably needs dwavesystems/dwave-optimization#185 in the case of NL to be done elegantly. For CQM/BQM/DQM we could use dimod.concatenate().

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions