PyQrackIsing 1.14.7__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.7/PyQrackIsing.egg-info → pyqrackising-1.14.9}/PKG-INFO +1 -1
  2. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/PyQrackIsing/maxcut_tfim.py +83 -108
  3. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/PyQrackIsing/tsp_symmetric.py +1 -1
  4. {pyqrackising-1.14.7 → pyqrackising-1.14.9/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  5. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/README.md +2 -2
  6. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/pyproject.toml +1 -1
  7. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/setup.py +1 -1
  8. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/CMakeLists.txt +0 -0
  9. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/LICENSE.md +0 -0
  10. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/MANIFEST.in +0 -0
  11. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/PyQrackIsing/__init__.py +0 -0
  12. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  13. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/PyQrackIsing/generate_tfim_samples.py +0 -0
  14. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/PyQrackIsing/spin_glass_solver.py +0 -0
  15. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/PyQrackIsing/tfim_magnetization.py +0 -0
  16. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  17. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  18. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  19. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  20. {pyqrackising-1.14.7 → pyqrackising-1.14.9}/PyQrackIsing.egg-info/top_level.txt +0 -0
  21. {pyqrackising-1.14.7 → 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.7
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,95 +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
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
161
+
162
+ chosen = []
163
+ available = set(range(len(nodes)))
164
164
 
165
165
  for _ in range(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:
166
+ if not available:
172
167
  break
173
168
 
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
193
-
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
202
-
203
-
204
- @njit(parallel=True)
205
- def local_repulsion_choice_sample(shots, thresholds, adjacency, degrees, weights, n):
206
- samples = np.zeros((shots, n), dtype=np.bool_) # (shots × n) boolean mask array
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)
207
173
 
208
- for s in prange(shots):
209
- # First dimension: Hamming weight
210
- mag_prob = np.random.random()
211
- m = 0
212
- while thresholds[m] < mag_prob:
213
- m += 1
214
- m += 1
174
+ # Sample one node
175
+ idx = np.random.choice(len(sub_nodes), p=sub_weights)
176
+ node = sub_nodes[idx]
177
+ chosen.append(node)
215
178
 
216
- # Second dimension: permutation within Hamming weight
217
- samples[s, :] = local_repulsion_choice(adjacency, degrees, weights, n, m)
179
+ # Remove node from available
180
+ available.remove(node)
218
181
 
219
- 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!)
220
187
 
188
+ # Build integer mask
189
+ mask = 0
190
+ for pos in chosen:
191
+ mask |= 1 << pos
221
192
 
222
- def mask_array_to_python_ints(masks):
223
- samples = []
224
- for mask in masks:
225
- sample = 0
226
- for b in reversed(mask):
227
- sample <<= 1
228
- if b:
229
- sample |= 1
230
- samples.append(sample)
193
+ return mask
231
194
 
232
- return samples
233
195
 
234
196
  def evaluate_cut_edges(samples, edge_keys, edge_values):
235
197
  best_value = float("-inf")
@@ -272,9 +234,19 @@ def maxcut_tfim(
272
234
  if n_qubits == 1:
273
235
  return "0", 0, ([nodes[0]], [])
274
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
+
275
248
  # Warp size is 32:
276
249
  group_size = n_qubits - 1
277
- shared_size = (n_qubits - 1) * 8
278
250
 
279
251
  if quality is None:
280
252
  quality = 10
@@ -302,19 +274,21 @@ def maxcut_tfim(
302
274
  dtype=np.float64,
303
275
  )
304
276
  # thresholds = tfim_sampler._maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
277
+
305
278
  n_bias = n_qubits - 1
306
- tot_prob = 2.0
307
- thresholds = np.zeros(n_bias)
308
- for q in range(1, n_qubits // 2):
309
- n = math.comb(n_qubits, q)
310
- thresholds[q - 1] = n
311
- thresholds[n_bias - q] = n
312
- tot_prob += n << 1
279
+ thresholds = np.zeros(n_bias, dtype=np.float64)
280
+ tot_prob = 0
281
+ p = 1.0
313
282
  if n_qubits & 1:
314
283
  q = n_qubits // 2
315
- n = math.comb(n_qubits, q)
316
- thresholds[q - 1] = n
317
- 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
318
292
  thresholds /= tot_prob
319
293
 
320
294
  if IS_CUDA_AVAILABLE and cuda.is_available() and grid_size >= 128:
@@ -336,7 +310,7 @@ def maxcut_tfim(
336
310
  )
337
311
  )
338
312
 
339
- 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)
340
314
 
341
315
  tot_prob = sum(thresholds)
342
316
  thresholds /= tot_prob
@@ -345,22 +319,23 @@ def maxcut_tfim(
345
319
  for i in range(n_bias):
346
320
  tot_prob += thresholds[i]
347
321
  thresholds[i] = tot_prob
348
- thresholds[-1] = 1.0
322
+ thresholds[-1] = 2.0
349
323
  else:
350
324
  maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, thresholds)
351
325
 
352
326
  G_dict = nx.to_dict_of_lists(G)
353
- max_degree = max(len(x) for x in G_dict.values())
354
- adjacency = np.full((len(nodes), max_degree), -1, dtype=np.int32)
355
- for i in range(len(nodes)):
356
- adj = G_dict.get(nodes[i], [])
357
- for j in range(len(adj)):
358
- adjacency[i, j] = adj[j]
359
-
360
327
  J_max = max(J_eff)
361
328
  weights = 1.0 / (1.0 + (J_max - J_eff))
362
- # We only need unique instances
363
- samples = list(set(mask_array_to_python_ints(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))
364
339
 
365
340
  edge_keys = []
366
341
  edge_values = []
@@ -21,7 +21,7 @@ def get_best_stitch(adjacency, terminals_a, terminals_b, is_cyclic):
21
21
  return best_weight, best_edge
22
22
 
23
23
 
24
- def tsp_symmetric(G, quality=None, shots=None, correction_quality=None, is_cyclic=True, start_node=None):
24
+ def tsp_symmetric(G, quality=0, shots=None, correction_quality=2, is_cyclic=True, start_node=None):
25
25
  nodes = list(G.nodes())
26
26
  n_nodes = len(nodes)
27
27
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.14.7
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
@@ -93,13 +93,13 @@ def generate_tsp_graph(n_nodes=64, seed=None):
93
93
 
94
94
  n_nodes = 128
95
95
  G = generate_tsp_graph(n_nodes=n_nodes, seed=42)
96
- circuit, path_length = tsp_symmetric(G, quality=1, is_cyclic=True, start_node=None)
96
+ circuit, path_length = tsp_symmetric(G, quality=0, correction_quality=2, is_cyclic=True, start_node=None)
97
97
 
98
98
  print(f"Node count: {n_nodes}")
99
99
  print(f"Path: {circuit}")
100
100
  print(f"Path length: {path_length}")
101
101
  ```
102
- The (integer) `quality` setting is optional, with a default value of `1`. We only provide a solver for the symmetric version of the TSP (i.e., the distance from "A" to "B" is considered the same as from "B" to "A"). `is_cyclic` controls whether the path must ultimately complete a circuit, if `True`, or a one-way trip, if `False`. When the desired route is acyclic, a fixed starting point can optionally be specified via passing the node label to `start_node` (or, without this parameter, the solver will also attempt to pick an optimal starting point).
102
+ We only provide a solver for the symmetric version of the TSP (i.e., the distance from "A" to "B" is considered the same as from "B" to "A"). `is_cyclic` controls whether the path must ultimately complete a circuit, if `True`, or a one-way trip, if `False`. When the desired route is acyclic, a fixed starting point can optionally be specified via passing the node label to `start_node` (or, without this parameter, the solver will also attempt to pick an optimal starting point).
103
103
 
104
104
  ## About
105
105
  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.
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "PyQrackIsing"
15
- version = "1.14.7"
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.7',
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