PyQrackIsing 1.14.8__tar.gz → 1.14.9__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.
Files changed (21) hide show
  1. {pyqrackising-1.14.8/PyQrackIsing.egg-info → pyqrackising-1.14.9}/PKG-INFO +1 -1
  2. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/PyQrackIsing/maxcut_tfim.py +84 -105
  3. {pyqrackising-1.14.8 → pyqrackising-1.14.9/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  4. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/pyproject.toml +1 -1
  5. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/setup.py +1 -1
  6. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/CMakeLists.txt +0 -0
  7. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/LICENSE.md +0 -0
  8. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/MANIFEST.in +0 -0
  9. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/PyQrackIsing/__init__.py +0 -0
  10. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  11. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/PyQrackIsing/generate_tfim_samples.py +0 -0
  12. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/PyQrackIsing/spin_glass_solver.py +0 -0
  13. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/PyQrackIsing/tfim_magnetization.py +0 -0
  14. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  15. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/PyQrackIsing/tsp_symmetric.py +0 -0
  16. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  17. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  18. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  19. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/PyQrackIsing.egg-info/top_level.txt +0 -0
  20. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/README.md +0 -0
  21. {pyqrackising-1.14.8 → pyqrackising-1.14.9}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.14.8
3
+ Version: 1.14.9
4
4
  Summary: Near-ideal closed-form solutions for transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -11,10 +11,7 @@ try:
11
11
  warnings.simplefilter('ignore', category=NumbaPerformanceWarning)
12
12
 
13
13
  @cuda.jit(device=True)
14
- def cuda_probability_by_hamming_weight(q, J, h, z, theta, t, n_qubits, bias):
15
- if J > 0.0:
16
- q = n_qubits - (2 + q)
17
-
14
+ def cuda_probability_by_hamming_weight(q, J, h, z, theta, t, n_qubits):
18
15
  # critical angle
19
16
  theta_c = np.arcsin(max(-1.0, min(1.0, abs(h) / (z * J))))
20
17
 
@@ -24,15 +21,18 @@ try:
24
21
  - 0.5
25
22
  )
26
23
 
27
- if (p * n_qubits) >= 1024:
28
- bias[q] = 0.0
29
- else:
30
- norm = pow(2.0, -(n_qubits + 1) * p) * (pow(2.0, (n_qubits + 2) * p) - 1.0) / (pow(2, p) - 1.0)
31
- bias[q] = 1.0 / pow(2.0, p * q) / norm
24
+ if (p * (n_qubits + 2.0)) >= 1024.0:
25
+ return 0.0
26
+
27
+ result = (pow(2.0, (n_qubits + 2.0) * p) - 1.0) * pow(2.0, -((n_qubits + 1.0) * p) - p * q) / (pow(2.0, p) - 1.0)
28
+
29
+ if math.isnan(result) or math.isinf(result):
30
+ return 0.0
31
+
32
+ return result
32
33
 
33
34
  @cuda.jit
34
35
  def cuda_maxcut_hamming_cdf(delta_t, tot_t, h_mult, J_func, degrees, theta, hamming_prob):
35
- bias = cuda.shared.array(0, dtype=np.float64)
36
36
  step = cuda.blockIdx.x
37
37
  qi = cuda.blockIdx.y
38
38
  J_eff = J_func[qi]
@@ -47,10 +47,11 @@ try:
47
47
  h_t = h_mult * (tot_t - t)
48
48
 
49
49
  qo = cuda.threadIdx.x
50
- cuda_probability_by_hamming_weight(qo, J_eff, h_t, z, theta_eff, t, n_qubits, bias)
51
- hamming_prob[qo] += bias[qo]
52
- cuda_probability_by_hamming_weight(qo, J_eff, h_t, z, theta_eff, tm1, n_qubits, bias)
53
- hamming_prob[qo] -= bias[qo]
50
+ if J_eff > 0.0:
51
+ qo = cuda.blockDim.x - (1 + qo)
52
+ diff = cuda_probability_by_hamming_weight(qo, J_eff, h_t, z, theta_eff, t, n_qubits)
53
+ diff -= cuda_probability_by_hamming_weight(qo, J_eff, h_t, z, theta_eff, tm1, n_qubits)
54
+ hamming_prob[qo] += diff
54
55
 
55
56
  except:
56
57
  IS_CUDA_AVAILABLE = False
@@ -58,7 +59,7 @@ except:
58
59
 
59
60
  @njit
60
61
  def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
61
- bias = np.zeros(n_qubits - 1)
62
+ bias = np.zeros(n_qubits - 1, dtype=np.float64)
62
63
 
