dwave-system
dwave-system copied to clipboard
Add the ability to filter out infeasible samples when calling the LeapHybridCQMSampler
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:
- Need to decide what to do in the case that there are no feasible solutions. Probably return an empty sample set, but could consider raising an exception.
- Would need to make
SampleSet.filter()non-blocking, or handle this server-side. I don't think there is much performance benefit from doing it server-side though.
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.