PyQrackIsing 1.14.9__tar.gz → 1.14.11__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.14.9/PyQrackIsing.egg-info → pyqrackising-1.14.11}/PKG-INFO +1 -1
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/PyQrackIsing/maxcut_tfim.py +81 -39
- pyqrackising-1.14.11/PyQrackIsing/tsp_symmetric.py +129 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11/PyQrackIsing.egg-info}/PKG-INFO +1 -1
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/pyproject.toml +1 -1
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/setup.py +1 -1
- pyqrackising-1.14.9/PyQrackIsing/tsp_symmetric.py +0 -79
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/CMakeLists.txt +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/LICENSE.md +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/MANIFEST.in +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/PyQrackIsing/__init__.py +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/PyQrackIsing/generate_tfim_samples.py +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/PyQrackIsing/spin_glass_solver.py +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/PyQrackIsing/tfim_magnetization.py +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/PyQrackIsing/tfim_square_magnetization.py +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/PyQrackIsing.egg-info/not-zip-safe +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/PyQrackIsing.egg-info/top_level.txt +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/README.md +0 -0
- {pyqrackising-1.14.9 → pyqrackising-1.14.11}/setup.cfg +0 -0
|
@@ -147,52 +147,95 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
|
147
147
|
|
|
148
148
|
|
|
149
149
|
# Written by Elara (OpenAI custom GPT)
|
|
150
|
-
|
|
150
|
+
@njit
|
|
151
|
+
def local_repulsion_choice(adjacency, degrees, weights, n, m):
|
|
151
152
|
"""
|
|
152
|
-
|
|
153
|
+
|
|
154
|
+
Pick m nodes out of n with repulsion bias:
|
|
153
155
|
- High-degree nodes are already less likely
|
|
154
156
|
- After choosing a node, its neighbors' probabilities are further reduced
|
|
157
|
+
adjacency: 2D int array (n x max_deg), padded with -1
|
|
158
|
+
|
|
159
|
+
degrees: int array of shape (n,)
|
|
160
|
+
weights: float64 array of shape (n,)
|
|
155
161
|
"""
|
|
156
162
|
|
|
157
|
-
# Base weights: inverse degree
|
|
158
|
-
# degrees = np.array([len(adjacency.get(i, [])) for i in range(n)], dtype=np.float64)
|
|
159
|
-
# weights = 1.0 / (degrees + 1.0)
|
|
160
163
|
weights = weights.copy()
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
+
chosen = np.zeros(m, dtype=np.int32) # store chosen indices
|
|
165
|
+
available = np.ones(n, dtype=np.int32) # 1 = available, 0 = not
|
|
166
|
+
mask = np.zeros(n, dtype=np.bool_)
|
|
167
|
+
chosen_count = 0
|
|
164
168
|
|
|
165
169
|
for _ in range(m):
|
|
166
|
-
|
|
170
|
+
# Count available
|
|
171
|
+
total_w = 0.0
|
|
172
|
+
for i in range(n):
|
|
173
|
+
if available[i] == 1:
|
|
174
|
+
total_w += weights[i]
|
|
175
|
+
if total_w <= 0:
|
|
167
176
|
break
|
|
168
177
|
|
|
169
|
-
# Normalize
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
+
# Normalize & sample
|
|
179
|
+
r = np.random.rand()
|
|
180
|
+
cum = 0.0
|
|
181
|
+
node = -1
|
|
182
|
+
for i in range(n):
|
|
183
|
+
if available[i] == 1:
|
|
184
|
+
cum += weights[i] / total_w
|
|
185
|
+
if r < cum:
|
|
186
|
+
node = i
|
|
187
|
+
break
|
|
188
|
+
|
|
189
|
+
if node == -1:
|
|
190
|
+
continue
|
|
178
191
|
|
|
179
|
-
#
|
|
180
|
-
|
|
192
|
+
# Select node
|
|
193
|
+
chosen[chosen_count] = node
|
|
194
|
+
chosen_count += 1
|
|
195
|
+
available[node] = 0
|
|
196
|
+
mask[node] = 1
|
|
181
197
|
|
|
182
198
|
# Repulsion: penalize neighbors
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
# Build integer mask
|
|
189
|
-
mask = 0
|
|
190
|
-
for pos in chosen:
|
|
191
|
-
mask |= 1 << pos
|
|
199
|
+
deg = degrees[node]
|
|
200
|
+
for j in range(deg):
|
|
201
|
+
nbr = adjacency[node, j]
|
|
202
|
+
if nbr >= 0 and available[nbr] == 1:
|
|
203
|
+
weights[nbr] *= 0.5 # tunable penalty factor
|
|
192
204
|
|
|
193
205
|
return mask
|
|
194
206
|
|
|
195
207
|
|
|
208
|
+
@njit(parallel=True)
|
|
209
|
+
def local_repulsion_choice_sample(shots, thresholds, adjacency, degrees, weights, n):
|
|
210
|
+
samples = np.zeros((shots, n), dtype=np.bool_) # (shots × n) boolean mask array
|
|
211
|
+
|
|
212
|
+
for s in prange(shots):
|
|
213
|
+
# First dimension: Hamming weight
|
|
214
|
+
mag_prob = np.random.random()
|
|
215
|
+
m = 0
|
|
216
|
+
while thresholds[m] < mag_prob:
|
|
217
|
+
m += 1
|
|
218
|
+
m += 1
|
|
219
|
+
|
|
220
|
+
# Second dimension: permutation within Hamming weight
|
|
221
|
+
samples[s, :] = local_repulsion_choice(adjacency, degrees, weights, n, m)
|
|
222
|
+
|
|
223
|
+
return samples
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def mask_array_to_python_ints(masks):
|
|
227
|
+
samples = []
|
|
228
|
+
for mask in masks:
|
|
229
|
+
sample = 0
|
|
230
|
+
for b in reversed(mask):
|
|
231
|
+
sample <<= 1
|
|
232
|
+
if b:
|
|
233
|
+
sample |= 1
|
|
234
|
+
samples.append(sample)
|
|
235
|
+
|
|
236
|
+
return samples
|
|
237
|
+
|
|
238
|
+
|
|
196
239
|
def evaluate_cut_edges(samples, edge_keys, edge_values):
|
|
197
240
|
best_value = float("-inf")
|
|
198
241
|
best_solution = None
|
|
@@ -324,18 +367,17 @@ def maxcut_tfim(
|
|
|
324
367
|
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, thresholds)
|
|
325
368
|
|
|
326
369
|
G_dict = nx.to_dict_of_lists(G)
|
|
370
|
+
max_degree = max(len(x) for x in G_dict.values())
|
|
371
|
+
adjacency = np.full((len(nodes), max_degree), -1, dtype=np.int32)
|
|
372
|
+
for i in range(len(nodes)):
|
|
373
|
+
adj = G_dict.get(nodes[i], [])
|
|
374
|
+
for j in range(len(adj)):
|
|
375
|
+
adjacency[i, j] = nodes.index(adj[j])
|
|
376
|
+
|
|
327
377
|
J_max = max(J_eff)
|
|
328
378
|
weights = 1.0 / (1.0 + (J_max - J_eff))
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
# First dimension: Hamming weight
|
|
332
|
-
mag_prob = np.random.random()
|
|
333
|
-
m = 0
|
|
334
|
-
while thresholds[m] < mag_prob:
|
|
335
|
-
m += 1
|
|
336
|
-
m += 1
|
|
337
|
-
# Second dimension: permutation within Hamming weight
|
|
338
|
-
samples.append(local_repulsion_choice(nodes, G_dict, degrees, weights, n_qubits, m))
|
|
379
|
+
# We only need unique instances
|
|
380
|
+
samples = list(set(mask_array_to_python_ints(local_repulsion_choice_sample(shots, thresholds, adjacency, degrees, weights, n_qubits))))
|
|
339
381
|
|
|
340
382
|
edge_keys = []
|
|
341
383
|
edge_values = []
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
from .spin_glass_solver import spin_glass_solver
|
|
2
|
+
import networkx as nx
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def get_best_stitch(adjacency, terminals_a, terminals_b, is_cyclic):
|
|
6
|
+
best_weight = float("inf")
|
|
7
|
+
best_edge = None
|
|
8
|
+
for a in range(2):
|
|
9
|
+
a_term = terminals_a[a]
|
|
10
|
+
for b in range(2):
|
|
11
|
+
b_term = terminals_b[b]
|
|
12
|
+
weight = adjacency[a_term][b_term]["weight"]
|
|
13
|
+
if is_cyclic:
|
|
14
|
+
n_a_term = terminals_a[0 if a else 1]
|
|
15
|
+
n_b_term = terminals_b[0 if b else 1]
|
|
16
|
+
weight += adjacency[n_a_term][n_b_term]["weight"]
|
|
17
|
+
if weight < best_weight:
|
|
18
|
+
best_weight = weight
|
|
19
|
+
best_edge = (a, b)
|
|
20
|
+
|
|
21
|
+
return best_weight, best_edge
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def tsp_symmetric(G, quality=0, shots=None, correction_quality=2, is_cyclic=True, start_node=None):
|
|
25
|
+
nodes = list(G.nodes())
|
|
26
|
+
n_nodes = len(nodes)
|
|
27
|
+
|
|
28
|
+
if n_nodes == 0:
|
|
29
|
+
return ([], 0)
|
|
30
|
+
if n_nodes == 1:
|
|
31
|
+
return ([nodes[0]], 0)
|
|
32
|
+
if n_nodes == 2:
|
|
33
|
+
return ([nodes[0], nodes[1]], G[nodes[0]][nodes[1]].get("weight", 1.0))
|
|
34
|
+
|
|
35
|
+
a = []
|
|
36
|
+
b = []
|
|
37
|
+
if not (start_node is None):
|
|
38
|
+
a = [start_node]
|
|
39
|
+
b = nodes
|
|
40
|
+
b.remove(start_node)
|
|
41
|
+
else:
|
|
42
|
+
while (len(a) == 0) or (len(b) == 0):
|
|
43
|
+
bits = ''
|
|
44
|
+
_, _, bits, _ = spin_glass_solver(G, quality=quality, shots=shots, correction_quality=correction_quality)
|
|
45
|
+
a = list(bits[0])
|
|
46
|
+
b = list(bits[1])
|
|
47
|
+
|
|
48
|
+
G_a = nx.Graph()
|
|
49
|
+
G_b = nx.Graph()
|
|
50
|
+
G_a.add_nodes_from(a)
|
|
51
|
+
G_b.add_nodes_from(b)
|
|
52
|
+
for u, v, data in G.edges(data=True):
|
|
53
|
+
if (u in a) and (v in a):
|
|
54
|
+
G_a.add_edge(u, v, weight=data.get("weight", 1.0))
|
|
55
|
+
continue
|
|
56
|
+
|
|
57
|
+
if (u in b) and (v in b):
|
|
58
|
+
G_b.add_edge(u, v, weight=data.get("weight", 1.0))
|
|
59
|
+
|
|
60
|
+
sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False)
|
|
61
|
+
sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False)
|
|
62
|
+
|
|
63
|
+
path_a = sol_a[0]
|
|
64
|
+
path_b = sol_b[0]
|
|
65
|
+
|
|
66
|
+
sol_weight = sol_a[1] + sol_b[1]
|
|
67
|
+
|
|
68
|
+
single = None
|
|
69
|
+
is_single_a = len(path_a) == 1
|
|
70
|
+
is_single_b = len(path_b) == 1
|
|
71
|
+
|
|
72
|
+
if is_single_a and is_single_b:
|
|
73
|
+
return (path_a + path_b, sol_weight + G[path_a[0]][path_b[0]].get("weight", 1.0))
|
|
74
|
+
|
|
75
|
+
singlet = None
|
|
76
|
+
bulk = None
|
|
77
|
+
if is_single_a:
|
|
78
|
+
singlet = path_a[0]
|
|
79
|
+
bulk = path_b
|
|
80
|
+
elif is_single_b:
|
|
81
|
+
singlet = path_b[0]
|
|
82
|
+
bulk = path_a
|
|
83
|
+
|
|
84
|
+
if not singlet is None:
|
|
85
|
+
best_weight = G[singlet][bulk[0]].get("weight", 1.0)
|
|
86
|
+
best_path = [singlet] + bulk
|
|
87
|
+
weight = G[singlet][bulk[-1]].get("weight", 1.0)
|
|
88
|
+
if weight < best_weight:
|
|
89
|
+
best_weight = weight
|
|
90
|
+
best_path = bulk + [singlet]
|
|
91
|
+
for i in range(len(bulk) - 1):
|
|
92
|
+
weight = (
|
|
93
|
+
G[singlet][bulk[i]].get("weight", 1.0) +
|
|
94
|
+
G[singlet][bulk[i + 1]].get("weight", 1.0) -
|
|
95
|
+
G[bulk[i]][bulk[i + 1]].get("weight", 1.0)
|
|
96
|
+
)
|
|
97
|
+
if weight < best_weight:
|
|
98
|
+
best_weight = weight
|
|
99
|
+
best_path = bulk.copy().insert(singlet, i + 1)
|
|
100
|
+
|
|
101
|
+
return (best_path, sol_weight + best_weight)
|
|
102
|
+
|
|
103
|
+
terminals_a = [path_a[0], path_a[-1]]
|
|
104
|
+
terminals_b = [path_b[0], path_b[-1]]
|
|
105
|
+
|
|
106
|
+
for _ in range(2):
|
|
107
|
+
for _ in range(2):
|
|
108
|
+
best_weight = G[terminals_a[1]][terminals_b[0]].get("weight", 1.0)
|
|
109
|
+
best_path = path_a + path_b
|
|
110
|
+
weight = G[terminals_b[1]][terminals_a[0]].get("weight", 1.0)
|
|
111
|
+
if weight < best_weight:
|
|
112
|
+
best_weight = weight
|
|
113
|
+
best_path = path_b + path_a
|
|
114
|
+
for i in range(len(path_b) - 1):
|
|
115
|
+
weight = (
|
|
116
|
+
G[terminals_a[0]][path_b[i]].get("weight", 1.0) +
|
|
117
|
+
G[terminals_a[1]][path_b[i + 1]].get("weight", 1.0) -
|
|
118
|
+
G[path_b[i]][path_b[i + 1]].get("weight", 1.0)
|
|
119
|
+
)
|
|
120
|
+
if weight < best_weight:
|
|
121
|
+
best_weight = weight
|
|
122
|
+
best_path = path_b.copy()
|
|
123
|
+
best_path[i + 1:i + 1] = path_a
|
|
124
|
+
path_a.reverse()
|
|
125
|
+
terminals_a.reverse()
|
|
126
|
+
path_a, path_b = path_b, path_a
|
|
127
|
+
terminals_a, terminals_b = terminals_b, terminals_a
|
|
128
|
+
|
|
129
|
+
return (best_path, sol_weight + best_weight)
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "PyQrackIsing"
|
|
15
|
-
version = "1.14.
|
|
15
|
+
version = "1.14.11"
|
|
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.14.
|
|
54
|
+
version='1.14.11',
|
|
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)',
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
from .spin_glass_solver import spin_glass_solver
|
|
2
|
-
import networkx as nx
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
def get_best_stitch(adjacency, terminals_a, terminals_b, is_cyclic):
|
|
6
|
-
best_weight = float("inf")
|
|
7
|
-
best_edge = None
|
|
8
|
-
for a in range(2):
|
|
9
|
-
a_term = terminals_a[a]
|
|
10
|
-
for b in range(2):
|
|
11
|
-
b_term = terminals_b[b]
|
|
12
|
-
weight = adjacency[a_term][b_term]["weight"]
|
|
13
|
-
if is_cyclic:
|
|
14
|
-
n_a_term = terminals_a[0 if a else 1]
|
|
15
|
-
n_b_term = terminals_b[0 if b else 1]
|
|
16
|
-
weight += adjacency[n_a_term][n_b_term]["weight"]
|
|
17
|
-
if weight < best_weight:
|
|
18
|
-
best_weight = weight
|
|
19
|
-
best_edge = (a, b)
|
|
20
|
-
|
|
21
|
-
return best_weight, best_edge
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def tsp_symmetric(G, quality=0, shots=None, correction_quality=2, is_cyclic=True, start_node=None):
|
|
25
|
-
nodes = list(G.nodes())
|
|
26
|
-
n_nodes = len(nodes)
|
|
27
|
-
|
|
28
|
-
if n_nodes == 0:
|
|
29
|
-
return ([], 0)
|
|
30
|
-
if n_nodes == 1:
|
|
31
|
-
return ([nodes[0]], 0)
|
|
32
|
-
if n_nodes == 2:
|
|
33
|
-
return ([nodes[0], nodes[1]], G[nodes[0]][nodes[1]]["weight"])
|
|
34
|
-
|
|
35
|
-
a = []
|
|
36
|
-
b = []
|
|
37
|
-
if not (start_node is None):
|
|
38
|
-
a = [start_node]
|
|
39
|
-
b = nodes
|
|
40
|
-
b.remove(start_node)
|
|
41
|
-
else:
|
|
42
|
-
while (len(a) == 0) or (len(b) == 0):
|
|
43
|
-
bits = ''
|
|
44
|
-
_, _, bits, _ = spin_glass_solver(G, quality=quality, shots=shots, correction_quality=correction_quality)
|
|
45
|
-
a = list(bits[0])
|
|
46
|
-
b = list(bits[1])
|
|
47
|
-
|
|
48
|
-
G_a = nx.Graph()
|
|
49
|
-
G_b = nx.Graph()
|
|
50
|
-
G_a.add_nodes_from(a)
|
|
51
|
-
G_b.add_nodes_from(b)
|
|
52
|
-
for u, v, data in G.edges(data=True):
|
|
53
|
-
if (u in a) and (v in a):
|
|
54
|
-
G_a.add_edge(u, v, weight=data.get("weight", 1.0))
|
|
55
|
-
continue
|
|
56
|
-
|
|
57
|
-
if (u in b) and (v in b):
|
|
58
|
-
G_b.add_edge(u, v, weight=data.get("weight", 1.0))
|
|
59
|
-
|
|
60
|
-
sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False)
|
|
61
|
-
sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False)
|
|
62
|
-
|
|
63
|
-
path_a = sol_a[0]
|
|
64
|
-
path_b = sol_b[0]
|
|
65
|
-
|
|
66
|
-
sol_weight = sol_a[1] + sol_b[1]
|
|
67
|
-
|
|
68
|
-
terminals_a = (path_a[0], path_a[-1])
|
|
69
|
-
terminals_b = (path_b[0], path_b[-1])
|
|
70
|
-
|
|
71
|
-
# Find the best edge to stitch "a" to "b"
|
|
72
|
-
best_weight, best_end = get_best_stitch(G, terminals_a, terminals_b, is_cyclic)
|
|
73
|
-
|
|
74
|
-
if best_end[0] == 0:
|
|
75
|
-
path_a.reverse()
|
|
76
|
-
if best_end[1] == 1:
|
|
77
|
-
path_b.reverse()
|
|
78
|
-
|
|
79
|
-
return (path_a + path_b, sol_weight + best_weight)
|
|
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
|