import dynex
import dimod
import networkx as nx
from dynex import DynexConfig, ComputeBackend
# Build a random graph
G = nx.gnp_random_graph(20, 0.4, seed=42)
# MaxCut QUBO: maximize sum of (1 - x_i*x_j) for each edge (i,j)
# Equivalent: minimize sum of x_i*x_j - constant
Q = {}
for i, j in G.edges():
Q[(i, i)] = Q.get((i, i), 0) - 1
Q[(j, j)] = Q.get((j, j), 0) - 1
Q[(i, j)] = Q.get((i, j), 0) + 2
bqm = dimod.BinaryQuadraticModel.from_qubo(Q)
model = dynex.BQM(bqm)
config = DynexConfig(compute_backend=ComputeBackend.GPU)
sampler = dynex.DynexSampler(model, config=config)
sampleset = sampler.sample(num_reads=1000, annealing_time=200)
best = sampleset.first.sample
partition_0 = [v for v, val in best.items() if val == 0]
partition_1 = [v for v, val in best.items() if val == 1]
cut_edges = [(u, v) for u, v in G.edges() if best[u] != best[v]]
print(f"Cut size: {len(cut_edges)}")