PyQrackIsing 2.0.0__tar.gz → 2.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyqrackising-2.0.0/pyqrackising.egg-info → pyqrackising-2.0.1}/PKG-INFO +1 -1
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyproject.toml +1 -1
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising/_pyqrack_ising.cpp +6 -4
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising/maxcut_tfim.py +9 -15
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising/spin_glass_solver.py +7 -8
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising/tsp_symmetric.py +26 -22
- {pyqrackising-2.0.0 → pyqrackising-2.0.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/setup.py +1 -1
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/CMakeLists.txt +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/LICENSE.md +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/MANIFEST.in +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/README.md +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-2.0.0 → pyqrackising-2.0.1}/setup.cfg +0 -0
|
@@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
|
|
|
11
11
|
|
|
12
12
|
[project]
|
|
13
13
|
name = "pyqrackising"
|
|
14
|
-
version = "2.0.
|
|
14
|
+
version = "2.0.1"
|
|
15
15
|
requires-python = ">=3.8"
|
|
16
16
|
description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
|
|
17
17
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -39,17 +39,19 @@ static inline std::vector<double> probability_by_hamming_weight(double J, double
|
|
|
39
39
|
std::fill(bias.begin(), bias.end(), 1.0 / (n_qubits + 1.0));
|
|
40
40
|
} else {
|
|
41
41
|
const double sin_delta = std::sin(delta_theta);
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
constexpr double omega = 1.5 * M_PI;
|
|
43
|
+
constexpr double t2 = 1.0;
|
|
44
44
|
const double p = std::pow(2.0, std::abs(J / h) - 1.0) * (1.0 + sin_delta * std::cos(J * omega * t + theta) / (1.0 + std::sqrt(t / t2))) - 0.5;
|
|
45
|
-
if (p >= 1024) {
|
|
45
|
+
if ((p * n_qubits) >= 1024) {
|
|
46
46
|
bias[0] = 1.0;
|
|
47
47
|
} else {
|
|
48
|
+
const double factor = std::pow(2.0, -p);
|
|
49
|
+
double n = 1.0;
|
|
48
50
|
double tot_n = 0.0;
|
|
49
51
|
for (size_t q = 0U; q <= n_qubits; ++q) {
|
|
50
|
-
double n = 1.0 / ((n_qubits + 1) * std::pow(2.0, p * q));
|
|
51
52
|
bias[q] = n;
|
|
52
53
|
tot_n += n;
|
|
54
|
+
n *= factor;
|
|
53
55
|
}
|
|
54
56
|
for (size_t q = 0U; q <= n_qubits; ++q) {
|
|
55
57
|
bias[q] /= tot_n;
|
|
@@ -28,15 +28,7 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
|
|
|
28
28
|
bias = np.empty(n_qubits - 1, dtype=np.float64)
|
|
29
29
|
|
|
30
30
|
# critical angle
|
|
31
|
-
theta_c = np.arcsin(
|
|
32
|
-
max(
|
|
33
|
-
-1.0,
|
|
34
|
-
min(
|
|
35
|
-
1.0,
|
|
36
|
-
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h) / (z * J)),
|
|
37
|
-
),
|
|
38
|
-
)
|
|
39
|
-
)
|
|
31
|
+
theta_c = np.arcsin(max(-1.0, min(1.0, abs(h) / (z * J))))
|
|
40
32
|
|
|
41
33
|
p = (
|
|
42
34
|
pow(2.0, abs(J / h) - 1.0)
|
|
@@ -48,8 +40,10 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
|
|
|
48
40
|
return bias
|
|
49
41
|
|
|
50
42
|
tot_n = 1.0 + 1.0 / pow(2.0, p * n_qubits)
|
|
43
|
+
factor = pow(2.0, -p)
|
|
44
|
+
n = 1.0
|
|
51
45
|
for q in range(1, n_qubits):
|
|
52
|
-
n
|
|
46
|
+
n *= factor
|
|
53
47
|
bias[q - 1] = n
|
|
54
48
|
tot_n += n
|
|
55
49
|
bias /= tot_n
|
|
@@ -376,14 +370,14 @@ def maxcut_tfim(
|
|
|
376
370
|
|
|
377
371
|
best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, degrees, J_eff, n_qubits)
|
|
378
372
|
|
|
379
|
-
bit_string = ""
|
|
380
|
-
bit_list = list(bit_string)
|
|
373
|
+
bit_string = ""
|
|
381
374
|
l, r = [], []
|
|
382
|
-
for i in range(len(
|
|
383
|
-
|
|
384
|
-
|
|
375
|
+
for i in range(len(best_solution)):
|
|
376
|
+
if best_solution[i]:
|
|
377
|
+
bit_string += "1"
|
|
385
378
|
r.append(nodes[i])
|
|
386
379
|
else:
|
|
380
|
+
bit_string += "0"
|
|
387
381
|
l.append(nodes[i])
|
|
388
382
|
|
|
389
383
|
return bit_string, best_value, (l, r)
|
|
@@ -6,15 +6,16 @@ from numba import njit, prange
|
|
|
6
6
|
import os
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
@njit
|
|
10
|
+
def evaluate_cut_edges(theta_bits, G_m):
|
|
10
11
|
n_qubits = len(G_m)
|
|
11
|
-
|
|
12
|
+
cut = 0
|
|
12
13
|
for u in range(n_qubits):
|
|
13
14
|
for v in range(u + 1, n_qubits):
|
|
14
|
-
if
|
|
15
|
-
|
|
15
|
+
if theta_bits[u] != theta_bits[v]:
|
|
16
|
+
cut += G_m[u, v]
|
|
16
17
|
|
|
17
|
-
return
|
|
18
|
+
return cut
|
|
18
19
|
|
|
19
20
|
|
|
20
21
|
@njit
|
|
@@ -122,7 +123,6 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best
|
|
|
122
123
|
improved = True
|
|
123
124
|
break
|
|
124
125
|
|
|
125
|
-
sample = 0
|
|
126
126
|
bitstring = ""
|
|
127
127
|
l, r = [], []
|
|
128
128
|
for i in range(len(best_theta)):
|
|
@@ -130,11 +130,10 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best
|
|
|
130
130
|
if b:
|
|
131
131
|
bitstring += "1"
|
|
132
132
|
r.append(nodes[i])
|
|
133
|
-
sample |= 1 << i
|
|
134
133
|
else:
|
|
135
134
|
bitstring += "0"
|
|
136
135
|
l.append(nodes[i])
|
|
137
136
|
|
|
138
|
-
cut_value = evaluate_cut_edges(
|
|
137
|
+
cut_value = evaluate_cut_edges(best_theta, G_m)
|
|
139
138
|
|
|
140
139
|
return bitstring, float(cut_value), (l, r), float(min_energy)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from .spin_glass_solver import spin_glass_solver
|
|
2
2
|
import networkx as nx
|
|
3
|
-
from numba import njit
|
|
3
|
+
from numba import njit, prange
|
|
4
4
|
import numpy as np
|
|
5
5
|
|
|
6
6
|
|
|
@@ -175,18 +175,18 @@ def targeted_three_opt(path, W, k_neighbors=20):
|
|
|
175
175
|
return best_path, best_dist
|
|
176
176
|
|
|
177
177
|
|
|
178
|
-
@njit
|
|
178
|
+
@njit(parallel=True)
|
|
179
179
|
def init_G_a_b(G_m, a, b):
|
|
180
180
|
n_a_nodes = len(a)
|
|
181
181
|
n_b_nodes = len(b)
|
|
182
182
|
G_a = np.zeros((n_a_nodes, n_a_nodes), dtype=np.float64)
|
|
183
183
|
G_b = np.zeros((n_b_nodes, n_b_nodes), dtype=np.float64)
|
|
184
|
-
for i in
|
|
184
|
+
for i in prange(n_a_nodes):
|
|
185
185
|
for j in range(n_a_nodes):
|
|
186
186
|
if i == j:
|
|
187
187
|
continue
|
|
188
188
|
G_a[i, j] = G_m[a[i], a[j]]
|
|
189
|
-
for i in
|
|
189
|
+
for i in prange(n_b_nodes):
|
|
190
190
|
for j in range(n_b_nodes):
|
|
191
191
|
if i == j:
|
|
192
192
|
continue
|
|
@@ -257,6 +257,19 @@ def stitch(G_m, path_a, path_b, sol_weight):
|
|
|
257
257
|
return best_path, best_weight
|
|
258
258
|
|
|
259
259
|
|
|
260
|
+
def monte_carlo_loop(n_nodes):
|
|
261
|
+
bits = ([], [])
|
|
262
|
+
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
263
|
+
bits = ([], [])
|
|
264
|
+
for i in range(n_nodes):
|
|
265
|
+
if np.random.random() < 0.5:
|
|
266
|
+
bits[0].append(i)
|
|
267
|
+
else:
|
|
268
|
+
bits[1].append(i)
|
|
269
|
+
|
|
270
|
+
return bits
|
|
271
|
+
|
|
272
|
+
|
|
260
273
|
def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, correction_quality=2, monte_carlo=False, is_3_opt=True, k_neighbors=20, is_cyclic=True, multi_start=1, is_top_level=True):
|
|
261
274
|
nodes = None
|
|
262
275
|
n_nodes = 0
|
|
@@ -288,26 +301,17 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, corr
|
|
|
288
301
|
b = []
|
|
289
302
|
c = []
|
|
290
303
|
if (start_node is None) and (end_node is None):
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
while (len(_a) == 0) or (len(_b) == 0):
|
|
304
|
+
if monte_carlo:
|
|
305
|
+
a, b = monte_carlo_loop(n_nodes)
|
|
306
|
+
else:
|
|
307
|
+
best_energy = float("inf")
|
|
308
|
+
for _ in range(multi_start):
|
|
297
309
|
bits = ([], [])
|
|
298
|
-
|
|
299
|
-
for i in range(n_nodes):
|
|
300
|
-
if np.random.random() < 0.5:
|
|
301
|
-
bits[0].append(i)
|
|
302
|
-
else:
|
|
303
|
-
bits[1].append(i)
|
|
304
|
-
else:
|
|
310
|
+
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
305
311
|
_, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots, correction_quality=correction_quality)
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
best_energy = energy
|
|
310
|
-
a, b = _a, _b
|
|
312
|
+
if energy < best_energy:
|
|
313
|
+
best_energy = energy
|
|
314
|
+
a, b = bits
|
|
311
315
|
else:
|
|
312
316
|
is_cyclic = False
|
|
313
317
|
a.append(nodes.index(start_node))
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='2.0.
|
|
54
|
+
version='2.0.1',
|
|
55
55
|
author='Dan Strano',
|
|
56
56
|
author_email='stranoj@gmail.com',
|
|
57
57
|
description='Near-ideal closed-form solutions for 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
|