The code in this example requires that your development environment have Ocean software and be configured to access SAPI, as described in the Configuring Access to the Leap Service (Basic) section.
The Basic Workflow: Formulation and Sampling section describes the problem-solving workflow as consisting of two main steps: (1) Formulate the problem as an objective function in a supported model and (2) Solve your model with a D‑Wave solver.
Note
The following code sets a sampler without specifying SAPI parameters. Configure a default solver, as described in the Configuring Access to the Leap Service (Basic) section, to run the code as is, or see the dwave-cloud-client tool on how to access a particular solver by setting explicit parameters in your code or environment variables.
The Leap quantum cloud service provides hybrid solvers you can submit your models to. These solvers, which implement state-of-the-art classical algorithms together with intelligent allocation of the quantum processing unit (QPU) to parts of the problem where it benefits most, are designed to accommodate even very large problems. The Leap services’solvers can relieve you of the burden of any current and future development and optimization of hybrid algorithms that best solve your problem.
The following code solves a random problem on a quantum computer.
>>> import dimod
>>> import dwave.system
...
>>> bqm = dimod.generators.ran_r(1, 20)
>>> sampler = dwave.system.EmbeddingComposite(dwave.system.DWaveSampler())
>>> sampleset = sampler.sample(bqm, num_reads=100)
The following code solves the known “minimum vertex cover” graph problem using an annealing quantum computer.
>>> import networkx as nx
>>> import dwave_networkx as dnx
>>> from dwave.system import DWaveSampler, EmbeddingComposite
...
>>> s5 = nx.star_graph(4)
>>> sampler = EmbeddingComposite(DWaveSampler())
>>> min_cover = dnx.min_vertex_cover(s5, sampler)
The following code solves an illustrative traveling-salesperson problem using a quantum-classical hybrid solver in the Leap service.
>>> from dwave.optimization.generators import traveling_salesperson
>>> from dwave.system import LeapHybridNLSampler
...
>>> DISTANCE_MATRIX = [
... [0, 656, 227, 578, 489],
... [656, 0, 889, 141, 170],
... [227, 889, 0, 773, 705],
... [578, 141, 773, 0, 161],
... [489, 170, 705, 161, 0]]
...
>>> model = traveling_salesperson(distance_matrix=DISTANCE_MATRIX)
>>> with LeapHybridNLSampler() as sampler:
... results = sampler.sample(
... model,
... label='SDK Examples - TSP')
The following code creates a constrained quadratic model (CQM) representing a knapsack problem and solves it using a quantum-classical hybrid solver in the Leap service.
>>> from dimod.generators import random_knapsack
>>> from dwave.system import LeapHybridCQMSampler
...
>>> cqm = random_knapsack(10)
>>> sampler = LeapHybridCQMSampler()
>>> sampleset = sampler.sample_cqm(cqm,
... time_limit=180,
... label="SDK Examples - Bin Packing")