PyQrackIsing 5.1.6__tar.gz → 5.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyqrackising-5.1.6/pyqrackising.egg-info → pyqrackising-5.2.1}/PKG-INFO +1 -1
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/README.md +2 -2
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyproject.toml +1 -1
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/maxcut_tfim.py +1 -1
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/spin_glass_solver.py +4 -4
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/spin_glass_solver_sparse.py +4 -4
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/spin_glass_solver_streaming.py +5 -5
- {pyqrackising-5.1.6 → pyqrackising-5.2.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising.egg-info/SOURCES.txt +0 -2
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/setup.py +1 -1
- pyqrackising-5.1.6/pyqrackising/maxcut_tfim_sparse_uniform.py +0 -273
- pyqrackising-5.1.6/pyqrackising/maxcut_tfim_streaming_uniform.py +0 -228
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/CMakeLists.txt +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/LICENSE.md +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/MANIFEST.in +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising/tsp.py +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-5.1.6 → pyqrackising-5.2.1}/setup.cfg +0 -0
|
@@ -73,10 +73,10 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
|
|
|
73
73
|
|
|
74
74
|
|
|
75
75
|
G = generate_spin_glass_graph(n_nodes=64, seed=42)
|
|
76
|
-
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=
|
|
76
|
+
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=5, best_guess=None)
|
|
77
77
|
# solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=6)[0])
|
|
78
78
|
```
|
|
79
|
-
We also provide `spin_glass_solver_sparse(G)`, for `scipy` **upper-triangular** CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `
|
|
79
|
+
We also provide `spin_glass_solver_sparse(G)`, for `scipy` **upper-triangular** CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `5`. `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). By default, `spin_glass_solver()` uses `maxcut_tfim(G)` with passed-through `quality` as `best_guess`, which typically works well, but it could be seeded with higher `maxcut_tfim()` `quality` or Goemans-Williamson, for example. This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
|
|
80
80
|
|
|
81
81
|
From the `spin_glass_solver()`, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
|
|
82
82
|
```py
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "pyqrackising"
|
|
15
|
-
version = "5.1
|
|
15
|
+
version = "5.2.1"
|
|
16
16
|
requires-python = ">=3.8"
|
|
17
17
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
18
18
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -118,7 +118,7 @@ def init_J_and_z(G_m):
|
|
|
118
118
|
J_max = -float("inf")
|
|
119
119
|
for n in prange(n_qubits):
|
|
120
120
|
degree = sum(G_m[n] != 0.0)
|
|
121
|
-
J = -G_m[n].sum() / degree if degree > 0 else 0
|
|
121
|
+
J = (-G_m[n].sum() / degree) if degree > 0 else 0
|
|
122
122
|
degrees[n] = degree
|
|
123
123
|
J_eff[n] = J
|
|
124
124
|
J_abs = abs(J)
|
|
@@ -42,8 +42,8 @@ def bootstrap_worker(theta, G_m, indices):
|
|
|
42
42
|
|
|
43
43
|
@njit(parallel=True)
|
|
44
44
|
def bootstrap(theta, G_m, k, indices_array):
|
|
45
|
-
n =
|
|
46
|
-
energies = np.empty(n, dtype=np.
|
|
45
|
+
n = theta.shape[0]
|
|
46
|
+
energies = np.empty(n, dtype=np.float32)
|
|
47
47
|
for i in prange(n):
|
|
48
48
|
j = i * k
|
|
49
49
|
energies[i] = bootstrap_worker(theta, G_m, indices_array[j : j + k])
|
|
@@ -56,7 +56,7 @@ def int_to_bitstring(integer, length):
|
|
|
56
56
|
return (bin(integer)[2:].zfill(length))[::-1]
|
|
57
57
|
|
|
58
58
|
|
|
59
|
-
def spin_glass_solver(G, quality=
|
|
59
|
+
def spin_glass_solver(G, quality=5, shots=None, best_guess=None):
|
|
60
60
|
nodes = None
|
|
61
61
|
n_qubits = 0
|
|
62
62
|
G_m = None
|
|
@@ -92,7 +92,7 @@ def spin_glass_solver(G, quality=6, shots=None, best_guess=None):
|
|
|
92
92
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
93
93
|
else:
|
|
94
94
|
bitstring, _, _ = maxcut_tfim(G_m, quality=quality, shots=shots)
|
|
95
|
-
best_theta = [b == "1" for b in list(bitstring)]
|
|
95
|
+
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
96
96
|
|
|
97
97
|
min_energy = compute_energy(best_theta, G_m)
|
|
98
98
|
improved = True
|
|
@@ -44,8 +44,8 @@ def bootstrap_worker(theta, G_data, G_rows, G_cols, indices):
|
|
|
44
44
|
|
|
45
45
|
@njit(parallel=True)
|
|
46
46
|
def bootstrap(theta, G_data, G_rows, G_cols, k, indices_array):
|
|
47
|
-
n =
|
|
48
|
-
energies = np.empty(n, dtype=np.
|
|
47
|
+
n = theta.shape[0]
|
|
48
|
+
energies = np.empty(n, dtype=np.float32)
|
|
49
49
|
for i in prange(n):
|
|
50
50
|
j = i * k
|
|
51
51
|
energies[i] = bootstrap_worker(theta, G_data, G_rows, G_cols, indices_array[j : j + k])
|
|
@@ -70,7 +70,7 @@ def int_to_bitstring(integer, length):
|
|
|
70
70
|
return (bin(integer)[2:].zfill(length))[::-1]
|
|
71
71
|
|
|
72
72
|
|
|
73
|
-
def spin_glass_solver_sparse(G, quality=
|
|
73
|
+
def spin_glass_solver_sparse(G, quality=5, shots=None, best_guess=None):
|
|
74
74
|
nodes = None
|
|
75
75
|
n_qubits = 0
|
|
76
76
|
G_m = None
|
|
@@ -106,7 +106,7 @@ def spin_glass_solver_sparse(G, quality=6, shots=None, best_guess=None):
|
|
|
106
106
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
107
107
|
else:
|
|
108
108
|
bitstring, _, _ = maxcut_tfim_sparse(G_m, quality=quality, shots=shots)
|
|
109
|
-
best_theta = [b == "1" for b in list(bitstring)]
|
|
109
|
+
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
110
110
|
|
|
111
111
|
min_energy = compute_energy(best_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
112
112
|
improved = True
|
|
@@ -42,8 +42,8 @@ def bootstrap_worker(theta, G_func, G_func_args_tuple, nodes, indices):
|
|
|
42
42
|
|
|
43
43
|
@njit(parallel=True)
|
|
44
44
|
def bootstrap(theta, G_func, G_func_args_tuple, nodes, k, indices_array):
|
|
45
|
-
n =
|
|
46
|
-
energies = np.empty(n, dtype=np.
|
|
45
|
+
n = theta.shape[0]
|
|
46
|
+
energies = np.empty(n, dtype=np.float32)
|
|
47
47
|
for i in prange(n):
|
|
48
48
|
j = i * k
|
|
49
49
|
energies[i] = bootstrap_worker(theta, G_func, G_func_args_tuple, nodes, indices_array[j : j + k])
|
|
@@ -60,7 +60,7 @@ def spin_glass_solver_streaming(
|
|
|
60
60
|
G_func,
|
|
61
61
|
nodes,
|
|
62
62
|
G_func_args_tuple=None,
|
|
63
|
-
quality=
|
|
63
|
+
quality=5,
|
|
64
64
|
shots=None,
|
|
65
65
|
best_guess=None
|
|
66
66
|
):
|
|
@@ -89,7 +89,7 @@ def spin_glass_solver_streaming(
|
|
|
89
89
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
90
90
|
else:
|
|
91
91
|
bitstring, _, _ = maxcut_tfim_streaming(G_func, nodes, G_func_args_tuple=G_func_args_tuple, quality=quality, shots=shots)
|
|
92
|
-
best_theta = [b == "1" for b in list(bitstring)]
|
|
92
|
+
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
93
93
|
|
|
94
94
|
min_energy = compute_energy(best_theta, G_func, G_func_args_tuple, nodes)
|
|
95
95
|
improved = True
|
|
@@ -106,7 +106,7 @@ def spin_glass_solver_streaming(
|
|
|
106
106
|
combos = list(
|
|
107
107
|
item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
|
|
108
108
|
)
|
|
109
|
-
energies = bootstrap(theta,
|
|
109
|
+
energies = bootstrap(theta, G_m.data, G_m.indptr, G_m.indices, k, combos)
|
|
110
110
|
|
|
111
111
|
energy = energies.min()
|
|
112
112
|
if energy < min_energy:
|
|
@@ -10,9 +10,7 @@ pyqrackising/generate_tfim_samples.py
|
|
|
10
10
|
pyqrackising/kernels.cl
|
|
11
11
|
pyqrackising/maxcut_tfim.py
|
|
12
12
|
pyqrackising/maxcut_tfim_sparse.py
|
|
13
|
-
pyqrackising/maxcut_tfim_sparse_uniform.py
|
|
14
13
|
pyqrackising/maxcut_tfim_streaming.py
|
|
15
|
-
pyqrackising/maxcut_tfim_streaming_uniform.py
|
|
16
14
|
pyqrackising/maxcut_tfim_util.py
|
|
17
15
|
pyqrackising/spin_glass_solver.py
|
|
18
16
|
pyqrackising/spin_glass_solver_sparse.py
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='5.1
|
|
54
|
+
version='5.2.1',
|
|
55
55
|
author='Dan Strano',
|
|
56
56
|
author_email='stranoj@gmail.com',
|
|
57
57
|
description='Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)',
|
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
import math
|
|
2
|
-
import networkx as nx
|
|
3
|
-
import numpy as np
|
|
4
|
-
import os
|
|
5
|
-
from numba import njit, prange
|
|
6
|
-
|
|
7
|
-
from .maxcut_tfim_util import init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
|
-
|
|
9
|
-
IS_OPENCL_AVAILABLE = True
|
|
10
|
-
try:
|
|
11
|
-
import pyopencl as cl
|
|
12
|
-
except ImportError:
|
|
13
|
-
IS_OPENCL_AVAILABLE = False
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
@njit
|
|
17
|
-
def binary_search(l, t):
|
|
18
|
-
left = 0
|
|
19
|
-
right = len(l) - 1
|
|
20
|
-
|
|
21
|
-
while left <= right:
|
|
22
|
-
mid = (left + right) >> 1
|
|
23
|
-
|
|
24
|
-
if l[mid] == t:
|
|
25
|
-
return mid
|
|
26
|
-
|
|
27
|
-
if l[mid] < t:
|
|
28
|
-
left = mid + 1
|
|
29
|
-
else:
|
|
30
|
-
right = mid - 1
|
|
31
|
-
|
|
32
|
-
return len(l)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
36
|
-
@njit
|
|
37
|
-
def local_repulsion_choice(G_cols, G_rows, n, m):
|
|
38
|
-
"""
|
|
39
|
-
Pick m nodes out of n with repulsion bias:
|
|
40
|
-
- High-degree nodes are already less likely
|
|
41
|
-
- After choosing a node, its neighbors' probabilities are further reduced
|
|
42
|
-
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
43
|
-
weights: float64 array of shape (n,)
|
|
44
|
-
"""
|
|
45
|
-
|
|
46
|
-
weights = np.ones(n, dtype=np.float32)
|
|
47
|
-
chosen = np.zeros(m, dtype=np.int32) # store chosen indices
|
|
48
|
-
available = np.ones(n, dtype=np.bool_) # True = available, False = not
|
|
49
|
-
mask = np.zeros(n, dtype=np.bool_)
|
|
50
|
-
chosen_count = 0
|
|
51
|
-
|
|
52
|
-
for _ in range(m):
|
|
53
|
-
# Count available
|
|
54
|
-
total_w = 0.0
|
|
55
|
-
for i in range(n):
|
|
56
|
-
if available[i]:
|
|
57
|
-
total_w += weights[i]
|
|
58
|
-
if total_w <= 0:
|
|
59
|
-
break
|
|
60
|
-
|
|
61
|
-
# Normalize & sample
|
|
62
|
-
r = np.random.rand()
|
|
63
|
-
cum = 0.0
|
|
64
|
-
node = -1
|
|
65
|
-
for i in range(n):
|
|
66
|
-
if available[i]:
|
|
67
|
-
cum += weights[i] / total_w
|
|
68
|
-
if r < cum:
|
|
69
|
-
node = i
|
|
70
|
-
break
|
|
71
|
-
|
|
72
|
-
if node == -1:
|
|
73
|
-
continue
|
|
74
|
-
|
|
75
|
-
# Select node
|
|
76
|
-
chosen[chosen_count] = node
|
|
77
|
-
chosen_count += 1
|
|
78
|
-
available[node] = False
|
|
79
|
-
mask[node] = True
|
|
80
|
-
|
|
81
|
-
# Repulsion: penalize neighbors
|
|
82
|
-
for j in range(G_rows[node], G_rows[node + 1]):
|
|
83
|
-
nbr = G_cols[j]
|
|
84
|
-
if available[nbr]:
|
|
85
|
-
weights[nbr] *= 0.5
|
|
86
|
-
|
|
87
|
-
for nbr in range(node):
|
|
88
|
-
if not available[nbr]:
|
|
89
|
-
continue
|
|
90
|
-
start = G_rows[nbr]
|
|
91
|
-
end = G_rows[nbr + 1]
|
|
92
|
-
j = binary_search(G_cols[start:end], node) + start
|
|
93
|
-
if j < end:
|
|
94
|
-
weights[nbr] *= 0.5
|
|
95
|
-
|
|
96
|
-
return mask
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
@njit
|
|
100
|
-
def compute_energy(sample, G_data, G_rows, G_cols):
|
|
101
|
-
n_qubits = G_rows.shape[0] - 1
|
|
102
|
-
energy = 0
|
|
103
|
-
for u in range(n_qubits):
|
|
104
|
-
for col in range(G_rows[u], G_rows[u + 1]):
|
|
105
|
-
v = G_cols[col]
|
|
106
|
-
energy += G_data[col] * (1 if sample[u] == sample[v] else -1)
|
|
107
|
-
|
|
108
|
-
return energy
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
@njit(parallel=True)
|
|
112
|
-
def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds):
|
|
113
|
-
n = G_rows.shape[0] - 1
|
|
114
|
-
|
|
115
|
-
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
116
|
-
energies = np.empty(shots, dtype=np.float32)
|
|
117
|
-
|
|
118
|
-
for s in prange(shots):
|
|
119
|
-
# First dimension: Hamming weight
|
|
120
|
-
mag_prob = np.random.random()
|
|
121
|
-
m = 0
|
|
122
|
-
while thresholds[m] < mag_prob:
|
|
123
|
-
m += 1
|
|
124
|
-
m += 1
|
|
125
|
-
|
|
126
|
-
# Second dimension: permutation within Hamming weight
|
|
127
|
-
sample = local_repulsion_choice(G_cols, G_rows, max_weight, n, m)
|
|
128
|
-
solutions[s] = sample
|
|
129
|
-
energies[s] = compute_energy(sample, G_data, G_rows, G_cols)
|
|
130
|
-
|
|
131
|
-
best_solution = solutions[np.argmin(energies)]
|
|
132
|
-
|
|
133
|
-
best_value = 0.0
|
|
134
|
-
for u in range(n):
|
|
135
|
-
for col in range(G_rows[u], G_rows[u + 1]):
|
|
136
|
-
v = G_cols[col]
|
|
137
|
-
if best_solution[u] != best_solution[v]:
|
|
138
|
-
best_value += G_data[col]
|
|
139
|
-
|
|
140
|
-
return best_solution, best_value
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
@njit(parallel=True)
|
|
144
|
-
def init_z(G_data, G_rows, G_cols):
|
|
145
|
-
n_qubits = G_rows.shape[0] - 1
|
|
146
|
-
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
147
|
-
for r in prange(n_qubits):
|
|
148
|
-
# Row sum
|
|
149
|
-
start = G_rows[r]
|
|
150
|
-
end = G_rows[r + 1]
|
|
151
|
-
degree = end - start
|
|
152
|
-
val = np.count_nonzero(G_data[start:end])
|
|
153
|
-
|
|
154
|
-
degrees[r] += degree
|
|
155
|
-
|
|
156
|
-
# Column sum
|
|
157
|
-
for idx in range(start, end):
|
|
158
|
-
c = G_cols[idx]
|
|
159
|
-
degrees[c] += 1
|
|
160
|
-
|
|
161
|
-
return degrees
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
|
|
165
|
-
lil = lil_matrix((n_nodes, n_nodes), dtype=np.float32)
|
|
166
|
-
for u in range(n_nodes):
|
|
167
|
-
u_node = nodes[u]
|
|
168
|
-
for v in range(u + 1, n_nodes):
|
|
169
|
-
v_node = nodes[v]
|
|
170
|
-
if G.has_edge(u_node, v_node):
|
|
171
|
-
lil[u, v] = G[u_node][v_node].get('weight', 1.0)
|
|
172
|
-
|
|
173
|
-
return lil.tocsr()
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
def maxcut_tfim_sparse(
|
|
177
|
-
G,
|
|
178
|
-
quality=None,
|
|
179
|
-
shots=None,
|
|
180
|
-
):
|
|
181
|
-
nodes = None
|
|
182
|
-
n_qubits = 0
|
|
183
|
-
G_m = None
|
|
184
|
-
if isinstance(G, nx.Graph):
|
|
185
|
-
nodes = list(G.nodes())
|
|
186
|
-
n_qubits = len(nodes)
|
|
187
|
-
G_m = to_scipy_sparse_upper_triangular(G, nodes, n_qubits)
|
|
188
|
-
else:
|
|
189
|
-
n_qubits = G.shape[0]
|
|
190
|
-
nodes = list(range(n_qubits))
|
|
191
|
-
G_m = G
|
|
192
|
-
|
|
193
|
-
if n_qubits < 3:
|
|
194
|
-
if n_qubits == 0:
|
|
195
|
-
return "", 0, ([], [])
|
|
196
|
-
|
|
197
|
-
if n_qubits == 1:
|
|
198
|
-
return "0", 0, (nodes, [])
|
|
199
|
-
|
|
200
|
-
if n_qubits == 2:
|
|
201
|
-
weight = G_m[0, 1]
|
|
202
|
-
if weight < 0.0:
|
|
203
|
-
return "00", 0, (nodes, [])
|
|
204
|
-
|
|
205
|
-
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
206
|
-
|
|
207
|
-
if quality is None:
|
|
208
|
-
quality = 8
|
|
209
|
-
|
|
210
|
-
if shots is None:
|
|
211
|
-
# Number of measurement shots
|
|
212
|
-
shots = n_qubits << quality
|
|
213
|
-
|
|
214
|
-
n_steps = 1 << quality
|
|
215
|
-
grid_size = n_steps * n_qubits
|
|
216
|
-
|
|
217
|
-
degrees = init_J_and_z(G_m.data, G_m.indptr, G_m.indices)
|
|
218
|
-
hamming_prob = init_thresholds(n_qubits)
|
|
219
|
-
|
|
220
|
-
if IS_OPENCL_AVAILABLE and grid_size >= 128:
|
|
221
|
-
delta_t = 1.0 / n_steps
|
|
222
|
-
tot_t = 2.0 * n_steps * delta_t
|
|
223
|
-
h_mult = 2.0 / tot_t
|
|
224
|
-
theta = init_theta(delta_t, tot_t, h_mult, n_qubits, degrees)
|
|
225
|
-
args = np.empty(3, dtype=np.float32)
|
|
226
|
-
args[0] = delta_t
|
|
227
|
-
args[1] = tot_t
|
|
228
|
-
args[2] = h_mult
|
|
229
|
-
|
|
230
|
-
# Warp size is 32:
|
|
231
|
-
group_size = n_qubits - 1
|
|
232
|
-
if group_size > 256:
|
|
233
|
-
group_size = 256
|
|
234
|
-
grid_dim = n_steps * n_qubits * group_size
|
|
235
|
-
|
|
236
|
-
# Move to GPU
|
|
237
|
-
mf = cl.mem_flags
|
|
238
|
-
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
239
|
-
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
240
|
-
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
241
|
-
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
242
|
-
|
|
243
|
-
# Kernel execution
|
|
244
|
-
opencl_context.maxcut_hamming_cdf_kernel(
|
|
245
|
-
opencl_context.queue, (grid_dim,), (group_size,),
|
|
246
|
-
np.int32(n_qubits), deg_buf, args_buf, theta_buf, ham_buf
|
|
247
|
-
)
|
|
248
|
-
|
|
249
|
-
# Fetch results
|
|
250
|
-
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
251
|
-
|
|
252
|
-
hamming_prob /= hamming_prob.sum()
|
|
253
|
-
tot_prob = 0.0
|
|
254
|
-
for i in range(n_qubits - 1):
|
|
255
|
-
tot_prob += hamming_prob[i]
|
|
256
|
-
hamming_prob[i] = tot_prob
|
|
257
|
-
hamming_prob[-1] = 2.0
|
|
258
|
-
else:
|
|
259
|
-
maxcut_hamming_cdf(n_qubits, degrees, quality, hamming_prob)
|
|
260
|
-
|
|
261
|
-
best_solution, best_value = sample_for_solution(G_m.data, G_m.indptr, G_m.indices, shots, hamming_prob)
|
|
262
|
-
|
|
263
|
-
bit_string = ""
|
|
264
|
-
l, r = [], []
|
|
265
|
-
for i in range(len(best_solution)):
|
|
266
|
-
if best_solution[i]:
|
|
267
|
-
bit_string += "1"
|
|
268
|
-
r.append(nodes[i])
|
|
269
|
-
else:
|
|
270
|
-
bit_string += "0"
|
|
271
|
-
l.append(nodes[i])
|
|
272
|
-
|
|
273
|
-
return bit_string, best_value, (l, r)
|
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
import math
|
|
2
|
-
import networkx as nx
|
|
3
|
-
import numpy as np
|
|
4
|
-
import os
|
|
5
|
-
from numba import njit, prange
|
|
6
|
-
|
|
7
|
-
from .maxcut_tfim_util import init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
|
-
|
|
9
|
-
IS_OPENCL_AVAILABLE = True
|
|
10
|
-
try:
|
|
11
|
-
import pyopencl as cl
|
|
12
|
-
except ImportError:
|
|
13
|
-
IS_OPENCL_AVAILABLE = False
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
17
|
-
@njit
|
|
18
|
-
def local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, m):
|
|
19
|
-
"""
|
|
20
|
-
Pick m nodes out of n with repulsion bias:
|
|
21
|
-
- High-degree nodes are already less likely
|
|
22
|
-
- After choosing a node, its neighbors' probabilities are further reduced
|
|
23
|
-
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
24
|
-
weights: float64 array of shape (n,)
|
|
25
|
-
"""
|
|
26
|
-
|
|
27
|
-
weights = weights.copy()
|
|
28
|
-
chosen = np.zeros(m, dtype=np.int32) # store chosen indices
|
|
29
|
-
available = np.ones(n, dtype=np.bool_) # True = available, False = not
|
|
30
|
-
mask = np.zeros(n, dtype=np.bool_)
|
|
31
|
-
chosen_count = 0
|
|
32
|
-
|
|
33
|
-
for _ in range(m):
|
|
34
|
-
# Count available
|
|
35
|
-
total_w = 0.0
|
|
36
|
-
for i in range(n):
|
|
37
|
-
if available[i]:
|
|
38
|
-
total_w += weights[i]
|
|
39
|
-
if total_w <= 0:
|
|
40
|
-
break
|
|
41
|
-
|
|
42
|
-
# Normalize & sample
|
|
43
|
-
r = np.random.rand()
|
|
44
|
-
cum = 0.0
|
|
45
|
-
node = -1
|
|
46
|
-
for i in range(n):
|
|
47
|
-
if available[i]:
|
|
48
|
-
cum += weights[i] / total_w
|
|
49
|
-
if r < cum:
|
|
50
|
-
node = i
|
|
51
|
-
break
|
|
52
|
-
|
|
53
|
-
if node == -1:
|
|
54
|
-
continue
|
|
55
|
-
|
|
56
|
-
# Select node
|
|
57
|
-
chosen[chosen_count] = node
|
|
58
|
-
chosen_count += 1
|
|
59
|
-
available[node] = False
|
|
60
|
-
mask[node] = True
|
|
61
|
-
|
|
62
|
-
# Repulsion: penalize neighbors
|
|
63
|
-
for nbr in range(n):
|
|
64
|
-
if available[nbr]:
|
|
65
|
-
weights[nbr] *= 0.5 ** (G_func((nodes[node], nodes[nbr]), G_func_args_tuple) / max_weight) # tunable penalty factor
|
|
66
|
-
|
|
67
|
-
return mask
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
@njit
|
|
71
|
-
def compute_energy(sample, G_func, G_func_args_tuple, nodes, n_qubits):
|
|
72
|
-
energy = 0
|
|
73
|
-
for u in range(n_qubits):
|
|
74
|
-
for v in range(u + 1, n_qubits):
|
|
75
|
-
energy += G_func((nodes[u], nodes[v]), G_func_args_tuple) * (1 if sample[u] == sample[v] else -1)
|
|
76
|
-
|
|
77
|
-
return energy
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
@njit(parallel=True)
|
|
81
|
-
def sample_for_solution(G_func, G_func_args_tuple, nodes, max_weight, shots, thresholds, degrees_sum, J_eff, n):
|
|
82
|
-
weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
|
|
83
|
-
|
|
84
|
-
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
85
|
-
energies = np.empty(shots, dtype=np.float32)
|
|
86
|
-
|
|
87
|
-
for s in prange(shots):
|
|
88
|
-
# First dimension: Hamming weight
|
|
89
|
-
mag_prob = np.random.random()
|
|
90
|
-
m = 0
|
|
91
|
-
while thresholds[m] < mag_prob:
|
|
92
|
-
m += 1
|
|
93
|
-
m += 1
|
|
94
|
-
|
|
95
|
-
# Second dimension: permutation within Hamming weight
|
|
96
|
-
sample = local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, m)
|
|
97
|
-
|
|
98
|
-
solutions[s] = sample
|
|
99
|
-
energies[s] = compute_energy(sample, G_func, G_func_args_tuple, nodes, n)
|
|
100
|
-
|
|
101
|
-
best_solution = solutions[np.argmin(energies)]
|
|
102
|
-
|
|
103
|
-
best_value = 0.0
|
|
104
|
-
for u in range(n):
|
|
105
|
-
for v in range(u + 1, n):
|
|
106
|
-
if best_solution[u] != best_solution[v]:
|
|
107
|
-
best_value += G_func((nodes[u], nodes[v]), G_func_args_tuple)
|
|
108
|
-
|
|
109
|
-
return best_solution, best_value
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
@njit(parallel=True)
|
|
113
|
-
def init_J_and_z(G_func, G_func_args_tuple, nodes):
|
|
114
|
-
n_qubits = len(nodes)
|
|
115
|
-
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
116
|
-
J_eff = np.empty(n_qubits, dtype=np.float32)
|
|
117
|
-
J_max = -float("inf")
|
|
118
|
-
G_max = -float("inf")
|
|
119
|
-
for n in prange(n_qubits):
|
|
120
|
-
degree = 0
|
|
121
|
-
J = 0.0
|
|
122
|
-
for m in range(n_qubits):
|
|
123
|
-
val = G_func((nodes[n], nodes[m]), G_func_args_tuple)
|
|
124
|
-
if val != 0.0:
|
|
125
|
-
degree += 1
|
|
126
|
-
J += val
|
|
127
|
-
G_max = max(val, G_max)
|
|
128
|
-
J = -J / degree if degree > 0 else 0
|
|
129
|
-
degrees[n] = degree
|
|
130
|
-
J_eff[n] = J
|
|
131
|
-
J_abs = abs(J)
|
|
132
|
-
J_max = max(J_abs, J_max)
|
|
133
|
-
J_eff /= J_max
|
|
134
|
-
|
|
135
|
-
return J_eff, degrees, G_max
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
def maxcut_tfim_streaming(
|
|
139
|
-
G_func,
|
|
140
|
-
nodes,
|
|
141
|
-
G_func_args_tuple=None,
|
|
142
|
-
quality=None,
|
|
143
|
-
shots=None,
|
|
144
|
-
):
|
|
145
|
-
n_qubits = len(nodes)
|
|
146
|
-
|
|
147
|
-
if n_qubits < 3:
|
|
148
|
-
if n_qubits == 0:
|
|
149
|
-
return "", 0, ([], [])
|
|
150
|
-
|
|
151
|
-
if n_qubits == 1:
|
|
152
|
-
return "0", 0, (nodes, [])
|
|
153
|
-
|
|
154
|
-
if n_qubits == 2:
|
|
155
|
-
weight = G_func((nodes[0], nodes[1]), G_func_args_tuple)
|
|
156
|
-
if weight < 0.0:
|
|
157
|
-
return "00", 0, (nodes, [])
|
|
158
|
-
|
|
159
|
-
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
160
|
-
|
|
161
|
-
if quality is None:
|
|
162
|
-
quality = 8
|
|
163
|
-
|
|
164
|
-
if shots is None:
|
|
165
|
-
# Number of measurement shots
|
|
166
|
-
shots = n_qubits << quality
|
|
167
|
-
|
|
168
|
-
n_steps = 1 << quality
|
|
169
|
-
grid_size = n_steps * n_qubits
|
|
170
|
-
|
|
171
|
-
J_eff, degrees, G_max = init_J_and_z(G_func, G_func_args_tuple, nodes)
|
|
172
|
-
hamming_prob = init_thresholds(n_qubits)
|
|
173
|
-
|
|
174
|
-
if IS_OPENCL_AVAILABLE and grid_size >= 128:
|
|
175
|
-
delta_t = 1.0 / n_steps
|
|
176
|
-
tot_t = 2.0 * n_steps * delta_t
|
|
177
|
-
h_mult = 2.0 / tot_t
|
|
178
|
-
theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
|
|
179
|
-
args = np.empty(3, dtype=np.float32)
|
|
180
|
-
args[0] = delta_t
|
|
181
|
-
args[1] = tot_t
|
|
182
|
-
args[2] = h_mult
|
|
183
|
-
|
|
184
|
-
# Warp size is 32:
|
|
185
|
-
group_size = n_qubits - 1
|
|
186
|
-
if group_size > 256:
|
|
187
|
-
group_size = 256
|
|
188
|
-
grid_dim = n_steps * n_qubits * group_size
|
|
189
|
-
|
|
190
|
-
# Move to GPU
|
|
191
|
-
mf = cl.mem_flags
|
|
192
|
-
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
193
|
-
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
194
|
-
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
195
|
-
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
196
|
-
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
197
|
-
|
|
198
|
-
# Kernel execution
|
|
199
|
-
opencl_context.maxcut_hamming_cdf_kernel(
|
|
200
|
-
opencl_context.queue, (grid_dim,), (group_size,),
|
|
201
|
-
np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
|
|
202
|
-
)
|
|
203
|
-
|
|
204
|
-
# Fetch results
|
|
205
|
-
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
206
|
-
|
|
207
|
-
hamming_prob /= hamming_prob.sum()
|
|
208
|
-
tot_prob = 0.0
|
|
209
|
-
for i in range(n_qubits - 1):
|
|
210
|
-
tot_prob += hamming_prob[i]
|
|
211
|
-
hamming_prob[i] = tot_prob
|
|
212
|
-
hamming_prob[-1] = 2.0
|
|
213
|
-
else:
|
|
214
|
-
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
|
|
215
|
-
|
|
216
|
-
best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob, degrees.sum(), J_eff, n_qubits)
|
|
217
|
-
|
|
218
|
-
bit_string = ""
|
|
219
|
-
l, r = [], []
|
|
220
|
-
for i in range(len(best_solution)):
|
|
221
|
-
if best_solution[i]:
|
|
222
|
-
bit_string += "1"
|
|
223
|
-
r.append(nodes[i])
|
|
224
|
-
else:
|
|
225
|
-
bit_string += "0"
|
|
226
|
-
l.append(nodes[i])
|
|
227
|
-
|
|
228
|
-
return bit_string, best_value, (l, r)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|