PyQrackIsing 1.18.2__tar.gz → 1.18.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-1.18.2/PyQrackIsing.egg-info → pyqrackising-1.18.4}/PKG-INFO +1 -1
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/PyQrackIsing/maxcut_tfim.py +22 -30
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/PyQrackIsing/spin_glass_solver.py +23 -34
- {pyqrackising-1.18.2 → pyqrackising-1.18.4/PyQrackIsing.egg-info}/PKG-INFO +1 -1
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/README.md +1 -1
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/pyproject.toml +1 -1
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/setup.py +1 -1
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/CMakeLists.txt +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/LICENSE.md +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/MANIFEST.in +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/PyQrackIsing/__init__.py +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/PyQrackIsing/generate_tfim_samples.py +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/PyQrackIsing/tfim_magnetization.py +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/PyQrackIsing/tfim_square_magnetization.py +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/PyQrackIsing/tsp_symmetric.py +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/PyQrackIsing.egg-info/not-zip-safe +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/PyQrackIsing.egg-info/top_level.txt +0 -0
- {pyqrackising-1.18.2 → pyqrackising-1.18.4}/setup.cfg +0 -0
|
@@ -59,7 +59,7 @@ except:
|
|
|
59
59
|
|
|
60
60
|
@njit
|
|
61
61
|
def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
|
|
62
|
-
bias = np.
|
|
62
|
+
bias = np.empty(n_qubits - 1, dtype=np.float64)
|
|
63
63
|
|
|
64
64
|
# critical angle
|
|
65
65
|
theta_c = np.arcsin(
|
|
@@ -106,7 +106,7 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
|
106
106
|
h_mult = 32.0 / tot_t
|
|
107
107
|
n_bias = n_qubits - 1
|
|
108
108
|
|
|
109
|
-
theta = np.
|
|
109
|
+
theta = np.empty(n_qubits, dtype=np.float64)
|
|
110
110
|
for q in prange(n_qubits):
|
|
111
111
|
J = J_func[q]
|
|
112
112
|
z = degrees[q]
|
|
@@ -136,7 +136,7 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
|
136
136
|
for i in range(n_bias):
|
|
137
137
|
hamming_prob[i] += bias[i] - last_bias[i]
|
|
138
138
|
|
|
139
|
-
tot_prob = sum(
|
|
139
|
+
tot_prob = hamming_prob.sum()
|
|
140
140
|
hamming_prob /= tot_prob
|
|
141
141
|
|
|
142
142
|
tot_prob = 0.0
|
|
@@ -207,7 +207,7 @@ def local_repulsion_choice(adjacency, degrees, weights, n, m):
|
|
|
207
207
|
|
|
208
208
|
@njit(parallel=True)
|
|
209
209
|
def local_repulsion_choice_sample(shots, thresholds, adjacency, degrees, weights, n):
|
|
210
|
-
samples = np.
|
|
210
|
+
samples = np.empty((shots, n), dtype=np.bool_) # (shots × n) boolean mask array
|
|
211
211
|
|
|
212
212
|
for s in prange(shots):
|
|
213
213
|
# First dimension: Hamming weight
|
|
@@ -236,7 +236,8 @@ def mask_array_to_python_ints(masks):
|
|
|
236
236
|
return samples
|
|
237
237
|
|
|
238
238
|
|
|
239
|
-
def evaluate_cut_edges(
|
|
239
|
+
def evaluate_cut_edges(G_m, samples):
|
|
240
|
+
n_qubits = len(G_m)
|
|
240
241
|
best_value = float("-inf")
|
|
241
242
|
best_solution = None
|
|
242
243
|
best_cut_edges = None
|
|
@@ -244,12 +245,10 @@ def evaluate_cut_edges(samples, edge_keys, edge_values):
|
|
|
244
245
|
for state in samples:
|
|
245
246
|
cut_edges = []
|
|
246
247
|
cut_value = 0
|
|
247
|
-
for
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
cut_value += edge_values[i]
|
|
252
|
-
|
|
248
|
+
for u in range(n_qubits):
|
|
249
|
+
for v in range(u + 1, n_qubits):
|
|
250
|
+
if ((state >> u) & 1) != ((state >> v) & 1):
|
|
251
|
+
cut_value += G_m[u, v]
|
|
253
252
|
if cut_value > best_value:
|
|
254
253
|
best_value = cut_value
|
|
255
254
|
best_solution = state
|
|
@@ -260,7 +259,7 @@ def evaluate_cut_edges(samples, edge_keys, edge_values):
|
|
|
260
259
|
@njit
|
|
261
260
|
def init_thresholds(n_qubits):
|
|
262
261
|
n_bias = n_qubits - 1
|
|
263
|
-
thresholds = np.
|
|
262
|
+
thresholds = np.empty(n_bias, dtype=np.float64)
|
|
264
263
|
tot_prob = 0
|
|
265
264
|
p = 1.0
|
|
266
265
|
if n_qubits & 1:
|
|
@@ -280,7 +279,7 @@ def init_thresholds(n_qubits):
|
|
|
280
279
|
|
|
281
280
|
@njit
|
|
282
281
|
def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
|
|
283
|
-
theta = np.
|
|
282
|
+
theta = np.empty(n_qubits, dtype=np.float64)
|
|
284
283
|
for q in range(n_qubits):
|
|
285
284
|
J = J_eff[q]
|
|
286
285
|
z = degrees[q]
|
|
@@ -351,7 +350,7 @@ def maxcut_tfim(
|
|
|
351
350
|
group_size = n_qubits - 1
|
|
352
351
|
|
|
353
352
|
if quality is None:
|
|
354
|
-
quality =
|
|
353
|
+
quality = 8
|
|
355
354
|
|
|
356
355
|
if shots is None:
|
|
357
356
|
# Number of measurement shots
|
|
@@ -361,8 +360,12 @@ def maxcut_tfim(
|
|
|
361
360
|
grid_size = n_steps * n_qubits
|
|
362
361
|
grid_dims = (n_steps, n_qubits)
|
|
363
362
|
|
|
364
|
-
degrees = np.
|
|
365
|
-
J_eff = np.
|
|
363
|
+
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
364
|
+
J_eff = np.empty(n_qubits, dtype=np.float64)
|
|
365
|
+
for n in range(n_qubits):
|
|
366
|
+
degree = sum(G_m[n] != 0.0)
|
|
367
|
+
degrees[n] = degree
|
|
368
|
+
J_eff[n] = -G_m[n].sum() / degree
|
|
366
369
|
|
|
367
370
|
thresholds = init_thresholds(n_qubits)
|
|
368
371
|
|
|
@@ -374,7 +377,7 @@ def maxcut_tfim(
|
|
|
374
377
|
|
|
375
378
|
cuda_maxcut_hamming_cdf[grid_dims, group_size](delta_t, tot_t, h_mult, J_eff, degrees, theta, thresholds)
|
|
376
379
|
|
|
377
|
-
tot_prob = sum(
|
|
380
|
+
tot_prob = thresholds.sum()
|
|
378
381
|
thresholds /= tot_prob
|
|
379
382
|
|
|
380
383
|
tot_prob = 0.0
|
|
@@ -386,23 +389,12 @@ def maxcut_tfim(
|
|
|
386
389
|
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, thresholds)
|
|
387
390
|
|
|
388
391
|
adjacency = compute_adjacency(G_m)
|
|
389
|
-
J_max = max(
|
|
392
|
+
J_max = J_eff.max()
|
|
390
393
|
weights = 1.0 / (1.0 + (J_max - J_eff))
|
|
391
394
|
# We only need unique instances
|
|
392
395
|
samples = list(set(mask_array_to_python_ints(local_repulsion_choice_sample(shots, thresholds, adjacency, degrees, weights, n_qubits))))
|
|
393
396
|
|
|
394
|
-
|
|
395
|
-
edge_values = []
|
|
396
|
-
for i in range(n_qubits):
|
|
397
|
-
for j in range(i + 1, n_qubits):
|
|
398
|
-
weight = G_m[i, j]
|
|
399
|
-
if weight == 0.0:
|
|
400
|
-
continue
|
|
401
|
-
edge_keys.append(i)
|
|
402
|
-
edge_keys.append(j)
|
|
403
|
-
edge_values.append(weight)
|
|
404
|
-
|
|
405
|
-
best_solution, best_value = evaluate_cut_edges(samples, edge_keys, edge_values)
|
|
397
|
+
best_solution, best_value = evaluate_cut_edges(G_m, samples)
|
|
406
398
|
|
|
407
399
|
bit_string = int_to_bitstring(best_solution, n_qubits)
|
|
408
400
|
bit_list = list(bit_string)
|
|
@@ -6,47 +6,47 @@ from numba import njit, prange
|
|
|
6
6
|
import os
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
def evaluate_cut_edges(state,
|
|
9
|
+
def evaluate_cut_edges(state, G_m):
|
|
10
|
+
n_qubits = len(G_m)
|
|
10
11
|
cut_value = 0
|
|
11
|
-
for
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
cut_value += edge_values[i]
|
|
12
|
+
for u in range(n_qubits):
|
|
13
|
+
for v in range(u + 1, n_qubits):
|
|
14
|
+
if ((state >> u) & 1) != ((state >> v) & 1):
|
|
15
|
+
cut_value += G_m[u, v]
|
|
16
16
|
|
|
17
17
|
return float(cut_value)
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
@njit
|
|
21
|
-
def compute_energy(theta_bits,
|
|
21
|
+
def compute_energy(theta_bits, G_m):
|
|
22
|
+
n_qubits = len(G_m)
|
|
22
23
|
energy = 0
|
|
23
|
-
for
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
energy += edge_values[i] * spin_u * spin_v
|
|
24
|
+
for u in range(n_qubits):
|
|
25
|
+
for v in range(u + 1, n_qubits):
|
|
26
|
+
spin_u = 1 if theta_bits[u] else -1
|
|
27
|
+
spin_v = 1 if theta_bits[v] else -1
|
|
28
|
+
energy += G_m[u, v] * spin_u * spin_v
|
|
29
29
|
|
|
30
30
|
return energy
|
|
31
31
|
|
|
32
32
|
|
|
33
33
|
@njit
|
|
34
|
-
def bootstrap_worker(theta,
|
|
34
|
+
def bootstrap_worker(theta, G_m, indices):
|
|
35
35
|
local_theta = theta.copy()
|
|
36
36
|
for i in indices:
|
|
37
37
|
local_theta[i] = not local_theta[i]
|
|
38
|
-
energy = compute_energy(local_theta,
|
|
38
|
+
energy = compute_energy(local_theta, G_m)
|
|
39
39
|
|
|
40
40
|
return energy
|
|
41
41
|
|
|
42
42
|
|
|
43
43
|
@njit(parallel=True)
|
|
44
|
-
def bootstrap(theta,
|
|
44
|
+
def bootstrap(theta, G_m, k, indices_array):
|
|
45
45
|
n = len(indices_array) // k
|
|
46
|
-
energies = np.empty(n)
|
|
47
|
-
j = [i * k for i in range(n)]
|
|
46
|
+
energies = np.empty(n, dtype=np.float64)
|
|
48
47
|
for i in prange(n):
|
|
49
|
-
|
|
48
|
+
j = i * k
|
|
49
|
+
energies[i] = bootstrap_worker(theta, G_m, indices_array[j : j + k])
|
|
50
50
|
|
|
51
51
|
return energies
|
|
52
52
|
|
|
@@ -98,18 +98,7 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best
|
|
|
98
98
|
bitstring, _, _ = maxcut_tfim(G_m, quality=quality, shots=shots)
|
|
99
99
|
best_theta = [b == "1" for b in list(bitstring)]
|
|
100
100
|
|
|
101
|
-
|
|
102
|
-
edge_values = []
|
|
103
|
-
for i in range(n_qubits):
|
|
104
|
-
for j in range(i + 1, n_qubits):
|
|
105
|
-
weight = G_m[i, j]
|
|
106
|
-
if weight == 0.0:
|
|
107
|
-
continue
|
|
108
|
-
edge_keys.append(i)
|
|
109
|
-
edge_keys.append(j)
|
|
110
|
-
edge_values.append(weight)
|
|
111
|
-
|
|
112
|
-
min_energy = compute_energy(best_theta, edge_keys, edge_values)
|
|
101
|
+
min_energy = compute_energy(best_theta, G_m)
|
|
113
102
|
improved = True
|
|
114
103
|
while improved:
|
|
115
104
|
improved = False
|
|
@@ -122,9 +111,9 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best
|
|
|
122
111
|
combos = list(
|
|
123
112
|
item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
|
|
124
113
|
)
|
|
125
|
-
energies = bootstrap(theta,
|
|
114
|
+
energies = bootstrap(theta, G_m, k, combos)
|
|
126
115
|
|
|
127
|
-
energy = min(
|
|
116
|
+
energy = energies.min()
|
|
128
117
|
if energy < min_energy:
|
|
129
118
|
index_match = np.random.choice(np.where(energies == energy)[0])
|
|
130
119
|
indices = combos[(index_match * k) : ((index_match + 1) * k)]
|
|
@@ -147,6 +136,6 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best
|
|
|
147
136
|
bitstring += "0"
|
|
148
137
|
l.append(nodes[i])
|
|
149
138
|
|
|
150
|
-
cut_value = evaluate_cut_edges(sample,
|
|
139
|
+
cut_value = evaluate_cut_edges(sample, G_m)
|
|
151
140
|
|
|
152
141
|
return bitstring, float(cut_value), (l, r), float(min_energy)
|
|
@@ -49,7 +49,7 @@ G = nx.petersen_graph()
|
|
|
49
49
|
best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=8)
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
The (integer) `quality` setting is optional, with a default value of `
|
|
52
|
+
The (integer) `quality` setting is optional, with a default value of `8`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
|
|
53
53
|
|
|
54
54
|
Based on a combination of the TFIM-inspired MAXCUT solver and another technique for finding ground-state energy in quantum chemistry that we call the _"binary Clifford eigensolver,"_ we also provide an (approximate) spin glass ground-state solver:
|
|
55
55
|
```py
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "PyQrackIsing"
|
|
15
|
-
version = "1.18.
|
|
15
|
+
version = "1.18.4"
|
|
16
16
|
requires-python = ">=3.8"
|
|
17
17
|
description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
|
|
18
18
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='PyQrackIsing',
|
|
54
|
-
version='1.18.
|
|
54
|
+
version='1.18.4',
|
|
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
|