dwavesystems / dwave-system

An API for easily incorporating the D-Wave system as a sampler, either directly or through Leap's cloud-based hybrid samplers
https://docs.ocean.dwavesys.com/
Apache License 2.0
90 stars 64 forks source link

Add the ability to filter out infeasible samples when calling the LeapHybridCQMSampler #460

Open arcondello opened 2 years ago

arcondello commented 2 years ago

Currently this can be done with

sampleset = LeapHybridCQMSampler().sample_cqm(cqm)
feasible_sampleset = sampleset.filter(lambda d: d.is_feasible)

see https://github.com/dwavesystems/dimod/blob/ec9c3b274ff4104c00fe702d680b320f0bffca25/dimod/sampleset.py#L1371

It would be relatively straightforward to add a filter_feasible (or similar) keyword argument to the LeapHybridCQMSampler.sample_cqm() method. Something like

feasible_sampleset = LeapHybridCQMSampler().sample_cqm(cqm, filter_feasible=True)

Some issues:

I personally feel like the existing syntax is explicit and unsurprising at the cost of one additional line of code, but perhaps some syntactic sugar here would be appreciated by users.