dwave-system icon indicating copy to clipboard operation
dwave-system copied to clipboard

Allow sampling by id from DWaveSampler, LeapHybridSampler, LeapHybridDQMSampler

Open arcondello opened this issue 4 years ago • 9 comments

We could either accept ids in the .sample method,

sampleset = LeapHybridSampler().sample(id_)

or be more explicit

sampleset = LeapHybridSampler().sample_id(id_)

My preference is for the latter, since the former might lead users to do things like

sampleset = EmbeddingComposite(DWaveSampler()).sample(id_)

which isn't well defined.

arcondello avatar Apr 28 '21 21:04 arcondello

This can be done currently via the cloud-client interface. Something like

import dimod

from dwave.system import LeapHybridSampler

sampler = LeapHybridSampler()

bqm = dimod.BQM('SPIN')

kwargs = dict(time_limit=sampler.min_time_limit(bqm))

bqm_id = sampler.solver.upload_bqm(bqm.to_file()).result()

ss1 = sampler.solver.sample_bqm(bqm_id, **kwargs).sampleset
ss2 = sampler.solver.sample_bqm(bqm_id, **kwargs).sampleset

print(ss1)
print(ss2)

edit: this was fixed based on @randomir 's comments below

arcondello avatar Mar 01 '24 17:03 arcondello

Actually, we first need to implement #398, i.e. https://github.com/dwavesystems/dwave-cloud-client/issues/466.

randomir avatar Mar 01 '24 17:03 randomir

I am not sure I understand, the snippet I provided works for me for both BQM and CQM, I haven't tested for the QPU.

Agree that the bigger feature needs work in several places.

arcondello avatar Mar 01 '24 17:03 arcondello

I'm not sure how it can work, because id_ refers to problem id (i.e. "job id"), and sample_cqm requires uploaded problem id (a.k.a bqm_id).

randomir avatar Mar 01 '24 17:03 randomir

Because I didn't actually resolve to see the error :flushed: , my mistake. I'll fix and update, one sec

arcondello avatar Mar 01 '24 17:03 arcondello

Ok, updated the comment, thanks @randomir

arcondello avatar Mar 01 '24 17:03 arcondello

Or if you realize you want to re-submit a hybrid problem long after the initial sampling, you can do:

# get problem id, either from Leap Dashboard, or your previous sampling
problem_id = '...'
# or: problem_id = sampleset.info['problem_id']
# or: problem_id = sampleset.wait_id()

# get problem info
from dwave.cloud.api import Problems
problems = Problems.from_config()
info = problems.get_problem_info(problem_id)

# initialize the relevant solver, e.g. CQM
from dwave.system import LeapHybridCQMSampler
sampler = LeapHybridCQMSampler()

# re-submit the original problem
sampleset = sampler.solver.sample_cqm(info.data.data, **info.params).sampleset

randomir avatar Mar 01 '24 18:03 randomir