PyQrackIsing 5.3.2__tar.gz → 5.4.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 (28) hide show
  1. {pyqrackising-5.3.2/pyqrackising.egg-info → pyqrackising-5.4.0}/PKG-INFO +1 -1
  2. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/README.md +4 -4
  3. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyproject.toml +1 -1
  4. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/maxcut_tfim.py +34 -22
  5. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/maxcut_tfim_sparse.py +37 -24
  6. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/maxcut_tfim_streaming.py +34 -22
  7. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/maxcut_tfim_util.py +3 -30
  8. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/spin_glass_solver.py +25 -32
  9. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/spin_glass_solver_sparse.py +26 -32
  10. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/spin_glass_solver_streaming.py +26 -32
  11. pyqrackising-5.4.0/pyqrackising/spin_glass_solver_util.py +21 -0
  12. {pyqrackising-5.3.2 → pyqrackising-5.4.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  13. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising.egg-info/SOURCES.txt +1 -0
  14. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/setup.py +1 -1
  15. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/CMakeLists.txt +0 -0
  16. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/LICENSE.md +0 -0
  17. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/MANIFEST.in +0 -0
  18. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/__init__.py +0 -0
  19. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
  20. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/generate_tfim_samples.py +0 -0
  21. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/kernels.cl +0 -0
  22. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/tfim_magnetization.py +0 -0
  23. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  24. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising/tsp.py +0 -0
  25. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-5.3.2 → pyqrackising-5.4.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.3.2
3
+ Version: 5.4.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
@@ -50,10 +50,10 @@ from pyqrackising import maxcut_tfim
50
50
  import networkx as nx
51
51
 
52
52
  G = nx.petersen_graph()
53
- best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=8)
53
+ best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=2)
54
54
  ```
55
55
 
56
- We also provide `maxcut_tfim_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs). 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.)
56
+ We also provide `maxcut_tfim_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs). The (integer) `quality` setting is optional, with a default value of `3`, 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.)
57
57
 
58
58
  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:
59
59
  ```py
@@ -73,10 +73,10 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
73
73
 
74
74
 
75
75
  G = generate_spin_glass_graph(n_nodes=64, seed=42)
76
- solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=5, best_guess=None)
76
+ solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=2, best_guess=None)
77
77
  # solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=6)[0])
78
78
  ```
79
- We also provide `spin_glass_solver_sparse(G)`, for `scipy` **upper-triangular** CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `6`. `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.
79
+ We also provide `spin_glass_solver_sparse(G)`, for `scipy` **upper-triangular** CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `3`. `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.
80
80
 
81
81
  From the `spin_glass_solver()`, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
82
82
  ```py
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "pyqrackising"
15
- version = "5.3.2"
15
+ version = "5.4.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"}
@@ -4,7 +4,7 @@ import numpy as np
4
4
  import os
5
5
  from numba import njit, prange
6
6
 
7
- from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, low_width, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
7
+ from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
8
8
 
9
9
  IS_OPENCL_AVAILABLE = True
10
10
  try:
@@ -26,45 +26,46 @@ def local_repulsion_choice(G_m, max_weight, weights, n, m):
26
26
 
27
27
  weights = weights.copy()
28
28
  chosen = np.zeros(m, dtype=np.int32) # store chosen indices
29
- available = np.ones(n, dtype=np.bool_) # True = available, False = not
30
- mask = np.zeros(n, dtype=np.bool_)
29
+ used = np.zeros(n, dtype=np.bool_) # False = available, True = used
31
30
  chosen_count = 0
32
31
 
33
32
  for _ in range(m):
34
33
  # Count available
35
34
  total_w = 0.0
36
35
  for i in range(n):
37
- if available[i]:
38
- total_w += weights[i]
39
- if total_w <= 0:
40
- break
36
+ if used[i]:
37
+ continue
38
+ total_w += weights[i]
41
39
 
42
40
  # Normalize & sample
43
41
  r = np.random.rand()
44
42
  cum = 0.0
45
43
  node = -1
46
44
  for i in range(n):
