# Samplers¶

A sampler accepts a binary quadratic model (BQM) and returns variable assignments. Samplers generally try to find minimizing values but can also sample from distributions defined by the BQM.

## DWaveSampler¶

class DWaveSampler(failover=False, retry_interval=-1, **config)[source]

A class for using the D-Wave system as a sampler.

Uses parameters set in a configuration file, as environment variables, or explicitly as input arguments for selecting and communicating with a D-Wave system. For more information, see D-Wave Cloud Client.

Inherits from dimod.Sampler and dimod.Structured.

Parameters: failover (bool, optional, default=False) – Switch to a new QPU in the rare event that the currently connected system goes offline. Note that different QPUs may have different hardware graphs and a failover will result in a regenerated nodelist, edgelist, properties and parameters. retry_interval (number, optional, default=-1) – The amount of time (in seconds) to wait to poll for a solver in the case that no solver is found. If retry_interval is negative then it will instead propogate the SolverNotFoundError to the user. **config – Keyword arguments passed to dwave.cloud.client.Client.from_config().

Note

Prior to version 1.0.0, DWaveSampler used the base client, allowing non-QPU solvers to be selected. To reproduce the old behavior, instantiate DWaveSampler with client='base'.

Examples

This example submits a two-variable Ising problem mapped directly to two adjacent qubits on a D-Wave system. qubit_a is the first qubit in the QPU’s indexed list of qubits and qubit_b is one of the qubits coupled to it. Other required parameters for communication with the system, such as its URL and an autentication token, are implicitly set in a configuration file or as environment variables, as described in Configuring Access to D-Wave Solvers. Given sufficient reads (here 100), the quantum computer should return the best solution, $${1, -1}$$ on qubit_a and qubit_b, respectively, as its first sample (samples are ordered from lowest energy).

