PyQrackIsing 8.10.0__py3-none-win_amd64.whl
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/__init__.py +12 -0
- pyqrackising/convert_tensor_network_to_tsp.py +97 -0
- pyqrackising/generate_tfim_samples.py +142 -0
- pyqrackising/kernels.cl +440 -0
- pyqrackising/maxcut_tfim.py +219 -0
- pyqrackising/maxcut_tfim_sparse.py +239 -0
- pyqrackising/maxcut_tfim_streaming.py +210 -0
- pyqrackising/maxcut_tfim_util.py +317 -0
- pyqrackising/spin_glass_solver.py +323 -0
- pyqrackising/spin_glass_solver_sparse.py +349 -0
- pyqrackising/spin_glass_solver_streaming.py +188 -0
- pyqrackising/spin_glass_solver_util.py +21 -0
- pyqrackising/tfim_magnetization.py +15 -0
- pyqrackising/tfim_square_magnetization.py +14 -0
- pyqrackising/tsp.py +1821 -0
- pyqrackising/tsp_maxcut.py +312 -0
- pyqrackising-8.10.0.dist-info/METADATA +27 -0
- pyqrackising-8.10.0.dist-info/RECORD +21 -0
- pyqrackising-8.10.0.dist-info/WHEEL +5 -0
- pyqrackising-8.10.0.dist-info/licenses/LICENSE.md +165 -0
- pyqrackising-8.10.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,219 @@
|
|
|
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 get_cut, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick, init_bit_pick
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
epsilon = opencl_context.epsilon
|
|
11
|
+
dtype = opencl_context.dtype
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@njit
|
|
15
|
+
def update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_base):
|
|
16
|
+
# Select node
|
|
17
|
+
used[node] = True
|
|
18
|
+
|
|
19
|
+
# Repulsion: penalize neighbors
|
|
20
|
+
for nbr in range(n):
|
|
21
|
+
if used[nbr]:
|
|
22
|
+
continue
|
|
23
|
+
weights[nbr] *= repulsion_base ** (-G_m[node, nbr] / max_edge)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
27
|
+
@njit
|
|
28
|
+
def local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m):
|
|
29
|
+
"""
|
|
30
|
+
Pick m nodes out of n with repulsion bias:
|
|
31
|
+
- High-degree nodes are already less likely
|
|
32
|
+
- After choosing a node, its neighbors' probabilities are further reduced
|
|
33
|
+
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
34
|
+
weights: float32 array of shape (n,)
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
weights = weights.copy()
|
|
38
|
+
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
39
|
+
|
|
40
|
+
# First bit:
|
|
41
|
+
node = init_bit_pick(weights, tot_init_weight, n)
|
|
42
|
+
|
|
43
|
+
if m == 1:
|
|
44
|
+
used[node] = True
|
|
45
|
+
return used
|
|
46
|
+
|
|
47
|
+
update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_base)
|
|
48
|
+
|
|
49
|
+
for _ in range(1, m - 1):
|
|
50
|
+
node = bit_pick(weights, used, n)
|
|
51
|
+
|
|
52
|
+
# Update answer and weights
|
|
53
|
+
update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_base)
|
|
54
|
+
|
|
55
|
+
# Count available
|
|
56
|
+
node = bit_pick(weights, used, n)
|
|
57
|
+
|
|
58
|
+
used[node] = True
|
|
59
|
+
|
|
60
|
+
return used
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@njit
|
|
64
|
+
def compute_energy(sample, G_m, n_qubits):
|
|
65
|
+
energy = 0
|
|
66
|
+
for u in range(n_qubits):
|
|
67
|
+
for v in range(u + 1, n_qubits):
|
|
68
|
+
val = G_m[u, v]
|
|
69
|
+
energy += val if sample[u] == sample[v] else -val
|
|
70
|
+
|
|
71
|
+
return -energy
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@njit
|
|
75
|
+
def compute_cut(sample, G_m, n_qubits):
|
|
76
|
+
cut = 0
|
|
77
|
+
for u in range(n_qubits):
|
|
78
|
+
for v in range(u + 1, n_qubits):
|
|
79
|
+
if sample[u] != sample[v]:
|
|
80
|
+
cut += G_m[u, v]
|
|
81
|
+
|
|
82
|
+
return cut
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@njit(parallel=True)
|
|
86
|
+
def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass):
|
|
87
|
+
shots = max(1, shots >> 1)
|
|
88
|
+
n = len(G_m)
|
|
89
|
+
tot_init_weight = weights.sum()
|
|
90
|
+
|
|
91
|
+
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
92
|
+
energies = np.empty(shots, dtype=dtype)
|
|
93
|
+
|
|
94
|
+
best_solution = solutions[0]
|
|
95
|
+
best_energy = -float("inf")
|
|
96
|
+
|
|
97
|
+
improved = True
|
|
98
|
+
while improved:
|
|
99
|
+
improved = False
|
|
100
|
+
for s in prange(shots):
|
|
101
|
+
# First dimension: Hamming weight
|
|
102
|
+
m = sample_mag(thresholds)
|
|
103
|
+
|
|
104
|
+
# Second dimension: permutation within Hamming weight
|
|
105
|
+
sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
106
|
+
solutions[s] = sample
|
|
107
|
+
energies[s] = compute_energy(sample, G_m, n) if is_spin_glass else compute_cut(sample, G_m, n)
|
|
108
|
+
|
|
109
|
+
best_index = np.argmax(energies)
|
|
110
|
+
energy = energies[best_index]
|
|
111
|
+
if energy > best_energy:
|
|
112
|
+
best_energy = energy
|
|
113
|
+
best_solution = solutions[best_index].copy()
|
|
114
|
+
improved = True
|
|
115
|
+
|
|
116
|
+
if is_spin_glass:
|
|
117
|
+
best_energy = compute_cut(best_solution, G_m, n)
|
|
118
|
+
|
|
119
|
+
return best_solution, best_energy
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@njit(parallel=True)
|
|
123
|
+
def init_J_and_z(G_m):
|
|
124
|
+
n_qubits = len(G_m)
|
|
125
|
+
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
126
|
+
J_eff = np.empty(n_qubits, dtype=np.float64)
|
|
127
|
+
G_max = -float("inf")
|
|
128
|
+
for n in prange(n_qubits):
|
|
129
|
+
degree = 0
|
|
130
|
+
J = 0.0
|
|
131
|
+
for m in range(n_qubits):
|
|
132
|
+
val = G_m[n, m]
|
|
133
|
+
if val != 0.0:
|
|
134
|
+
degree += 1
|
|
135
|
+
J += val
|
|
136
|
+
val = abs(val)
|
|
137
|
+
if val > G_max:
|
|
138
|
+
G_max = val
|
|
139
|
+
J = -J / degree if degree > 0 else 0
|
|
140
|
+
degrees[n] = degree
|
|
141
|
+
J_eff[n] = J
|
|
142
|
+
|
|
143
|
+
return J_eff, degrees, G_max
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@njit
|
|
147
|
+
def cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
148
|
+
J_eff, degrees, max_edge = init_J_and_z(G_m)
|
|
149
|
+
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
150
|
+
|
|
151
|
+
degrees = None
|
|
152
|
+
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
153
|
+
|
|
154
|
+
best_solution, best_value = sample_measurement(G_m, max_edge, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass)
|
|
155
|
+
|
|
156
|
+
bit_string, l, r = get_cut(best_solution, nodes)
|
|
157
|
+
|
|
158
|
+
return bit_string, best_value, (l, r)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def maxcut_tfim(
|
|
162
|
+
G,
|
|
163
|
+
quality=None,
|
|
164
|
+
shots=None,
|
|
165
|
+
is_spin_glass=False,
|
|
166
|
+
anneal_t=None,
|
|
167
|
+
anneal_h=None,
|
|
168
|
+
repulsion_base=None
|
|
169
|
+
):
|
|
170
|
+
wgs = opencl_context.work_group_size
|
|
171
|
+
nodes = None
|
|
172
|
+
n_qubits = 0
|
|
173
|
+
G_m = None
|
|
174
|
+
if isinstance(G, nx.Graph):
|
|
175
|
+
nodes = list(G.nodes())
|
|
176
|
+
n_qubits = len(nodes)
|
|
177
|
+
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0, dtype=dtype)
|
|
178
|
+
else:
|
|
179
|
+
n_qubits = len(G)
|
|
180
|
+
nodes = list(range(n_qubits))
|
|
181
|
+
G_m = G
|
|
182
|
+
|
|
183
|
+
if n_qubits < 3:
|
|
184
|
+
if n_qubits == 0:
|
|
185
|
+
return "", 0, ([], [])
|
|
186
|
+
|
|
187
|
+
if n_qubits == 1:
|
|
188
|
+
return "0", 0, (nodes, [])
|
|
189
|
+
|
|
190
|
+
if n_qubits == 2:
|
|
191
|
+
weight = G_m[0, 1]
|
|
192
|
+
if weight < 0.0:
|
|
193
|
+
return "00", 0, (nodes, [])
|
|
194
|
+
|
|
195
|
+
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
196
|
+
|
|
197
|
+
if quality is None:
|
|
198
|
+
quality = 6
|
|
199
|
+
|
|
200
|
+
if shots is None:
|
|
201
|
+
# Number of measurement shots
|
|
202
|
+
shots = n_qubits << quality
|
|
203
|
+
|
|
204
|
+
if anneal_t is None:
|
|
205
|
+
anneal_t = 8.0
|
|
206
|
+
|
|
207
|
+
if anneal_h is None:
|
|
208
|
+
anneal_h = 8.0
|
|
209
|
+
|
|
210
|
+
if repulsion_base is None:
|
|
211
|
+
repulsion_base = 8.0
|
|
212
|
+
|
|
213
|
+
bit_string, best_value, partition = cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
|
|
214
|
+
|
|
215
|
+
if best_value < 0.0:
|
|
216
|
+
# Best cut is trivial partition, all/empty
|
|
217
|
+
return '0' * n_qubits, 0.0, (nodes, [])
|
|
218
|
+
|
|
219
|
+
return bit_string, best_value, partition
|
|
@@ -0,0 +1,239 @@
|
|
|
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 binary_search, get_cut, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick, init_bit_pick, to_scipy_sparse_upper_triangular
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
epsilon = opencl_context.epsilon
|
|
11
|
+
dtype = opencl_context.dtype
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@njit
|
|
15
|
+
def update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node, repulsion_base):
|
|
16
|
+
# Select node
|
|
17
|
+
used[node] = True
|
|
18
|
+
|
|
19
|
+
# Repulsion: penalize neighbors
|
|
20
|
+
for j in range(G_rows[node], G_rows[node + 1]):
|
|
21
|
+
nbr = G_cols[j]
|
|
22
|
+
if used[nbr]:
|
|
23
|
+
continue
|
|
24
|
+
weights[nbr] *= repulsion_base ** (-G_data[j] / max_edge)
|
|
25
|
+
|
|
26
|
+
for nbr in range(node):
|
|
27
|
+
if used[nbr]:
|
|
28
|
+
continue
|
|
29
|
+
start = G_rows[nbr]
|
|
30
|
+
end = G_rows[nbr + 1]
|
|
31
|
+
j = binary_search(G_cols[start:end], node) + start
|
|
32
|
+
if j < end:
|
|
33
|
+
weights[nbr] *= repulsion_base ** (-G_data[j] / max_edge)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
37
|
+
@njit
|
|
38
|
+
def local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, repulsion_base, n, m):
|
|
39
|
+
"""
|
|
40
|
+
Pick m nodes out of n with repulsion bias:
|
|
41
|
+
- High-degree nodes are already less likely
|
|
42
|
+
- After choosing a node, its neighbors' probabilities are further reduced
|
|
43
|
+
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
44
|
+
weights: float32 array of shape (n,)
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
weights = weights.copy()
|
|
48
|
+
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
49
|
+
|
|
50
|
+
# First bit:
|
|
51
|
+
node = init_bit_pick(weights, tot_init_weight, n)
|
|
52
|
+
|
|
53
|
+
if m == 1:
|
|
54
|
+
used[node] = True
|
|
55
|
+
return used
|
|
56
|
+
|
|
57
|
+
update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node, repulsion_base)
|
|
58
|
+
|
|
59
|
+
for _ in range(1, m - 1):
|
|
60
|
+
node = bit_pick(weights, used, n)
|
|
61
|
+
|
|
62
|
+
# Update answer and weights
|
|
63
|
+
update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node, repulsion_base)
|
|
64
|
+
|
|
65
|
+
node = bit_pick(weights, used, n)
|
|
66
|
+
|
|
67
|
+
used[node] = True
|
|
68
|
+
|
|
69
|
+
return used
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@njit
|
|
73
|
+
def compute_energy(sample, G_data, G_rows, G_cols, n_qubits):
|
|
74
|
+
energy = 0
|
|
75
|
+
for u in range(n_qubits):
|
|
76
|
+
for col in range(G_rows[u], G_rows[u + 1]):
|
|
77
|
+
v = G_cols[col]
|
|
78
|
+
val = G_data[col]
|
|
79
|
+
energy += val if sample[u] == sample[v] else -val
|
|
80
|
+
|
|
81
|
+
return -energy
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@njit
|
|
85
|
+
def compute_cut(sample, G_data, G_rows, G_cols, n_qubits):
|
|
86
|
+
cut = 0
|
|
87
|
+
for u in range(n_qubits):
|
|
88
|
+
for col in range(G_rows[u], G_rows[u + 1]):
|
|
89
|
+
v = G_cols[col]
|
|
90
|
+
if sample[u] != sample[v]:
|
|
91
|
+
cut += G_data[col]
|
|
92
|
+
|
|
93
|
+
return cut
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@njit(parallel=True)
|
|
97
|
+
def sample_measurement(max_edge, G_data, G_rows, G_cols, shots, thresholds, weights, repulsion_base, is_spin_glass):
|
|
98
|
+
shots = max(1, shots >> 1)
|
|
99
|
+
n = G_rows.shape[0] - 1
|
|
100
|
+
tot_init_weight = weights.sum()
|
|
101
|
+
|
|
102
|
+
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
103
|
+
energies = np.empty(shots, dtype=dtype)
|
|
104
|
+
|
|
105
|
+
best_solution = solutions[0]
|
|
106
|
+
best_energy = -float("inf")
|
|
107
|
+
|
|
108
|
+
improved = True
|
|
109
|
+
while improved:
|
|
110
|
+
improved = False
|
|
111
|
+
for s in prange(shots):
|
|
112
|
+
# First dimension: Hamming weight
|
|
113
|
+
m = sample_mag(thresholds)
|
|
114
|
+
|
|
115
|
+
# Second dimension: permutation within Hamming weight
|
|
116
|
+
sample = local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
117
|
+
solutions[s] = sample
|
|
118
|
+
energies[s] = compute_energy(sample, G_data, G_rows, G_cols, n) if is_spin_glass else compute_cut(sample, G_data, G_rows, G_cols, n)
|
|
119
|
+
|
|
120
|
+
best_index = np.argmax(energies)
|
|
121
|
+
energy = energies[best_index]
|
|
122
|
+
if energy > best_energy:
|
|
123
|
+
best_energy = energy
|
|
124
|
+
best_solution = solutions[best_index].copy()
|
|
125
|
+
improved = True
|
|
126
|
+
|
|
127
|
+
if is_spin_glass:
|
|
128
|
+
best_energy = compute_cut(best_solution, G_data, G_rows, G_cols, n)
|
|
129
|
+
|
|
130
|
+
return best_solution, best_energy
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@njit(parallel=True)
|
|
134
|
+
def init_J_and_z(G_data, G_rows, G_cols):
|
|
135
|
+
n_qubits = G_rows.shape[0] - 1
|
|
136
|
+
degrees = np.zeros(n_qubits, dtype=np.uint32)
|
|
137
|
+
J_eff = np.zeros(n_qubits, dtype=np.float64)
|
|
138
|
+
for r in prange(n_qubits):
|
|
139
|
+
# Row sum
|
|
140
|
+
start = G_rows[r]
|
|
141
|
+
end = G_rows[r + 1]
|
|
142
|
+
degree = end - start
|
|
143
|
+
val = G_data[start:end].sum()
|
|
144
|
+
|
|
145
|
+
degrees[r] += degree
|
|
146
|
+
J_eff[r] += val
|
|
147
|
+
|
|
148
|
+
# Column sum
|
|
149
|
+
for idx in range(start, end):
|
|
150
|
+
c = G_cols[idx]
|
|
151
|
+
degrees[c] += 1
|
|
152
|
+
J_eff[c] += G_data[idx]
|
|
153
|
+
|
|
154
|
+
for r in prange(n_qubits):
|
|
155
|
+
degree = degrees[r]
|
|
156
|
+
J_eff[r] = -J_eff[r] / degree if degree > 0 else 0
|
|
157
|
+
|
|
158
|
+
return J_eff, degrees
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
@njit
|
|
162
|
+
def cpu_footer(shots, quality, n_qubits, max_edge, G_data, G_rows, G_col, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
163
|
+
J_eff, degrees = init_J_and_z(G_data, G_rows, G_col)
|
|
164
|
+
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
165
|
+
|
|
166
|
+
degrees = None
|
|
167
|
+
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
168
|
+
|
|
169
|
+
best_solution, best_value = sample_measurement(max_edge, G_data, G_rows, G_col, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass)
|
|
170
|
+
|
|
171
|
+
bit_string, l, r = get_cut(best_solution, nodes)
|
|
172
|
+
|
|
173
|
+
return bit_string, best_value, (l, r)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def maxcut_tfim_sparse(
|
|
177
|
+
G,
|
|
178
|
+
quality=None,
|
|
179
|
+
shots=None,
|
|
180
|
+
is_spin_glass=False,
|
|
181
|
+
anneal_t=None,
|
|
182
|
+
anneal_h=None,
|
|
183
|
+
repulsion_base=None
|
|
184
|
+
):
|
|
185
|
+
wgs = opencl_context.work_group_size
|
|
186
|
+
nodes = None
|
|
187
|
+
n_qubits = 0
|
|
188
|
+
G_m = None
|
|
189
|
+
if isinstance(G, nx.Graph):
|
|
190
|
+
nodes = list(G.nodes())
|
|
191
|
+
n_qubits = len(nodes)
|
|
192
|
+
G_m = to_scipy_sparse_upper_triangular(G, nodes, n_qubits)
|
|
193
|
+
else:
|
|
194
|
+
n_qubits = G.shape[0]
|
|
195
|
+
nodes = list(range(n_qubits))
|
|
196
|
+
G_m = G
|
|
197
|
+
|
|
198
|
+
if n_qubits < 3:
|
|
199
|
+
if n_qubits == 0:
|
|
200
|
+
return "", 0, ([], [])
|
|
201
|
+
|
|
202
|
+
if n_qubits == 1:
|
|
203
|
+
return "0", 0, (nodes, [])
|
|
204
|
+
|
|
205
|
+
if n_qubits == 2:
|
|
206
|
+
weight = G_m[0, 1]
|
|
207
|
+
if weight < 0.0:
|
|
208
|
+
return "00", 0, (nodes, [])
|
|
209
|
+
|
|
210
|
+
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
211
|
+
|
|
212
|
+
if quality is None:
|
|
213
|
+
quality = 6
|
|
214
|
+
|
|
215
|
+
if shots is None:
|
|
216
|
+
# Number of measurement shots
|
|
217
|
+
shots = n_qubits << quality
|
|
218
|
+
|
|
219
|
+
if anneal_t is None:
|
|
220
|
+
anneal_t = 8.0
|
|
221
|
+
|
|
222
|
+
if anneal_h is None:
|
|
223
|
+
anneal_h = 8.0
|
|
224
|
+
|
|
225
|
+
if repulsion_base is None:
|
|
226
|
+
repulsion_base = 8.0
|
|
227
|
+
|
|
228
|
+
max_edge = abs(G_m.max())
|
|
229
|
+
min_edge = abs(G_m.min())
|
|
230
|
+
if min_edge > max_edge:
|
|
231
|
+
max_edge = min_edge
|
|
232
|
+
|
|
233
|
+
bit_string, best_value, partition = cpu_footer(shots, quality, n_qubits, max_edge, G_m.data, G_m.indptr, G_m.indices, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
|
|
234
|
+
|
|
235
|
+
if best_value < 0.0:
|
|
236
|
+
# Best cut is trivial partition, all/empty
|
|
237
|
+
return '0' * n_qubits, 0.0, (nodes, [])
|
|
238
|
+
|
|
239
|
+
return bit_string, best_value, partition
|
|
@@ -0,0 +1,210 @@
|
|
|
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 get_cut, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick, init_bit_pick
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
epsilon = opencl_context.epsilon
|
|
11
|
+
dtype = opencl_context.dtype
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@njit
|
|
15
|
+
def update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, repulsion_base):
|
|
16
|
+
# Select node
|
|
17
|
+
used[node] = True
|
|
18
|
+
|
|
19
|
+
# Repulsion: penalize neighbors
|
|
20
|
+
for nbr in range(n):
|
|
21
|
+
if used[nbr]:
|
|
22
|
+
continue
|
|
23
|
+
weights[nbr] *= repulsion_base ** (-G_func(nodes[node], nodes[nbr]) / max_edge)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
27
|
+
@njit
|
|
28
|
+
def local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m):
|
|
29
|
+
"""
|
|
30
|
+
Pick m nodes out of n with repulsion bias:
|
|
31
|
+
- High-degree nodes are already less likely
|
|
32
|
+
- After choosing a node, its neighbors' probabilities are further reduced
|
|
33
|
+
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
34
|
+
weights: float array of shape (n,)
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
weights = weights.copy()
|
|
38
|
+
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
39
|
+
|
|
40
|
+
# First bit:
|
|
41
|
+
node = init_bit_pick(weights, tot_init_weight, n)
|
|
42
|
+
|
|
43
|
+
if m == 1:
|
|
44
|
+
used[node] = True
|
|
45
|
+
return used
|
|
46
|
+
|
|
47
|
+
update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, repulsion_base)
|
|
48
|
+
|
|
49
|
+
for _ in range(1, m - 1):
|
|
50
|
+
node = bit_pick(weights, used, n)
|
|
51
|
+
|
|
52
|
+
# Update answer and weights
|
|
53
|
+
update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, repulsion_base)
|
|
54
|
+
|
|
55
|
+
node = bit_pick(weights, used, n)
|
|
56
|
+
|
|
57
|
+
# Select node
|
|
58
|
+
used[node] = True
|
|
59
|
+
|
|
60
|
+
return used
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@njit
|
|
64
|
+
def compute_energy(sample, G_func, nodes, n_qubits):
|
|
65
|
+
energy = 0
|
|
66
|
+
for u in range(n_qubits):
|
|
67
|
+
for v in range(u + 1, n_qubits):
|
|
68
|
+
val = G_func(nodes[u], nodes[v])
|
|
69
|
+
energy += val if sample[u] == sample[v] else -val
|
|
70
|
+
|
|
71
|
+
return -energy
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@njit
|
|
75
|
+
def compute_cut(sample, G_func, nodes, n_qubits):
|
|
76
|
+
cut = 0
|
|
77
|
+
for u in range(n_qubits):
|
|
78
|
+
for v in range(u + 1, n_qubits):
|
|
79
|
+
if sample[u] != sample[v]:
|
|
80
|
+
cut += G_func(nodes[u], nodes[v])
|
|
81
|
+
|
|
82
|
+
return cut
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@njit(parallel=True)
|
|
86
|
+
def sample_measurement(G_func, nodes, max_edge, shots, thresholds, degrees_sum, weights, n, repulsion_base, is_spin_glass):
|
|
87
|
+
shots = max(1, shots >> 1)
|
|
88
|
+
tot_init_weight = weights.sum()
|
|
89
|
+
|
|
90
|
+
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
91
|
+
energies = np.empty(shots, dtype=dtype)
|
|
92
|
+
|
|
93
|
+
best_solution = solutions[0]
|
|
94
|
+
best_energy = -float("inf")
|
|
95
|
+
|
|
96
|
+
improved = True
|
|
97
|
+
while improved:
|
|
98
|
+
improved = False
|
|
99
|
+
for s in prange(shots):
|
|
100
|
+
# First dimension: Hamming weight
|
|
101
|
+
m = sample_mag(thresholds)
|
|
102
|
+
|
|
103
|
+
# Second dimension: permutation within Hamming weight
|
|
104
|
+
sample = local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
105
|
+
solutions[s] = sample
|
|
106
|
+
energies[s] = compute_energy(sample, G_func, nodes, n) if is_spin_glass else compute_cut(sample, G_func, nodes, n)
|
|
107
|
+
|
|
108
|
+
best_index = np.argmax(energies)
|
|
109
|
+
energy = energies[best_index]
|
|
110
|
+
if energy > best_energy:
|
|
111
|
+
best_energy = energy
|
|
112
|
+
best_solution = solutions[best_index].copy()
|
|
113
|
+
improved = True
|
|
114
|
+
|
|
115
|
+
if is_spin_glass:
|
|
116
|
+
best_energy = compute_cut(best_solution, G_func, nodes, n)
|
|
117
|
+
|
|
118
|
+
return best_solution, best_energy
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@njit(parallel=True)
|
|
122
|
+
def init_J_and_z(G_func, nodes):
|
|
123
|
+
n_qubits = len(nodes)
|
|
124
|
+
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
125
|
+
J_eff = np.empty(n_qubits, dtype=np.float64)
|
|
126
|
+
G_max = -float("inf")
|
|
127
|
+
for n in prange(n_qubits):
|
|
128
|
+
degree = 0
|
|
129
|
+
J = 0.0
|
|
130
|
+
for m in range(n_qubits):
|
|
131
|
+
val = G_func(nodes[n], nodes[m])
|
|
132
|
+
if val != 0.0:
|
|
133
|
+
degree += 1
|
|
134
|
+
J += val
|
|
135
|
+
val = abs(val)
|
|
136
|
+
if val > G_max:
|
|
137
|
+
G_max = val
|
|
138
|
+
J = -J / degree if degree > 0 else 0
|
|
139
|
+
degrees[n] = degree
|
|
140
|
+
J_eff[n] = J
|
|
141
|
+
|
|
142
|
+
return J_eff, degrees, G_max
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@njit
|
|
146
|
+
def cpu_footer(shots, quality, n_qubits, G_func, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
147
|
+
J_eff, degrees, max_edge = init_J_and_z(G_func, nodes)
|
|
148
|
+
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
149
|
+
degrees_sum = degrees.sum()
|
|
150
|
+
|
|
151
|
+
degrees = None
|
|
152
|
+
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
153
|
+
|
|
154
|
+
best_solution, best_value = sample_measurement(G_func, nodes, max_edge, shots, hamming_prob, degrees_sum, J_eff, n_qubits, repulsion_base, is_spin_glass)
|
|
155
|
+
|
|
156
|
+
bit_string, l, r = get_cut(best_solution, nodes)
|
|
157
|
+
|
|
158
|
+
return bit_string, best_value, (l, r)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def maxcut_tfim_streaming(
|
|
162
|
+
G_func,
|
|
163
|
+
nodes,
|
|
164
|
+
quality=None,
|
|
165
|
+
shots=None,
|
|
166
|
+
is_spin_glass=False,
|
|
167
|
+
anneal_t=None,
|
|
168
|
+
anneal_h=None,
|
|
169
|
+
repulsion_base=None
|
|
170
|
+
):
|
|
171
|
+
wgs = opencl_context.work_group_size
|
|
172
|
+
n_qubits = len(nodes)
|
|
173
|
+
|
|
174
|
+
if n_qubits < 3:
|
|
175
|
+
if n_qubits == 0:
|
|
176
|
+
return "", 0, ([], [])
|
|
177
|
+
|
|
178
|
+
if n_qubits == 1:
|
|
179
|
+
return "0", 0, (nodes, [])
|
|
180
|
+
|
|
181
|
+
if n_qubits == 2:
|
|
182
|
+
weight = G_func(nodes[0], nodes[1])
|
|
183
|
+
if weight < 0.0:
|
|
184
|
+
return "00", 0, (nodes, [])
|
|
185
|
+
|
|
186
|
+
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
187
|
+
|
|
188
|
+
if quality is None:
|
|
189
|
+
quality = 6
|
|
190
|
+
|
|
191
|
+
if shots is None:
|
|
192
|
+
# Number of measurement shots
|
|
193
|
+
shots = n_qubits << quality
|
|
194
|
+
|
|
195
|
+
if anneal_t is None:
|
|
196
|
+
anneal_t = 8.0
|
|
197
|
+
|
|
198
|
+
if anneal_h is None:
|
|
199
|
+
anneal_h = 8.0
|
|
200
|
+
|
|
201
|
+
if repulsion_base is None:
|
|
202
|
+
repulsion_base = 8.0
|
|
203
|
+
|
|
204
|
+
bit_string, best_value, partition = cpu_footer(shots, quality, n_qubits, G_func, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
|
|
205
|
+
|
|
206
|
+
if best_value < 0.0:
|
|
207
|
+
# Best cut is trivial partition, all/empty
|
|
208
|
+
return '0' * n_qubits, 0.0, (nodes, [])
|
|
209
|
+
|
|
210
|
+
return bit_string, best_value, partition
|