Skip to content

Commit 671209d

Browse files
labs.py
1 parent 43c4c10 commit 671209d

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

scripts/labs.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Spin Glass Ground State (considered NP-complete)
2+
# Produced by Dan Strano, Elara (the OpenAI custom GPT)
3+
4+
from pyqrackising import spin_glass_solver
5+
import networkx as nx
6+
import numpy as np
7+
8+
import sys
9+
10+
11+
# LABS spin glass
12+
def generate_labs_maxcut(N, seed=None):
13+
if seed is not None:
14+
np.random.seed(seed)
15+
16+
# Initialize random ±1 sequence
17+
s = np.random.choice([-1, 1], size=N)
18+
19+
# Initialize weight matrix
20+
W = np.zeros((N, N))
21+
22+
# Build LABS autocorrelation-derived weights
23+
for k in range(1, N):
24+
for i in range(N - k):
25+
j = i + k
26+
# Each off-peak correlation contributes to (i,j)
27+
W[i, j] += 1
28+
W[j, i] += 1 # symmetry
29+
30+
# Normalize or scale weights (optional)
31+
W /= np.max(W)
32+
33+
return W, s
34+
35+
36+
if __name__ == "__main__":
37+
n_nodes = int(sys.argv[1]) if len(sys.argv) > 1 else 64
38+
quality = int(sys.argv[2]) if len(sys.argv) > 2 else None
39+
seed = int(sys.argv[3]) if len(sys.argv) > 3 else None
40+
G_m, s = generate_labs_maxcut(n_nodes, seed)
41+
best_bitstring, best_cut_value, best_cut, best_energy = spin_glass_solver(G_m, quality=quality)
42+
43+
print(f"Ground State Energy: {best_energy}")
44+
if not (seed is None):
45+
print(f"Seed {seed} solution: {best_bitstring}")

scripts/maxcut_approx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
7777
# NP-complete spin glass
7878
# G = generate_spin_glass_graph(seed=42)
7979

80-
# bitstring, cut_value, cut = maxcut_tfim_sparse(G)
81-
bitstring, cut_value, cut, energy = spin_glass_solver_sparse(G, is_spin_glass=False)
80+
bitstring, cut_value, cut = maxcut_tfim_sparse(G)
81+
# bitstring, cut_value, cut, energy = spin_glass_solver_sparse(G, is_spin_glass=False)
8282
# bitstring, cut_value, cut, energy = spin_glass_solver_sparse(G, best_guess=maxcut_tfim_sparse(G, quality=8)[0])
8383
print((bitstring, cut_value, cut))
8484

scripts/spin_glass.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Spin Glass Ground State (considered NP-complete)
22
# Produced by Dan Strano, Elara (the OpenAI custom GPT)
33

4-
from pyqrackising import spin_glass_solver
4+
from pyqrackising import spin_glass_solver_sparse
55
import networkx as nx
66
import numpy as np
77

@@ -19,7 +19,7 @@ def generate_spin_glass_graph(n_nodes=64, degree=3, seed=None):
1919
if __name__ == "__main__":
2020
# NP-complete spin glass
2121
G = generate_spin_glass_graph(n_nodes=64, seed=42)
22-
best_bitstring, best_cut_value, best_cut, best_energy = spin_glass_solver(G)
22+
best_bitstring, best_cut_value, best_cut, best_energy = spin_glass_solver_sparse(G)
2323

2424
print((best_bitstring, best_cut_value, best_cut))
2525
print(f"Ground State Energy: {best_energy}")

0 commit comments

Comments
 (0)