Source code for qlauncher.routines.dwave.algorithms

 1"""DWave algorithms"""
 2
 3from collections.abc import Callable
 4
 5from qlauncher.base import Algorithm, Problem, Result
 6from qlauncher.exceptions import DependencyError
 7from qlauncher.routines.dwave.backends import BQMBackend
 8try:
 9    from dimod.binary.binary_quadratic_model import BinaryQuadraticModel
10    from dimod import SampleSet
11except ImportError as e:
12    raise DependencyError(e, install_hint='dwave') from e
13
14
[docs] 15class DwaveSolver(Algorithm): 16 _algorithm_format = 'bqm' 17 18 def __init__(self, chain_strength=1, num_reads=1000, **alg_kwargs) -> None: 19 self.chain_strength = chain_strength 20 self.num_reads = num_reads 21 self.label: str = 'TBD_TBD' 22 super().__init__(**alg_kwargs) 23
[docs] 24 def run(self, problem: Problem, backend: BQMBackend, formatter: Callable) -> Result: 25 self.label = f'{problem.name}_{problem.instance_name}' 26 27 bqm: BinaryQuadraticModel = formatter(problem) 28 29 res = self._solve_bqm(bqm, backend.sampler, **self.alg_kwargs) 30 return self._construct_result(res)
31 32 def _solve_bqm(self, bqm, sampler, **kwargs): 33 res = sampler.sample( 34 bqm, num_reads=self.num_reads, label=self.label, chain_strength=self.chain_strength, **kwargs) 35 return res 36 37 def _construct_result(self, result: SampleSet) -> Result: 38 distribution = {} 39 energies = {} 40 for (value, energy, occ) in zip(result.record.sample, result.record.energy, result.record.num_occurrences, strict=True): 41 bitstring = ''.join(map(str, value)) 42 if bitstring in distribution: 43 distribution[bitstring] += occ 44 continue 45 distribution[bitstring] = occ 46 energies[bitstring] = energy 47 48 return Result.from_distributions(distribution, energies, result)