47
- if available[i]:
48
- cum += weights[i] / total_w
49
- if r < cum:
50
- node = i
51
- break
45
+ if used[i]:
46
+ continue
47
+ cum += weights[i]
48
+ if (total_w * r) < cum:
49
+ node = i
50
+ break
52
51
 
53
52
  if node == -1:
54
- continue
53
+ node = 0
54
+ while used[node]:
55
+ node += 1
55
56
 
56
57
  # Select node
57
58
  chosen[chosen_count] = node
58
59
  chosen_count += 1
59
- available[node] = False
60
- mask[node] = True
60
+ used[node] = True
61
61
 
62
62
  # Repulsion: penalize neighbors
63
63
  for nbr in range(n):
64
- if available[nbr]:
65
- weights[nbr] *= 0.5 ** (G_m[node, nbr] / max_weight) # tunable penalty factor
64
+ if used[nbr]:
65
+ continue
66
+ weights[nbr] *= max(2e-7, 1 - G_m[node, nbr] / max_weight)
66
67
 
67
- return mask
68
+ return used
68
69
 
69
70
 
70
71
  @njit
@@ -81,7 +82,7 @@ def compute_energy(sample, G_m, n_qubits):
81
82
  def sample_for_solution(G_m, shots, thresholds, J_eff):
82
83
  n = len(G_m)
83
84
  max_weight = G_m.max()
84
- weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
85
+ weights = (1.0 / (1.0 + (2 ** -52) - J_eff)).astype(np.float64)
85
86
 
86
87
  solutions = np.empty((shots, n), dtype=np.bool_)
87
88
  energies = np.empty(shots, dtype=np.float32)
@@ -177,16 +178,27 @@ def maxcut_tfim(
177
178
  G_m = G
178
179
 
179
180
  if n_qubits < 3:
180
- return low_width(G_m, nodes, n_qubits)
181
+ if n_qubits == 0:
182
+ return "", 0, ([], [])
183
+
184
+ if n_qubits == 1:
185
+ return "0", 0, (nodes, [])
186
+
187
+ if n_qubits == 2:
188
+ weight = G_m[0, 1]
189
+ if weight < 0.0:
190
+ return "00", 0, (nodes, [])
191
+
192
+ return "01", weight, ([nodes[0]], [nodes[1]])
181
193
 
182
194
  if quality is None:
183
- quality = 8
195
+ quality = 3
184
196
 
185
197
  if shots is None:
186
198
  # Number of measurement shots
187
199
  shots = n_qubits << quality
188
200
 
189
- n_steps = 1 << quality
201
+ n_steps = 2 << quality
190
202
  grid_size = n_steps * n_qubits
191
203
 
192
204
  if not (IS_OPENCL_AVAILABLE and grid_size >= 128):
@@ -3,8 +3,9 @@ import networkx as nx
3
3
  import numpy as np
4
4
  import os
5
5
  from numba import njit, prange
6
+ from scipy.sparse import lil_matrix, csr_matrix
6
7
 
7
- from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, low_width, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
8
+ from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
8
9
 
9
10
  IS_OPENCL_AVAILABLE = True
10
11
  try:
@@ -45,55 +46,56 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, m):
45
46
 
46
47
  weights = weights.copy()
47
48
  chosen = np.zeros(m, dtype=np.int32) # store chosen indices
48
- available = np.ones(n, dtype=np.bool_) # True = available, False = not
49
- mask = np.zeros(n, dtype=np.bool_)
49
+ used = np.zeros(n, dtype=np.bool_) # False = available, True = used
50
50
  chosen_count = 0
51
51
 
52
52
  for _ in range(m):
53
53
  # Count available
54
54
  total_w = 0.0
55
55
  for i in range(n):
56
- if available[i]:
57
- total_w += weights[i]
58
- if total_w <= 0:
59
- break
56
+ if used[i]:
57
+ continue
58
+ total_w += weights[i]
60
59
 
61
60
  # Normalize & sample
62
61
  r = np.random.rand()
63
62
  cum = 0.0
64
63
  node = -1
65
64
  for i in range(n):
66
- if available[i]:
67
- cum += weights[i] / total_w
68
- if r < cum:
69
- node = i
70
- break
65
+ if used[i]:
66
+ continue
67
+ cum += weights[i]
68
+ if (total_w * r) < cum:
69
+ node = i
70
+ break
71
71
 
