PyQrackIsing 1.14.5__tar.gz → 1.14.7__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.5/PyQrackIsing.egg-info → pyqrackising-1.14.7}/PKG-INFO +1 -1
  2. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/PyQrackIsing/maxcut_tfim.py +79 -42
  3. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/PyQrackIsing/spin_glass_solver.py +5 -3
  4. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/PyQrackIsing/tsp_symmetric.py +1 -1
  5. {pyqrackising-1.14.5 → pyqrackising-1.14.7/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  6. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/README.md +2 -2
  7. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/pyproject.toml +1 -1
  8. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/setup.py +1 -1
  9. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/CMakeLists.txt +0 -0
  10. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/LICENSE.md +0 -0
  11. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/MANIFEST.in +0 -0
  12. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/PyQrackIsing/__init__.py +0 -0
  13. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  14. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/PyQrackIsing/generate_tfim_samples.py +0 -0
  15. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/PyQrackIsing/tfim_magnetization.py +0 -0
  16. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  17. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  18. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  19. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  20. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/PyQrackIsing.egg-info/top_level.txt +0 -0
  21. {pyqrackising-1.14.5 → pyqrackising-1.14.7}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.14.5
3
+ Version: 1.14.7
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
@@ -145,52 +145,92 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
145
145
 
146
146
 
147
147
  # Written by Elara (OpenAI custom GPT)
148
- def local_repulsion_choice(nodes, adjacency, degrees, weights, n, m):
148
+ @njit
149
+ def local_repulsion_choice(adjacency, degrees, weights, n, m):
149
150
  """
150
- Pick m nodes (bit positions) out of n with repulsion bias:
151
+ Pick m nodes out of n with repulsion bias:
151
152
  - High-degree nodes are already less likely
152
153
  - 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,)
153
157
  """
154
158
 
155
- # Base weights: inverse degree
156
- # degrees = np.array([len(adjacency.get(i, [])) for i in range(n)], dtype=np.float64)
157
- # weights = 1.0 / (degrees + 1.0)
158
159
  weights = weights.copy()
159
-
160
- chosen = []
161
- available = set(range(len(nodes)))
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
162
164
 
163
165
  for _ in range(m):
164
- if not available:
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:
165
172
  break
166
173
 
167
- # Normalize weights over remaining nodes
168
- sub_weights = np.array([weights[i] for i in available], dtype=np.float64)
169
- sub_weights /= sub_weights.sum()
170
- sub_nodes = list(available)
171
-
172
- # Sample one node
173
- idx = np.random.choice(len(sub_nodes), p=sub_weights)
174
- node = sub_nodes[idx]
175
- chosen.append(node)
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
176
187
 
177
- # Remove node from available
178
- available.remove(node)
188
+ # Select node
189
+ chosen[chosen_count] = node
190
+ chosen_count += 1
191
+ available[node] = 0
192
+ mask[node] = True
179
193
 
180
194
  # Repulsion: penalize neighbors
181
- for nbr in adjacency.get(nodes[node], []):
182
- idx = nodes.index(nbr)
183
- if idx in available:
184
- weights[idx] *= 0.5 # halve neighbor's weight (tunable!)
185
-
186
- # Build integer mask
187
- mask = 0
188
- for pos in chosen:
189
- mask |= 1 << pos
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
190
200
 
191
201
  return mask
192
202
 
193
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
207
+
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
215
+
216
+ # Second dimension: permutation within Hamming weight
217
+ samples[s, :] = local_repulsion_choice(adjacency, degrees, weights, n, m)
218
+
219
+ return samples
220
+
221
+
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)
231
+
232
+ return samples
233
+
194
234
  def evaluate_cut_edges(samples, edge_keys, edge_values):
195
235
  best_value = float("-inf")
196
236
  best_solution = None
@@ -237,7 +277,7 @@ def maxcut_tfim(
237
277
  shared_size = (n_qubits - 1) * 8
238
278
 
239
279
  if quality is None:
240
- quality = 8
280
+ quality = 10
241
281
 
242
282
  if shots is None:
243
283
  # Number of measurement shots
@@ -308,22 +348,19 @@ def maxcut_tfim(
308
348
  thresholds[-1] = 1.0
309
349
  else:
310
350
  maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, thresholds)
351
+
311
352
  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
+
312
360
  J_max = max(J_eff)
313
361
  weights = 1.0 / (1.0 + (J_max - J_eff))
314
- samples = []
315
- for s in range(shots):
316
- # First dimension: Hamming weight
317
- mag_prob = np.random.random()
318
- m = 0
319
- while thresholds[m] < mag_prob:
320
- m += 1
321
- m += 1
322
- # Second dimension: permutation within Hamming weight
323
- samples.append(local_repulsion_choice(nodes, G_dict, degrees, weights, n_qubits, m))
324
-
325
362
  # We only need unique instances
326
- samples = list(set(samples))
363
+ samples = list(set(mask_array_to_python_ints(local_repulsion_choice_sample(shots, thresholds, adjacency, degrees, weights, n_qubits))))
327
364
 
328
365
  edge_keys = []
329
366
  edge_values = []
@@ -56,7 +56,7 @@ def int_to_bitstring(integer, length):
56
56
  return (bin(integer)[2:].zfill(length))[::-1]
57
57
 
58
58
 
59
- def spin_glass_solver(G, quality=None, shots=None, correction_quality=2, best_guess=None):
59
+ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best_guess=None):
60
60
  nodes = list(G.nodes())
61
61
  n_qubits = len(nodes)
62
62
 
@@ -66,8 +66,10 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=2, best_gu
66
66
  if n_qubits == 1:
67
67
  return "0", 0, ([nodes[0]], [])
68
68
 
69
- if quality is None:
70
- quality = 3
69
+ if correction_quality is None:
70
+ # maxcut_tfim(G) scales roughly like n^4,
71
+ # so its match order of overhead.
72
+ correction_quality = 4
71
73
 
72
74
  bitstring = ""
73
75
  if isinstance(best_guess, str):
@@ -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=2, is_cyclic=True, start_node=None):
24
+ def tsp_symmetric(G, quality=None, shots=None, correction_quality=None, 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.5
3
+ Version: 1.14.7
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
@@ -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 `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.)
52
+ The (integer) `quality` setting is optional, with a default value of `10`, 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
@@ -72,7 +72,7 @@ G = generate_spin_glass_graph(n_nodes=64, seed=42)
72
72
  solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=5, correction_quality=2, best_guess=None)
73
73
  # solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=8)[0])
74
74
  ```
75
- The (integer) `quality` setting is the same as `maxcut_tfim`. `correction_quality` controls an additional convex optimization procedure on top of `best_guess` and defaults to 2. `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). By default, `spin_glass_solver()` uses `maxcut_tfim(G)` with passed-through `quality` as `best_guess`, which typically works well, but it could be seeded with higher `maxcut_tfim()` `quality` or Goemans-Williamson, for example. This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
75
+ The (integer) `quality` setting is the same as `maxcut_tfim`. `correction_quality` controls an additional convex optimization procedure on top of `best_guess` and defaults to 4. `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). By default, `spin_glass_solver()` uses `maxcut_tfim(G)` with passed-through `quality` as `best_guess`, which typically works well, but it could be seeded with higher `maxcut_tfim()` `quality` or Goemans-Williamson, for example. This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
76
76
 
77
77
  From the `spin_glass_solver()`, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
78
78
  ```py
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "PyQrackIsing"
15
- version = "1.14.5"
15
+ version = "1.14.7"
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.5',
54
+ version='1.14.7',
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