Skip to main content

Simple BQM Sampling

This example demonstrates the basic usage of Dynex SDK for sampling Binary Quadratic Models.

Problem Setup

import dynex
import dimod

# Create a simple BQM
bqm = dimod.BinaryQuadraticModel(
    {0: 1.0, 1: -1.0},  # Linear coefficients
    {(0, 1): 0.5},      # Quadratic coefficients
    0.0,                # Offset
    'BINARY'
)

CPU Sampling

from dynex import ComputeBackend, DynexConfig

# Configure for CPU backend
config = DynexConfig(compute_backend=ComputeBackend.CPU)

# Create model and sampler
model = dynex.BQM(bqm)
sampler = dynex.DynexSampler(model, config=config)

# Sample the problem
sampleset = sampler.sample(num_reads=100)

# Print results
print(f"Best solution: {sampleset.first.sample}")
print(f"Best energy: {sampleset.first.energy}")

QPU Sampling

from dynex import QPUModel

# Configure for QPU backend
config = DynexConfig(
    compute_backend=ComputeBackend.QPU,
    qpu_model=QPUModel.APOLLO_RC1
)

sampler = dynex.DynexSampler(model, config=config)
sampleset = sampler.sample(num_reads=10, annealing_time=200)  # QPU: num_reads 1–100, annealing_time 10–1000

print(f"Quantum solution: {sampleset.first.sample}")

Next Steps