63
64
  # critical angle
64
65
  theta_c = np.arcsin(
@@ -96,7 +97,8 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
96
97
  @njit(parallel=True)
97
98
  def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
98
99
  if n_qubits < 2:
99
- return np.empty(0, dtype=np.float64)
100
+ hamming_prob.fill(0.0)
101
+ return
100
102
 
101
103
  n_steps = 1 << quality
102
104
  delta_t = 1.0 / n_steps
@@ -141,91 +143,55 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
141
143
  for i in range(n_bias):
142
144
  tot_prob += hamming_prob[i]
143
145
  hamming_prob[i] = tot_prob
144
- hamming_prob[-1] = 1.0
146
+ hamming_prob[-1] = 2.0
145
147
 
146
148
 
147
149
  # Written by Elara (OpenAI custom GPT)
148
- @njit(parallel=True)
149
- def local_repulsion_choice(adjacency, degrees, weights, n, m):
150
+ def local_repulsion_choice(nodes, adjacency, degrees, weights, n, m):
150
151
  """
151
- Pick m nodes out of n with repulsion bias:
152
+ Pick m nodes (bit positions) out of n with repulsion bias:
152
153
  - High-degree nodes are already less likely
153
154
  - After choosing a node, its neighbors' probabilities are further reduced
154
- adjacency: 2D int array (n x max_deg), padded with -1
155
- degrees: int array of shape (n,)
156
- weights: float64 array of shape (n,)
157
155
  """
158
156
 
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)
159
160
  weights = weights.copy()
160
- chosen = np.zeros(m, dtype=np.int32) # store chosen indices
161
- available = np.ones(n, dtype=np.int32) # 1 = available, 0 = not
162
- mask = np.zeros(n, dtype=np.bool_)
163
- chosen_count = 0
164
-
165
- for _ in prange(m):
166
- # Count available
167
- total_w = 0.0
168
- for i in range(n):
169
- if available[i] == 1:
170
- total_w += weights[i]
171
- if total_w <= 0:
172
- break
173
161
 
174
- # Normalize & sample
175
- r = np.random.rand()
176
- cum = 0.0
177
- node = -1
178
- for i in range(n):
179
- if available[i] == 1:
180
- cum += weights[i] / total_w
181
- if r < cum:
182
- node = i
183
- break
184
-
185
- if node == -1:
186
- continue
187
-
188
- # Select node
189
- chosen[chosen_count] = node
190
- chosen_count += 1
191
- available[node] = 0
192
- mask[node] = True
162
+ chosen = []
163
+ available = set(range(len(nodes)))
193
164
 
194
- # Repulsion: penalize neighbors
195
- deg = degrees[node]
196
- for j in range(deg):
197
- nbr = adjacency[node, j]
198
- if nbr >= 0 and available[nbr] == 1:
199
- weights[nbr] *= 0.5 # tunable penalty factor
200
-
201
- return mask
165
+ for _ in range(m):
166
+ if not available:
167
+ break
202
168
 
169
+ # Normalize weights over remaining nodes
170
+ sub_weights = np.array([weights[i] for i in available], dtype=np.float64)
171
+ sub_weights /= sub_weights.sum()
172
+ sub_nodes = list(available)
203
173
 
204
- def local_repulsion_choice_sample(shots, thresholds, adjacency, degrees, weights, n):
205
- samples = []
174
+ # Sample one node
175
+ idx = np.random.choice(len(sub_nodes), p=sub_weights)
176
+ node = sub_nodes[idx]
177
+ chosen.append(node)
206
178
 
207
- for s in range(shots):
208
- # First dimension: Hamming weight
209
- mag_prob = np.random.random()
210
- m = 0
211
- while thresholds[m] < mag_prob:
212
- m += 1
213
- m += 1
179
+ # Remove node from available
180
+ available.remove(node)
214
181
 
215
- # Second dimension: permutation within Hamming weight
216
- samples.append(mask_array_to_python_int(local_repulsion_choice(adjacency, degrees, weights, n, m)))
217
-
218
- return samples
182
+ # Repulsion: penalize neighbors
183
+ for nbr in adjacency.get(nodes[node], []):
184
+ idx = nodes.index(nbr)
185
+ if idx in available:
186
+ weights[idx] *= 0.5 # halve neighbor's weight (tunable!)
219
187
 
188
+ # Build integer mask
189
+ mask = 0
190
+ for pos in chosen:
191
+ mask |= 1 << pos
220
192
 
