Skip to content

Commit 5a884e5

Browse files
maxcut_multiobjective.py
1 parent e4308e6 commit 5a884e5

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

scripts/maxcut_multiobjective.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Random MAXCUT (for execution time tests)
2+
# Produced by Dan Strano, Elara (the OpenAI custom GPT)
3+
4+
from pyqrackising import maxcut_tfim_sparse, spin_glass_solver_sparse
5+
import networkx as nx
6+
import numpy as np
7+
import sys
8+
import time
9+
10+
11+
# Random heavy hex spin glass adjacency matrix
12+
def generate_heavy_hex_graph(
13+
num_nodes=42,
14+
num_edges=46,
15+
mean_weight=0.0,
16+
std_weight=1.0,
17+
seed=None,
18+
):
19+
"""
20+
Generate a random heavy-hex-like graph with Gaussian edge weights.
21+
22+
Parameters
23+
----------
24+
num_nodes : int
25+
Number of nodes in the graph.
26+
num_edges : int
27+
Number of edges in the graph.
28+
mean_weight : float
29+
Mean of Gaussian-distributed edge weights.
30+
std_weight : float
31+
Standard deviation of Gaussian-distributed edge weights.
32+
seed : int, optional
33+
Random seed for reproducibility.
34+
35+
Returns
36+
-------
37+
G : networkx.Graph
38+
Weighted heavy-hex-like graph.
39+
"""
40+
41+
rng = np.random.default_rng(seed)
42+
G = nx.Graph()
43+
G.add_nodes_from(range(num_nodes))
44+
45+
# build a pseudo-lattice backbone
46+
for i in range(num_nodes - 1):
47+
if rng.random() < 0.8: # 80% chance to connect to next node
48+
G.add_edge(i, (i + 1) % num_nodes)
49+
50+
# randomly add or remove edges to reach target edge count
51+
while len(G.edges) < num_edges:
52+
u, v = rng.integers(0, num_nodes, 2)
53+
if u != v and not G.has_edge(u, v):
54+
# Avoid too-high degree nodes (>=3) to mimic heavy hex
55+
if G.degree(u) < 3 and G.degree(v) < 3:
56+
G.add_edge(u, v)
57+
while len(G.edges) > num_edges:
58+
edge = random.choice(list(G.edges))
59+
G.remove_edge(*edge)
60+
61+
# assign Gaussian weights
62+
for u, v in G.edges:
63+
G[u][v]["weight"] = rng.normal(mean_weight, std_weight)
64+
65+
return G
66+
67+
68+
if __name__ == "__main__":
69+
seed = int(sys.argv[3]) if len(sys.argv) > 3 else 42
70+
71+
start = time.perf_counter()
72+
G = generate_heavy_hex_graph(num_nodes=42, num_edges=46, seed=42)
73+
seconds = time.perf_counter() - start
74+
print(f"{seconds} seconds to initialize the graph (statement of the problem itself)")
75+
76+
print(f"Random seed: {seed}")
77+
print(f"Node count: 42")
78+
start = time.perf_counter()
79+
bitstring, cut_value, cut, energy = spin_glass_solver_sparse(G, is_spin_glass=False)
80+
seconds = time.perf_counter() - start
81+
82+
print(f"Seconds to solution: {seconds}")
83+
print(f"Bipartite cut bit string: {bitstring}")
84+
print(f"Cut weight: {cut_value}")

0 commit comments

Comments
 (0)