Samplers¶
A sampler accepts a problem in binary quadratic model (BQM) or discrete quadratic model (DQM) format and returns variable assignments. Samplers generally try to find minimizing values but can also sample from distributions defined by the problem.
DWaveSampler¶

class
DWaveSampler
(failover=False, retry_interval=1, **config)[source]¶ A class for using the DWave system as a sampler for binary quadratic models.
Uses parameters set in a configuration file, as environment variables, or explicitly as input arguments for selecting and communicating with a DWave system. For more information, see DWave Cloud Client.
Inherits from
dimod.Sampler
anddimod.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
andparameters
.  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 thebase
client, allowing nonQPU solvers to be selected. To reproduce the old behavior, instantiateDWaveSampler
withclient='base'
.Examples
This example submits a twovariable Ising problem mapped directly to two adjacent qubits on a DWave system.
qubit_a
is the first qubit in the QPU’s indexed list of qubits andqubit_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 DWave Solvers. Given sufficient reads (here 100), the quantum computer should return the best solution, \({1, 1}\) onqubit_a
andqubit_b
, respectively, as its first sample (samples are ordered from lowest energy).>>> from dwave.system import DWaveSampler ... >>> sampler = DWaveSampler() ... >>> qubit_a = sampler.nodelist[0] >>> qubit_b = next(iter(sampler.adjacency[qubit_a])) >>> sampleset = sampler.sample_ising({qubit_a: 1, qubit_b: 1}, ... {}, ... num_reads=100) >>> 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.
 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
Properties¶
For parameters and properties of DWave systems, see DWave System Documentation.
DWaveSampler.properties 
DWave solver properties as returned by a SAPI query. 
DWaveSampler.parameters 
DWave 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 DWave solver. 
DWaveSampler.edgelist 
List of active couplers for the DWave 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 3tuple values are the nodelist , edgelist and adjacency attributes. 
Methods¶
DWaveSampler.sample (bqm[, warnings]) 
Sample from the specified binary quadratic 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 binary quadratic models on the DWave 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, usingEmbeddingComposite
withDWaveSampler
is preferred.Parameters: **config – Keyword arguments, as accepted by DWaveSampler
Examples
This example creates a BQM based on a 6node clique (complete graph), with random \(\pm 1\) values assigned to nodes, and submits it to a DWave 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 DWave 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 cloudbased hybrid BQM solvers.
Leap’s quantumclassical hybrid BQM solvers are intended to solve arbitrary application problems formulated as binary quadratic models (BQM).
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 DWave 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 maximumindependent set problem from a random graph >>> problem_node_count = 300 >>> G = nx.random_geometric_graph(problem_node_count, radius=0.0005*problem_node_count) >>> 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. 
LeapHybridSampler.min_time_limit (bqm) 
Return the minimum time_limit accepted for the given problem. 
LeapHybridDQMSampler¶

class
LeapHybridDQMSampler
(solver=None, connection_close=True, **config)[source]¶ A class for using Leap’s cloudbased hybrid DQM solvers.
Leap’s quantumclassical hybrid DQM solvers are intended to solve arbitrary application problems formulated as discrete quadratic models (DQM).
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 DWave Cloud Client.
Parameters: **config – Keyword arguments passed to dwave.cloud.client.Client.from_config()
.Examples
This example solves a small, illustrative problem: a game of rockpaperscissors. The DQM has two variables representing two hands, with cases for rock, paper, scissors. Quadratic biases are set to produce a lower value of the DQM for cases of variable
my_hand
interacting with cases of variabletheir_hand
such that the former wins over the latter; for example, the interaction ofrockscissors
is set to 1 whilescissorsrock
is set to +1.>>> import dimod >>> from dwave.system import LeapHybridDQMSampler ... >>> cases = ["rock", "paper", "scissors"] >>> win = {"rock": "scissors", "paper": "rock", "scissors": "paper"} ... >>> dqm = dimod.DiscreteQuadraticModel() >>> dqm.add_variable(3, label='my_hand') 'my_hand' >>> dqm.add_variable(3, label='their_hand') 'their_hand' >>> for my_idx, my_case in enumerate(cases): ... for their_idx, their_case in enumerate(cases): ... if win[my_case] == their_case: ... dqm.set_quadratic('my_hand', 'their_hand', ... {(my_idx, their_idx): 1}) ... if win[their_case] == my_case: ... dqm.set_quadratic('my_hand', 'their_hand', ... {(my_idx, their_idx): 1}) ... >>> dqm_sampler = LeapHybridDQMSampler() # doctest: +SKIP ... >>> sampleset = dqm_sampler.sample_dqm(dqm) # doctest: +SKIP >>> print("{} beats {}".format(cases[sampleset.first.sample['my_hand']], ... cases[sampleset.first.sample['their_hand']])) # doctest: +SKIP rock beats scissors
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 discrete quadratic model. 
LeapHybridDQMSampler.min_time_limit (dqm) 
Return the minimum time_limit accepted for the given problem. 