>>> from dwave.system import DWaveSampler
...
>>> sampler = DWaveSampler()
...
>>> qubit_a = sampler.nodelist[0]
>>> sampleset = sampler.sample_ising({qubit_a: -1, qubit_b: 1},
...                                  {},
>>> sampleset.first.sample[qubit_a] == 1 and sampleset.first.sample[qubit_b] == -1
True


See Ocean Glossary for explanations of technical terms in descriptions of Ocean tools.

### Properties¶

For parameters and properties of D-Wave systems, see D-Wave System Documentation.

 DWaveSampler.properties D-Wave solver properties as returned by a SAPI query. DWaveSampler.parameters D-Wave solver parameters in the form of a dict, where keys are keyword parameters accepted by a SAPI query and values are lists of properties in properties for each key. DWaveSampler.nodelist List of active qubits for the D-Wave solver. DWaveSampler.edgelist List of active couplers for the D-Wave solver. DWaveSampler.adjacency Adjacency structure formatted as a dict, where keys are the nodes of the structured sampler and values are sets of all adjacent nodes for each key node. DWaveSampler.structure Structure of the structured sampler formatted as a namedtuple, Structure(nodelist, edgelist, adjacency), where the 3-tuple values are the nodelist, edgelist and adjacency attributes.

### Methods¶

 DWaveSampler.sample(bqm[, warnings]) Sample from the specified Ising model. DWaveSampler.sample_ising(h, *args, **kwargs) Sample from an Ising model using the implemented sample method. DWaveSampler.sample_qubo(Q, **parameters) Sample from a QUBO using the implemented sample method. DWaveSampler.validate_anneal_schedule(…) Raise an exception if the specified schedule is invalid for the sampler. DWaveSampler.to_networkx_graph() Converts DWaveSampler’s structure to a Chimera or Pegasus NetworkX graph.

## DWaveCliqueSampler¶

class DWaveCliqueSampler(**config)[source]

A sampler for solving clique problems on the D-Wave system.

This sampler wraps find_clique_embedding() to generate embeddings with even chain length. These embeddings work well for dense binary quadratic models. For sparse models, using EmbeddingComposite with DWaveSampler is preferred.

Parameters: **config – Keyword arguments, as accepted by DWaveSampler

Examples

This example creates a BQM based on a 6-node clique (complete graph), with random $$\pm 1$$ values assigned to nodes, and submits it to a D-Wave system. Parameters for communication with the system, such as its URL and an autentication token, are implicitly set in a configuration file or as environment variables, as described in Configuring Access to D-Wave Solvers.

>>> from dwave.system import DWaveCliqueSampler
>>> import dimod
...
>>> bqm = dimod.generators.ran_r(1, 6)
...
>>> sampler = DWaveCliqueSampler()   # doctest: +SKIP
>>> sampler.largest_clique_size > 5  # doctest: +SKIP
True
>>> sampleset = sampler.sample(bqm, num_reads=100)   # doctest: +SKIP


### Properties¶

 DWaveCliqueSampler.largest_clique_size The maximum number of variables that can be embedded. DWaveCliqueSampler.properties A dict containing any additional information about the sampler. DWaveCliqueSampler.parameters A dict where keys are the keyword parameters accepted by the sampler methods and values are lists of the properties relevent to each parameter.

### Methods¶

 DWaveCliqueSampler.largest_clique() The clique embedding with the maximum number of source variables. DWaveCliqueSampler.sample(bqm[, chain_strength]) Sample from the specified binary quadratic model. DWaveCliqueSampler.sample_ising(h, J, …) Sample from an Ising model using the implemented sample method. DWaveCliqueSampler.sample_qubo(Q, **parameters) Sample from a QUBO using the implemented sample method.

## LeapHybridSampler¶

class LeapHybridSampler(solver=None, connection_close=True, **config)[source]

A class for using Leap’s cloud-based hybrid solvers.

Uses parameters set in a configuration file, as environment variables, or explicitly as input arguments for selecting and communicating with a hybrid solver. For more information, see D-Wave Cloud Client.

Inherits from dimod.Sampler.

Parameters: **config – Keyword arguments passed to dwave.cloud.client.Client.from_config().

Examples

This example builds a random sparse graph and uses a hybrid solver to find a maximum independent set.

>>> import dimod
>>> import networkx as nx
>>> import dwave_networkx as dnx
>>> import numpy as np
>>> from dwave.system import LeapHybridSampler
...
>>> # Create a maximum-independent set problem from a random graph
>>> problem_node_count = 300
>>> qubo = dnx.algorithms.independent_set.maximum_weighted_independent_set_qubo(G)
>>> bqm = dimod.BQM.from_qubo(qubo)
...
>>> # Find a good solution
>>> sampler = LeapHybridSampler()    # doctest: +SKIP
>>> sampleset = sampler.sample(bqm)           # doctest: +SKIP


### Properties¶

 LeapHybridSampler.properties Solver properties as returned by a SAPI query. LeapHybridSampler.parameters Solver parameters in the form of a dict, where keys are keyword parameters accepted by a SAPI query and values are lists of properties in properties for each key.

### Methods¶

 LeapHybridSampler.sample(bqm[, time_limit]) Sample from the specified binary quadratic model. LeapHybridSampler.sample_ising(h, J, …) Sample from an Ising model using the implemented sample method. LeapHybridSampler.sample_qubo(Q, **parameters) Sample from a QUBO using the implemented sample method.

## LeapHybridDQMSampler¶

class LeapHybridDQMSampler(solver=None, connection_close=True, **config)[source]

A class for using Leap’s cloud-based hybrid DQM solvers.

### Properties¶

 LeapHybridDQMSampler.properties Solver properties as returned by a SAPI query. LeapHybridDQMSampler.parameters Solver parameters in the form of a dict, where keys are keyword parameters accepted by a SAPI query and values are lists of properties in properties for each key.

### Methods¶

 LeapHybridDQMSampler.sample_dqm(dqm[, …]) Sample from the specified binary quadratic model.