PyQrackIsing 5.0.2__tar.gz → 5.1.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-5.0.2/pyqrackising.egg-info → pyqrackising-5.1.1}/PKG-INFO +1 -1
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/README.md +25 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyproject.toml +1 -1
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising/__init__.py +2 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising/maxcut_tfim.py +9 -19
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising/maxcut_tfim_sparse.py +10 -20
- pyqrackising-5.1.1/pyqrackising/maxcut_tfim_streaming.py +353 -0
- pyqrackising-5.1.1/pyqrackising/maxcut_tfim_util.py +32 -0
- pyqrackising-5.1.1/pyqrackising/spin_glass_solver_streaming.py +138 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising.egg-info/SOURCES.txt +3 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/setup.py +1 -1
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/CMakeLists.txt +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/LICENSE.md +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/MANIFEST.in +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising/tsp.py +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-5.0.2 → pyqrackising-5.1.1}/setup.cfg +0 -0
|
@@ -114,6 +114,31 @@ print(f"Path length: {path_length}")
|
|
|
114
114
|
```
|
|
115
115
|
We provide solvers for both the symmetric version of the TSP (i.e., the distance from "A" to "B" is considered the same as from "B" to "A") and asymmetric version (`tsp_asymmetric()`). `monte_carlo=True` switches out the MAXCUT-based heuristic for pure Monte Carlo recursive bipartitioning. `multi_start` controls how many stochastic repeats of MAXCUT are tried to select the best result, at every level of recursion. `k_neighbors` limits the count of nearest-neighbor connections considered for 3-opt.
|
|
116
116
|
|
|
117
|
+
If memory footprint of the graph or adjacency matrix is a concern, but the weights can be reconstructed by formula on demand, we offer `maxcut_tfim_streaming()` and `spin_glass_solver_streaming()`:
|
|
118
|
+
```py
|
|
119
|
+
from pyqrackising import spin_glass_solver_streaming
|
|
120
|
+
# from pyqrackising import maxcut_tfim_streaming
|
|
121
|
+
from numba import njit
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
# This is a contrived example.
|
|
125
|
+
# The function must use numba NJIT.
|
|
126
|
+
# (In practice, even if you use other Python functionality like itertools,
|
|
127
|
+
# you can pre-calculate and load the data as a list through the arguments tuple.)
|
|
128
|
+
@njit
|
|
129
|
+
def G_func(node_pair, args_tuple):
|
|
130
|
+
i, j = min(node_pair), max(node_pair)
|
|
131
|
+
return ((j + 1) % (i + 1)) / args_tuple[0]
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
n_qubits = 64
|
|
135
|
+
nodes = list(range(n_qubits))
|
|
136
|
+
args_tuple = (n_qubits,)
|
|
137
|
+
|
|
138
|
+
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver_streaming(G_func, nodes, G_func_args_tuple=args_tuple, quality=6, best_guess=None)
|
|
139
|
+
# solution_bit_string, cut_value, node_groups = maxcut_tfim_streaming(G_func, nodes, G_func_args_tuple=args_tuple)
|
|
140
|
+
```
|
|
141
|
+
|
|
117
142
|
## About
|
|
118
143
|
Transverse field Ising model (TFIM) is the basis of most claimed algorithmic "quantum advantage," circa 2025, with the notable exception of Shor's integer factoring algorithm.
|
|
119
144
|
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "pyqrackising"
|
|
15
|
-
version = "5.
|
|
15
|
+
version = "5.1.1"
|
|
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"}
|
|
@@ -3,6 +3,8 @@ from .tfim_magnetization import tfim_magnetization
|
|
|
3
3
|
from .tfim_square_magnetization import tfim_square_magnetization
|
|
4
4
|
from .maxcut_tfim import maxcut_tfim
|
|
5
5
|
from .maxcut_tfim_sparse import maxcut_tfim_sparse
|
|
6
|
+
from .maxcut_tfim_streaming import maxcut_tfim_streaming
|
|
6
7
|
from .spin_glass_solver import spin_glass_solver
|
|
7
8
|
from .spin_glass_solver_sparse import spin_glass_solver_sparse
|
|
9
|
+
from .spin_glass_solver_streaming import spin_glass_solver_streaming
|
|
8
10
|
from .tsp import tsp_symmetric, tsp_asymmetric
|
|
@@ -4,21 +4,11 @@ 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
8
|
|
|
8
9
|
IS_OPENCL_AVAILABLE = True
|
|
9
|
-
ctx = None
|
|
10
|
-
queue = None
|
|
11
10
|
try:
|
|
12
11
|
import pyopencl as cl
|
|
13
|
-
|
|
14
|
-
# Pick a device (GPU if available)
|
|
15
|
-
ctx = cl.create_some_context()
|
|
16
|
-
queue = cl.CommandQueue(ctx)
|
|
17
|
-
|
|
18
|
-
# Load and build OpenCL kernels
|
|
19
|
-
kernel_src = open(os.path.dirname(os.path.abspath(__file__)) + "/kernels.cl").read()
|
|
20
|
-
program = cl.Program(ctx, kernel_src).build()
|
|
21
|
-
maxcut_hamming_cdf_kernel = program.maxcut_hamming_cdf
|
|
22
12
|
except ImportError:
|
|
23
13
|
IS_OPENCL_AVAILABLE = False
|
|
24
14
|
|
|
@@ -324,20 +314,20 @@ def maxcut_tfim(
|
|
|
324
314
|
|
|
325
315
|
# Move to GPU
|
|
326
316
|
mf = cl.mem_flags
|
|
327
|
-
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
328
|
-
J_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
329
|
-
deg_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
330
|
-
theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
331
|
-
ham_buf = cl.Buffer(ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
317
|
+
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
318
|
+
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
319
|
+
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
320
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
321
|
+
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
332
322
|
|
|
333
323
|
# Kernel execution
|
|
334
|
-
maxcut_hamming_cdf_kernel(
|
|
335
|
-
queue, (grid_dim,), (group_size,),
|
|
324
|
+
opencl_context.maxcut_hamming_cdf_kernel(
|
|
325
|
+
opencl_context.queue, (grid_dim,), (group_size,),
|
|
336
326
|
np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
|
|
337
327
|
)
|
|
338
328
|
|
|
339
329
|
# Fetch results
|
|
340
|
-
cl.enqueue_copy(queue, hamming_prob, ham_buf)
|
|
330
|
+
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
341
331
|
|
|
342
332
|
hamming_prob /= hamming_prob.sum()
|
|
343
333
|
tot_prob = 0.0
|
|
@@ -4,21 +4,11 @@ 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
8
|
|
|
8
9
|
IS_OPENCL_AVAILABLE = True
|
|
9
|
-
ctx = None
|
|
10
|
-
queue = None
|
|
11
10
|
try:
|
|
12
11
|
import pyopencl as cl
|
|
13
|
-
|
|
14
|
-
# Pick a device (GPU if available)
|
|
15
|
-
ctx = cl.create_some_context()
|
|
16
|
-
queue = cl.CommandQueue(ctx)
|
|
17
|
-
|
|
18
|
-
# Load and build OpenCL kernels
|
|
19
|
-
kernel_src = open(os.path.dirname(os.path.abspath(__file__)) + "/kernels.cl").read()
|
|
20
|
-
program = cl.Program(ctx, kernel_src).build()
|
|
21
|
-
maxcut_hamming_cdf_kernel = program.maxcut_hamming_cdf
|
|
22
12
|
except ImportError:
|
|
23
13
|
IS_OPENCL_AVAILABLE = False
|
|
24
14
|
|
|
@@ -177,7 +167,7 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, m):
|
|
|
177
167
|
if available[nbr]:
|
|
178
168
|
weights[nbr] *= 0.5 ** (G_data[j] / max_weight) # tunable penalty factor
|
|
179
169
|
|
|
180
|
-
for nbr in range(
|
|
170
|
+
for nbr in range(node):
|
|
181
171
|
if not available[nbr]:
|
|
182
172
|
continue
|
|
183
173
|
start = G_rows[nbr]
|
|
@@ -383,20 +373,20 @@ def maxcut_tfim_sparse(
|
|
|
383
373
|
|
|
384
374
|
# Move to GPU
|
|
385
375
|
mf = cl.mem_flags
|
|
386
|
-
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
387
|
-
J_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
388
|
-
deg_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
389
|
-
theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
390
|
-
ham_buf = cl.Buffer(ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
376
|
+
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
377
|
+
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
378
|
+
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
379
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
380
|
+
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
391
381
|
|
|
392
382
|
# Kernel execution
|
|
393
|
-
maxcut_hamming_cdf_kernel(
|
|
394
|
-
queue, (grid_dim,), (group_size,),
|
|
383
|
+
opencl_context.maxcut_hamming_cdf_kernel(
|
|
384
|
+
opencl_context.queue, (grid_dim,), (group_size,),
|
|
395
385
|
np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
|
|
396
386
|
)
|
|
397
387
|
|
|
398
388
|
# Fetch results
|
|
399
|
-
cl.enqueue_copy(queue, hamming_prob, ham_buf)
|
|
389
|
+
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
400
390
|
|
|
401
391
|
hamming_prob /= hamming_prob.sum()
|
|
402
392
|
tot_prob = 0.0
|
|
@@ -0,0 +1,353 @@
|
|
|
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 opencl_context
|
|
8
|
+
|
|
9
|
+
IS_OPENCL_AVAILABLE = True
|
|
10
|
+
try:
|
|
11
|
+
import pyopencl as cl
|
|
12
|
+
except ImportError:
|
|
13
|
+
IS_OPENCL_AVAILABLE = False
|
|
14
|
+
|
|
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
|
+
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
100
|
+
@njit
|
|
101
|
+
def local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, m):
|
|
102
|
+
"""
|
|
103
|
+
Pick m nodes out of n with repulsion bias:
|
|
104
|
+
- High-degree nodes are already less likely
|
|
105
|
+
- After choosing a node, its neighbors' probabilities are further reduced
|
|
106
|
+
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
107
|
+
weights: float64 array of shape (n,)
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
weights = weights.copy()
|
|
111
|
+
chosen = np.zeros(m, dtype=np.int32) # store chosen indices
|
|
112
|
+
available = np.ones(n, dtype=np.bool_) # True = available, False = not
|
|
113
|
+
mask = np.zeros(n, dtype=np.bool_)
|
|
114
|
+
chosen_count = 0
|
|
115
|
+
|
|
116
|
+
for _ in range(m):
|
|
117
|
+
# Count available
|
|
118
|
+
total_w = 0.0
|
|
119
|
+
for i in range(n):
|
|
120
|
+
if available[i]:
|
|
121
|
+
total_w += weights[i]
|
|
122
|
+
if total_w <= 0:
|
|
123
|
+
break
|
|
124
|
+
|
|
125
|
+
# Normalize & sample
|
|
126
|
+
r = np.random.rand()
|
|
127
|
+
cum = 0.0
|
|
128
|
+
node = -1
|
|
129
|
+
for i in range(n):
|
|
130
|
+
if available[i]:
|
|
131
|
+
cum += weights[i] / total_w
|
|
132
|
+
if r < cum:
|
|
133
|
+
node = i
|
|
134
|
+
break
|
|
135
|
+
|
|
136
|
+
if node == -1:
|
|
137
|
+
continue
|
|
138
|
+
|
|
139
|
+
# Select node
|
|
140
|
+
chosen[chosen_count] = node
|
|
141
|
+
chosen_count += 1
|
|
142
|
+
available[node] = False
|
|
143
|
+
mask[node] = True
|
|
144
|
+
|
|
145
|
+
# Repulsion: penalize neighbors
|
|
146
|
+
for nbr in range(n):
|
|
147
|
+
if available[nbr]:
|
|
148
|
+
weights[nbr] *= 0.5 ** (G_func((nodes[node], nodes[nbr]), G_func_args_tuple) / max_weight) # tunable penalty factor
|
|
149
|
+
|
|
150
|
+
return mask
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@njit
|
|
154
|
+
def compute_energy(sample, G_func, G_func_args_tuple, nodes, n_qubits):
|
|
155
|
+
energy = 0
|
|
156
|
+
for u in range(n_qubits):
|
|
157
|
+
for v in range(u + 1, n_qubits):
|
|
158
|
+
energy += G_func((nodes[u], nodes[v]), G_func_args_tuple) * (1 if sample[u] == sample[v] else -1)
|
|
159
|
+
|
|
160
|
+
return energy
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
@njit(parallel=True)
|
|
164
|
+
def sample_for_solution(G_func, G_func_args_tuple, nodes, max_weight, shots, thresholds, degrees_sum, J_eff, n):
|
|
165
|
+
weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
|
|
166
|
+
|
|
167
|
+
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
168
|
+
energies = np.empty(shots, dtype=np.float64)
|
|
169
|
+
|
|
170
|
+
for s in prange(shots):
|
|
171
|
+
# First dimension: Hamming weight
|
|
172
|
+
mag_prob = np.random.random()
|
|
173
|
+
m = 0
|
|
174
|
+
while thresholds[m] < mag_prob:
|
|
175
|
+
m += 1
|
|
176
|
+
m += 1
|
|
177
|
+
|
|
178
|
+
# Second dimension: permutation within Hamming weight
|
|
179
|
+
sample = local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, m)
|
|
180
|
+
|
|
181
|
+
solutions[s] = sample
|
|
182
|
+
energies[s] = compute_energy(sample, G_func, G_func_args_tuple, nodes, n)
|
|
183
|
+
|
|
184
|
+
best_solution = solutions[np.argmin(energies)]
|
|
185
|
+
|
|
186
|
+
best_value = 0.0
|
|
187
|
+
for u in range(n):
|
|
188
|
+
for v in range(u + 1, n):
|
|
189
|
+
if best_solution[u] != best_solution[v]:
|
|
190
|
+
best_value += G_func((nodes[u], nodes[v]), G_func_args_tuple)
|
|
191
|
+
|
|
192
|
+
return best_solution, best_value
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
@njit
|
|
196
|
+
def init_J_and_z(G_func, G_func_args_tuple, nodes):
|
|
197
|
+
n_qubits = len(nodes)
|
|
198
|
+
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
199
|
+
J_eff = np.empty(n_qubits, dtype=np.float64)
|
|
200
|
+
J_max = -float("inf")
|
|
201
|
+
G_max = -float("inf")
|
|
202
|
+
for n in range(n_qubits):
|
|
203
|
+
degree = 0
|
|
204
|
+
J = 0.0
|
|
205
|
+
for m in range(n_qubits):
|
|
206
|
+
val = G_func((nodes[n], nodes[m]), G_func_args_tuple)
|
|
207
|
+
if val != 0.0:
|
|
208
|
+
degree += 1
|
|
209
|
+
J += val
|
|
210
|
+
if val > G_max:
|
|
211
|
+
G_max = val
|
|
212
|
+
J = -J / degree if degree > 0 else 0
|
|
213
|
+
degrees[n] = degree
|
|
214
|
+
J_eff[n] = J
|
|
215
|
+
J_abs = abs(J)
|
|
216
|
+
if J_abs > J_max:
|
|
217
|
+
J_max = J_abs
|
|
218
|
+
J_eff /= J_max
|
|
219
|
+
|
|
220
|
+
return J_eff, degrees, G_max
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
@njit
|
|
224
|
+
def init_thresholds(n_qubits):
|
|
225
|
+
n_bias = n_qubits - 1
|
|
226
|
+
thresholds = np.empty(n_bias, dtype=np.float64)
|
|
227
|
+
tot_prob = 0
|
|
228
|
+
p = 1.0
|
|
229
|
+
if n_qubits & 1:
|
|
230
|
+
q = n_qubits // 2
|
|
231
|
+
thresholds[q - 1] = p
|
|
232
|
+
tot_prob = p
|
|
233
|
+
p /= 2
|
|
234
|
+
for q in range(1, n_qubits // 2):
|
|
235
|
+
thresholds[q - 1] = p
|
|
236
|
+
thresholds[n_bias - q] = p
|
|
237
|
+
tot_prob += 2 * p
|
|
238
|
+
p /= 2
|
|
239
|
+
thresholds /= tot_prob
|
|
240
|
+
|
|
241
|
+
return thresholds
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
@njit
|
|
245
|
+
def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
|
|
246
|
+
theta = np.empty(n_qubits, dtype=np.float64)
|
|
247
|
+
for q in range(n_qubits):
|
|
248
|
+
J = J_eff[q]
|
|
249
|
+
z = degrees[q]
|
|
250
|
+
theta[q] = np.arcsin(
|
|
251
|
+
max(
|
|
252
|
+
-1.0,
|
|
253
|
+
min(
|
|
254
|
+
1.0,
|
|
255
|
+
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
|
|
256
|
+
),
|
|
257
|
+
)
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
return theta
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def maxcut_tfim_streaming(
|
|
264
|
+
G_func,
|
|
265
|
+
nodes,
|
|
266
|
+
G_func_args_tuple=None,
|
|
267
|
+
quality=None,
|
|
268
|
+
shots=None,
|
|
269
|
+
):
|
|
270
|
+
n_qubits = len(nodes)
|
|
271
|
+
|
|
272
|
+
if n_qubits < 3:
|
|
273
|
+
if n_qubits == 0:
|
|
274
|
+
return "", 0, ([], [])
|
|
275
|
+
|
|
276
|
+
if n_qubits == 1:
|
|
277
|
+
return "0", 0, (nodes, [])
|
|
278
|
+
|
|
279
|
+
if n_qubits == 2:
|
|
280
|
+
weight = G_func((nodes[0], nodes[1]), G_func_args_tuple)
|
|
281
|
+
if weight < 0.0:
|
|
282
|
+
return "00", 0, (nodes, [])
|
|
283
|
+
|
|
284
|
+
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
285
|
+
|
|
286
|
+
if quality is None:
|
|
287
|
+
quality = 8
|
|
288
|
+
|
|
289
|
+
if shots is None:
|
|
290
|
+
# Number of measurement shots
|
|
291
|
+
shots = n_qubits << quality
|
|
292
|
+
|
|
293
|
+
n_steps = 1 << quality
|
|
294
|
+
grid_size = n_steps * n_qubits
|
|
295
|
+
|
|
296
|
+
J_eff, degrees, G_max = init_J_and_z(G_func, G_func_args_tuple, nodes)
|
|
297
|
+
hamming_prob = init_thresholds(n_qubits)
|
|
298
|
+
|
|
299
|
+
if IS_OPENCL_AVAILABLE and grid_size >= 128:
|
|
300
|
+
delta_t = 1.0 / n_steps
|
|
301
|
+
tot_t = 2.0 * n_steps * delta_t
|
|
302
|
+
h_mult = 2.0 / tot_t
|
|
303
|
+
theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
|
|
304
|
+
args = np.empty(3, dtype=np.float32)
|
|
305
|
+
args[0] = delta_t
|
|
306
|
+
args[1] = tot_t
|
|
307
|
+
args[2] = h_mult
|
|
308
|
+
|
|
309
|
+
# Warp size is 32:
|
|
310
|
+
group_size = n_qubits - 1
|
|
311
|
+
if group_size > 256:
|
|
312
|
+
group_size = 256
|
|
313
|
+
grid_dim = n_steps * n_qubits * group_size
|
|
314
|
+
|
|
315
|
+
# Move to GPU
|
|
316
|
+
mf = cl.mem_flags
|
|
317
|
+
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
318
|
+
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
319
|
+
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
320
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
321
|
+
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
322
|
+
|
|
323
|
+
# Kernel execution
|
|
324
|
+
opencl_context.maxcut_hamming_cdf_kernel(
|
|
325
|
+
opencl_context.queue, (grid_dim,), (group_size,),
|
|
326
|
+
np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
# Fetch results
|
|
330
|
+
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
331
|
+
|
|
332
|
+
hamming_prob /= hamming_prob.sum()
|
|
333
|
+
tot_prob = 0.0
|
|
334
|
+
for i in range(n_qubits - 1):
|
|
335
|
+
tot_prob += hamming_prob[i]
|
|
336
|
+
hamming_prob[i] = tot_prob
|
|
337
|
+
hamming_prob[-1] = 2.0
|
|
338
|
+
else:
|
|
339
|
+
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
|
|
340
|
+
|
|
341
|
+
best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob, degrees.sum(), J_eff, n_qubits)
|
|
342
|
+
|
|
343
|
+
bit_string = ""
|
|
344
|
+
l, r = [], []
|
|
345
|
+
for i in range(len(best_solution)):
|
|
346
|
+
if best_solution[i]:
|
|
347
|
+
bit_string += "1"
|
|
348
|
+
r.append(nodes[i])
|
|
349
|
+
else:
|
|
350
|
+
bit_string += "0"
|
|
351
|
+
l.append(nodes[i])
|
|
352
|
+
|
|
353
|
+
return bit_string, best_value, (l, r)
|
|
@@ -0,0 +1,32 @@
|
|
|
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)
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
from .maxcut_tfim_streaming import maxcut_tfim_streaming
|
|
2
|
+
import itertools
|
|
3
|
+
import networkx as nx
|
|
4
|
+
import numpy as np
|
|
5
|
+
from numba import njit, prange
|
|
6
|
+
import os
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@njit
|
|
10
|
+
def evaluate_cut_edges(theta_bits, G_func, G_func_args_tuple, nodes):
|
|
11
|
+
n_qubits = len(nodes)
|
|
12
|
+
cut = 0
|
|
13
|
+
for u in range(n_qubits):
|
|
14
|
+
for v in range(u + 1, n_qubits):
|
|
15
|
+
if theta_bits[u] != theta_bits[v]:
|
|
16
|
+
cut += G_func((nodes[u], nodes[v]), G_func_args_tuple)
|
|
17
|
+
|
|
18
|
+
return cut
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@njit
|
|
22
|
+
def compute_energy(theta_bits, G_func, G_func_args_tuple, nodes):
|
|
23
|
+
n_qubits = len(nodes)
|
|
24
|
+
energy = 0
|
|
25
|
+
for u in range(n_qubits):
|
|
26
|
+
for v in range(u + 1, n_qubits):
|
|
27
|
+
eigen = 1 if theta_bits[u] == theta_bits[v] else -1
|
|
28
|
+
energy += G_func((nodes[u], nodes[v]), G_func_args_tuple) * eigen
|
|
29
|
+
|
|
30
|
+
return energy
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@njit
|
|
34
|
+
def bootstrap_worker(theta, G_func, G_func_args_tuple, nodes, indices):
|
|
35
|
+
local_theta = theta.copy()
|
|
36
|
+
for i in indices:
|
|
37
|
+
local_theta[i] = not local_theta[i]
|
|
38
|
+
energy = compute_energy(local_theta, G_func, G_func_args_tuple, nodes)
|
|
39
|
+
|
|
40
|
+
return energy
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@njit(parallel=True)
|
|
44
|
+
def bootstrap(theta, G_func, G_func_args_tuple, nodes, k, indices_array):
|
|
45
|
+
n = len(indices_array) // k
|
|
46
|
+
energies = np.empty(n, dtype=np.float64)
|
|
47
|
+
for i in prange(n):
|
|
48
|
+
j = i * k
|
|
49
|
+
energies[i] = bootstrap_worker(theta, G_func, G_func_args_tuple, nodes, indices_array[j : j + k])
|
|
50
|
+
|
|
51
|
+
return energies
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# By Gemini (Google Search AI)
|
|
55
|
+
def int_to_bitstring(integer, length):
|
|
56
|
+
return (bin(integer)[2:].zfill(length))[::-1]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def spin_glass_solver_streaming(
|
|
60
|
+
G_func,
|
|
61
|
+
nodes,
|
|
62
|
+
G_func_args_tuple=None,
|
|
63
|
+
quality=6,
|
|
64
|
+
shots=None,
|
|
65
|
+
best_guess=None
|
|
66
|
+
):
|
|
67
|
+
n_qubits = len(nodes)
|
|
68
|
+
|
|
69
|
+
if n_qubits < 3:
|
|
70
|
+
if n_qubits == 0:
|
|
71
|
+
return "", 0, ([], []), 0
|
|
72
|
+
|
|
73
|
+
if n_qubits == 1:
|
|
74
|
+
return "0", 0, (nodes, []), 0
|
|
75
|
+
|
|
76
|
+
if n_qubits == 2:
|
|
77
|
+
weight = G_func((nodes[0], nodes[1]), G_func_args_tuple)
|
|
78
|
+
if weight < 0.0:
|
|
79
|
+
return "00", 0, (nodes, []), weight
|
|
80
|
+
|
|
81
|
+
return "01", weight, ([nodes[0]], [nodes[1]]), -weight
|
|
82
|
+
|
|
83
|
+
bitstring = ""
|
|
84
|
+
if isinstance(best_guess, str):
|
|
85
|
+
bitstring = best_guess
|
|
86
|
+
elif isinstance(best_guess, int):
|
|
87
|
+
bitstring = int_to_bitstring(best_guess, n_qubits)
|
|
88
|
+
elif isinstance(best_guess, list):
|
|
89
|
+
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
90
|
+
else:
|
|
91
|
+
bitstring, _, _ = maxcut_tfim_streaming(G_func, nodes, G_func_args_tuple=G_func_args_tuple, quality=quality, shots=shots)
|
|
92
|
+
best_theta = [b == "1" for b in list(bitstring)]
|
|
93
|
+
|
|
94
|
+
min_energy = compute_energy(best_theta, G_func, G_func_args_tuple, nodes)
|
|
95
|
+
improved = True
|
|
96
|
+
correction_quality = 1
|
|
97
|
+
while improved:
|
|
98
|
+
improved = False
|
|
99
|
+
k = 1
|
|
100
|
+
while k <= correction_quality:
|
|
101
|
+
if n_qubits < k:
|
|
102
|
+
break
|
|
103
|
+
|
|
104
|
+
theta = best_theta.copy()
|
|
105
|
+
|
|
106
|
+
combos = list(
|
|
107
|
+
item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
|
|
108
|
+
)
|
|
109
|
+
energies = bootstrap(theta, G_func, G_func_args_tuple, nodes, k, combos)
|
|
110
|
+
|
|
111
|
+
energy = energies.min()
|
|
112
|
+
if energy < min_energy:
|
|
113
|
+
index_match = np.random.choice(np.where(energies == energy)[0])
|
|
114
|
+
indices = combos[(index_match * k) : ((index_match + 1) * k)]
|
|
115
|
+
min_energy = energy
|
|
116
|
+
for i in indices:
|
|
117
|
+
best_theta[i] = not best_theta[i]
|
|
118
|
+
improved = True
|
|
119
|
+
if correction_quality < (k + 1):
|
|
120
|
+
correction_quality = k + 1
|
|
121
|
+
break
|
|
122
|
+
|
|
123
|
+
k = k + 1
|
|
124
|
+
|
|
125
|
+
bitstring = ""
|
|
126
|
+
l, r = [], []
|
|
127
|
+
for i in range(len(best_theta)):
|
|
128
|
+
b = best_theta[i]
|
|
129
|
+
if b:
|
|
130
|
+
bitstring += "1"
|
|
131
|
+
r.append(nodes[i])
|
|
132
|
+
else:
|
|
133
|
+
bitstring += "0"
|
|
134
|
+
l.append(nodes[i])
|
|
135
|
+
|
|
136
|
+
cut_value = evaluate_cut_edges(best_theta, G_func, G_func_args_tuple, nodes)
|
|
137
|
+
|
|
138
|
+
return bitstring, float(cut_value), (l, r), float(min_energy)
|
|
@@ -10,8 +10,11 @@ pyqrackising/generate_tfim_samples.py
|
|
|
10
10
|
pyqrackising/kernels.cl
|
|
11
11
|
pyqrackising/maxcut_tfim.py
|
|
12
12
|
pyqrackising/maxcut_tfim_sparse.py
|
|
13
|
+
pyqrackising/maxcut_tfim_streaming.py
|
|
14
|
+
pyqrackising/maxcut_tfim_util.py
|
|
13
15
|
pyqrackising/spin_glass_solver.py
|
|
14
16
|
pyqrackising/spin_glass_solver_sparse.py
|
|
17
|
+
pyqrackising/spin_glass_solver_streaming.py
|
|
15
18
|
pyqrackising/tfim_magnetization.py
|
|
16
19
|
pyqrackising/tfim_square_magnetization.py
|
|
17
20
|
pyqrackising/tsp.py
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='5.
|
|
54
|
+
version='5.1.1',
|
|
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
|