PyQrackIsing 8.9.2__tar.gz → 8.9.4__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-8.9.2/pyqrackising.egg-info → pyqrackising-8.9.4}/PKG-INFO +1 -1
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyproject.toml +1 -1
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/maxcut_tfim.py +15 -107
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/maxcut_tfim_sparse.py +15 -108
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/maxcut_tfim_streaming.py +16 -108
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/maxcut_tfim_util.py +60 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/setup.py +1 -1
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/CMakeLists.txt +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/LICENSE.md +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/MANIFEST.in +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/README.md +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/__init__.py +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/tsp.py +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-8.9.2 → pyqrackising-8.9.4}/setup.cfg +0 -0
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "pyqrackising"
|
|
15
|
-
version = "8.9.
|
|
15
|
+
version = "8.9.4"
|
|
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"}
|
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
|
4
4
|
import os
|
|
5
5
|
from numba import njit, prange
|
|
6
6
|
|
|
7
|
-
from .maxcut_tfim_util import get_cut, maxcut_hamming_cdf, opencl_context
|
|
7
|
+
from .maxcut_tfim_util import get_cut, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick, init_bit_pick
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
epsilon = opencl_context.epsilon
|
|
@@ -38,14 +38,7 @@ def local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_ba
|
|
|
38
38
|
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
39
39
|
|
|
40
40
|
# First bit:
|
|
41
|
-
|
|
42
|
-
cum = 0.0
|
|
43
|
-
node = 0
|
|
44
|
-
for i in range(n):
|
|
45
|
-
cum += weights[i]
|
|
46
|
-
if (tot_init_weight * r) < cum:
|
|
47
|
-
node = i
|
|
48
|
-
break
|
|
41
|
+
node = init_bit_pick(weights, tot_init_weight, n)
|
|
49
42
|
|
|
50
43
|
if m == 1:
|
|
51
44
|
used[node] = True
|
|
@@ -54,56 +47,13 @@ def local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_ba
|
|
|
54
47
|
update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_base)
|
|
55
48
|
|
|
56
49
|
for _ in range(1, m - 1):
|
|
57
|
-
|
|
58
|
-
total_w = 0.0
|
|
59
|
-
for i in range(n):
|
|
60
|
-
if used[i]:
|
|
61
|
-
continue
|
|
62
|
-
total_w += weights[i]
|
|
63
|
-
|
|
64
|
-
# Normalize & sample
|
|
65
|
-
r = np.random.rand()
|
|
66
|
-
cum = 0.0
|
|
67
|
-
node = -1
|
|
68
|
-
for i in range(n):
|
|
69
|
-
if used[i]:
|
|
70
|
-
continue
|
|
71
|
-
cum += weights[i]
|
|
72
|
-
if (total_w * r) < cum:
|
|
73
|
-
node = i
|
|
74
|
-
break
|
|
75
|
-
|
|
76
|
-
if node == -1:
|
|
77
|
-
node = 0
|
|
78
|
-
while used[node]:
|
|
79
|
-
node += 1
|
|
50
|
+
node = bit_pick(weights, used, n)
|
|
80
51
|
|
|
81
52
|
# Update answer and weights
|
|
82
53
|
update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_base)
|
|
83
54
|
|
|
84
55
|
# Count available
|
|
85
|
-
|
|
86
|
-
for i in range(n):
|
|
87
|
-
if used[i]:
|
|
88
|
-
continue
|
|
89
|
-
total_w += weights[i]
|
|
90
|
-
|
|
91
|
-
# Normalize & sample
|
|
92
|
-
r = np.random.rand()
|
|
93
|
-
cum = 0.0
|
|
94
|
-
node = -1
|
|
95
|
-
for i in range(n):
|
|
96
|
-
if used[i]:
|
|
97
|
-
continue
|
|
98
|
-
cum += weights[i]
|
|
99
|
-
if (total_w * r) < cum:
|
|
100
|
-
node = i
|
|
101
|
-
break
|
|
102
|
-
|
|
103
|
-
if node == -1:
|
|
104
|
-
node = 0
|
|
105
|
-
while used[node]:
|
|
106
|
-
node += 1
|
|
56
|
+
node = bit_pick(weights, used, n)
|
|
107
57
|
|
|
108
58
|
used[node] = True
|
|
109
59
|
|
|
@@ -118,7 +68,7 @@ def compute_energy(sample, G_m, n_qubits):
|
|
|
118
68
|
val = G_m[u, v]
|
|
119
69
|
energy += val if sample[u] == sample[v] else -val
|
|
120
70
|
|
|
121
|
-
return energy
|
|
71
|
+
return -energy
|
|
122
72
|
|
|
123
73
|
|
|
124
74
|
@njit
|
|
@@ -133,7 +83,7 @@ def compute_cut(sample, G_m, n_qubits):
|
|
|
133
83
|
|
|
134
84
|
|
|
135
85
|
@njit(parallel=True)
|
|
136
|
-
def
|
|
86
|
+
def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass):
|
|
137
87
|
shots = max(1, shots >> 1)
|
|
138
88
|
n = len(G_m)
|
|
139
89
|
tot_init_weight = weights.sum()
|
|
@@ -142,70 +92,31 @@ def sample_for_energy(G_m, max_edge, shots, thresholds, weights, repulsion_base)
|
|
|
142
92
|
energies = np.empty(shots, dtype=dtype)
|
|
143
93
|
|
|
144
94
|
best_solution = solutions[0]
|
|
145
|
-
best_energy = float("inf")
|
|
95
|
+
best_energy = -float("inf")
|
|
146
96
|
|
|
147
97
|
improved = True
|
|
148
98
|
while improved:
|
|
149
99
|
improved = False
|
|
150
100
|
for s in prange(shots):
|
|
151
101
|
# First dimension: Hamming weight
|
|
152
|
-
|
|
153
|
-
m = 0
|
|
154
|
-
while thresholds[m] < mag_prob:
|
|
155
|
-
m += 1
|
|
156
|
-
m += 1
|
|
102
|
+
m = sample_mag(thresholds)
|
|
157
103
|
|
|
158
104
|
# Second dimension: permutation within Hamming weight
|
|
159
105
|
sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
160
106
|
solutions[s] = sample
|
|
161
|
-
energies[s] = compute_energy(sample, G_m, n)
|
|
107
|
+
energies[s] = compute_energy(sample, G_m, n) if is_spin_glass else compute_cut(sample, G_m, n)
|
|
162
108
|
|
|
163
|
-
best_index = np.
|
|
109
|
+
best_index = np.argmax(energies)
|
|
164
110
|
energy = energies[best_index]
|
|
165
|
-
if energy
|
|
111
|
+
if energy > best_energy:
|
|
166
112
|
best_energy = energy
|
|
167
113
|
best_solution = solutions[best_index].copy()
|
|
168
114
|
improved = True
|
|
169
115
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
@njit(parallel=True)
|
|
174
|
-
def sample_for_cut(G_m, max_edge, shots, thresholds, weights, repulsion_base):
|
|
175
|
-
shots = max(1, shots >> 1)
|
|
176
|
-
n = len(G_m)
|
|
177
|
-
tot_init_weight = weights.sum()
|
|
178
|
-
|
|
179
|
-
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
180
|
-
cuts = np.empty(shots, dtype=dtype)
|
|
181
|
-
|
|
182
|
-
best_solution = solutions[0]
|
|
183
|
-
best_cut = -float("inf")
|
|
184
|
-
|
|
185
|
-
improved = True
|
|
186
|
-
while improved:
|
|
187
|
-
improved = False
|
|
188
|
-
for s in prange(shots):
|
|
189
|
-
# First dimension: Hamming weight
|
|
190
|
-
mag_prob = np.random.random()
|
|
191
|
-
m = 0
|
|
192
|
-
while thresholds[m] < mag_prob:
|
|
193
|
-
m += 1
|
|
194
|
-
m += 1
|
|
195
|
-
|
|
196
|
-
# Second dimension: permutation within Hamming weight
|
|
197
|
-
sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
198
|
-
solutions[s] = sample
|
|
199
|
-
cuts[s] = compute_cut(sample, G_m, n)
|
|
200
|
-
|
|
201
|
-
best_index = np.argmax(cuts)
|
|
202
|
-
cut = cuts[best_index]
|
|
203
|
-
if cut > best_cut:
|
|
204
|
-
best_cut = cut
|
|
205
|
-
best_solution = solutions[best_index].copy()
|
|
206
|
-
improved = True
|
|
116
|
+
if is_spin_glass:
|
|
117
|
+
best_energy = compute_cut(best_solution, G_m, n)
|
|
207
118
|
|
|
208
|
-
return best_solution,
|
|
119
|
+
return best_solution, best_energy
|
|
209
120
|
|
|
210
121
|
|
|
211
122
|
@njit(parallel=True)
|
|
@@ -240,10 +151,7 @@ def cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, an
|
|
|
240
151
|
degrees = None
|
|
241
152
|
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
242
153
|
|
|
243
|
-
|
|
244
|
-
best_solution, best_value = sample_for_energy(G_m, max_edge, shots, hamming_prob, J_eff, repulsion_base)
|
|
245
|
-
else:
|
|
246
|
-
best_solution, best_value = sample_for_cut(G_m, max_edge, shots, hamming_prob, J_eff, repulsion_base)
|
|
154
|
+
best_solution, best_value = sample_measurement(G_m, max_edge, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass)
|
|
247
155
|
|
|
248
156
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
249
157
|
|
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
|
4
4
|
import os
|
|
5
5
|
from numba import njit, prange
|
|
6
6
|
|
|
7
|
-
from .maxcut_tfim_util import binary_search, get_cut, maxcut_hamming_cdf, opencl_context, to_scipy_sparse_upper_triangular
|
|
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
8
|
|
|
9
9
|
|
|
10
10
|
epsilon = opencl_context.epsilon
|
|
@@ -48,14 +48,7 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_w
|
|
|
48
48
|
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
49
49
|
|
|
50
50
|
# First bit:
|
|
51
|
-
|
|
52
|
-
cum = 0.0
|
|
53
|
-
node = 0
|
|
54
|
-
for i in range(n):
|
|
55
|
-
cum += weights[i]
|
|
56
|
-
if (tot_init_weight * r) < cum:
|
|
57
|
-
node = i
|
|
58
|
-
break
|
|
51
|
+
node = init_bit_pick(weights, tot_init_weight, n)
|
|
59
52
|
|
|
60
53
|
if m == 1:
|
|
61
54
|
used[node] = True
|
|
@@ -64,56 +57,12 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_w
|
|
|
64
57
|
update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node, repulsion_base)
|
|
65
58
|
|
|
66
59
|
for _ in range(1, m - 1):
|
|
67
|
-
|
|
68
|
-
total_w = 0.0
|
|
69
|
-
for i in range(n):
|
|
70
|
-
if used[i]:
|
|
71
|
-
continue
|
|
72
|
-
total_w += weights[i]
|
|
73
|
-
|
|
74
|
-
# Normalize & sample
|
|
75
|
-
r = np.random.rand()
|
|
76
|
-
cum = 0.0
|
|
77
|
-
node = -1
|
|
78
|
-
for i in range(n):
|
|
79
|
-
if used[i]:
|
|
80
|
-
continue
|
|
81
|
-
cum += weights[i]
|
|
82
|
-
if (total_w * r) < cum:
|
|
83
|
-
node = i
|
|
84
|
-
break
|
|
85
|
-
|
|
86
|
-
if node == -1:
|
|
87
|
-
node = 0
|
|
88
|
-
while used[node]:
|
|
89
|
-
node += 1
|
|
60
|
+
node = bit_pick(weights, used, n)
|
|
90
61
|
|
|
91
62
|
# Update answer and weights
|
|
92
63
|
update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node, repulsion_base)
|
|
93
64
|
|
|
94
|
-
|
|
95
|
-
total_w = 0.0
|
|
96
|
-
for i in range(n):
|
|
97
|
-
if used[i]:
|
|
98
|
-
continue
|
|
99
|
-
total_w += weights[i]
|
|
100
|
-
|
|
101
|
-
# Normalize & sample
|
|
102
|
-
r = np.random.rand()
|
|
103
|
-
cum = 0.0
|
|
104
|
-
node = -1
|
|
105
|
-
for i in range(n):
|
|
106
|
-
if used[i]:
|
|
107
|
-
continue
|
|
108
|
-
cum += weights[i]
|
|
109
|
-
if (total_w * r) < cum:
|
|
110
|
-
node = i
|
|
111
|
-
break
|
|
112
|
-
|
|
113
|
-
if node == -1:
|
|
114
|
-
node = 0
|
|
115
|
-
while used[node]:
|
|
116
|
-
node += 1
|
|
65
|
+
node = bit_pick(weights, used, n)
|
|
117
66
|
|
|
118
67
|
used[node] = True
|
|
119
68
|
|
|
@@ -129,7 +78,7 @@ def compute_energy(sample, G_data, G_rows, G_cols, n_qubits):
|
|
|
129
78
|
val = G_data[col]
|
|
130
79
|
energy += val if sample[u] == sample[v] else -val
|
|
131
80
|
|
|
132
|
-
return energy
|
|
81
|
+
return -energy
|
|
133
82
|
|
|
134
83
|
|
|
135
84
|
@njit
|
|
@@ -145,7 +94,7 @@ def compute_cut(sample, G_data, G_rows, G_cols, n_qubits):
|
|
|
145
94
|
|
|
146
95
|
|
|
147
96
|
@njit(parallel=True)
|
|
148
|
-
def
|
|
97
|
+
def sample_measurement(max_edge, G_data, G_rows, G_cols, shots, thresholds, weights, repulsion_base, is_spin_glass):
|
|
149
98
|
shots = max(1, shots >> 1)
|
|
150
99
|
n = G_rows.shape[0] - 1
|
|
151
100
|
tot_init_weight = weights.sum()
|
|
@@ -154,70 +103,31 @@ def sample_for_energy(max_edge, G_data, G_rows, G_cols, shots, thresholds, weigh
|
|
|
154
103
|
energies = np.empty(shots, dtype=dtype)
|
|
155
104
|
|
|
156
105
|
best_solution = solutions[0]
|
|
157
|
-
best_energy = float("inf")
|
|
106
|
+
best_energy = -float("inf")
|
|
158
107
|
|
|
159
108
|
improved = True
|
|
160
109
|
while improved:
|
|
161
110
|
improved = False
|
|
162
111
|
for s in prange(shots):
|
|
163
112
|
# First dimension: Hamming weight
|
|
164
|
-
|
|
165
|
-
m = 0
|
|
166
|
-
while thresholds[m] < mag_prob:
|
|
167
|
-
m += 1
|
|
168
|
-
m += 1
|
|
113
|
+
m = sample_mag(thresholds)
|
|
169
114
|
|
|
170
115
|
# Second dimension: permutation within Hamming weight
|
|
171
116
|
sample = local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
172
117
|
solutions[s] = sample
|
|
173
|
-
energies[s] = compute_energy(sample, G_data, G_rows, G_cols, n)
|
|
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)
|
|
174
119
|
|
|
175
|
-
best_index = np.
|
|
120
|
+
best_index = np.argmax(energies)
|
|
176
121
|
energy = energies[best_index]
|
|
177
|
-
if energy
|
|
122
|
+
if energy > best_energy:
|
|
178
123
|
best_energy = energy
|
|
179
124
|
best_solution = solutions[best_index].copy()
|
|
180
125
|
improved = True
|
|
181
126
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
@njit(parallel=True)
|
|
186
|
-
def sample_for_cut(max_edge, G_data, G_rows, G_cols, shots, thresholds, weights, repulsion_base):
|
|
187
|
-
shots = max(1, shots >> 1)
|
|
188
|
-
n = G_rows.shape[0] - 1
|
|
189
|
-
tot_init_weight = weights.sum()
|
|
190
|
-
|
|
191
|
-
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
192
|
-
cuts = np.empty(shots, dtype=dtype)
|
|
193
|
-
|
|
194
|
-
best_solution = solutions[0]
|
|
195
|
-
best_cut = -float("inf")
|
|
196
|
-
|
|
197
|
-
improved = True
|
|
198
|
-
while improved:
|
|
199
|
-
improved = False
|
|
200
|
-
for s in prange(shots):
|
|
201
|
-
# First dimension: Hamming weight
|
|
202
|
-
mag_prob = np.random.random()
|
|
203
|
-
m = 0
|
|
204
|
-
while thresholds[m] < mag_prob:
|
|
205
|
-
m += 1
|
|
206
|
-
m += 1
|
|
207
|
-
|
|
208
|
-
# Second dimension: permutation within Hamming weight
|
|
209
|
-
sample = local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
210
|
-
solutions[s] = sample
|
|
211
|
-
cuts[s] = compute_cut(sample, G_data, G_rows, G_cols, n)
|
|
212
|
-
|
|
213
|
-
best_index = np.argmax(cuts)
|
|
214
|
-
cut = cuts[best_index]
|
|
215
|
-
if cut > best_cut:
|
|
216
|
-
best_cut = cut
|
|
217
|
-
best_solution = solutions[best_index].copy()
|
|
218
|
-
improved = True
|
|
127
|
+
if is_spin_glass:
|
|
128
|
+
best_energy = compute_cut(best_solution, G_data, G_rows, G_cols, n)
|
|
219
129
|
|
|
220
|
-
return best_solution,
|
|
130
|
+
return best_solution, best_energy
|
|
221
131
|
|
|
222
132
|
|
|
223
133
|
@njit(parallel=True)
|
|
@@ -256,10 +166,7 @@ def cpu_footer(shots, quality, n_qubits, max_edge, G_data, G_rows, G_col, nodes,
|
|
|
256
166
|
degrees = None
|
|
257
167
|
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
258
168
|
|
|
259
|
-
|
|
260
|
-
best_solution, best_value = sample_for_energy(max_edge, G_data, G_rows, G_col, shots, hamming_prob, J_eff, repulsion_base)
|
|
261
|
-
else:
|
|
262
|
-
best_solution, best_value = sample_for_cut(max_edge, G_data, G_rows, G_col, shots, hamming_prob, J_eff, repulsion_base)
|
|
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)
|
|
263
170
|
|
|
264
171
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
265
172
|
|
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
|
4
4
|
import os
|
|
5
5
|
from numba import njit, prange
|
|
6
6
|
|
|
7
|
-
from .maxcut_tfim_util import get_cut, maxcut_hamming_cdf, opencl_context
|
|
7
|
+
from .maxcut_tfim_util import get_cut, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick, init_bit_pick
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
epsilon = opencl_context.epsilon
|
|
@@ -38,14 +38,7 @@ def local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, re
|
|
|
38
38
|
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
39
39
|
|
|
40
40
|
# First bit:
|
|
41
|
-
|
|
42
|
-
cum = 0.0
|
|
43
|
-
node = 0
|
|
44
|
-
for i in range(n):
|
|
45
|
-
cum += weights[i]
|
|
46
|
-
if (tot_init_weight * r) < cum:
|
|
47
|
-
node = i
|
|
48
|
-
break
|
|
41
|
+
node = init_bit_pick(weights, tot_init_weight, n)
|
|
49
42
|
|
|
50
43
|
if m == 1:
|
|
51
44
|
used[node] = True
|
|
@@ -54,56 +47,12 @@ def local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, re
|
|
|
54
47
|
update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, repulsion_base)
|
|
55
48
|
|
|
56
49
|
for _ in range(1, m - 1):
|
|
57
|
-
|
|
58
|
-
total_w = 0.0
|
|
59
|
-
for i in range(n):
|
|
60
|
-
if used[i]:
|
|
61
|
-
continue
|
|
62
|
-
total_w += weights[i]
|
|
63
|
-
|
|
64
|
-
# Normalize & sample
|
|
65
|
-
r = np.random.rand()
|
|
66
|
-
cum = 0.0
|
|
67
|
-
node = -1
|
|
68
|
-
for i in range(n):
|
|
69
|
-
if used[i]:
|
|
70
|
-
continue
|
|
71
|
-
cum += weights[i]
|
|
72
|
-
if (total_w * r) < cum:
|
|
73
|
-
node = i
|
|
74
|
-
break
|
|
75
|
-
|
|
76
|
-
if node == -1:
|
|
77
|
-
node = 0
|
|
78
|
-
while used[node]:
|
|
79
|
-
node += 1
|
|
50
|
+
node = bit_pick(weights, used, n)
|
|
80
51
|
|
|
81
52
|
# Update answer and weights
|
|
82
53
|
update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, repulsion_base)
|
|
83
54
|
|
|
84
|
-
|
|
85
|
-
total_w = 0.0
|
|
86
|
-
for i in range(n):
|
|
87
|
-
if used[i]:
|
|
88
|
-
continue
|
|
89
|
-
total_w += weights[i]
|
|
90
|
-
|
|
91
|
-
# Normalize & sample
|
|
92
|
-
r = np.random.rand()
|
|
93
|
-
cum = 0.0
|
|
94
|
-
node = -1
|
|
95
|
-
for i in range(n):
|
|
96
|
-
if used[i]:
|
|
97
|
-
continue
|
|
98
|
-
cum += weights[i]
|
|
99
|
-
if (total_w * r) < cum:
|
|
100
|
-
node = i
|
|
101
|
-
break
|
|
102
|
-
|
|
103
|
-
if node == -1:
|
|
104
|
-
node = 0
|
|
105
|
-
while used[node]:
|
|
106
|
-
node += 1
|
|
55
|
+
node = bit_pick(weights, used, n)
|
|
107
56
|
|
|
108
57
|
# Select node
|
|
109
58
|
used[node] = True
|
|
@@ -119,7 +68,7 @@ def compute_energy(sample, G_func, nodes, n_qubits):
|
|
|
119
68
|
val = G_func(nodes[u], nodes[v])
|
|
120
69
|
energy += val if sample[u] == sample[v] else -val
|
|
121
70
|
|
|
122
|
-
return energy
|
|
71
|
+
return -energy
|
|
123
72
|
|
|
124
73
|
|
|
125
74
|
@njit
|
|
@@ -134,7 +83,7 @@ def compute_cut(sample, G_func, nodes, n_qubits):
|
|
|
134
83
|
|
|
135
84
|
|
|
136
85
|
@njit(parallel=True)
|
|
137
|
-
def
|
|
86
|
+
def sample_measurement(G_func, nodes, max_edge, shots, thresholds, degrees_sum, weights, n, repulsion_base, is_spin_glass):
|
|
138
87
|
shots = max(1, shots >> 1)
|
|
139
88
|
tot_init_weight = weights.sum()
|
|
140
89
|
|
|
@@ -142,69 +91,31 @@ def sample_for_energy(G_func, nodes, max_edge, shots, thresholds, degrees_sum, w
|
|
|
142
91
|
energies = np.empty(shots, dtype=dtype)
|
|
143
92
|
|
|
144
93
|
best_solution = solutions[0]
|
|
145
|
-
best_energy = float("inf")
|
|
94
|
+
best_energy = -float("inf")
|
|
146
95
|
|
|
147
96
|
improved = True
|
|
148
97
|
while improved:
|
|
149
98
|
improved = False
|
|
150
99
|
for s in prange(shots):
|
|
151
100
|
# First dimension: Hamming weight
|
|
152
|
-
|
|
153
|
-
m = 0
|
|
154
|
-
while thresholds[m] < mag_prob:
|
|
155
|
-
m += 1
|
|
156
|
-
m += 1
|
|
101
|
+
m = sample_mag(thresholds)
|
|
157
102
|
|
|
158
103
|
# Second dimension: permutation within Hamming weight
|
|
159
104
|
sample = local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
160
105
|
solutions[s] = sample
|
|
161
|
-
energies[s] = compute_energy(sample, G_func, nodes, n)
|
|
106
|
+
energies[s] = compute_energy(sample, G_func, nodes, n) if is_spin_glass else compute_cut(sample, G_func, nodes, n)
|
|
162
107
|
|
|
163
|
-
best_index = np.
|
|
108
|
+
best_index = np.argmax(energies)
|
|
164
109
|
energy = energies[best_index]
|
|
165
|
-
if energy
|
|
110
|
+
if energy > best_energy:
|
|
166
111
|
best_energy = energy
|
|
167
112
|
best_solution = solutions[best_index].copy()
|
|
168
113
|
improved = True
|
|
169
114
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
@njit(parallel=True)
|
|
174
|
-
def sample_for_cut(G_func, nodes, max_edge, shots, thresholds, degrees_sum, weights, n, repulsion_base):
|
|
175
|
-
shots = max(1, shots >> 1)
|
|
176
|
-
tot_init_weight = weights.sum()
|
|
177
|
-
|
|
178
|
-
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
179
|
-
cuts = np.empty(shots, dtype=dtype)
|
|
180
|
-
|
|
181
|
-
best_solution = solutions[0]
|
|
182
|
-
best_cut = -float("inf")
|
|
183
|
-
|
|
184
|
-
improved = True
|
|
185
|
-
while improved:
|
|
186
|
-
improved = False
|
|
187
|
-
for s in prange(shots):
|
|
188
|
-
# First dimension: Hamming weight
|
|
189
|
-
mag_prob = np.random.random()
|
|
190
|
-
m = 0
|
|
191
|
-
while thresholds[m] < mag_prob:
|
|
192
|
-
m += 1
|
|
193
|
-
m += 1
|
|
194
|
-
|
|
195
|
-
# Second dimension: permutation within Hamming weight
|
|
196
|
-
sample = local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
197
|
-
solutions[s] = sample
|
|
198
|
-
cuts[s] = compute_cut(sample, G_func, nodes, n)
|
|
199
|
-
|
|
200
|
-
best_index = np.argmax(cuts)
|
|
201
|
-
cut = cuts[best_index]
|
|
202
|
-
if cut > best_cut:
|
|
203
|
-
best_cut = cut
|
|
204
|
-
best_solution = solutions[best_index].copy()
|
|
205
|
-
improved = True
|
|
115
|
+
if is_spin_glass:
|
|
116
|
+
best_energy = compute_cut(best_solution, G_func, nodes, n)
|
|
206
117
|
|
|
207
|
-
return best_solution,
|
|
118
|
+
return best_solution, best_energy
|
|
208
119
|
|
|
209
120
|
|
|
210
121
|
@njit(parallel=True)
|
|
@@ -235,15 +146,12 @@ def init_J_and_z(G_func, nodes):
|
|
|
235
146
|
def cpu_footer(shots, quality, n_qubits, G_func, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
236
147
|
J_eff, degrees, max_edge = init_J_and_z(G_func, nodes)
|
|
237
148
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
238
|
-
|
|
149
|
+
degrees_sum = degrees.sum()
|
|
239
150
|
|
|
240
151
|
degrees = None
|
|
241
152
|
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
242
153
|
|
|
243
|
-
|
|
244
|
-
best_solution, best_value = sample_for_energy(G_func, nodes, max_edge, shots, hamming_prob, max_edge, J_eff, n_qubits, repulsion_base)
|
|
245
|
-
else:
|
|
246
|
-
best_solution, best_value = sample_for_cut(G_func, nodes, max_edge, shots, hamming_prob, max_edge, J_eff, n_qubits, repulsion_base)
|
|
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)
|
|
247
155
|
|
|
248
156
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
249
157
|
|
|
@@ -255,3 +255,63 @@ def fix_cdf(hamming_prob):
|
|
|
255
255
|
cum_prob[-1] = 2.0
|
|
256
256
|
|
|
257
257
|
return cum_prob
|
|
258
|
+
|
|
259
|
+
@njit
|
|
260
|
+
def sample_mag(cum_prob):
|
|
261
|
+
p = np.random.random()
|
|
262
|
+
m = 0
|
|
263
|
+
left = 0
|
|
264
|
+
right = len(cum_prob) - 1
|
|
265
|
+
while True:
|
|
266
|
+
m = (left + right) >> 1
|
|
267
|
+
|
|
268
|
+
if (cum_prob[m] >= p) and ((m == 0) or cum_prob[m - 1] < p):
|
|
269
|
+
break
|
|
270
|
+
|
|
271
|
+
if cum_prob[m] < p:
|
|
272
|
+
left = m + 1
|
|
273
|
+
else:
|
|
274
|
+
right = m - 1
|
|
275
|
+
|
|
276
|
+
return m + 1
|
|
277
|
+
|
|
278
|
+
@njit
|
|
279
|
+
def init_bit_pick(weights, tot_init_weight, n):
|
|
280
|
+
r = np.random.rand()
|
|
281
|
+
cum = 0.0
|
|
282
|
+
node = 0
|
|
283
|
+
for i in range(n):
|
|
284
|
+
cum += weights[i]
|
|
285
|
+
if (tot_init_weight * r) < cum:
|
|
286
|
+
node = i
|
|
287
|
+
break
|
|
288
|
+
|
|
289
|
+
return node
|
|
290
|
+
|
|
291
|
+
@njit
|
|
292
|
+
def bit_pick(weights, used, n):
|
|
293
|
+
# Count available
|
|
294
|
+
total_w = 0.0
|
|
295
|
+
for i in range(n):
|
|
296
|
+
if used[i]:
|
|
297
|
+
continue
|
|
298
|
+
total_w += weights[i]
|
|
299
|
+
|
|
300
|
+
# Normalize & sample
|
|
301
|
+
r = np.random.rand()
|
|
302
|
+
cum = 0.0
|
|
303
|
+
node = -1
|
|
304
|
+
for i in range(n):
|
|
305
|
+
if used[i]:
|
|
306
|
+
continue
|
|
307
|
+
cum += weights[i]
|
|
308
|
+
if (total_w * r) < cum:
|
|
309
|
+
node = i
|
|
310
|
+
break
|
|
311
|
+
|
|
312
|
+
if node == -1:
|
|
313
|
+
node = 0
|
|
314
|
+
while used[node]:
|
|
315
|
+
node += 1
|
|
316
|
+
|
|
317
|
+
return node
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='8.9.
|
|
54
|
+
version='8.9.4',
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|