PyQrackIsing 4.0.0__tar.gz → 4.1.0__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-4.0.0/pyqrackising.egg-info → pyqrackising-4.1.0}/PKG-INFO +1 -1
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyproject.toml +1 -1
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising/maxcut_tfim.py +25 -19
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising/maxcut_tfim_sparse.py +21 -17
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising/spin_glass_solver.py +10 -9
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising/spin_glass_solver_sparse.py +10 -11
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising/tsp.py +127 -104
- {pyqrackising-4.0.0 → pyqrackising-4.1.0/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/setup.py +1 -1
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/CMakeLists.txt +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/LICENSE.md +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/MANIFEST.in +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/README.md +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising/__init__.py +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.1.0}/setup.cfg +0 -0
|
@@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
|
|
|
11
11
|
|
|
12
12
|
[project]
|
|
13
13
|
name = "pyqrackising"
|
|
14
|
-
version = "4.
|
|
14
|
+
version = "4.1.0"
|
|
15
15
|
requires-python = ">=3.8"
|
|
16
16
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
17
17
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -108,10 +108,8 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
|
108
108
|
|
|
109
109
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
110
110
|
@njit
|
|
111
|
-
def local_repulsion_choice(adjacency_data, adjacency_rows, weights, n, m):
|
|
111
|
+
def local_repulsion_choice(adjacency_data, adjacency_vals, adjacency_rows, weights, n, m):
|
|
112
112
|
"""
|
|
113
|
-
|
|
114
|
-
|
|
115
113
|
Pick m nodes out of n with repulsion bias:
|
|
116
114
|
- High-degree nodes are already less likely
|
|
117
115
|
- After choosing a node, its neighbors' probabilities are further reduced
|
|
@@ -158,7 +156,7 @@ def local_repulsion_choice(adjacency_data, adjacency_rows, weights, n, m):
|
|
|
158
156
|
for j in range(adjacency_rows[node], adjacency_rows[node + 1]):
|
|
159
157
|
nbr = adjacency_data[j]
|
|
160
158
|
if available[nbr]:
|
|
161
|
-
weights[nbr] *=
|
|
159
|
+
weights[nbr] *= adjacency_vals[j] # tunable penalty factor
|
|
162
160
|
|
|
163
161
|
return mask
|
|
164
162
|
|
|
@@ -176,26 +174,33 @@ def compute_energy(sample, G_m, n_qubits):
|
|
|
176
174
|
@njit(parallel=True)
|
|
177
175
|
def compute_adjacency(G_m, size):
|
|
178
176
|
n_qubits = G_m.shape[0]
|
|
177
|
+
nrm = G_m.max()
|
|
179
178
|
adjacency_rows = np.empty(n_qubits + 1, dtype=np.int32)
|
|
180
179
|
adjacency_rows[0] = 0
|
|
181
|
-
adjacency_data = np.
|
|
180
|
+
adjacency_data = np.empty(size, dtype=np.int32)
|
|
181
|
+
adjacency_vals = np.empty(size, dtype=np.float64)
|
|
182
182
|
k = 0
|
|
183
183
|
for i in prange(n_qubits):
|
|
184
184
|
for j in range(n_qubits):
|
|
185
|
-
if
|
|
185
|
+
if i == j:
|
|
186
|
+
continue
|
|
187
|
+
val = G_m[i, j]
|
|
188
|
+
if val <= 0.0:
|
|
186
189
|
continue
|
|
187
190
|
adjacency_data[k] = j
|
|
191
|
+
adjacency_vals[k] = 0.5 ** (val / nrm)
|
|
188
192
|
k += 1
|
|
189
193
|
adjacency_rows[i + 1] = k
|
|
190
194
|
|
|
191
195
|
np.resize(adjacency_data, k)
|
|
196
|
+
np.resize(adjacency_vals, k)
|
|
192
197
|
|
|
193
|
-
return adjacency_data, adjacency_rows
|
|
198
|
+
return adjacency_data, adjacency_vals, adjacency_rows
|
|
194
199
|
|
|
195
200
|
|
|
196
201
|
@njit(parallel=True)
|
|
197
202
|
def sample_for_solution(G_m, shots, thresholds, degrees_sum, J_eff, n):
|
|
198
|
-
adjacency_data, adjacency_rows = compute_adjacency(G_m, degrees_sum)
|
|
203
|
+
adjacency_data, adjacency_vals, adjacency_rows = compute_adjacency(G_m, degrees_sum)
|
|
199
204
|
weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
|
|
200
205
|
|
|
201
206
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
@@ -210,7 +215,7 @@ def sample_for_solution(G_m, shots, thresholds, degrees_sum, J_eff, n):
|
|
|
210
215
|
m += 1
|
|
211
216
|
|
|
212
217
|
# Second dimension: permutation within Hamming weight
|
|
213
|
-
sample = local_repulsion_choice(adjacency_data, adjacency_rows, weights, n, m)
|
|
218
|
+
sample = local_repulsion_choice(adjacency_data, adjacency_vals, adjacency_rows, weights, n, m)
|
|
214
219
|
|
|
215
220
|
solutions[s] = sample
|
|
216
221
|
energies[s] = compute_energy(sample, G_m, n)
|
|
@@ -302,21 +307,22 @@ def maxcut_tfim(
|
|
|
302
307
|
nodes = list(range(n_qubits))
|
|
303
308
|
G_m = G
|
|
304
309
|
|
|
305
|
-
if n_qubits
|
|
306
|
-
|
|
310
|
+
if n_qubits < 3:
|
|
311
|
+
if n_qubits == 0:
|
|
312
|
+
return "", 0, ([], [])
|
|
307
313
|
|
|
308
|
-
|
|
309
|
-
|
|
314
|
+
if n_qubits == 1:
|
|
315
|
+
return "0", 0, (nodes, [])
|
|
310
316
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
317
|
+
if n_qubits == 2:
|
|
318
|
+
weight = G_m[0, 1]
|
|
319
|
+
if weight < 0.0:
|
|
320
|
+
return "00", 0, (nodes, [])
|
|
315
321
|
|
|
316
|
-
|
|
322
|
+
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
317
323
|
|
|
318
324
|
if quality is None:
|
|
319
|
-
quality =
|
|
325
|
+
quality = 6
|
|
320
326
|
|
|
321
327
|
if shots is None:
|
|
322
328
|
# Number of measurement shots
|
|
@@ -108,9 +108,8 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
|
108
108
|
|
|
109
109
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
110
110
|
@njit
|
|
111
|
-
def local_repulsion_choice(adjacency_data, adjacency_rows, weights, n, m):
|
|
111
|
+
def local_repulsion_choice(adjacency_data, adjacency_vals, adjacency_rows, weights, n, m):
|
|
112
112
|
"""
|
|
113
|
-
|
|
114
113
|
Pick m nodes out of n with repulsion bias:
|
|
115
114
|
- High-degree nodes are already less likely
|
|
116
115
|
- After choosing a node, its neighbors' probabilities are further reduced
|
|
@@ -157,7 +156,7 @@ def local_repulsion_choice(adjacency_data, adjacency_rows, weights, n, m):
|
|
|
157
156
|
for j in range(adjacency_rows[node], adjacency_rows[node + 1]):
|
|
158
157
|
nbr = adjacency_data[j]
|
|
159
158
|
if available[nbr]:
|
|
160
|
-
weights[nbr] *=
|
|
159
|
+
weights[nbr] *= adjacency_vals[j] # tunable penalty factor
|
|
161
160
|
|
|
162
161
|
return mask
|
|
163
162
|
|
|
@@ -179,26 +178,30 @@ def compute_energy(sample, G_data, G_rows, G_cols):
|
|
|
179
178
|
@njit(parallel=True)
|
|
180
179
|
def compute_adjacency(G_data, G_rows, G_cols):
|
|
181
180
|
n_qubits = G_rows.shape[0] - 1
|
|
181
|
+
nrm = G_data.max()
|
|
182
182
|
adjacency_rows = np.empty(n_qubits + 1, dtype=np.int32)
|
|
183
183
|
adjacency_rows[0] = 0
|
|
184
|
-
adjacency_data = np.
|
|
184
|
+
adjacency_data = np.empty(G_data.shape[0], dtype=np.int32)
|
|
185
|
+
adjacency_vals = np.empty(G_data.shape[0], dtype=np.float64)
|
|
185
186
|
k = 0
|
|
186
187
|
for i in prange(n_qubits):
|
|
187
188
|
for j in range(G_rows[i], G_rows[i + 1]):
|
|
188
189
|
if G_data[j] > 0.0:
|
|
189
190
|
adjacency_data[k] = G_cols[j]
|
|
191
|
+
adjacency_vals[k] = 0.5 ** (G_data[j] / nrm)
|
|
190
192
|
k += 1
|
|
191
193
|
adjacency_rows[i + 1] = k
|
|
192
194
|
|
|
193
195
|
np.resize(adjacency_data, k)
|
|
196
|
+
np.resize(adjacency_vals, k)
|
|
194
197
|
|
|
195
|
-
return adjacency_data, adjacency_rows
|
|
198
|
+
return adjacency_data, adjacency_vals, adjacency_rows
|
|
196
199
|
|
|
197
200
|
|
|
198
201
|
@njit(parallel=True)
|
|
199
202
|
def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds, J_eff):
|
|
200
203
|
n = G_rows.shape[0] - 1
|
|
201
|
-
adjacency_data, adjacency_rows = compute_adjacency(G_data, G_rows, G_cols)
|
|
204
|
+
adjacency_data, adjacency_vals, adjacency_rows = compute_adjacency(G_data, G_rows, G_cols)
|
|
202
205
|
weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
|
|
203
206
|
|
|
204
207
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
@@ -213,7 +216,7 @@ def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds, J_eff):
|
|
|
213
216
|
m += 1
|
|
214
217
|
|
|
215
218
|
# Second dimension: permutation within Hamming weight
|
|
216
|
-
sample = local_repulsion_choice(adjacency_data, adjacency_rows, weights, n, m)
|
|
219
|
+
sample = local_repulsion_choice(adjacency_data, adjacency_vals, adjacency_rows, weights, n, m)
|
|
217
220
|
solutions[s] = sample
|
|
218
221
|
energies[s] = compute_energy(sample, G_data, G_rows, G_cols)
|
|
219
222
|
|
|
@@ -308,21 +311,22 @@ def maxcut_tfim_sparse(
|
|
|
308
311
|
nodes = list(range(n_qubits))
|
|
309
312
|
G_m = G
|
|
310
313
|
|
|
311
|
-
if n_qubits
|
|
312
|
-
|
|
314
|
+
if n_qubits < 3:
|
|
315
|
+
if n_qubits == 0:
|
|
316
|
+
return "", 0, ([], [])
|
|
313
317
|
|
|
314
|
-
|
|
315
|
-
|
|
318
|
+
if n_qubits == 1:
|
|
319
|
+
return "0", 0, (nodes, [])
|
|
316
320
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
+
if n_qubits == 2:
|
|
322
|
+
weight = G_m[0, 1]
|
|
323
|
+
if weight < 0.0:
|
|
324
|
+
return "00", 0, (nodes, [])
|
|
321
325
|
|
|
322
|
-
|
|
326
|
+
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
323
327
|
|
|
324
328
|
if quality is None:
|
|
325
|
-
quality =
|
|
329
|
+
quality = 6
|
|
326
330
|
|
|
327
331
|
if shots is None:
|
|
328
332
|
# Number of measurement shots
|
|
@@ -69,18 +69,19 @@ def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
|
|
|
69
69
|
nodes = list(range(n_qubits))
|
|
70
70
|
G_m = G
|
|
71
71
|
|
|
72
|
-
if n_qubits
|
|
73
|
-
|
|
72
|
+
if n_qubits < 3:
|
|
73
|
+
if n_qubits == 0:
|
|
74
|
+
return "", 0, ([], []), 0
|
|
74
75
|
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
if n_qubits == 1:
|
|
77
|
+
return "0", 0, (nodes, []), 0
|
|
77
78
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
if n_qubits == 2:
|
|
80
|
+
weight = G_m[0, 1]
|
|
81
|
+
if weight < 0.0:
|
|
82
|
+
return "00", 0, (nodes, []), weight
|
|
82
83
|
|
|
83
|
-
|
|
84
|
+
return "01", weight, ([nodes[0]], [nodes[1]]), -weight
|
|
84
85
|
|
|
85
86
|
bitstring = ""
|
|
86
87
|
if isinstance(best_guess, str):
|
|
@@ -74,18 +74,19 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
|
|
|
74
74
|
nodes = list(range(n_qubits))
|
|
75
75
|
G_m = G
|
|
76
76
|
|
|
77
|
-
if n_qubits
|
|
78
|
-
|
|
77
|
+
if n_qubits < 3:
|
|
78
|
+
if n_qubits == 0:
|
|
79
|
+
return "", 0, ([], []), 0
|
|
79
80
|
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
if n_qubits == 1:
|
|
82
|
+
return "0", 0, (nodes, []), 0
|
|
82
83
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
if n_qubits == 2:
|
|
85
|
+
weight = G_m[0, 1]
|
|
86
|
+
if weight < 0.0:
|
|
87
|
+
return "00", 0, (nodes, []), weight
|
|
87
88
|
|
|
88
|
-
|
|
89
|
+
return "01", weight, ([nodes[0]], [nodes[1]]), -weight
|
|
89
90
|
|
|
90
91
|
bitstring = ""
|
|
91
92
|
if isinstance(best_guess, str):
|
|
@@ -98,8 +99,6 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
|
|
|
98
99
|
bitstring, _, _ = maxcut_tfim_sparse(G_m, quality=quality, shots=shots)
|
|
99
100
|
best_theta = [b == "1" for b in list(bitstring)]
|
|
100
101
|
|
|
101
|
-
|
|
102
|
-
|
|
103
102
|
min_energy = compute_energy(best_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
104
103
|
improved = True
|
|
105
104
|
correction_quality = 1
|
|
@@ -270,6 +270,7 @@ def stitch_symmetric(G_m, path_a, path_b):
|
|
|
270
270
|
return best_path
|
|
271
271
|
|
|
272
272
|
|
|
273
|
+
@njit
|
|
273
274
|
def stitch_asymmetric(G_m, path_a, path_b):
|
|
274
275
|
if len(path_a) == 1:
|
|
275
276
|
return stich_singlet(G_m, path_a[0], path_b)
|
|
@@ -311,6 +312,7 @@ def stitch_asymmetric(G_m, path_a, path_b):
|
|
|
311
312
|
return best_path
|
|
312
313
|
|
|
313
314
|
|
|
315
|
+
@njit
|
|
314
316
|
def restitch(G_m, path, is_sym):
|
|
315
317
|
l = len(path)
|
|
316
318
|
mid = ((l + 1) if (l & 1) and (np.random.random() < 0.5) else l) >> 1
|
|
@@ -406,98 +408,43 @@ def tsp_bruteforce_acyclic(G_m, perms):
|
|
|
406
408
|
return best_path, best_weight
|
|
407
409
|
|
|
408
410
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
n_nodes
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
n_nodes = len(nodes)
|
|
416
|
-
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
|
|
417
|
-
else:
|
|
418
|
-
n_nodes = len(G)
|
|
419
|
-
nodes = list(range(n_nodes))
|
|
420
|
-
G_m = G
|
|
421
|
-
|
|
422
|
-
if is_cyclic:
|
|
423
|
-
start_node = None
|
|
424
|
-
end_node = None
|
|
425
|
-
|
|
426
|
-
if n_nodes < 7:
|
|
427
|
-
if n_nodes > 3:
|
|
428
|
-
if is_cyclic:
|
|
429
|
-
best_path, best_weight = tsp_bruteforce_cyclic(G_m, list(itertools.permutations(list(range(1, n_nodes)))))
|
|
430
|
-
else:
|
|
431
|
-
best_path, best_weight = tsp_bruteforce_acyclic(G_m, list(itertools.permutations(list(range(n_nodes)))))
|
|
432
|
-
|
|
433
|
-
return [nodes[x] for x in best_path], best_weight
|
|
434
|
-
|
|
435
|
-
if n_nodes == 3:
|
|
436
|
-
if is_cyclic:
|
|
437
|
-
weight_0 = G_m[0, 1] + G_m[1, 2] + G_m[2, 0]
|
|
438
|
-
weight_1 = G_m[0, 2] + G_m[2, 1] + G_m[1, 0]
|
|
439
|
-
|
|
440
|
-
if weight_0 >= weight_1:
|
|
441
|
-
return (nodes + [nodes[0]], weight_0)
|
|
411
|
+
@njit
|
|
412
|
+
def tsp_symmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic):
|
|
413
|
+
if n_nodes == 3:
|
|
414
|
+
if is_cyclic:
|
|
415
|
+
weight_0 = G_m[0, 1] + G_m[1, 2] + G_m[2, 0]
|
|
416
|
+
weight_1 = G_m[0, 2] + G_m[2, 1] + G_m[1, 0]
|
|
442
417
|
|
|
443
|
-
|
|
418
|
+
if weight_0 >= weight_1:
|
|
419
|
+
return (nodes + [nodes[0]], weight_0)
|
|
444
420
|
|
|
445
|
-
|
|
421
|
+
nodes.reverse()
|
|
446
422
|
|
|
447
|
-
|
|
448
|
-
w_021 = G_m[0, 1] + G_m[0, 2]
|
|
449
|
-
w_120 = G_m[0, 2] + G_m[1, 2]
|
|
423
|
+
return ([nodes[2]] + nodes, weight_1)
|
|
450
424
|
|
|
451
|
-
|
|
452
|
-
|
|
425
|
+
w_012 = G_m[0, 1] + G_m[1, 2]
|
|
426
|
+
w_021 = G_m[0, 1] + G_m[0, 2]
|
|
427
|
+
w_120 = G_m[0, 2] + G_m[1, 2]
|
|
453
428
|
|
|
454
|
-
|
|
455
|
-
|
|
429
|
+
if w_012 >= w_021 and w_012 >= w_120:
|
|
430
|
+
return (nodes, w_012)
|
|
456
431
|
|
|
457
|
-
|
|
432
|
+
if w_021 >= w_012 and w_021 >= w_120:
|
|
433
|
+
return ([nodes[1], nodes[0], nodes[2]], w_021)
|
|
458
434
|
|
|
459
|
-
|
|
460
|
-
if is_cyclic:
|
|
461
|
-
return (nodes + [nodes[0]], 2 * G_m[0, 1])
|
|
435
|
+
return ([nodes[0], nodes[2], nodes[1]], w_120)
|
|
462
436
|
|
|
463
|
-
|
|
437
|
+
if n_nodes == 2:
|
|
438
|
+
if is_cyclic:
|
|
439
|
+
return (nodes + [nodes[0]], 2 * G_m[0, 1])
|
|
464
440
|
|
|
465
|
-
return (nodes, 0)
|
|
441
|
+
return (nodes, G_m[0, 1])
|
|
466
442
|
|
|
443
|
+
return (nodes, 0)
|
|
467
444
|
|
|
468
|
-
if (start_node is None) and not (end_node is None):
|
|
469
|
-
start_node = end_node
|
|
470
|
-
end_node = None
|
|
471
|
-
|
|
472
|
-
a = []
|
|
473
|
-
b = []
|
|
474
|
-
c = []
|
|
475
|
-
if (start_node is None) and (end_node is None):
|
|
476
|
-
if monte_carlo:
|
|
477
|
-
a, b = monte_carlo_loop(n_nodes)
|
|
478
|
-
else:
|
|
479
|
-
best_energy = float("inf")
|
|
480
|
-
for _ in range(multi_start):
|
|
481
|
-
bits = ([], [])
|
|
482
|
-
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
483
|
-
_, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots)
|
|
484
|
-
if energy < best_energy:
|
|
485
|
-
best_energy = energy
|
|
486
|
-
a, b = bits
|
|
487
|
-
else:
|
|
488
|
-
is_cyclic = False
|
|
489
|
-
a.append(nodes.index(start_node))
|
|
490
|
-
b = list(range(n_nodes))
|
|
491
|
-
b.remove(a[0])
|
|
492
|
-
if end_node is not None:
|
|
493
|
-
c.append(nodes.index(end_node))
|
|
494
|
-
b.remove(c[0])
|
|
495
|
-
|
|
496
|
-
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
497
|
-
|
|
498
|
-
sol_a = tsp_symmetric(G_a, quality=quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, k_neighbors=0, multi_start=multi_start)
|
|
499
|
-
sol_b = tsp_symmetric(G_b, quality=quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, k_neighbors=0, multi_start=multi_start)
|
|
500
445
|
|
|
446
|
+
@njit
|
|
447
|
+
def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c):
|
|
501
448
|
path_a = [a[x] for x in sol_a[0]]
|
|
502
449
|
path_b = [b[x] for x in sol_b[0]]
|
|
503
450
|
|
|
@@ -515,7 +462,8 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, mont
|
|
|
515
462
|
best_path = path_a + path_b
|
|
516
463
|
best_weight = weight
|
|
517
464
|
|
|
518
|
-
|
|
465
|
+
if not c is None:
|
|
466
|
+
best_path.append(c)
|
|
519
467
|
|
|
520
468
|
if is_top_level:
|
|
521
469
|
if is_cyclic:
|
|
@@ -541,7 +489,7 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, mont
|
|
|
541
489
|
return [nodes[x] for x in best_path], best_weight
|
|
542
490
|
|
|
543
491
|
|
|
544
|
-
def
|
|
492
|
+
def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, monte_carlo=True, k_neighbors=20, is_cyclic=True, multi_start=1, is_top_level=True):
|
|
545
493
|
nodes = None
|
|
546
494
|
n_nodes = 0
|
|
547
495
|
G_m = None
|
|
@@ -559,7 +507,7 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, mon
|
|
|
559
507
|
end_node = None
|
|
560
508
|
|
|
561
509
|
if n_nodes < 7:
|
|
562
|
-
if n_nodes >
|
|
510
|
+
if n_nodes > 3:
|
|
563
511
|
if is_cyclic:
|
|
564
512
|
best_path, best_weight = tsp_bruteforce_cyclic(G_m, list(itertools.permutations(list(range(1, n_nodes)))))
|
|
565
513
|
else:
|
|
@@ -567,26 +515,11 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, mon
|
|
|
567
515
|
|
|
568
516
|
return [nodes[x] for x in best_path], best_weight
|
|
569
517
|
|
|
570
|
-
|
|
571
|
-
weight = G_m[0, 1]
|
|
572
|
-
if G_m[1, 0] < weight:
|
|
573
|
-
weight = G_m[1, 0]
|
|
574
|
-
nodes.reverse()
|
|
518
|
+
return tsp_symmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic)
|
|
575
519
|
|
|
576
|
-
if is_cyclic:
|
|
577
|
-
return (nodes + [nodes[0]], 2 * weight)
|
|
578
|
-
|
|
579
|
-
return (nodes, weight)
|
|
580
|
-
|
|
581
|
-
return (nodes, 0)
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
is_reversed = False
|
|
585
520
|
if (start_node is None) and not (end_node is None):
|
|
586
|
-
is_reversed = True
|
|
587
521
|
start_node = end_node
|
|
588
522
|
end_node = None
|
|
589
|
-
G_m = G_m.T
|
|
590
523
|
|
|
591
524
|
a = []
|
|
592
525
|
b = []
|
|
@@ -599,7 +532,7 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, mon
|
|
|
599
532
|
for _ in range(multi_start):
|
|
600
533
|
bits = ([], [])
|
|
601
534
|
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
602
|
-
_, _, bits, energy = spin_glass_solver(
|
|
535
|
+
_, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots)
|
|
603
536
|
if energy < best_energy:
|
|
604
537
|
best_energy = energy
|
|
605
538
|
a, b = bits
|
|
@@ -614,9 +547,30 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, mon
|
|
|
614
547
|
|
|
615
548
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
616
549
|
|
|
617
|
-
sol_a =
|
|
618
|
-
sol_b =
|
|
550
|
+
sol_a = tsp_symmetric(G_a, quality=quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, k_neighbors=0, multi_start=multi_start)
|
|
551
|
+
sol_b = tsp_symmetric(G_b, quality=quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, k_neighbors=0, multi_start=multi_start)
|
|
552
|
+
|
|
553
|
+
return tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c[0] if len(c) else None)
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
@njit
|
|
557
|
+
def tsp_asymmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic):
|
|
558
|
+
if n_nodes == 2:
|
|
559
|
+
weight = G_m[0, 1]
|
|
560
|
+
if G_m[1, 0] < weight:
|
|
561
|
+
weight = G_m[1, 0]
|
|
562
|
+
nodes.reverse()
|
|
563
|
+
|
|
564
|
+
if is_cyclic:
|
|
565
|
+
return (nodes + [nodes[0]], 2 * weight)
|
|
566
|
+
|
|
567
|
+
return (nodes, weight)
|
|
619
568
|
|
|
569
|
+
return (nodes, 0)
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
@njit
|
|
573
|
+
def tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c):
|
|
620
574
|
path_a = [a[x] for x in sol_a[0]]
|
|
621
575
|
path_b = [b[x] for x in sol_b[0]]
|
|
622
576
|
|
|
@@ -628,7 +582,8 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, mon
|
|
|
628
582
|
else:
|
|
629
583
|
best_path = path_a + path_b
|
|
630
584
|
|
|
631
|
-
|
|
585
|
+
if not c is None:
|
|
586
|
+
best_path.append(c)
|
|
632
587
|
|
|
633
588
|
if is_top_level:
|
|
634
589
|
if is_cyclic:
|
|
@@ -672,9 +627,77 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, mon
|
|
|
672
627
|
else:
|
|
673
628
|
final_path = best_path
|
|
674
629
|
|
|
675
|
-
best_weight = path_length(
|
|
630
|
+
best_weight = path_length(final_path, G_m)
|
|
676
631
|
|
|
677
632
|
if is_reversed:
|
|
678
633
|
final_path.reverse()
|
|
679
634
|
|
|
680
635
|
return [nodes[x] for x in final_path], best_weight
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, monte_carlo=True, k_neighbors=20, is_cyclic=True, multi_start=1, is_top_level=True):
|
|
639
|
+
nodes = None
|
|
640
|
+
n_nodes = 0
|
|
641
|
+
G_m = None
|
|
642
|
+
if isinstance(G, nx.Graph):
|
|
643
|
+
nodes = list(G.nodes())
|
|
644
|
+
n_nodes = len(nodes)
|
|
645
|
+
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
|
|
646
|
+
else:
|
|
647
|
+
n_nodes = len(G)
|
|
648
|
+
nodes = list(range(n_nodes))
|
|
649
|
+
G_m = G
|
|
650
|
+
|
|
651
|
+
if is_cyclic:
|
|
652
|
+
start_node = None
|
|
653
|
+
end_node = None
|
|
654
|
+
|
|
655
|
+
if n_nodes < 7:
|
|
656
|
+
if n_nodes > 2:
|
|
657
|
+
if is_cyclic:
|
|
658
|
+
best_path, best_weight = tsp_bruteforce_cyclic(G_m, list(itertools.permutations(list(range(1, n_nodes)))))
|
|
659
|
+
else:
|
|
660
|
+
best_path, best_weight = tsp_bruteforce_acyclic(G_m, list(itertools.permutations(list(range(n_nodes)))))
|
|
661
|
+
|
|
662
|
+
return [nodes[x] for x in best_path], best_weight
|
|
663
|
+
|
|
664
|
+
return tsp_asymmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic)
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
is_reversed = False
|
|
668
|
+
if (start_node is None) and not (end_node is None):
|
|
669
|
+
is_reversed = True
|
|
670
|
+
start_node = end_node
|
|
671
|
+
end_node = None
|
|
672
|
+
G_m = G_m.T
|
|
673
|
+
|
|
674
|
+
a = []
|
|
675
|
+
b = []
|
|
676
|
+
c = []
|
|
677
|
+
if (start_node is None) and (end_node is None):
|
|
678
|
+
if monte_carlo:
|
|
679
|
+
a, b = monte_carlo_loop(n_nodes)
|
|
680
|
+
else:
|
|
681
|
+
best_energy = float("inf")
|
|
682
|
+
for _ in range(multi_start):
|
|
683
|
+
bits = ([], [])
|
|
684
|
+
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
685
|
+
_, _, bits, energy = spin_glass_solver((G_m + G_m.T) / 2, quality=quality, shots=shots)
|
|
686
|
+
if energy < best_energy:
|
|
687
|
+
best_energy = energy
|
|
688
|
+
a, b = bits
|
|
689
|
+
else:
|
|
690
|
+
is_cyclic = False
|
|
691
|
+
a.append(nodes.index(start_node))
|
|
692
|
+
b = list(range(n_nodes))
|
|
693
|
+
b.remove(a[0])
|
|
694
|
+
if end_node is not None:
|
|
695
|
+
c.append(nodes.index(end_node))
|
|
696
|
+
b.remove(c[0])
|
|
697
|
+
|
|
698
|
+
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
699
|
+
|
|
700
|
+
sol_a = tsp_asymmetric(G_a, quality=quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, k_neighbors=0, multi_start=multi_start)
|
|
701
|
+
sol_b = tsp_asymmetric(G_b, quality=quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, k_neighbors=0, multi_start=multi_start)
|
|
702
|
+
|
|
703
|
+
return tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c[0] if len(c) else None)
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='4.
|
|
54
|
+
version='4.1.0',
|
|
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)',
|
|
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
|