72
72
  if node == -1:
73
- continue
73
+ node = 0
74
+ while used[node]:
75
+ node += 1
74
76
 
75
77
  # Select node
76
78
  chosen[chosen_count] = node
77
79
  chosen_count += 1
78
- available[node] = False
79
- mask[node] = True
80
+ used[node] = True
80
81
 
81
82
  # Repulsion: penalize neighbors
82
83
  for j in range(G_rows[node], G_rows[node + 1]):
83
84
  nbr = G_cols[j]
84
- if available[nbr]:
85
- weights[nbr] *= 0.5 ** (G_data[j] / max_weight) # tunable penalty factor
85
+ if used[nbr]:
86
+ continue
87
+ weights[nbr] *= max(2e-7, 1 - G_data[j] / max_weight)
86
88
 
87
89
  for nbr in range(node):
88
- if not available[nbr]:
90
+ if used[nbr]:
89
91
  continue
90
92
  start = G_rows[nbr]
91
93
  end = G_rows[nbr + 1]
92
94
  j = binary_search(G_cols[start:end], node) + start
93
95
  if j < end:
94
- weights[nbr] *= 0.5 ** (G_data[j] / max_weight) # tunable penalty factor
96
+ weights[nbr] *= max(2e-7, 1 - G_data[j] / max_weight)
95
97
 
96
- return mask
98
+ return used
97
99
 
98
100
 
99
101
  @njit
@@ -112,7 +114,7 @@ def compute_energy(sample, G_data, G_rows, G_cols):
112
114
  def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds, J_eff):
113
115
  n = G_rows.shape[0] - 1
114
116
  max_weight = G_data.max()
115
- weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
117
+ weights = (1.0 / (1.0 + (2 ** -52) - J_eff)).astype(np.float64)
116
118
 
117
119
  solutions = np.empty((shots, n), dtype=np.bool_)
118
120
  energies = np.empty(shots, dtype=np.float32)
