MCPcopy Create free account
hub / github.com/dwavesystems/dwave-cloud-client / wait_sampleset

Method wait_sampleset

dwave/cloud/computation.py:784–832  ·  view source on GitHub ↗

Blocking sampleset getter.

(self)

Source from the content-addressed store, hash-verified

782 return [1] * len(result['solutions'])
783
784 def wait_sampleset(self):
785 """Blocking sampleset getter."""
786
787 # resolve response
788 self._load_result()
789
790 # check if sampleset already resolved (e.g. returned in response)
791 sampleset = self._sampleset() if self._sampleset else None
792 if sampleset is not None:
793 return sampleset
794
795 # construct sampleset from available data
796 try:
797 import dimod
798 except ImportError:
799 raise RuntimeError("Can't construct SampleSet without dimod. "
800 "Re-install the library with 'bqm' support.")
801
802 # filter inactive variables from samples
803 variables = self.variables
804 samples = [[sample[v] for v in variables] for sample in self.samples]
805
806 # infer vartype from problem type
807 # note: KeyError on unknown problem types. BQM/DQM should be handled above.
808 vartype_from_problem_type = {'ising': 'SPIN', 'qubo': 'BINARY'}
809 vartype = vartype_from_problem_type[self.problem_type]
810
811 # for QPU jobs, info field is blank; add timing info
812 info = dict(timing=self.timing)
813 # also extend info with problem metadata like id and label
814 info.update(self._get_problem_info())
815
816 sampleset = dimod.SampleSet.from_samples(
817 (samples, variables), vartype=vartype,
818 energy=self.energies, num_occurrences=self.num_occurrences,
819 info=info, sort_labels=True)
820
821 if not hasattr(sampleset, 'wait_id'):
822 # the id is stored in the info field, but to be consistent with the
823 # samplesets constructed .from_future, we add the method as well
824 # note: sampleset has .wait_id() since dimod 0.12.18+, but we want
825 # to support older dimods as well
826 sampleset.wait_id = self.wait_id
827
828 # cache the constructed sampleset on computation, but don't prevent the
829 # gc from collecting it if unused in parent closure
830 self._sampleset = weakref.ref(sampleset)
831
832 return sampleset
833
834 @property
835 def sampleset(self):

Callers 2

samplesetMethod · 0.80
test_sampleset_refMethod · 0.80

Calls 2

_load_resultMethod · 0.95
_get_problem_infoMethod · 0.95

Tested by 1

test_sampleset_refMethod · 0.64