Ocean-compatible collection of solvers/samplers
Project description
dwave-samplers
Ocean software provides a variety of quantum, classical, and quantum-classical dimod samplers that run either remotely (for example, in D-Wave’s Leap environment) or locally on your CPU.
dwave-samplers implements the following classical algorithms for solving binary quadratic models (BQM):
Planar: an exact solver for planar Ising problems with no linear biases.
Random: a sampler that draws uniform random samples.
Simulated Annealing: a probabilistic heuristic for optimization and approximate Boltzmann sampling well suited to finding good solutions of large problems.
Steepest Descent: a discrete analogue of gradient descent, often used in machine learning, that quickly finds a local minimum.
Tabu: a heuristic that employs local search with methods to escape local minima.
Tree Decomposition: an exact solver for problems with low treewidth.
Planar
There are polynomial-time algorithms for finding the ground state of a planar Ising model [1].
>>> from dwave.samplers import PlanarGraphSolver >>> solver = PlanarGraphSolver()
Get the ground state of a planar Ising model
>>> h = {} >>> J = {(0, 1): -1, (1, 2): -1, (0, 2): 1}
>>> sampleset = solver.sample_ising(h, J)
Random
Random samplers provide a useful baseline performance comparison. The variable assignments in each sample are chosen by a coin flip.
>>> from dwave.samplers import RandomSampler >>> sampler = RandomSampler()
Create a random binary quadratic model.
>>> import dimod >>> bqm = dimod.generators.gnp_random_bqm(100, .5, 'BINARY')
Get the best 5 sample found in .1 seconds.
>>> sampleset = sampler.sample(bqm, time_limit=.1, max_num_samples=5) >>> num_reads = sampleset.info['num_reads'] # the total number of samples generated
Simulated Annealing
Simulated annealing can be used for heuristic optimization or approximate Boltzmann sampling. The dwave-samplers implementation approaches the equilibrium distribution by performing updates at a sequence of decreasing temperatures, terminating at the target β.[2] Each spin is updated once in a fixed order per point per temperature according to a Metropolis-Hastings update. When the temperature is low the target distribution concentrates, at equilibrium, over ground states of the model. Samples are guaranteed to match the equilibrium for long, smooth temperature schedules.
β represents the inverse temperature, 1/(k T), of a Boltzmann distribution where T is the thermodynamic temperature in kelvin and k is Boltzmann’s constant.
>>> from dwave.samplers import SimulatedAnnealingSampler >>> sampler = SimulatedAnnealingSampler()
Create a random binary quadratic model.
>>> import dimod >>> bqm = dimod.generators.gnp_random_bqm(100, .5, 'BINARY')
Sample using simulated annealing with both the default temperature schedule and a custom one.
>>> sampleset = sampler.sample(bqm) >>> sampleset = sampler.sample(bqm, beta_range=[.1, 4.2], beta_schedule_type='linear')
Steepest Descent
Steepest descent is the discrete analogue of gradient descent, but the best move is computed using a local minimization rather rather than computing a gradient. The dimension along which to descend is determined, at each step, by the variable flip that causes the greatest reduction in energy.
Steepest descent is fast and effective for unfrustrated problems, but it can get stuck in local minima.
The quadratic unconstrained binary optimization (QUBO) E(x, y) = x + y - 2.5 * x * y, for example, has two local minima: (0, 0) with an energy of 0 and (1, 1) with an energy of -0.5.
>>> from dwave.samplers import SteepestDescentSolver >>> solver = SteepestDescentSolver()
Construct the QUBO:
>>> from dimod import Binaries >>> x, y = Binaries(['x', 'y']) >>> qubo = x + y - 2.5 * x * y
If the solver starts uphill from the global minimum, it takes the steepest path and finds the optimal solution.
>>> sampleset = solver.sample(qubo, initial_states={'x': 0, 'y': 1}) >>> print(sampleset) x y energy num_oc. num_st. 0 1 1 -0.5 1 1 ['BINARY', 1 rows, 1 samples, 2 variables]
If the solver starts in a local minimum, it gets stuck.
>>> sampleset = solver.sample(qubo, initial_states={'x': 0, 'y': 0}) >>> print(sampleset) x y energy num_oc. num_st. 0 0 0 0.0 1 0 ['BINARY', 1 rows, 1 samples, 2 variables]
Tabu
Tabu search is a heuristic that employs local search and can escape local minima by maintaining a “tabu list” of recently explored states that it does not revisit. The length of this tabu list is called the “tenure”. dwave-samplers implementats the MST2 multistart tabu search algorithm for quadratic unconstrained binary optimization (QUBO) problems.
Each read of the tabu algorithm consists of many starts. The solver takes the best non-tabu step repeatedly until it does not improve its energy any more.
>>> from dwave.samplers import TabuSampler >>> sampler = TabuSampler()
Construct a simple problem.
>>> from dimod import Binaries >>> a, b = Binaries(['a', 'b']) >>> qubo = -.5 * a + b - a * b
Sample using both default and custom values of tenure and number of restarts.
>>> sampleset0 = sampler.sample(qubo) >>> sampleset1 = sampler.sample(qubo, tenure=1, num_restarts=1)
Tree Decomposition
Tree decomposition-based solvers have a runtime that is exponential in the treewidth of the problem graph. For problems with low treewidth, the solver can find ground states very quickly. However, for even moderately dense problems, performance is very poor.
>>> from dwave.samplers import TreeDecompositionSolver >>> solver = TreeDecompositionSolver()
Construct a large, tree-shaped problem.
>>> import dimod >>> import networkx as nx >>> tree = nx.balanced_tree(2, 5) # binary tree with a height of five >>> bqm = dimod.BinaryQuadraticModel('SPIN') >>> bqm.set_linear(0, .5) >>> for u, v in tree.edges: ... bqm.set_quadratic(u, v, 1)
Because the BQM is a binary tree, it has a treewidth of 1 and can be solved exactly.
>>> sampleset = solver.sample(bqm) >>> print(sampleset) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ... 62 energy num_oc. 0 -1 +1 +1 -1 -1 -1 -1 +1 +1 +1 +1 +1 +1 +1 +1 -1 -1 -1 ... +1 -62.5 1 ['SPIN', 1 rows, 1 samples, 63 variables]
Installation
To install the core package:
pip install dwave-samplers
License
Released under the Apache License 2.0
Contributing
Ocean’s contributing guide has guidelines for contributing to Ocean packages.
Release Notes
dwave-samplers makes use of reno to manage its release notes.
When making a contribution to dwave-samplers that will affect users, create a new release note file by running
reno new your-short-descriptor-here
You can then edit the file created under releasenotes/notes/. Remove any sections not relevant to your changes. Commit the file along with your changes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for dwave_samplers-1.4.0-cp313-cp313-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88c97c606c4f5d45dc8183700939f2b6aa6ef95b0c2d93ad462c09487b03e3bd |
|
MD5 | 8ea42a050155f91bd0b073ee05a847a2 |
|
BLAKE2b-256 | 0440c05398fa1f7ec900fce468bd3e472a77aaf6ce0b004d66bccceb0538a2a2 |
Hashes for dwave_samplers-1.4.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88471baba97cc7f236c4b03c2210829fe41e1702acfc690a267bf5781c84c3b6 |
|
MD5 | c2bec8a7a7c2e0257750d6348966c078 |
|
BLAKE2b-256 | 6eb5c2b7a0c1f41716221f2b863b3bead610a9f0f0118c1588f78e6801fa8aa1 |
Hashes for dwave_samplers-1.4.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be461795b7f85447d6a3c15b348d53836a2de081c94385c5077eb783c1e5cd15 |
|
MD5 | fd1121214c3970d01dba09fa54edca70 |
|
BLAKE2b-256 | 98e61fa18e8f0d3d75cfa0102e8b3ee15bd6ecb4e02d8130855faa04007e8123 |
Hashes for dwave_samplers-1.4.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9468a0095e5fcbc6fed0f526f06ccebe045c3570e80f7f414555cc489d328caf |
|
MD5 | 86f6d54f54ae55f8d015e214e74d4b31 |
|
BLAKE2b-256 | db6780d6ef1f48938029bda38fe932c5dad66768435ac00854ea8228a11f479b |
Hashes for dwave_samplers-1.4.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 041434f9892c84d505a107f0ed78a5b0b43f6ec5be2bf0c8a9667838a39df1e8 |
|
MD5 | af56a86263499dd31bbc0c385ed333a6 |
|
BLAKE2b-256 | 94870edb51394c7dd0f23e6a4d090fb9d02a84f2cd6dde21f9b1bc26b99f1d16 |
Hashes for dwave_samplers-1.4.0-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc6b1a6831458385e3977afa0ae6f57fe6f52a76613dd138c398688f2f6e900b |
|
MD5 | e0b87feb7e6d43fb7a71994e8230de74 |
|
BLAKE2b-256 | 0db3da5b2086203db4527a8fd4d2fd9ea68e0304436fa83ffedb054b8a06c433 |
Hashes for dwave_samplers-1.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db90f6e71e24b35da1e91d32d565bab7c5f5b138d7cb68208ef46b2137983263 |
|
MD5 | 16c1c803a307acf177921997645bd31c |
|
BLAKE2b-256 | dd9dcca958efbcd473e03731924928382347a1116d571b282f0bcf7c75f45ddd |
Hashes for dwave_samplers-1.4.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3246bc4383201ee05c0e554a2737e321354569130dd6a3d90cd091ddfd87411 |
|
MD5 | 1a0d9b0231c6c254a04d2545d2725678 |
|
BLAKE2b-256 | 1b5b988fed1d8a745de309813050f96a95da5dfaeed9d748698410e1b482efcd |
Hashes for dwave_samplers-1.4.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03d845f45435ec906cbcf4cc2c643ff060e0483c03b82f17679306c1721a286c |
|
MD5 | 4b2b2d3681126b01c787a33109037448 |
|
BLAKE2b-256 | 05c2230d28fac40e2a915974582be351e1085f7d1a309cd81e1d75ba034268ef |
Hashes for dwave_samplers-1.4.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 782b992e2e760730406a49afeac738e1fa8915cd65a25a811b1431124219f655 |
|
MD5 | 2c25e7269c972779e8a5c9cf50f95a10 |
|
BLAKE2b-256 | ab317d347e1dd064db6a51165ceb659d7b1f14ec707ebc9acc9e65ec030dd236 |
Hashes for dwave_samplers-1.4.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ad0d07f5d48c31de239ed00688d91d2ced9627c9132f7eb108dd4c79ea4f3c2 |
|
MD5 | e800ea1314d461993f998193ccdc6408 |
|
BLAKE2b-256 | ffb8b3446c4c06ca68f7d58adefae979b5a3e9c97395c9cda5f7ea8d9100874d |
Hashes for dwave_samplers-1.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9459c7f0629f51a965c814a9f42946ffcdb44477b8698503fc6fd0493c4ddfb8 |
|
MD5 | 4d322784d2d37919af06af0c0e7e1b7c |
|
BLAKE2b-256 | 7e761b98098b740601abf85ae83c14657e51c38f1ddddd23662b791d8e70a820 |
Hashes for dwave_samplers-1.4.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fe5dee5bd3c9ddb6295335c0eb1b7b0056cdd0d64afcc6685506958f24d577a |
|
MD5 | 7e2bb6f523d2c8e6fbe184e4fe7f3913 |
|
BLAKE2b-256 | 1c3e01e86a6e5928b51896a9878d3708b1bf64b8711126e8bd90ee296d0ed177 |
Hashes for dwave_samplers-1.4.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fd4e28d2d4ada343b9d9288ceef845463aade759fb614e91facb98c557323ee |
|
MD5 | f115df538e0a4b2cd7ce827b0ed3985b |
|
BLAKE2b-256 | df6eaedcc98907c7a6ac45023656731ef286c4cf6935601ac46bc3dbba436309 |
Hashes for dwave_samplers-1.4.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52633c950d4c73d399cf73db58382f413b3affe8f674731a9070a4da3bac75fa |
|
MD5 | bef58129478363bb7c2c7789bf14538c |
|
BLAKE2b-256 | f2f30e0a08314620f45c1d54228a3baae7b59276dde061afcce7403ac921d986 |
Hashes for dwave_samplers-1.4.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e8fe5e70319ed230bdd94b6d05984379adbc5cefb25610e99baf663d33b5169 |
|
MD5 | bc203bfe0930864d8d8309b622f644b3 |
|
BLAKE2b-256 | ab4995cb67aea2178880feea728d8d54fa0241943dac5da18bf88dd657700688 |
Hashes for dwave_samplers-1.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 647af252e69d3dfe2df3793f1fd21e397fe16a7bf04d999428e34a89ba0143e9 |
|
MD5 | 541a5eeddd262371746a606c6eccf720 |
|
BLAKE2b-256 | 61a1451313cdcba8b312c3263d036cd0fc056b4f8db7764a96e55da4394cfe6a |
Hashes for dwave_samplers-1.4.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a35841abbfe2ceaa56036a71061df966568b3f03b4408e6990563491ba4da52 |
|
MD5 | 9eec9ad127aa3ab3e8395cdd546e2177 |
|
BLAKE2b-256 | 3ccc0b8b5bbdf5f6e452e327b7ae23baa184f4ddbb7ad2491c794c86043af56f |
Hashes for dwave_samplers-1.4.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a995f7ec2287525e4b7b5d4c37ce7ee3d97b31965a271b7637c5a9da037612e |
|
MD5 | a0b867957bf3cf5d8b3fb36763df9ab7 |
|
BLAKE2b-256 | b32e24297e8d2451a6c66ef4fd09dbb15200d75091d1a4865e87adfd50f611d2 |
Hashes for dwave_samplers-1.4.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 268675c867efef88e97ca591e1bfcf52dc1ca6508cd210fa6f0fde29da0cec1e |
|
MD5 | 5ce98a01a9bb1b9f755de54c7e012dcc |
|
BLAKE2b-256 | d13838358eb2832d0db58137310fc827060afd11d855c47045063e35a10a1606 |
Hashes for dwave_samplers-1.4.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80db29be04bae8860a04555298a2b7e808160a5bf1ebad4dfabd8243b2ffc743 |
|
MD5 | 81a634a765aaa9493b4a714d92fce0f8 |
|
BLAKE2b-256 | 8a32b469d40dcf5bb9ffdeb318c41f11819f11aa5562447b3d93bba9ad5a2953 |
Hashes for dwave_samplers-1.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9028d1aca1d887afaecceca560cbfd7bab3e14f65ea2758920bd62656ef8b5c |
|
MD5 | 10946b0d9ddc19f8d1a7943f69ac23cf |
|
BLAKE2b-256 | b7c8716c2e72363f21bb04c4c9d39727f5c3dc604e919af8d47474b8b0e0db2c |
Hashes for dwave_samplers-1.4.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee6acda48d0cfb5ae514627007d7e0cdf9c883e3e1a8ac53320e040fee547b1a |
|
MD5 | fbe4015009cad695a11073706257adb0 |
|
BLAKE2b-256 | 76cd881d3a8c5cc1eb73171832a3cb36acd14b027ebd5513a3e00d4dadb08242 |
Hashes for dwave_samplers-1.4.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f57cd1611f683dd98f552782b02685cc168dc9f66b71f9f0efafa653b125ca34 |
|
MD5 | 5a00d34c9c05ffc3f6a4f6b59837211f |
|
BLAKE2b-256 | 0137d068f88ec0260a576ce26acba21dafb399828ad19c9f567094e16e3b44a8 |
Hashes for dwave_samplers-1.4.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46a779b7b8b92412f0acbce2d1ab49db54069e5d0a26327fe4c54f84a1730f20 |
|
MD5 | 6ce054e43b6a37d59918128b9329c941 |
|
BLAKE2b-256 | 0784608f08bf0bed5b3cddd725766077152b2f78386439a93bc9cc6caea22e2c |