def sample(self, bqm, num_reads=10): """Gives random samples. Args: todo Returns: :obj:`.Response`: The vartype will match the given binary quadratic model. Notes: For each variable in each sample, the value is chosen by a coin flip. """ values = tuple(bqm.vartype.value) def _itersample(): for __ in range(num_reads): sample = {v: choice(values) for v in bqm.linear} energy = bqm.energy(sample) yield sample, energy samples, energies = zip(*_itersample()) return Response.from_dicts(samples, {'energy': energies}, vartype=bqm.vartype)
def sample(self, bqm, beta_range=None, num_reads=10, num_sweeps=1000): """Sample from low-energy spin states using simulated annealing. Args: bqm (:obj:`~dimod.BinaryQuadraticModel`): Binary quadratic model to be sampled from. beta_range (tuple, optional): Beginning and end of the beta schedule (beta is the inverse temperature) as a 2-tuple. The schedule is applied linearly in beta. Default is chosen based on the total bias associated with each node. num_reads (int, optional): Number of reads. Each sample is the result of a single run of the simulated annealing algorithm. num_sweeps (int, optional): Number of sweeps or steps. Default is 1000. Returns: :obj:`~dimod.Response`: A `dimod` :obj:`.~dimod.Response` object. Note: This is a reference implementation, not optimized for speed and therefore not an appropriate sampler for benchmarking. Examples: This example provides samples for a two-variable QUBO model. >>> import dimod >>> sampler = dimod.SimulatedAnnealingSampler() >>> Q = {(0, 0): -1, (1, 1): -1, (0, 1): 2} >>> bqm = dimod.BinaryQuadraticModel.from_qubo(Q, offset = 0.0) >>> response = sampler.sample(bqm, num_reads=2) >>> response.data_vectors['energy'] # doctest: +SKIP array([-1., -1.]) """ # input checking # h, J are handled by the @ising decorator # beta_range, sweeps are handled by ising_simulated_annealing if not isinstance(num_reads, int): raise TypeError("'samples' should be a positive integer") if num_reads < 1: raise ValueError("'samples' should be a positive integer") h, J, offset = bqm.to_ising() # run the simulated annealing algorithm samples = [] energies = [] for __ in range(num_reads): sample, energy = ising_simulated_annealing(h, J, beta_range, num_sweeps) samples.append(sample) energies.append(energy) response = Response.from_dicts(samples, {'energy': energies}, vartype=Vartype.SPIN) response.change_vartype(bqm.vartype, {'energy': offset}, inplace=True) return response
def sample(self, bqm, beta_range=None, num_reads=10, num_sweeps=1000): """Sample from low-energy spin states using simulated annealing. Args: todo beta_range (tuple, optional): A 2-tuple defining the beginning and end of the beta schedule (beta is the inverse temperature). The schedule is applied linearly in beta. Default is chosen based on the total bias associated with each node. num_reads (int, optional): Each sample is the result of a single run of the simulated annealing algorithm. num_sweeps (int, optional): The number of sweeps or steps. Default is 1000. Returns: :obj:`Response` Examples: >>> sampler = SimulatedAnnealingSampler() >>> h = {0: -1, 1: -1} >>> J = {(0, 1): -1} >>> response = sampler.sample_ising(h, J, num_reads=1) >>> list(response.samples()) [{0: 1, 1: 1}] Note: This is a reference implementation, not optimized for speed and therefore not an appropriate sampler for benchmarking. """ # input checking # h, J are handled by the @ising decorator # beta_range, sweeps are handled by ising_simulated_annealing if not isinstance(num_reads, int): raise TypeError("'samples' should be a positive integer") if num_reads < 1: raise ValueError("'samples' should be a positive integer") h, J, offset = bqm.to_ising() # run the simulated annealing algorithm samples = [] energies = [] for __ in range(num_reads): sample, energy = ising_simulated_annealing(h, J, beta_range, num_sweeps) samples.append(sample) energies.append(energy) response = Response.from_dicts(samples, {'energy': energies}, vartype=Vartype.SPIN) response.change_vartype(bqm.vartype, {'energy': offset}, inplace=True) return response
def sample(self, bqm, num_reads=10): """Give random samples for a binary quadratic model. Args: bqm (:obj:`~dimod.BinaryQuadraticModel`): Binary quadratic model to be sampled from. num_reads (int, optional): Number of reads. Returns: :obj:`~dimod.Response`: A `dimod` :obj:`.~dimod.Response` object. Notes: For each variable in each sample, the value is chosen by a coin flip. Examples: This example provides samples for a two-variable Ising model. >>> import dimod >>> sampler = dimod.RandomSampler() >>> h = {0: -1, 1: -1} >>> J = {(0, 1): -1} >>> bqm = dimod.BinaryQuadraticModel(h, J, -0.5, dimod.SPIN) >>> response = sampler.sample(bqm, num_reads=3) >>> len(response) 3 >>> response.data_vectors['energy'] # doctest: +SKIP array([ 0.5, -3.5, 0.5]) """ values = tuple(bqm.vartype.value) def _itersample(): for __ in range(num_reads): sample = {v: choice(values) for v in bqm.linear} energy = bqm.energy(sample) yield sample, energy samples, energies = zip(*_itersample()) return Response.from_dicts(samples, {'energy': energies}, vartype=bqm.vartype)