A sampler accepts a problem in quadratic model (e.g., BQM, CQM) or
nonlinear model format and returns variable assignments.
Samplers generally try to find minimizing values but can also sample from
distributions defined by the problem.
These samplers are non-blocking: the returned SampleSet is constructed
from a Future-like object that is resolved on the first
read of any of its properties; for example, by printing the results. Your code can
query its status with the done() method or ensure resolution
with the resolve() method.
Other Ocean packages provide additional samplers; for example,
dimod provides samplers for testing
your code.
Submits binary quadratic models directly to D-Wave quantum computers.
Linear and quadratic terms of the binary quadratic model (BQM) must
map directly to qubit and coupler indices of the selected QPU.
Typically this mapping (minor-embedding) is handled by software
(e.g., the EmbeddingComposite class) but for small problems can be
manual.
You can configure your solver selection and usage by setting
parameters, hierarchically, in a configuration file, as environment
variables, or explicitly as input arguments. For more information, see the
get_solvers() method. By default, online
QPUs are returned ordered by highest number of qubits.
Signal a failover condition if a sampling error occurs. When
True, raises FailoverCondition
or RetryCondition on sampleset
resolve() to signal failover.
Actual failover (i.e., selection of a new solver) has to be handled
by the user. A convenience method trigger_failover() is
available for this. Note that hardware graphs vary between QPUs, so
triggering failover results in regenerated nodelist,
edgelist, properties and parameters.
Changed in version 1.16.0: In the past, the sample() method was blocking and
failover=True caused a solver failover and sampling retry.
However, this failover implementation broke when sample()
became non-blocking (asynchronous), Setting failover=True had
no effect.
retry_interval (number, optional, default=-1) –
Ignored, but kept for backward compatibility.
Changed in version 1.16.0: Ignored since 1.16.0. See note for failover parameter above.
**config – Keyword arguments passed to
from_config().
Added in version 1.29.0: Support for context manager protocol.
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'.
Alternatively, call the close() method to
terminate the sampler resources:
>>> sampler=DWaveSampler()...>>> sampler.close()
Examples
This example submits a two-variable Ising problem mapped directly to two
adjacent qubits on a QPU. 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 authentication token, are implicitly set
in a configuration file or as environment variables, as described in the
Configuring Access to the Leap Service (Basic) section. 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).
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.
Signal a failover condition if a sampling error occurs. When
True, raises FailoverCondition
or RetryCondition on sampleset
resolve() to signal failover.
Actual failover (i.e., selection of a new solver) has to be handled
by the user. A convenience method trigger_failover() is
available for this. Note that hardware graphs vary between QPUs, so
triggering failover results in regenerated
nodelist,
edgelist,
properties and parameters.
Changed in version 1.16.0: In the past, the sample() method was blocking and
failover=True caused a solver failover and sampling retry.
However, this failover implementation broke when sample()
became non-blocking (asynchronous), Setting failover=True had
no effect.
retry_interval (number, optional, default=-1) –
Ignored, but kept for backward compatibility.
Changed in version 1.16.0: Ignored since 1.16.0. See note for failover parameter above.
**config – Keyword arguments, as accepted by DWaveSampler.
Added in version 1.29.0: Support for context manager protocol.
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 authentication token, are implicitly set in a
configuration file or as environment variables, as described in
the Configuring Access to the Leap Service (Basic) section.
Parameters as a dict, where keys are keyword parameters accepted by the sampler methods and values are lists of the properties relevent to each parameter.
Submits binary quadratic models to a hybrid solver in the Leap service.
The Leap service’s quantum-classical hybridbinary quadratic model (BQM) solvers are intended to solve arbitrary
application problems formulated as BQMs.
Submits constrained quadratic models to a hybrid solver in the Leap service.
The Leap service’s quantum-classical hybridconstrained quadratic model (CQM) solvers are intended to solve
arbitrary application problems formulated as CQMs.
**config – Keyword arguments passed to
from_config().
Examples
This example solves a simple problem of finding the rectangle with the
greatest area when the perimeter is limited. In this example, the
perimeter of the rectangle is set to 8 (meaning the largest area is for
the \(2X2\) square).
A CQM is created that will have two integer variables, \(i, j\), each
limited to half the maximum perimeter length of 8, to represent the
lengths of the rectangle’s sides:
The area of the rectangle is given by the multiplication of side \(i\)
by side \(j\). The goal is to maximize the area, \(i*j\). Because
D-Wave samplers minimize, the objective should have its lowest value when
this goal is met. Objective \(-i*j\) has its minimum value when
\(i*j\), the area, is greatest:
>>> cqm.set_objective(-i*j)
Finally, the requirement that the sum of both sides must not exceed the
perimeter is represented as constraint \(2i + 2j <= 8\):
The best (lowest-energy) solution found has \(i=j=2\) as expected,
a solution that is feasible because all the constraints (one in this
example) are satisfied.
>>> fromdwave.optimization.generatorsimportflow_shop_scheduling>>> fromdwave.systemimportLeapHybridNLSampler...>>> withLeapHybridNLSampler()assampler:... processing_times=[[10,5,7],[20,10,15]]... model=flow_shop_scheduling(processing_times=processing_times)... results=sampler.sample(model,label="Small FSS problem")... job_order=next(model.iter_decisions())... print(f"State 0 of {model.objective.state_size()} has an "... f"objective value {model.objective.state(0)} for order "... f"{job_order.state(0)}.")State 0 of 8 has an objective value 50.0 for order [1. 2. 0.].
Submits discrete quadratic models to a hybrid solver in the Leap service.
The Leap service’s quantum-classical hybriddiscrete quadratic model (DQM) solvers are intended to solve
arbitrary application problems formulated as DQMs.
**config – Keyword arguments passed to
from_config().
Examples
This example solves a small, illustrative problem: a game of
rock-paper-scissors. 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 variable their_hand such that the former
wins over the latter; for example, the interaction of rock-scissors
is set to -1 while scissors-rock is set to +1.