dimod¶
dimod is a shared API for binary quadratic samplers. It provides a binary quadratic model (BQM) class that contains Ising and quadratic unconstrained binary optimization (QUBO) models used by samplers such as the D-Wave system. It also provides utilities for constructing new samplers and composed samplers and for minor-embedding. Its reference examples include several samplers and composed samplers.
Learn more about dimod on Read the Docs.
Example Usage¶
This example constructs a simple QUBO and converts it to Ising format.
>>> import dimod
>>> bqm = dimod.BinaryQuadraticModel({0: -1, 1: -1}, {(0, 1): 2}, 0.0, dimod.BINARY) # QUBO
>>> bqm_ising = bqm.change_vartype(dimod.SPIN, inplace=False) # Ising
An example of using one of the built-in test Samplers.
>>> import dimod
>>> h = {0: 0.0, 1: 0.0}
>>> J = {(0, 1): -1.0}
>>> bqm = dimod.BinaryQuadraticModel.from_ising(h, J)
>>> response = dimod.ExactSolver().sample(bqm)
>>> response.samples_matrix
matrix([[-1, -1],
[ 1, -1],
[ 1, 1],
[-1, 1]])
The example’s QUBO form, \(\text{E}(a_i, b_{i,j}; q_i) = -q_1 -q_2 + 2q_1 q_2\), is related to the Ising form, \(\text{E}(h_i, j_{i,j}; s_i) = \frac{1}{2}(s_1s_2-1)\), via the simple manipulation \(s_i=2q_i-1\).