dimod: Binary Quadratic Model API

0.12.21 · active · verified Thu Apr 16

dimod is a shared API for binary quadratic model (BQM) samplers. It provides classes for representing quadratic models like Ising and QUBO, as well as higher-order models, used by solvers such as the D-Wave quantum computer. The library also offers utilities for constructing new samplers, composed samplers, and tools for working with BQMs. The current version is 0.12.21, with an active release cadence, frequently adding new features and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a BinaryQuadraticModel (BQM) with binary variables, solve it using dimod's built-in ExactSolver, and inspect the resulting samples and their energies. It also shows how to convert the BQM's variable type (vartype) between BINARY and SPIN.

import dimod

# Construct a simple QUBO problem
# Represents E(x0, x1) = -x0 - x1 + 2*x0*x1
bqm = dimod.BinaryQuadraticModel({0: -1, 1: -1}, {(0, 1): 2}, 0.0, dimod.BINARY)

# Use dimod's brute-force exact solver
sampler = dimod.ExactSolver()
response = sampler.sample(bqm)

print("SampleSet:\n", response)

# Access samples and energies
for sample, energy in response.data(['sample', 'energy']):
    print(f"Sample: {sample}, Energy: {energy}")

# Convert the BQM to Ising format
bqm_ising = bqm.change_vartype(dimod.SPIN, inplace=False)
print("\nIsing BQM vartype:", bqm_ising.vartype)

view raw JSON →