Solving Problems by Sampling

Having followed the steps of the Formulate Your Problem for a Quantum Computer section, you sample the BQM that now represents your problem for solutions. Ocean software provides quantum, classical, and quantum-classical hybrid samplers that run either remotely (for example, in D-Wave’s Leap environment) or locally on your CPU. These compute resources are known as solvers.


Some classical samplers actually brute-force solve small problems rather than sample, and these are also referred to as “solvers”.

Sample the BQM on a Solver

Ocean’s samplers enable you to submit your problem to remote or local compute resources (solvers) of different types:

The example code below submits the BQM of the AND gate of the Formulate Your Problem for a Quantum Computer section,

>>> import dimod
>>> bqm = dimod.BinaryQuadraticModel({'x1': 0.0, 'x2': 0.0, 'y1': 6.0},
...                  {('x2', 'x1'): 2.0, ('y1', 'x1'): -4.0, ('y1', 'x2'): -4.0},
...                  0, 'BINARY')

to a Leap hybrid solver. In this case, dwave-system’s LeapHybridSampler is the Ocean sampler and the remote compute resource selected might be Leap hybrid solver hybrid_binary_quadratic_model_version<x>.

>>> from dwave.system import LeapHybridSampler
>>> sampler = LeapHybridSampler()    
>>> answer = sampler.sample(bqm)   
>>> print(answer)    
x1 x2 y1 energy num_oc.
0  0  1  0    0.0       1
['BINARY', 1 rows, 1 samples, 3 variables]

Improve the Solutions

More complex problems than the ones shown above can benefit from some of the D-Wave system’s advanced features and Ocean software’s advanced tools.

When sampling directly on the D-Wave QPU, the mapping from problem variables to qubits, minor-embedding, can significantly affect performance. Ocean tools perform this mapping heuristically so simply rerunning a problem might improve results. Advanced users may customize the mapping by directly using the minorminer tool, setting a minor-embedding themselves, or using D-Wave’s problem-inspector tool.

For example, the Boolean AND Gate example submits the BQM representing an AND gate to a D-Wave system, which requires mapping the problem’s logical variables to qubits on the QPU. The code below invokes D-Wave’s problem-inspector tool to visualize the minor-embedding.

>>> import dwave.inspector
View rendered by Ocean's problem inspector.

View of the logical and embedded problem rendered by Ocean’s problem inspector. The AND gate’s original BQM is represented on the left; its embedded representation on a D-Wave 2000Q system, on the right, shows a two-qubit chain (qubits 176 and 180) for variable \(x2\). The tool is helpful in visualizing the quality of your embedding.

Example Postprocessing with a Greedy Solver improves samples returned from a QPU by post-processing with a classical greedy algorthim. D-Wave systems offer features such as spin-reversal (gauge) transforms and anneal offsets, which reduce the impact of possible analog and systematic errors.

You can see the parameters and properties a sampler supports. For example, Ocean’s dwave-system lets you use the D-Wave’s virtual graphs feature to simplify minor-embedding. The following example maps a problem’s variables x, y to qubits 1, 5 and variable z to two qubits 0 and 4, and checks some features supported on a D-Wave 2000Q system used as a sampler.


D-Wave’s virtual graphs feature can require many seconds of D-Wave system time to calibrate qubits to compensate for the effects of biases. If your account has limited D-Wave system access, consider using FixedEmbeddingComposite instead.

>>> from dwave.system import DWaveSampler, VirtualGraphComposite
>>> qpu = DWaveSampler(solver={'qpu': True, 'topology__type': 'chimera'})
[-2.0, 1.0]
>>> embedding = {'x': {1}, 'y': {5}, 'z': {0, 4}}
>>> sampler = VirtualGraphComposite(qpu, embedding)   
>>> sampler.parameters         
{u'anneal_offsets': ['parameters'],
 u'anneal_schedule': ['parameters'],
 u'annealing_time': ['parameters'],
 u'answer_mode': ['parameters'],
 'apply_flux_bias_offsets': [],
 u'auto_scale': ['parameters'], ...

Note that the composed sampler (VirtualGraphComposite in the last example) inherits properties from the child sampler (DWaveSampler in that example).

See the resources under Additional Tutorials and the System Documentation for more information.