PyQrackIsing 6.1.2__tar.gz → 6.2.0__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 (29) hide show
  1. {pyqrackising-6.1.2/pyqrackising.egg-info → pyqrackising-6.2.0}/PKG-INFO +1 -1
  2. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyproject.toml +1 -1
  3. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/__init__.py +1 -0
  4. pyqrackising-6.2.0/pyqrackising/convert_tensor_network_to_tsp.py +96 -0
  5. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/maxcut_tfim.py +6 -6
  6. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/maxcut_tfim_sparse.py +6 -6
  7. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/maxcut_tfim_util.py +9 -2
  8. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/spin_glass_solver.py +2 -1
  9. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/spin_glass_solver_sparse.py +2 -1
  10. {pyqrackising-6.1.2 → pyqrackising-6.2.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  11. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising.egg-info/SOURCES.txt +1 -0
  12. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/setup.py +1 -1
  13. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/CMakeLists.txt +0 -0
  14. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/LICENSE.md +0 -0
  15. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/MANIFEST.in +0 -0
  16. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/README.md +0 -0
  17. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
  18. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/generate_tfim_samples.py +0 -0
  19. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/kernels.cl +0 -0
  20. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  21. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  22. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/spin_glass_solver_util.py +0 -0
  23. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/tfim_magnetization.py +0 -0
  24. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  25. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising/tsp.py +0 -0
  26. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  27. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  28. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/pyqrackising.egg-info/top_level.txt +0 -0
  29. {pyqrackising-6.1.2 → pyqrackising-6.2.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 6.1.2
3
+ Version: 6.2.0
4
4
  Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal 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 = "6.1.2"
15
+ version = "6.2.0"
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"}
@@ -8,3 +8,4 @@ from .spin_glass_solver import spin_glass_solver
8
8
  from .spin_glass_solver_sparse import spin_glass_solver_sparse
9
9
  from .spin_glass_solver_streaming import spin_glass_solver_streaming
10
10
  from .tsp import tsp_symmetric, tsp_asymmetric
11
+ from .convert_tensor_network_to_tsp import convert_tensor_network_to_tsp, convert_quimb_tree_to_tsp
@@ -0,0 +1,96 @@
1
+ # Provided by Elara, the custom OpenAI GPT
2
+ from collections import defaultdict
3
+ import itertools
4
+ import numpy as np
5
+
6
+ def convert_tensor_network_to_tsp(tensors, index_dims, dtype=np.float32):
7
+ """
8
+ Converts a tensor network to a symmetric TSP distance matrix based on pairwise contraction costs.
9
+
10
+ Args:
11
+ tensors: List of dictionaries, each with keys:
12
+ - 'id': Unique tensor identifier (str)
13
+ - 'indices': Set of indices (Set[str])
14
+ index_dims: Dictionary mapping index name to its dimension (e.g., {'i': 2, 'j': 4})
15
+
16
+ Returns:
17
+ A tuple containing:
18
+ - A normalized 2D numpy array representing the symmetric TSP distance matrix.
19
+ - A list of tensor IDs corresponding to the node order in the distance matrix.
20
+ """
21
+ # Step 1: Build inverse map: index -> tensors using it
22
+ index_to_tensors = defaultdict(set)
23
+ for tensor in tensors:
24
+ for idx in tensor["indices"]:
25
+ index_to_tensors[idx].add(tensor["id"])
26
+
27
+ # Step 2: Build graph of neighboring tensors
28
+ graph = defaultdict(set)
29
+ for idx, tensor_ids in index_to_tensors.items():
30
+ for t1, t2 in itertools.combinations(tensor_ids, 2):
31
+ graph[t1].add(t2)
32
+ graph[t2].add(t1)
33
+
34
+ # Step 3: Estimate pairwise contraction costs
35
+ def estimate_cost(t1, t2):
36
+ common = t1["indices"] & t2["indices"]
37
+ if not common:
38
+ return float("inf")
39
+ all_indices = t1["indices"] | t2["indices"]
40
+ return sum(index_dims.get(i, 1) for i in all_indices)
41
+
42
+ max_cost = float("-inf")
43
+ edge_costs = {}
44
+ for t1, neighbors in graph.items():
45
+ for t2 in neighbors:
46
+ key = tuple(sorted([t1, t2]))
47
+ if key not in edge_costs:
48
+ t1_data = next(t for t in tensors if t["id"] == t1)
49
+ t2_data = next(t for t in tensors if t["id"] == t2)
50
+ cost = estimate_cost(t1_data, t2_data)
51
+ edge_costs[key] = cost
52
+ if (cost < float("inf")) and (cost > max_cost):
53
+ max_cost = cost
54
+
55
+ # Step 4: Build distance matrix
56
+ tensor_ids = [t["id"] for t in tensors]
57
+ id_to_node = {tid: i for i, tid in enumerate(tensor_ids)}
58
+ n = len(tensor_ids)
59
+ dist_matrix = np.full((n, n), 1e8, dtype=dtype)
60
+
61
+ for (t1, t2), cost in edge_costs.items():
62
+ if cost == float("inf"):
63
+ continue
64
+ i, j = id_to_node[t1], id_to_node[t2]
65
+ dist_matrix[i, j] = dist_matrix[j, i] = cost / max_cost
66
+
67
+ return dist_matrix, tensor_ids
68
+
69
+
70
+
71
+ def convert_quimb_tree_to_tsp(tn, dtype=np.float32):
72
+ """
73
+ Converts a quimb TensorNetwork to a TSP distance matrix for contraction optimization.
74
+
75
+ Args:
76
+ tn: A quimb.tensor.TensorNetwork instance.
77
+
78
+ Returns:
79
+ - Normalized symmetric distance matrix (2D numpy array of floats)
80
+ - List of tensor IDs (tags) corresponding to the matrix order
81
+ """
82
+ tensors = []
83
+ index_dims = {}
84
+
85
+ for tensor in tn.tensors:
86
+ # Use a unique tag or fallback to name
87
+ tensor_id = sorted(tensor.tags)[0] if tensor.tags else str(tensor.name)
88
+ tensors.append({
89
+ "id": tensor_id,
90
+ "indices": set(tensor.inds)
91
+ })
92
+
93
+ for ind, dim in zip(tensor.inds, tensor.shape):
94
+ index_dims[ind] = dim # capture dimension size
95
+
96
+ return convert_tensor_network_to_tsp(tensors, index_dims, dtype)
@@ -139,8 +139,9 @@ def run_sampling_opencl(G_m_np, thresholds_np, shots, n, is_g_buf_reused):
139
139
  queue = opencl_context.queue
140
140
  kernel = opencl_context.sample_for_solution_best_bitset_kernel
141
141
  dtype = opencl_context.dtype
142
+ wgs = opencl_context.work_group_size
142
143
 
143
- max_local_size = 64 # tune
144
+ max_local_size = wgs # tune
144
145
  max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + max_local_size - 1) // max_local_size) * max_local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
145
146
  global_size = min(((shots + max_local_size - 1) // max_local_size) * max_local_size, max_global_size)
146
147
  local_size = max_local_size
@@ -248,6 +249,7 @@ def maxcut_tfim(
248
249
  is_base_maxcut_gpu=True
249
250
  ):
250
251
  dtype = opencl_context.dtype
252
+ wgs = opencl_context.work_group_size
251
253
  nodes = None
252
254
  n_qubits = 0
253
255
  G_m = None
@@ -284,7 +286,7 @@ def maxcut_tfim(
284
286
  n_steps = n_qubits << quality
285
287
  grid_size = n_steps * n_qubits
286
288
 
287
- if (not is_base_maxcut_gpu) or not (IS_OPENCL_AVAILABLE and grid_size >= 128):
289
+ if (not is_base_maxcut_gpu) or (not IS_OPENCL_AVAILABLE):
288
290
  return cpu_footer(shots, quality, n_qubits, G_m, nodes, dtype)
289
291
 
290
292
  J_eff, degrees = init_J_and_z(G_m, dtype)
@@ -306,7 +308,7 @@ def maxcut_tfim(
306
308
  theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits * dtype().nbytes))
307
309
 
308
310
  # Warp size is 32:
309
- group_size = min(n_qubits, 64)
311
+ group_size = min(wgs, n_qubits)
310
312
  global_size = ((n_qubits + group_size - 1) // group_size) * group_size
311
313
 
312
314
  opencl_context.init_theta_kernel(
@@ -317,9 +319,7 @@ def maxcut_tfim(
317
319
  hamming_prob = init_thresholds(n_qubits, dtype)
318
320
 
319
321
  # Warp size is 32:
320
- group_size = n_qubits - 1
321
- if group_size > 256:
322
- group_size = 256
322
+ group_size = min(wgs, n_qubits - 1)
323
323
  grid_dim = n_steps * n_qubits * group_size
324
324
 
325
325
  # Move to GPU
@@ -186,8 +186,9 @@ def run_sampling_opencl(G_m_csr, thresholds_np, shots, n, is_g_buf_reused):
186
186
  queue = opencl_context.queue
187
187
  kernel = opencl_context.sample_for_solution_best_bitset_sparse_kernel
188
188
  dtype = opencl_context.dtype
189
+ wgs = opencl_context.work_group_size
189
190
 
190
- max_local_size = 64 # tune
191
+ max_local_size = wqs # tune
191
192
  max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + max_local_size - 1) // max_local_size) * max_local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
192
193
  global_size = min(((shots + max_local_size - 1) // max_local_size) * max_local_size, max_global_size)
193
194
  local_size = max_local_size
@@ -313,6 +314,7 @@ def maxcut_tfim_sparse(
313
314
  is_base_maxcut_gpu=True
314
315
  ):
315
316
  dtype = opencl_context.dtype
317
+ wgs = opencl_context.work_group_size
316
318
  nodes = None
317
319
  n_qubits = 0
318
320
  G_m = None
@@ -349,7 +351,7 @@ def maxcut_tfim_sparse(
349
351
  n_steps = n_qubits << quality
350
352
  grid_size = n_steps * n_qubits
351
353
 
352
- if (not is_base_maxcut_gpu) or not (IS_OPENCL_AVAILABLE and grid_size >= 128):
354
+ if (not is_base_maxcut_gpu) or (not IS_OPENCL_AVAILABLE):
353
355
  return cpu_footer(shots, quality, n_qubits, G_m.data, G_m.indptr, G_m.indices, nodes, dtype)
354
356
 
355
357
  J_eff, degrees = init_J_and_z(G_m.data, G_m.indptr, G_m.indices, dtype)
@@ -371,7 +373,7 @@ def maxcut_tfim_sparse(
371
373
  theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits * 4))
372
374
 
373
375
  # Warp size is 32:
374
- group_size = min(n_qubits, 64)
376
+ group_size = min(wgs, n_qubits)
375
377
  global_size = ((n_qubits + group_size - 1) // group_size) * group_size
376
378
 
377
379
  opencl_context.init_theta_kernel(
@@ -382,9 +384,7 @@ def maxcut_tfim_sparse(
382
384
  hamming_prob = init_thresholds(n_qubits, dtype)
383
385
 
384
386
  # Warp size is 32:
385
- group_size = n_qubits - 1
386
- if group_size > 256:
387
- group_size = 256
387
+ group_size = min(wgs, n_qubits - 1)
388
388
  grid_dim = n_steps * n_qubits * group_size
389
389
 
390
390
  # Move to GPU
@@ -146,9 +146,10 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits, dtype):
146
146
 
147
147
 
148
148
  class OpenCLContext:
149
- def __init__(self, p, a, d, c, q, i, m, b, s, k, l):
149
+ def __init__(self, p, a, w, d, c, q, i, m, b, s, k, l):
150
150
  self.MAX_GPU_PROC_ELEM = p
151
151
  self.IS_OPENCL_AVAILABLE = a
152
+ self.work_group_size = w
152
153
  self.dtype = d
153
154
  self.ctx = c
154
155
  self.queue = q
@@ -168,6 +169,7 @@ ctx = None
168
169
  queue = None
169
170
  compute_units = None
170
171
  dtype = np.float32
172
+ work_group_size = 32
171
173
  init_theta_kernel = None
172
174
  maxcut_hamming_cdf_kernel = None
173
175
  bootstrap_kernel = None
@@ -224,8 +226,13 @@ try:
224
226
  bootstrap_sparse_kernel = program.bootstrap_sparse
225
227
  sample_for_solution_best_bitset_kernel = program.sample_for_solution_best_bitset
226
228
  sample_for_solution_best_bitset_sparse_kernel = program.sample_for_solution_best_bitset_sparse
229
+
230
+ work_group_size = bootstrap_kernel.get_work_group_info(
231
+ cl.kernel_work_group_info.PREFERRED_WORK_GROUP_SIZE_MULTIPLE,
232
+ ctx.devices[0]
233
+ )
227
234
  except ImportError:
228
235
  IS_OPENCL_AVAILABLE = False
229
236
  print("PyOpenCL not installed. (If you have any OpenCL accelerator devices with available ICDs, you might want to optionally install pyopencl.)")
230
237
 
231
- opencl_context = OpenCLContext(compute_units, IS_OPENCL_AVAILABLE, dtype, ctx, queue, init_theta_kernel, maxcut_hamming_cdf_kernel, bootstrap_kernel, bootstrap_sparse_kernel, sample_for_solution_best_bitset_kernel, sample_for_solution_best_bitset_sparse_kernel)
238
+ opencl_context = OpenCLContext(compute_units, IS_OPENCL_AVAILABLE, work_group_size, dtype, ctx, queue, init_theta_kernel, maxcut_hamming_cdf_kernel, bootstrap_kernel, bootstrap_sparse_kernel, sample_for_solution_best_bitset_kernel, sample_for_solution_best_bitset_sparse_kernel)
@@ -72,6 +72,7 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
72
72
  queue = opencl_context.queue
73
73
  bootstrap_kernel = opencl_context.bootstrap_kernel
74
74
  dtype = opencl_context.dtype
75
+ wgs = opencl_context.work_group_size
75
76
 
76
77
  n = best_theta.shape[0]
77
78
  combo_count = len(indices_array_np) // k
@@ -95,7 +96,7 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
95
96
  min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
96
97
 
97
98
  # Local memory allocation (1 float per work item)
98
- local_size = min(64, n)
99
+ local_size = min(wgs, n)
99
100
  global_size = ((combo_count + local_size - 1) // local_size) * local_size
100
101
  local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
101
102
  local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
@@ -75,6 +75,7 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
75
75
  queue = opencl_context.queue
76
76
  bootstrap_kernel = opencl_context.bootstrap_sparse_kernel
77
77
  dtype = opencl_context.dtype
78
+ wgs = opencl_context.work_group_size
78
79
 
79
80
  n = best_theta.shape[0]
80
81
  combo_count = len(indices_array_np) // k
@@ -98,7 +99,7 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
98
99
  min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
99
100
 
100
101
  # Local memory allocation (1 float per work item)
101
- local_size = min(64, n)
102
+ local_size = min(wgs, n)
102
103
  global_size = ((combo_count + local_size - 1) // local_size) * local_size
103
104
  local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
104
105
  local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 6.1.2
3
+ Version: 6.2.0
4
4
  Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -6,6 +6,7 @@ pyproject.toml
6
6
  setup.py
7
7
  pyqrackising/__init__.py
8
8
  pyqrackising/_pyqrack_ising.cpp
9
+ pyqrackising/convert_tensor_network_to_tsp.py
9
10
  pyqrackising/generate_tfim_samples.py
10
11
  pyqrackising/kernels.cl
11
12
  pyqrackising/maxcut_tfim.py
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='pyqrackising',
54
- version='6.1.2',
54
+ version='6.2.0',
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