D-Wave Optimization
raw JSON → 0.6.12 verified Fri May 01 auth: no python
Enables the formulation of nonlinear models for industrial optimization problems using D-Wave quantum and hybrid solvers. Current version 0.6.12, released approximately monthly.
pip install dwave-optimization Common errors
error ImportError: No module named 'dwave_optimization' ↓
cause Incorrect import path; package uses dot notation.
fix
Use 'from dwave.optimization import Model' (dot, not underscore).
error ModuleNotFoundError: No module named 'dwave.system' ↓
cause Sampler is in dwave-system, which is not installed by default.
fix
Install the missing package: pip install dwave-system
error AttributeError: 'Model' object has no attribute 'sample' ↓
cause The Model class does not have a sample method; use a sampler from dwave.system.
fix
Use DWaveSampler from dwave.system to sample the model.
Warnings
gotcha The Model class is imported from dwave.optimization, not dwave_optimization. Use dot notation. ↓
fix from dwave.optimization import Model
gotcha Symbols are defined and added to the model implicitly upon creation. Do not manually add symbols. ↓
fix x = model.integer(...) — the symbol is automatically registered.
deprecated The use of 'model.minimize()' with a list instead of a sum expression is deprecated. ↓
fix Use sum(x) or model.minimize(reduce(lambda a,b: a+b, symbols, 0))
gotcha Samplers are not included; you must install dwave-system (or dwave-neal for local QUBO sampling). ↓
fix pip install dwave-system
breaking In version 0.6.0, the symbol creation API changed: use model.integer(), model.binary(), model.real() instead of model.symbol(type=...). ↓
fix Replace model.symbol('Integer') with model.integer(...).
Imports
- Model wrong
from dwave_optimization import Modelcorrectfrom dwave.optimization import Model - DWaveSampler wrong
from dwave.optimization.samplers import DWaveSamplercorrectfrom dwave.system import DWaveSampler - Symbol
from dwave.optimization import Symbol
Quickstart
from dwave.optimization import Model
import os
# Create a model with a symbol
model = Model()
x = model.integer(0, 10, num=3)
model.minimize(sum(x))
# Submit to a solver (requires Leap API token)
token = os.environ.get('DWAVE_API_TOKEN', '')
if token:
from dwave.system import DWaveSampler
sampler = DWaveSampler(token=token)
sampleset = sampler.sample(model)
print(sampleset.first)
else:
print('DWAVE_API_TOKEN not set; install dwave-neal for local sampling from dimod')