221
- def mask_array_to_python_int(mask):
222
- sample = 0
223
- for b in reversed(mask):
224
- sample <<= 1
225
- if b:
226
- sample |= 1
193
+ return mask
227
194
 
228
- return sample
229
195
 
230
196
  def evaluate_cut_edges(samples, edge_keys, edge_values):
231
197
  best_value = float("-inf")
@@ -268,9 +234,19 @@ def maxcut_tfim(
268
234
  if n_qubits == 1:
269
235
  return "0", 0, ([nodes[0]], [])
270
236
 
237
+ if n_qubits == 2:
238
+ ed = G.get_edge_data(nodes[0], nodes[1], default={})
239
+ if ed == {}:
240
+ return "01", 0, ([nodes[0]], [nodes[1]])
241
+
242
+ weight = ed.get("weight", 1.0)
243
+ if weight < 0.0:
244
+ return "00", 0, (nodes, [])
245
+
246
+ return "01", weight, ([nodes[0]], [nodes[1]])
247
+
271
248
  # Warp size is 32:
272
249
  group_size = n_qubits - 1
273
- shared_size = (n_qubits - 1) * 8
274
250
 
275
251
  if quality is None:
276
252
  quality = 10
@@ -298,19 +274,21 @@ def maxcut_tfim(
298
274
  dtype=np.float64,
299
275
  )
300
276
  # thresholds = tfim_sampler._maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
277
+
301
278
  n_bias = n_qubits - 1
302
- tot_prob = 2.0
303
- thresholds = np.zeros(n_bias)
304
- for q in range(1, n_qubits // 2):
305
- n = math.comb(n_qubits, q)
306
- thresholds[q - 1] = n
307
- thresholds[n_bias - q] = n
308
- tot_prob += n << 1
279
+ thresholds = np.zeros(n_bias, dtype=np.float64)
280
+ tot_prob = 0
281
+ p = 1.0
309
282
  if n_qubits & 1:
310
283
  q = n_qubits // 2
311
- n = math.comb(n_qubits, q)
312
- thresholds[q - 1] = n
313
- tot_prob += n
284
+ thresholds[q - 1] = p
285
+ tot_prob = p
286
+ p /= 2
287
+ for q in range(1, n_qubits // 2):
288
+ thresholds[q - 1] = p
289
+ thresholds[n_bias - q] = p
290
+ tot_prob += 2 * p
291
+ p /= 2
314
292
  thresholds /= tot_prob
315
293
 
316
294
  if IS_CUDA_AVAILABLE and cuda.is_available() and grid_size >= 128:
@@ -332,7 +310,7 @@ def maxcut_tfim(
332
310
  )
333
311
  )
334
312
 
335
- cuda_maxcut_hamming_cdf[grid_dims, group_size, 0, shared_size](delta_t, tot_t, h_mult, J_eff, degrees, theta, thresholds)
313
+ cuda_maxcut_hamming_cdf[grid_dims, group_size](delta_t, tot_t, h_mult, J_eff, degrees, theta, thresholds)
336
314
 
337
315
  tot_prob = sum(thresholds)
338
316
  thresholds /= tot_prob
@@ -341,22 +319,23 @@ def maxcut_tfim(
341
319
  for i in range(n_bias):
342
320
  tot_prob += thresholds[i]
343
321
  thresholds[i] = tot_prob
344
- thresholds[-1] = 1.0
322
+ thresholds[-1] = 2.0
345
323
  else:
346
324
  maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, thresholds)
347
325
 
348
326
  G_dict = nx.to_dict_of_lists(G)
349
- max_degree = max(len(x) for x in G_dict.values())
350
- adjacency = np.full((len(nodes), max_degree), -1, dtype=np.int32)
351
- for i in range(len(nodes)):
352
- adj = G_dict.get(nodes[i], [])
353
- for j in range(len(adj)):
354
- adjacency[i, j] = adj[j]
355
-
356
327
  J_max = max(J_eff)
357
328
  weights = 1.0 / (1.0 + (J_max - J_eff))
358
- # We only need unique instances
359
- samples = list(set(local_repulsion_choice_sample(shots, thresholds, adjacency, degrees, weights, n_qubits)))
329
+ samples = []
330
+ for s in range(shots):
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))
360
339
 
361
340
  edge_keys = []
362
341
  edge_values = []
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.14.8
3
+ Version: 1.14.9
4
4
  Summary: Near-ideal closed-form solutions for transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "PyQrackIsing"
15
- version = "1.14.8"
15
+ version = "1.14.9"
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.8',
54
+ version='1.14.9',
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