# 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.

Note

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:

- Hybrid Solvers such as Leap’s hybrid_v1 solver
- Classical Solvers such as
`dimod.ExactSolver`

for exact solutions to small problems - Quantum Solvers such a D-Wave 2000Q system.

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
`dwave.system.LeapHybridSampler`

is the Ocean sampler and the remote compute
resource selected might be Leap hybrid solver hybrid_v1.

```
>>> from dwave.system import LeapHybridSampler
>>> sampler = LeapHybridSampler() # doctest: +SKIP
>>> answer = sampler.sample(bqm) # doctest: +SKIP
>>> print(answer) # doctest: +SKIP
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
>>> dwave.inspector.show(response) # doctest: +SKIP
```

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 the D-Wave system used as a sampler.

Attention

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
>>> from dwave.system.composites import VirtualGraphComposite
>>> DWaveSampler(solver={'qpu':True}).properties['extended_j_range']
[-2.0, 1.0]
>>> embedding = {'x': {1}, 'y': {5}, 'z': {0, 4}}
>>> sampler = VirtualGraphComposite(DWaveSampler(solver={'qpu':True}), embedding) # doctest: +SKIP
>>> sampler.parameters # doctest: +SKIP
{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.