PyQrackIsing 5.1.2__tar.gz → 5.1.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-5.1.2/pyqrackising.egg-info → pyqrackising-5.1.4}/PKG-INFO +1 -1
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyproject.toml +1 -1
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/maxcut_tfim.py +3 -126
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/maxcut_tfim_sparse.py +4 -127
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/maxcut_tfim_streaming.py +3 -126
- pyqrackising-5.1.4/pyqrackising/maxcut_tfim_util.py +155 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/setup.py +1 -1
- pyqrackising-5.1.2/pyqrackising/maxcut_tfim_util.py +0 -32
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/CMakeLists.txt +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/LICENSE.md +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/MANIFEST.in +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/README.md +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/__init__.py +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising/tsp.py +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.4}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-5.1.2 → pyqrackising-5.1.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 = "5.1.
|
|
15
|
+
version = "5.1.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 opencl_context
|
|
7
|
+
from .maxcut_tfim_util import init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -13,89 +13,6 @@ except ImportError:
|
|
|
13
13
|
IS_OPENCL_AVAILABLE = False
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
@njit
|
|
17
|
-
def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
|
|
18
|
-
bias = np.empty(n_qubits - 1, dtype=np.float64)
|
|
19
|
-
|
|
20
|
-
# critical angle
|
|
21
|
-
theta_c = np.arcsin(max(-1.0, min(1.0, abs(h) / (z * J))))
|
|
22
|
-
|
|
23
|
-
p = (
|
|
24
|
-
pow(2.0, abs(J / h) - 1.0)
|
|
25
|
-
* (1.0 + np.sin(theta - theta_c) * np.cos(1.5 * np.pi * J * t + theta) / (1.0 + np.sqrt(t)))
|
|
26
|
-
- 0.5
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
if (p * n_qubits) >= 1024:
|
|
30
|
-
return bias
|
|
31
|
-
|
|
32
|
-
tot_n = 1.0 + 1.0 / pow(2.0, p * n_qubits)
|
|
33
|
-
factor = pow(2.0, -p)
|
|
34
|
-
n = 1.0
|
|
35
|
-
for q in range(1, n_qubits):
|
|
36
|
-
n *= factor
|
|
37
|
-
bias[q - 1] = n
|
|
38
|
-
tot_n += n
|
|
39
|
-
bias /= tot_n
|
|
40
|
-
|
|
41
|
-
if J > 0.0:
|
|
42
|
-
return bias[::-1]
|
|
43
|
-
|
|
44
|
-
return bias
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
@njit(parallel=True)
|
|
48
|
-
def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
49
|
-
if n_qubits < 2:
|
|
50
|
-
hamming_prob.fill(0.0)
|
|
51
|
-
return
|
|
52
|
-
|
|
53
|
-
n_steps = 1 << quality
|
|
54
|
-
delta_t = 1.0 / n_steps
|
|
55
|
-
tot_t = 2.0 * n_steps * delta_t
|
|
56
|
-
h_mult = 2.0 / tot_t
|
|
57
|
-
n_bias = n_qubits - 1
|
|
58
|
-
|
|
59
|
-
theta = np.empty(n_qubits, dtype=np.float64)
|
|
60
|
-
for q in range(n_qubits):
|
|
61
|
-
J = J_func[q]
|
|
62
|
-
z = degrees[q]
|
|
63
|
-
theta[q] = np.arcsin(
|
|
64
|
-
max(
|
|
65
|
-
-1.0,
|
|
66
|
-
min(
|
|
67
|
-
1.0,
|
|
68
|
-
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
|
|
69
|
-
),
|
|
70
|
-
)
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
for qc in prange(n_qubits, n_steps * n_qubits):
|
|
74
|
-
step = qc // n_qubits
|
|
75
|
-
q = qc % n_qubits
|
|
76
|
-
J_eff = J_func[q]
|
|
77
|
-
if np.isclose(abs(J_eff), 0.0):
|
|
78
|
-
continue
|
|
79
|
-
z = degrees[q]
|
|
80
|
-
theta_eff = theta[q]
|
|
81
|
-
t = step * delta_t
|
|
82
|
-
tm1 = (step - 1) * delta_t
|
|
83
|
-
h_t = h_mult * (tot_t - t)
|
|
84
|
-
bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, t, n_qubits)
|
|
85
|
-
last_bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_qubits)
|
|
86
|
-
for i in range(n_bias):
|
|
87
|
-
hamming_prob[i] += bias[i] - last_bias[i]
|
|
88
|
-
|
|
89
|
-
tot_prob = hamming_prob.sum()
|
|
90
|
-
hamming_prob /= tot_prob
|
|
91
|
-
|
|
92
|
-
tot_prob = 0.0
|
|
93
|
-
for i in range(n_bias):
|
|
94
|
-
tot_prob += hamming_prob[i]
|
|
95
|
-
hamming_prob[i] = tot_prob
|
|
96
|
-
hamming_prob[-1] = 2.0
|
|
97
|
-
|
|
98
|
-
|
|
99
16
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
100
17
|
@njit
|
|
101
18
|
def local_repulsion_choice(G_m, max_weight, weights, n, m):
|
|
@@ -166,7 +83,7 @@ def sample_for_solution(G_m, shots, thresholds, degrees_sum, J_eff, n):
|
|
|
166
83
|
weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
|
|
167
84
|
|
|
168
85
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
169
|
-
energies = np.empty(shots, dtype=np.
|
|
86
|
+
energies = np.empty(shots, dtype=np.float32)
|
|
170
87
|
|
|
171
88
|
for s in prange(shots):
|
|
172
89
|
# First dimension: Hamming weight
|
|
@@ -197,7 +114,7 @@ def sample_for_solution(G_m, shots, thresholds, degrees_sum, J_eff, n):
|
|
|
197
114
|
def init_J_and_z(G_m):
|
|
198
115
|
n_qubits = len(G_m)
|
|
199
116
|
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
200
|
-
J_eff = np.empty(n_qubits, dtype=np.
|
|
117
|
+
J_eff = np.empty(n_qubits, dtype=np.float32)
|
|
201
118
|
J_max = -float("inf")
|
|
202
119
|
for n in prange(n_qubits):
|
|
203
120
|
degree = sum(G_m[n] != 0.0)
|
|
@@ -211,46 +128,6 @@ def init_J_and_z(G_m):
|
|
|
211
128
|
return J_eff, degrees
|
|
212
129
|
|
|
213
130
|
|
|
214
|
-
@njit
|
|
215
|
-
def init_thresholds(n_qubits):
|
|
216
|
-
n_bias = n_qubits - 1
|
|
217
|
-
thresholds = np.empty(n_bias, dtype=np.float64)
|
|
218
|
-
tot_prob = 0
|
|
219
|
-
p = 1.0
|
|
220
|
-
if n_qubits & 1:
|
|
221
|
-
q = n_qubits // 2
|
|
222
|
-
thresholds[q - 1] = p
|
|
223
|
-
tot_prob = p
|
|
224
|
-
p /= 2
|
|
225
|
-
for q in range(1, n_qubits // 2):
|
|
226
|
-
thresholds[q - 1] = p
|
|
227
|
-
thresholds[n_bias - q] = p
|
|
228
|
-
tot_prob += 2 * p
|
|
229
|
-
p /= 2
|
|
230
|
-
thresholds /= tot_prob
|
|
231
|
-
|
|
232
|
-
return thresholds
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
@njit(parallel=True)
|
|
236
|
-
def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
|
|
237
|
-
theta = np.empty(n_qubits, dtype=np.float64)
|
|
238
|
-
for q in prange(n_qubits):
|
|
239
|
-
J = J_eff[q]
|
|
240
|
-
z = degrees[q]
|
|
241
|
-
theta[q] = np.arcsin(
|
|
242
|
-
max(
|
|
243
|
-
-1.0,
|
|
244
|
-
min(
|
|
245
|
-
1.0,
|
|
246
|
-
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
|
|
247
|
-
),
|
|
248
|
-
)
|
|
249
|
-
)
|
|
250
|
-
|
|
251
|
-
return theta
|
|
252
|
-
|
|
253
|
-
|
|
254
131
|
def maxcut_tfim(
|
|
255
132
|
G,
|
|
256
133
|
quality=None,
|
|
@@ -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 opencl_context
|
|
7
|
+
from .maxcut_tfim_util import init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -13,89 +13,6 @@ except ImportError:
|
|
|
13
13
|
IS_OPENCL_AVAILABLE = False
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
@njit
|
|
17
|
-
def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
|
|
18
|
-
bias = np.empty(n_qubits - 1, dtype=np.float64)
|
|
19
|
-
|
|
20
|
-
# critical angle
|
|
21
|
-
theta_c = np.arcsin(max(-1.0, min(1.0, abs(h) / (z * J))))
|
|
22
|
-
|
|
23
|
-
p = (
|
|
24
|
-
pow(2.0, abs(J / h) - 1.0)
|
|
25
|
-
* (1.0 + np.sin(theta - theta_c) * np.cos(1.5 * np.pi * J * t + theta) / (1.0 + np.sqrt(t)))
|
|
26
|
-
- 0.5
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
if (p * n_qubits) >= 1024:
|
|
30
|
-
return bias
|
|
31
|
-
|
|
32
|
-
tot_n = 1.0 + 1.0 / pow(2.0, p * n_qubits)
|
|
33
|
-
factor = pow(2.0, -p)
|
|
34
|
-
n = 1.0
|
|
35
|
-
for q in range(1, n_qubits):
|
|
36
|
-
n *= factor
|
|
37
|
-
bias[q - 1] = n
|
|
38
|
-
tot_n += n
|
|
39
|
-
bias /= tot_n
|
|
40
|
-
|
|
41
|
-
if J > 0.0:
|
|
42
|
-
return bias[::-1]
|
|
43
|
-
|
|
44
|
-
return bias
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
@njit(parallel=True)
|
|
48
|
-
def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
49
|
-
if n_qubits < 2:
|
|
50
|
-
hamming_prob.fill(0.0)
|
|
51
|
-
return
|
|
52
|
-
|
|
53
|
-
n_steps = 1 << quality
|
|
54
|
-
delta_t = 1.0 / n_steps
|
|
55
|
-
tot_t = 2.0 * n_steps * delta_t
|
|
56
|
-
h_mult = 2.0 / tot_t
|
|
57
|
-
n_bias = n_qubits - 1
|
|
58
|
-
|
|
59
|
-
theta = np.empty(n_qubits, dtype=np.float64)
|
|
60
|
-
for q in range(n_qubits):
|
|
61
|
-
J = J_func[q]
|
|
62
|
-
z = degrees[q]
|
|
63
|
-
theta[q] = np.arcsin(
|
|
64
|
-
max(
|
|
65
|
-
-1.0,
|
|
66
|
-
min(
|
|
67
|
-
1.0,
|
|
68
|
-
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
|
|
69
|
-
),
|
|
70
|
-
)
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
for qc in prange(n_qubits, n_steps * n_qubits):
|
|
74
|
-
step = qc // n_qubits
|
|
75
|
-
q = qc % n_qubits
|
|
76
|
-
J_eff = J_func[q]
|
|
77
|
-
if np.isclose(abs(J_eff), 0.0):
|
|
78
|
-
continue
|
|
79
|
-
z = degrees[q]
|
|
80
|
-
theta_eff = theta[q]
|
|
81
|
-
t = step * delta_t
|
|
82
|
-
tm1 = (step - 1) * delta_t
|
|
83
|
-
h_t = h_mult * (tot_t - t)
|
|
84
|
-
bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, t, n_qubits)
|
|
85
|
-
last_bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_qubits)
|
|
86
|
-
for i in range(n_bias):
|
|
87
|
-
hamming_prob[i] += bias[i] - last_bias[i]
|
|
88
|
-
|
|
89
|
-
tot_prob = hamming_prob.sum()
|
|
90
|
-
hamming_prob /= tot_prob
|
|
91
|
-
|
|
92
|
-
tot_prob = 0.0
|
|
93
|
-
for i in range(n_bias):
|
|
94
|
-
tot_prob += hamming_prob[i]
|
|
95
|
-
hamming_prob[i] = tot_prob
|
|
96
|
-
hamming_prob[-1] = 2.0
|
|
97
|
-
|
|
98
|
-
|
|
99
16
|
@njit
|
|
100
17
|
def binary_search(l, t):
|
|
101
18
|
left = 0
|
|
@@ -198,7 +115,7 @@ def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds, J_eff):
|
|
|
198
115
|
weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
|
|
199
116
|
|
|
200
117
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
201
|
-
energies = np.empty(shots, dtype=np.
|
|
118
|
+
energies = np.empty(shots, dtype=np.float32)
|
|
202
119
|
|
|
203
120
|
for s in prange(shots):
|
|
204
121
|
# First dimension: Hamming weight
|
|
@@ -229,7 +146,7 @@ def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds, J_eff):
|
|
|
229
146
|
def init_J_and_z(G_data, G_rows, G_cols):
|
|
230
147
|
n_qubits = G_rows.shape[0] - 1
|
|
231
148
|
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
232
|
-
J_eff = np.empty(n_qubits, dtype=np.
|
|
149
|
+
J_eff = np.empty(n_qubits, dtype=np.float32)
|
|
233
150
|
for r in prange(n_qubits):
|
|
234
151
|
# Row sum
|
|
235
152
|
start = G_rows[r]
|
|
@@ -259,48 +176,8 @@ def init_J_and_z(G_data, G_rows, G_cols):
|
|
|
259
176
|
return J_eff, degrees
|
|
260
177
|
|
|
261
178
|
|
|
262
|
-
@njit
|
|
263
|
-
def init_thresholds(n_qubits):
|
|
264
|
-
n_bias = n_qubits - 1
|
|
265
|
-
thresholds = np.empty(n_bias, dtype=np.float64)
|
|
266
|
-
tot_prob = 0
|
|
267
|
-
p = 1.0
|
|
268
|
-
if n_qubits & 1:
|
|
269
|
-
q = n_qubits // 2
|
|
270
|
-
thresholds[q - 1] = p
|
|
271
|
-
tot_prob = p
|
|
272
|
-
p /= 2
|
|
273
|
-
for q in range(1, n_qubits // 2):
|
|
274
|
-
thresholds[q - 1] = p
|
|
275
|
-
thresholds[n_bias - q] = p
|
|
276
|
-
tot_prob += 2 * p
|
|
277
|
-
p /= 2
|
|
278
|
-
thresholds /= tot_prob
|
|
279
|
-
|
|
280
|
-
return thresholds
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
@njit(parallel=True)
|
|
284
|
-
def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
|
|
285
|
-
theta = np.empty(n_qubits, dtype=np.float64)
|
|
286
|
-
for q in prange(n_qubits):
|
|
287
|
-
J = J_eff[q]
|
|
288
|
-
z = degrees[q]
|
|
289
|
-
theta[q] = np.arcsin(
|
|
290
|
-
max(
|
|
291
|
-
-1.0,
|
|
292
|
-
min(
|
|
293
|
-
1.0,
|
|
294
|
-
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
|
|
295
|
-
),
|
|
296
|
-
)
|
|
297
|
-
)
|
|
298
|
-
|
|
299
|
-
return theta
|
|
300
|
-
|
|
301
|
-
|
|
302
179
|
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
|
|
303
|
-
lil = lil_matrix((n_nodes, n_nodes), dtype=np.
|
|
180
|
+
lil = lil_matrix((n_nodes, n_nodes), dtype=np.float32)
|
|
304
181
|
for u in range(n_nodes):
|
|
305
182
|
u_node = nodes[u]
|
|
306
183
|
for v in range(u + 1, n_nodes):
|
|
@@ -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 opencl_context
|
|
7
|
+
from .maxcut_tfim_util import init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -13,89 +13,6 @@ except ImportError:
|
|
|
13
13
|
IS_OPENCL_AVAILABLE = False
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
@njit
|
|
17
|
-
def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
|
|
18
|
-
bias = np.empty(n_qubits - 1, dtype=np.float64)
|
|
19
|
-
|
|
20
|
-
# critical angle
|
|
21
|
-
theta_c = np.arcsin(max(-1.0, min(1.0, abs(h) / (z * J))))
|
|
22
|
-
|
|
23
|
-
p = (
|
|
24
|
-
pow(2.0, abs(J / h) - 1.0)
|
|
25
|
-
* (1.0 + np.sin(theta - theta_c) * np.cos(1.5 * np.pi * J * t + theta) / (1.0 + np.sqrt(t)))
|
|
26
|
-
- 0.5
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
if (p * n_qubits) >= 1024:
|
|
30
|
-
return bias
|
|
31
|
-
|
|
32
|
-
tot_n = 1.0 + 1.0 / pow(2.0, p * n_qubits)
|
|
33
|
-
factor = pow(2.0, -p)
|
|
34
|
-
n = 1.0
|
|
35
|
-
for q in range(1, n_qubits):
|
|
36
|
-
n *= factor
|
|
37
|
-
bias[q - 1] = n
|
|
38
|
-
tot_n += n
|
|
39
|
-
bias /= tot_n
|
|
40
|
-
|
|
41
|
-
if J > 0.0:
|
|
42
|
-
return bias[::-1]
|
|
43
|
-
|
|
44
|
-
return bias
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
@njit(parallel=True)
|
|
48
|
-
def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
49
|
-
if n_qubits < 2:
|
|
50
|
-
hamming_prob.fill(0.0)
|
|
51
|
-
return
|
|
52
|
-
|
|
53
|
-
n_steps = 1 << quality
|
|
54
|
-
delta_t = 1.0 / n_steps
|
|
55
|
-
tot_t = 2.0 * n_steps * delta_t
|
|
56
|
-
h_mult = 2.0 / tot_t
|
|
57
|
-
n_bias = n_qubits - 1
|
|
58
|
-
|
|
59
|
-
theta = np.empty(n_qubits, dtype=np.float64)
|
|
60
|
-
for q in range(n_qubits):
|
|
61
|
-
J = J_func[q]
|
|
62
|
-
z = degrees[q]
|
|
63
|
-
theta[q] = np.arcsin(
|
|
64
|
-
max(
|
|
65
|
-
-1.0,
|
|
66
|
-
min(
|
|
67
|
-
1.0,
|
|
68
|
-
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
|
|
69
|
-
),
|
|
70
|
-
)
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
for qc in prange(n_qubits, n_steps * n_qubits):
|
|
74
|
-
step = qc // n_qubits
|
|
75
|
-
q = qc % n_qubits
|
|
76
|
-
J_eff = J_func[q]
|
|
77
|
-
if np.isclose(abs(J_eff), 0.0):
|
|
78
|
-
continue
|
|
79
|
-
z = degrees[q]
|
|
80
|
-
theta_eff = theta[q]
|
|
81
|
-
t = step * delta_t
|
|
82
|
-
tm1 = (step - 1) * delta_t
|
|
83
|
-
h_t = h_mult * (tot_t - t)
|
|
84
|
-
bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, t, n_qubits)
|
|
85
|
-
last_bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_qubits)
|
|
86
|
-
for i in range(n_bias):
|
|
87
|
-
hamming_prob[i] += bias[i] - last_bias[i]
|
|
88
|
-
|
|
89
|
-
tot_prob = hamming_prob.sum()
|
|
90
|
-
hamming_prob /= tot_prob
|
|
91
|
-
|
|
92
|
-
tot_prob = 0.0
|
|
93
|
-
for i in range(n_bias):
|
|
94
|
-
tot_prob += hamming_prob[i]
|
|
95
|
-
hamming_prob[i] = tot_prob
|
|
96
|
-
hamming_prob[-1] = 2.0
|
|
97
|
-
|
|
98
|
-
|
|
99
16
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
100
17
|
@njit
|
|
101
18
|
def local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, m):
|
|
@@ -165,7 +82,7 @@ def sample_for_solution(G_func, G_func_args_tuple, nodes, max_weight, shots, thr
|
|
|
165
82
|
weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
|
|
166
83
|
|
|
167
84
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
168
|
-
energies = np.empty(shots, dtype=np.
|
|
85
|
+
energies = np.empty(shots, dtype=np.float32)
|
|
169
86
|
|
|
170
87
|
for s in prange(shots):
|
|
171
88
|
# First dimension: Hamming weight
|
|
@@ -196,7 +113,7 @@ def sample_for_solution(G_func, G_func_args_tuple, nodes, max_weight, shots, thr
|
|
|
196
113
|
def init_J_and_z(G_func, G_func_args_tuple, nodes):
|
|
197
114
|
n_qubits = len(nodes)
|
|
198
115
|
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
199
|
-
J_eff = np.empty(n_qubits, dtype=np.
|
|
116
|
+
J_eff = np.empty(n_qubits, dtype=np.float32)
|
|
200
117
|
J_max = -float("inf")
|
|
201
118
|
G_max = -float("inf")
|
|
202
119
|
for n in prange(n_qubits):
|
|
@@ -218,46 +135,6 @@ def init_J_and_z(G_func, G_func_args_tuple, nodes):
|
|
|
218
135
|
return J_eff, degrees, G_max
|
|
219
136
|
|
|
220
137
|
|
|
221
|
-
@njit
|
|
222
|
-
def init_thresholds(n_qubits):
|
|
223
|
-
n_bias = n_qubits - 1
|
|
224
|
-
thresholds = np.empty(n_bias, dtype=np.float64)
|
|
225
|
-
tot_prob = 0
|
|
226
|
-
p = 1.0
|
|
227
|
-
if n_qubits & 1:
|
|
228
|
-
q = n_qubits // 2
|
|
229
|
-
thresholds[q - 1] = p
|
|
230
|
-
tot_prob = p
|
|
231
|
-
p /= 2
|
|
232
|
-
for q in range(1, n_qubits // 2):
|
|
233
|
-
thresholds[q - 1] = p
|
|
234
|
-
thresholds[n_bias - q] = p
|
|
235
|
-
tot_prob += 2 * p
|
|
236
|
-
p /= 2
|
|
237
|
-
thresholds /= tot_prob
|
|
238
|
-
|
|
239
|
-
return thresholds
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
@njit(parallel=True)
|
|
243
|
-
def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
|
|
244
|
-
theta = np.empty(n_qubits, dtype=np.float64)
|
|
245
|
-
for q in prange(n_qubits):
|
|
246
|
-
J = J_eff[q]
|
|
247
|
-
z = degrees[q]
|
|
248
|
-
theta[q] = np.arcsin(
|
|
249
|
-
max(
|
|
250
|
-
-1.0,
|
|
251
|
-
min(
|
|
252
|
-
1.0,
|
|
253
|
-
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
|
|
254
|
-
),
|
|
255
|
-
)
|
|
256
|
-
)
|
|
257
|
-
|
|
258
|
-
return theta
|
|
259
|
-
|
|
260
|
-
|
|
261
138
|
def maxcut_tfim_streaming(
|
|
262
139
|
G_func,
|
|
263
140
|
nodes,
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import networkx as nx
|
|
3
|
+
import numpy as np
|
|
4
|
+
import os
|
|
5
|
+
from numba import njit, prange
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@njit(parallel=True)
|
|
9
|
+
def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
|
|
10
|
+
theta = np.empty(n_qubits, dtype=np.float32)
|
|
11
|
+
for q in prange(n_qubits):
|
|
12
|
+
J = J_eff[q]
|
|
13
|
+
z = degrees[q]
|
|
14
|
+
theta[q] = np.arcsin(
|
|
15
|
+
max(
|
|
16
|
+
-1.0,
|
|
17
|
+
min(
|
|
18
|
+
1.0,
|
|
19
|
+
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
|
|
20
|
+
),
|
|
21
|
+
)
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
return theta
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@njit
|
|
28
|
+
def init_thresholds(n_qubits):
|
|
29
|
+
n_bias = n_qubits - 1
|
|
30
|
+
thresholds = np.empty(n_bias, dtype=np.float32)
|
|
31
|
+
tot_prob = 0
|
|
32
|
+
p = 1.0
|
|
33
|
+
if n_qubits & 1:
|
|
34
|
+
q = n_qubits // 2
|
|
35
|
+
thresholds[q - 1] = p
|
|
36
|
+
tot_prob = p
|
|
37
|
+
p /= 2
|
|
38
|
+
for q in range(1, n_qubits // 2):
|
|
39
|
+
thresholds[q - 1] = p
|
|
40
|
+
thresholds[n_bias - q] = p
|
|
41
|
+
tot_prob += 2 * p
|
|
42
|
+
p /= 2
|
|
43
|
+
thresholds /= tot_prob
|
|
44
|
+
|
|
45
|
+
return thresholds
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@njit(parallel=True)
|
|
49
|
+
def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
50
|
+
if n_qubits < 2:
|
|
51
|
+
hamming_prob.fill(0.0)
|
|
52
|
+
return
|
|
53
|
+
|
|
54
|
+
n_steps = 1 << quality
|
|
55
|
+
delta_t = 1.0 / n_steps
|
|
56
|
+
tot_t = 2.0 * n_steps * delta_t
|
|
57
|
+
h_mult = 2.0 / tot_t
|
|
58
|
+
n_bias = n_qubits - 1
|
|
59
|
+
|
|
60
|
+
theta = np.empty(n_qubits, dtype=np.float32)
|
|
61
|
+
for q in range(n_qubits):
|
|
62
|
+
J = J_func[q]
|
|
63
|
+
z = degrees[q]
|
|
64
|
+
theta[q] = np.arcsin(
|
|
65
|
+
max(
|
|
66
|
+
-1.0,
|
|
67
|
+
min(
|
|
68
|
+
1.0,
|
|
69
|
+
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
|
|
70
|
+
),
|
|
71
|
+
)
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
for qc in prange(n_qubits, n_steps * n_qubits):
|
|
75
|
+
step = qc // n_qubits
|
|
76
|
+
q = qc % n_qubits
|
|
77
|
+
J_eff = J_func[q]
|
|
78
|
+
if np.isclose(abs(J_eff), 0.0):
|
|
79
|
+
continue
|
|
80
|
+
z = degrees[q]
|
|
81
|
+
theta_eff = theta[q]
|
|
82
|
+
t = step * delta_t
|
|
83
|
+
tm1 = (step - 1) * delta_t
|
|
84
|
+
h_t = h_mult * (tot_t - t)
|
|
85
|
+
bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, t, n_qubits)
|
|
86
|
+
last_bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_qubits)
|
|
87
|
+
for i in range(n_bias):
|
|
88
|
+
hamming_prob[i] += bias[i] - last_bias[i]
|
|
89
|
+
|
|
90
|
+
tot_prob = hamming_prob.sum()
|
|
91
|
+
hamming_prob /= tot_prob
|
|
92
|
+
|
|
93
|
+
tot_prob = 0.0
|
|
94
|
+
for i in range(n_bias):
|
|
95
|
+
tot_prob += hamming_prob[i]
|
|
96
|
+
hamming_prob[i] = tot_prob
|
|
97
|
+
hamming_prob[-1] = 2.0
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@njit
|
|
101
|
+
def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
|
|
102
|
+
bias = np.empty(n_qubits - 1, dtype=np.float32)
|
|
103
|
+
|
|
104
|
+
# critical angle
|
|
105
|
+
theta_c = np.arcsin(max(-1.0, min(1.0, abs(h) / (z * J))))
|
|
106
|
+
|
|
107
|
+
p = (
|
|
108
|
+
pow(2.0, abs(J / h) - 1.0)
|
|
109
|
+
* (1.0 + np.sin(theta - theta_c) * np.cos(1.5 * np.pi * J * t + theta) / (1.0 + np.sqrt(t)))
|
|
110
|
+
- 0.5
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
if (p * n_qubits) >= 1024:
|
|
114
|
+
return bias
|
|
115
|
+
|
|
116
|
+
tot_n = 1.0 + 1.0 / pow(2.0, p * n_qubits)
|
|
117
|
+
factor = pow(2.0, -p)
|
|
118
|
+
n = 1.0
|
|
119
|
+
for q in range(1, n_qubits):
|
|
120
|
+
n *= factor
|
|
121
|
+
bias[q - 1] = n
|
|
122
|
+
tot_n += n
|
|
123
|
+
bias /= tot_n
|
|
124
|
+
|
|
125
|
+
if J > 0.0:
|
|
126
|
+
return bias[::-1]
|
|
127
|
+
|
|
128
|
+
return bias
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
class OpenCLContext:
|
|
132
|
+
def __init__(self, a, c, q, k):
|
|
133
|
+
self.IS_OPENCL_AVAILABLE = a
|
|
134
|
+
self.ctx = c
|
|
135
|
+
self.queue = q
|
|
136
|
+
self.maxcut_hamming_cdf_kernel = k
|
|
137
|
+
|
|
138
|
+
IS_OPENCL_AVAILABLE = True
|
|
139
|
+
ctx = None
|
|
140
|
+
queue = None
|
|
141
|
+
try:
|
|
142
|
+
import pyopencl as cl
|
|
143
|
+
|
|
144
|
+
# Pick a device (GPU if available)
|
|
145
|
+
ctx = cl.create_some_context()
|
|
146
|
+
queue = cl.CommandQueue(ctx)
|
|
147
|
+
|
|
148
|
+
# Load and build OpenCL kernels
|
|
149
|
+
kernel_src = open(os.path.dirname(os.path.abspath(__file__)) + "/kernels.cl").read()
|
|
150
|
+
program = cl.Program(ctx, kernel_src).build()
|
|
151
|
+
maxcut_hamming_cdf_kernel = program.maxcut_hamming_cdf
|
|
152
|
+
except ImportError:
|
|
153
|
+
IS_OPENCL_AVAILABLE = False
|
|
154
|
+
|
|
155
|
+
opencl_context = OpenCLContext(IS_OPENCL_AVAILABLE, ctx, queue, maxcut_hamming_cdf_kernel)
|
|
@@ -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.1.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)',
|
|
@@ -1,32 +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
|
-
|
|
8
|
-
class OpenCLContext:
|
|
9
|
-
def __init__(self, a, c, q, k):
|
|
10
|
-
self.IS_OPENCL_AVAILABLE = a
|
|
11
|
-
self.ctx = c
|
|
12
|
-
self.queue = q
|
|
13
|
-
self.maxcut_hamming_cdf_kernel = k
|
|
14
|
-
|
|
15
|
-
IS_OPENCL_AVAILABLE = True
|
|
16
|
-
ctx = None
|
|
17
|
-
queue = None
|
|
18
|
-
try:
|
|
19
|
-
import pyopencl as cl
|
|
20
|
-
|
|
21
|
-
# Pick a device (GPU if available)
|
|
22
|
-
ctx = cl.create_some_context()
|
|
23
|
-
queue = cl.CommandQueue(ctx)
|
|
24
|
-
|
|
25
|
-
# Load and build OpenCL kernels
|
|
26
|
-
kernel_src = open(os.path.dirname(os.path.abspath(__file__)) + "/kernels.cl").read()
|
|
27
|
-
program = cl.Program(ctx, kernel_src).build()
|
|
28
|
-
maxcut_hamming_cdf_kernel = program.maxcut_hamming_cdf
|
|
29
|
-
except ImportError:
|
|
30
|
-
IS_OPENCL_AVAILABLE = False
|
|
31
|
-
|
|
32
|
-
opencl_context = OpenCLContext(IS_OPENCL_AVAILABLE, ctx, queue, maxcut_hamming_cdf_kernel)
|
|
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
|