@@ -235,16 +237,27 @@ def maxcut_tfim_sparse(
235
237
  G_m = G
236
238
 
237
239
  if n_qubits < 3:
238
- return low_width(G_m, nodes, n_qubits)
240
+ if n_qubits == 0:
241
+ return "", 0, ([], [])
242
+
243
+ if n_qubits == 1:
244
+ return "0", 0, (nodes, [])
245
+
246
+ if n_qubits == 2:
247
+ weight = G_m[0, 1]
248
+ if weight < 0.0:
249
+ return "00", 0, (nodes, [])
250
+
251
+ return "01", weight, ([nodes[0]], [nodes[1]])
239
252
 
240
253
  if quality is None:
241
- quality = 8
254
+ quality = 3
242
255
 
243
256
  if shots is None:
244
257
  # Number of measurement shots
245
258
  shots = n_qubits << quality
246
259
 
247
- n_steps = 1 << quality
260
+ n_steps = 2 << quality
248
261
  grid_size = n_steps * n_qubits
249
262
 
250
263
  if not (IS_OPENCL_AVAILABLE and grid_size >= 128):
@@ -4,7 +4,7 @@ import numpy as np
4
4
  import os
5
5
  from numba import njit, prange
6
6
 
7
- from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, low_width, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
7
+ from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
8
8
 
9
9
  IS_OPENCL_AVAILABLE = True
10
10
  try:
@@ -26,45 +26,46 @@ def local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights
26
26
 
27
27
  weights = weights.copy()
28
28
  chosen = np.zeros(m, dtype=np.int32) # store chosen indices
29
- available = np.ones(n, dtype=np.bool_) # True = available, False = not
30
- mask = np.zeros(n, dtype=np.bool_)
29
+ used = np.zeros(n, dtype=np.bool_) # False = available, True = used
31
30
  chosen_count = 0
32
31
 
33
32
  for _ in range(m):
34
33
  # Count available
35
34
  total_w = 0.0
36
35
  for i in range(n):
37
- if available[i]:
38
- total_w += weights[i]
39
- if total_w <= 0:
40
- break
36
+ if used[i]:
37
+ continue
38
+ total_w += weights[i]
41
39
 
42
40
  # Normalize & sample
43
41
  r = np.random.rand()
44
42
  cum = 0.0
45
43
  node = -1
46
44
  for i in range(n):
47
- if available[i]:
48
- cum += weights[i] / total_w
49
- if r < cum:
50
- node = i
51
- break
45
+ if used[i]:
46
+ continue
47
+ cum += weights[i]
48
+ if (total_w * r) < cum:
49
+ node = i
50
+ break
52
51
 
53
52
  if node == -1:
54
- continue
53
+ node = 0
54
+ while used[node]:
55
+ node += 1
55
56
 
56
57
  # Select node
57
58
  chosen[chosen_count] = node
58
59
  chosen_count += 1
59
- available[node] = False
60
- mask[node] = True
60
+ used[node] = True
61
61
 
62
62
  # Repulsion: penalize neighbors
63
63
  for nbr in range(n):
64
- if available[nbr]:
65
- weights[nbr] *= 0.5 ** (G_func((nodes[node], nodes[nbr]), G_func_args_tuple) / max_weight) # tunable penalty factor
64
+ if used[nbr]:
65
+ continue
66
+ weights[nbr] *= max(2e-7, 1 - G_func((nodes[node], nodes[nbr]), G_func_args_tuple) / max_weight)
66
67
 
67
- return mask
68
+ return used
68
69
 
69
70
 
70
71
  @njit
@@ -79,7 +80,7 @@ def compute_energy(sample, G_func, G_func_args_tuple, nodes, n_qubits):
79
80
 
80
81
  @njit(parallel=True)
81
82
  def sample_for_solution(G_func, G_func_args_tuple, nodes, max_weight, shots, thresholds, degrees_sum, J_eff, n):
82
- weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
83
+ weights = (1.0 / (1.0 + (2 ** -52) - J_eff)).astype(np.float64)
83
84
 
84
85
  solutions = np.empty((shots, n), dtype=np.bool_)
85
86
  energies = np.empty(shots, dtype=np.float32)
@@ -175,16 +176,27 @@ def maxcut_tfim_streaming(
175
176
  n_qubits = len(nodes)
176
177
 
177
178
  if n_qubits < 3:
178
- return low_width(G_m, nodes, n_qubits)
179
+ if n_qubits == 0:
180
+ return "", 0, ([], [])
181
+
182
+ if n_qubits == 1:
183
+ return "0", 0, (nodes, [])
184
+
185
+ if n_qubits == 2:
186
+ weight = G_func((nodes[0], nodes[1]), G_func_args_tuple)
187
+ if weight < 0.0:
188
+ return "00", 0, (nodes, [])
189
+
190
+ return "01", weight, ([nodes[0]], [nodes[1]])
179
191
 
180
192
  if quality is None:
181
- quality = 8
193
+ quality = 3
182
194
 
183
195
  if shots is None:
184
196
  # Number of measurement shots
185
197
  shots = n_qubits << quality
186
198
 
187
- n_steps = 1 << quality
199
+ n_steps = 2 << quality
188
200
  grid_size = n_steps * n_qubits
189
201
 
190
202
  if not (IS_OPENCL_AVAILABLE and grid_size >= 128):
@@ -23,6 +23,7 @@ def get_cut(solution, nodes):
23
23
  @njit(parallel=True)
24
24
  def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
25
25
  theta = np.empty(n_qubits, dtype=np.float32)
26
+ h_mult = abs(h_mult)
26
27
  for q in prange(n_qubits):
27
28
  J = J_eff[q]
28
29
  z = degrees[q]
@@ -31,7 +32,7 @@ def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
31
32
  -1.0,
32
33
  min(
33
34
  1.0,
34
- (1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
35
+ np.sign(J) if np.isclose(abs(z * J), 0.0) else (h_mult / (z * J)),
35
36
  ),
36
37
  )
37
38
  )
@@ -60,22 +61,6 @@ def init_thresholds(n_qubits):
60
61
  return thresholds
61
62
 
62
63
 
63
- @njit
64
- def low_width(G_m, nodes, n_qubits):
65
- if n_qubits == 0:
66
- return "", 0, ([], [])
67
-
68
- if n_qubits == 1:
69
- return "0", 0, (nodes, [])
70
-
71
- if n_qubits == 2:
72
- weight = G_m[0, 1]
73
- if weight < 0.0:
74
- return "00", 0, (nodes, [])
75
-
76
- return "01", weight, ([nodes[0]], [nodes[1]])
77
-
78
-
79
64
  @njit(parallel=True)
80
65
  def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
81
66
  if n_qubits < 2:
@@ -88,19 +73,7 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
88
73
  h_mult = 2.0 / tot_t
89
74
  n_bias = n_qubits - 1
90
75
 
91
- theta = np.empty(n_qubits, dtype=np.float32)
92
- for q in range(n_qubits):
93
- J = J_func[q]
94
- z = degrees[q]
95
- theta[q] = np.arcsin(
96
- max(
97
- -1.0,
98
- min(
99
- 1.0,
100
- (1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
101
- ),
102
- )
103
- )
76
+ theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_func, degrees)
104
77
 
105
78
  for qc in prange(n_qubits, n_steps * n_qubits):
106
79
  step = qc // n_qubits
@@ -1,4 +1,5 @@
1
1
  from .maxcut_tfim import maxcut_tfim
2
+ from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
2
3
  import itertools
3
4
  import networkx as nx
4
5
  import numpy as np
@@ -24,8 +25,7 @@ def compute_energy(theta_bits, G_m):
24
25
  energy = 0
25
26
  for u in range(n_qubits):
26
27
  for v in range(u + 1, n_qubits):
27
- eigen = 1 if theta_bits[u] == theta_bits[v] else -1
28
- energy += G_m[u, v] * eigen
28
+ energy += G_m[u, v] if theta_bits[u] == theta_bits[v] else -G_m[u, v]
29
29
 
30
30
  return energy
31
31
 
@@ -41,19 +41,22 @@ def bootstrap_worker(theta, G_m, indices):
41
41
 
42
42
 
43
43
  @njit(parallel=True)
44
- def bootstrap(theta, G_m, k, indices_array):
45
- n = theta.shape[0]
44
+ def bootstrap(best_theta, G_m, k, indices_array, min_energy):
45
+ n = best_theta.shape[0]
46
46
  energies = np.empty(n, dtype=np.float32)
47
47
  for i in prange(n):
48
48
  j = i * k
49
- energies[i] = bootstrap_worker(theta, G_m, indices_array[j : j + k])
49
+ energies[i] = bootstrap_worker(best_theta, G_m, indices_array[j : j + k])
50
50
 
51
- return energies
51
+ energy = energies.min()
52
+ if energy < min_energy:
53
+ index_match = np.random.choice(np.where(energies == energy)[0])
54
+ indices = indices_array[(index_match * k) : ((index_match + 1) * k)]
55
+ min_energy = energy
56
+ for i in indices:
57
+ best_theta[i] = not best_theta[i]
52
58
 
53
-
54
- # By Gemini (Google Search AI)
55
- def int_to_bitstring(integer, length):
56
- return (bin(integer)[2:].zfill(length))[::-1]
59
+ return min_energy
57
60
 
58
61
 
59
62
  def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
@@ -84,7 +87,7 @@ def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
84
87
  return "01", weight, ([nodes[0]], [nodes[1]]), -weight
85
88
 
86
89
  if quality is None:
87
- quality = 6
90
+ quality = 3
88
91
 
89
92
  bitstring = ""
90
93
  if isinstance(best_guess, str):
@@ -100,6 +103,7 @@ def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
100
103
  min_energy = compute_energy(best_theta, G_m)
101
104
  improved = True
102
105
  correction_quality = 1
106
+ combos_list = []
103
107
  while improved:
104
108
  improved = False
105
109
  k = 1
@@ -107,20 +111,19 @@ def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
107
111
  if n_qubits < k:
108
112
  break
109
113
 
110
- theta = best_theta.copy()
114
+ combos = []
115
+ if len(combos_list) < k:
116
+ combos = list(
117
+ item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
118
+ )
119
+ combos_list.append(combos)
120
+ else:
121
+ combos = combos_list[k - 1]
111
122
 
112
- combos = list(
113
- item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
114
- )
115
- energies = bootstrap(theta, G_m, k, combos)
123
+ energy = bootstrap(best_theta, G_m, k, combos, min_energy)
116
124
 
117
- energy = energies.min()
118
125
  if energy < min_energy:
119
- index_match = np.random.choice(np.where(energies == energy)[0])
120
- indices = combos[(index_match * k) : ((index_match + 1) * k)]
121
126
  min_energy = energy
122
- for i in indices:
123
- best_theta[i] = not best_theta[i]
124
127
  improved = True
125
128
  if correction_quality < (k + 1):
126
129
  correction_quality = k + 1
@@ -128,17 +131,7 @@ def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
128
131
 
129
132
  k = k + 1
130
133
 
131
- bitstring = ""
132
- l, r = [], []
133
- for i in range(len(best_theta)):
134
- b = best_theta[i]
135
- if b:
136
- bitstring += "1"
137
- r.append(nodes[i])
138
- else:
139
- bitstring += "0"
140
- l.append(nodes[i])
141
-
134
+ bitstring, l, r = get_cut_from_bit_array(best_theta, nodes)
142
135
  cut_value = evaluate_cut_edges(best_theta, G_m)
143
136
 
144
137
  return bitstring, float(cut_value), (l, r), float(min_energy)
@@ -1,4 +1,5 @@
1
1
  from .maxcut_tfim_sparse import maxcut_tfim_sparse
2
+ from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
2
3
  import itertools
3
4
  import networkx as nx
4
5
  import numpy as np
@@ -27,7 +28,7 @@ def compute_energy(sample, G_data, G_rows, G_cols):
27
28
  for u in range(n_qubits):
28
29
  for col in range(G_rows[u], G_rows[u + 1]):
29
30
  v = G_cols[col]
30
- energy += G_data[col] * (1 if sample[u] == sample[v] else -1)
31
+ energy += (G_data[col] if sample[u] == sample[v] else -G_data[col])
31
32
 
32
33
  return energy
33
34
 
@@ -43,14 +44,22 @@ def bootstrap_worker(theta, G_data, G_rows, G_cols, indices):
43
44
 
44
45
 
45
46
  @njit(parallel=True)
46
- def bootstrap(theta, G_data, G_rows, G_cols, k, indices_array):
47
- n = theta.shape[0]
47
+ def bootstrap(best_theta, G_data, G_rows, G_cols, k, indices_array, min_energy):
48
+ n = best_theta.shape[0]
48
49
  energies = np.empty(n, dtype=np.float32)
49
50
  for i in prange(n):
50
51
  j = i * k
51
- energies[i] = bootstrap_worker(theta, G_data, G_rows, G_cols, indices_array[j : j + k])
52
+ energies[i] = bootstrap_worker(best_theta, G_data, G_rows, G_cols, indices_array[j : j + k])
52
53
 
53
- return energies
54
+ energy = energies.min()
55
+ if energy < min_energy:
56
+ index_match = np.random.choice(np.where(energies == energy)[0])
57
+ indices = indices_array[(index_match * k) : ((index_match + 1) * k)]
58
+ min_energy = energy
59
+ for i in indices:
60
+ best_theta[i] = not best_theta[i]
61
+
62
+ return min_energy
54
63
 
55
64
 
56
65
  def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
@@ -65,11 +74,6 @@ def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
65
74
  return lil.tocsr()
66
75
 
67
76
 
68
- # By Gemini (Google Search AI)
69
- def int_to_bitstring(integer, length):
70
- return (bin(integer)[2:].zfill(length))[::-1]
71
-
72
-
73
77
  def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
74
78
  nodes = None
75
79
  n_qubits = 0
@@ -98,7 +102,7 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
98
102
  return "01", weight, ([nodes[0]], [nodes[1]]), -weight
99
103
 
100
104
  if quality is None:
101
- quuality = 6
105
+ quuality = 3
102
106
 
103
107
  bitstring = ""
104
108
  if isinstance(best_guess, str):
@@ -114,6 +118,7 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
114
118
  min_energy = compute_energy(best_theta, G_m.data, G_m.indptr, G_m.indices)
115
119
  improved = True
116
120
  correction_quality = 1
121
+ combos_list = []
117
122
  while improved:
118
123
  improved = False
119
124
  k = 1
@@ -121,20 +126,19 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
121
126
  if n_qubits < k:
122
127
  break
123
128
 
124
- theta = best_theta.copy()
129
+ combos = []
130
+ if len(combos_list) < k:
131
+ combos = list(
132
+ item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
133
+ )
134
+ combos_list.append(combos)
135
+ else:
136
+ combos = combos_list[k - 1]
125
137
 
126
- combos = list(
127
- item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
128
- )
129
- energies = bootstrap(theta, G_m.data, G_m.indptr, G_m.indices, k, combos)
138
+ energy = bootstrap(best_theta, G_m.data, G_m.indptr, G_m.indices, k, combos, min_energy)
130
139
 
131
- energy = energies.min()
132
140
  if energy < min_energy:
133
- index_match = np.random.choice(np.where(energies == energy)[0])
134
- indices = combos[(index_match * k) : ((index_match + 1) * k)]
135
141
  min_energy = energy
136
- for i in indices:
137
- best_theta[i] = not best_theta[i]
138
142
  improved = True
139
143
  if correction_quality < (k + 1):
140
144
  correction_quality = k + 1
@@ -142,17 +146,7 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
142
146
 
143
147
  k = k + 1
144
148
 
145
- bitstring = ""
146
- l, r = [], []
147
- for i in range(len(best_theta)):
148
- b = best_theta[i]
149
- if b:
150
- bitstring += "1"
151
- r.append(nodes[i])
152
- else:
153
- bitstring += "0"
154
- l.append(nodes[i])
155
-
149
+ bitstring, l, r = get_cut_from_bit_array(best_theta, nodes)
156
150
  cut_value = evaluate_cut_edges(best_theta, G_m.data, G_m.indptr, G_m.indices)
157
151
 
158
152
  return bitstring, float(cut_value), (l, r), float(min_energy)
@@ -1,4 +1,5 @@
1
1
  from .maxcut_tfim_streaming import maxcut_tfim_streaming
2
+ from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
2
3
  import itertools
3
4
  import networkx as nx
4
5
  import numpy as np
@@ -24,8 +25,8 @@ def compute_energy(theta_bits, G_func, G_func_args_tuple, nodes):
24
25
  energy = 0
25
26
  for u in range(n_qubits):
26
27
  for v in range(u + 1, n_qubits):
27
- eigen = 1 if theta_bits[u] == theta_bits[v] else -1
28
- energy += G_func((nodes[u], nodes[v]), G_func_args_tuple) * eigen
28
+ val = G_func((nodes[u], nodes[v]), G_func_args_tuple)
29
+ energy += (val if theta_bits[u] == theta_bits[v] else -val)
29
30
 
30
31
  return energy
31
32
 
@@ -41,19 +42,22 @@ def bootstrap_worker(theta, G_func, G_func_args_tuple, nodes, indices):
41
42
 
42
43
 
43
44
  @njit(parallel=True)
44
- def bootstrap(theta, G_func, G_func_args_tuple, nodes, k, indices_array):
45
- n = theta.shape[0]
45
+ def bootstrap(best_theta, G_func, G_func_args_tuple, nodes, k, indices_array, min_energy):
46
+ n = best_theta.shape[0]
46
47
  energies = np.empty(n, dtype=np.float32)
47
48
  for i in prange(n):
48
49
  j = i * k
49
- energies[i] = bootstrap_worker(theta, G_func, G_func_args_tuple, nodes, indices_array[j : j + k])
50
+ energies[i] = bootstrap_worker(best_theta, G_func, G_func_args_tuple, nodes, indices_array[j : j + k])
50
51
 
51
- return energies
52
+ energy = energies.min()
53
+ if energy < min_energy:
54
+ index_match = np.random.choice(np.where(energies == energy)[0])
55
+ indices = indices_array[(index_match * k) : ((index_match + 1) * k)]
56
+ min_energy = energy
57
+ for i in indices:
58
+ best_theta[i] = not best_theta[i]
52
59
 
53
-
54
- # By Gemini (Google Search AI)
55
- def int_to_bitstring(integer, length):
56
- return (bin(integer)[2:].zfill(length))[::-1]
60
+ return min_energy
57
61
 
58
62
 
59
63
  def spin_glass_solver_streaming(
@@ -81,7 +85,7 @@ def spin_glass_solver_streaming(
81
85
  return "01", weight, ([nodes[0]], [nodes[1]]), -weight
82
86
 
83
87
  if quality is None:
84
- quality = 6
88
+ quality = 3
85
89
 
86
90
  bitstring = ""
87
91
  if isinstance(best_guess, str):
@@ -97,6 +101,7 @@ def spin_glass_solver_streaming(
97
101
  min_energy = compute_energy(best_theta, G_func, G_func_args_tuple, nodes)
98
102
  improved = True
99
103
  correction_quality = 1
104
+ combos_list = []
100
105
  while improved:
101
106
  improved = False
102
107
  k = 1
@@ -104,20 +109,19 @@ def spin_glass_solver_streaming(
104
109
  if n_qubits < k:
105
110
  break
106
111
 
107
- theta = best_theta.copy()
112
+ combos = []
113
+ if len(combos_list) < k:
114
+ combos = list(
115
+ item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
116
+ )
117
+ combos_list.append(combos)
118
+ else:
119
+ combos = combos_list[k - 1]
108
120
 
109
- combos = list(
110
- item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
111
- )
112
- energies = bootstrap(theta, G_func, G_func_args_tuple, nodes, k, combos)
121
+ energy = bootstrap(best_theta, G_func, G_func_args_tuple, nodes, k, combos, min_energy)
113
122
 
114
- energy = energies.min()
115
123
  if energy < min_energy:
116
- index_match = np.random.choice(np.where(energies == energy)[0])
117
- indices = combos[(index_match * k) : ((index_match + 1) * k)]
118
124
  min_energy = energy
119
- for i in indices:
120
- best_theta[i] = not best_theta[i]
121
125
  improved = True
122
126
  if correction_quality < (k + 1):
123
127
  correction_quality = k + 1
@@ -125,17 +129,7 @@ def spin_glass_solver_streaming(
125
129
 
126
130
  k = k + 1
127
131
 
128
- bitstring = ""
129
- l, r = [], []
130
- for i in range(len(best_theta)):
131
- b = best_theta[i]
132
- if b:
133
- bitstring += "1"
134
- r.append(nodes[i])
135
- else:
136
- bitstring += "0"
137
- l.append(nodes[i])
138
-
132
+ bitstring, l, r = get_cut_from_bit_array(best_theta, nodes)
139
133
  cut_value = evaluate_cut_edges(best_theta, G_func, G_func_args_tuple, nodes)
140
134
 
141
135
  return bitstring, float(cut_value), (l, r), float(min_energy)
@@ -0,0 +1,21 @@
1
+ from numba import njit
2
+
3
+
4
+ @njit
5
+ def get_cut_from_bit_array(theta, nodes):
6
+ bitstring = ""
7
+ l, r = [], []
8
+ for i in range(len(theta)):
9
+ b = theta[i]
10
+ if b:
11
+ bitstring += "1"
12
+ r.append(nodes[i])
13
+ else:
14
+ bitstring += "0"
15
+ l.append(nodes[i])
16
+
17
+ return bitstring, l, r
18
+
19
+ # By Gemini (Google Search AI)
20
+ def int_to_bitstring(integer, length):
21
+ return (bin(integer)[2:].zfill(length))[::-1]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.3.2
3
+ Version: 5.4.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
@@ -15,6 +15,7 @@ pyqrackising/maxcut_tfim_util.py
15
15
  pyqrackising/spin_glass_solver.py
16
16
  pyqrackising/spin_glass_solver_sparse.py
17
17
  pyqrackising/spin_glass_solver_streaming.py
18
+ pyqrackising/spin_glass_solver_util.py
18
19
  pyqrackising/tfim_magnetization.py
19
20
  pyqrackising/tfim_square_magnetization.py
20
21
  pyqrackising/tsp.py
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='pyqrackising',
54
- version='5.3.2',
54
+ version='5.4.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