PyQrackIsing 9.0.1__tar.gz → 9.1.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 (27) hide show
  1. {pyqrackising-9.0.1/pyqrackising.egg-info → pyqrackising-9.1.0}/PKG-INFO +1 -1
  2. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/README.md +2 -2
  3. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyproject.toml +1 -1
  4. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/maxcut_tfim.py +39 -23
  5. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/maxcut_tfim_sparse.py +40 -24
  6. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/maxcut_tfim_streaming.py +39 -23
  7. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/maxcut_tfim_util.py +2 -5
  8. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/spin_glass_solver.py +47 -8
  9. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/spin_glass_solver_sparse.py +47 -8
  10. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/spin_glass_solver_streaming.py +48 -8
  11. {pyqrackising-9.0.1 → pyqrackising-9.1.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  12. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/setup.py +1 -1
  13. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/LICENSE.md +0 -0
  14. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/MANIFEST.in +0 -0
  15. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/__init__.py +0 -0
  16. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  17. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/generate_tfim_samples.py +0 -0
  18. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/kernels.cl +0 -0
  19. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/tfim_magnetization.py +0 -0
  20. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  21. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/tsp.py +0 -0
  22. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising/tsp_maxcut.py +0 -0
  23. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
  24. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  25. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  26. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/pyqrackising.egg-info/top_level.txt +0 -0
  27. {pyqrackising-9.0.1 → pyqrackising-9.1.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.0.1
3
+ Version: 9.1.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
@@ -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=6, shots=None, anneal_t=8.0, anneal_h=8.0, is_spin_glass=True, best_guess=None, is_maxcut_gpu=True)
76
+ solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=6, shots=None, anneal_t=8.0, anneal_h=8.0, is_spin_glass=True, best_guess=None, is_maxcut_gpu=True, gray_iterations=None, gray_seed_multiple=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)` and `spin_glass_solver_streaming(G_func, nodes)`. `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. `is_spin_glass` controls whether the solver optimizes for cut value or spin-glass energy. 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)` and `spin_glass_solver_streaming(G_func, nodes)`. `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. `is_spin_glass` controls whether the solver optimizes for cut value or spin-glass energy. 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. `gray_iterations` gives manual control over how many iterations are carried out of a parallel Gray-code search on `best_guess`. `gray_seed_multiple` controls how many parallel search seeds (as a multiple of your CPU thread count) are tested for the best parallel seeds, and a value of `1` will perfectly cover the search space without collision if your node count is a power of 2.
80
80
 
81
81
  From the MAXCUT solvers, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
82
82
  ```py
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
10
10
 
11
11
  [project]
12
12
  name = "pyqrackising"
13
- version = "9.0.1"
13
+ version = "9.1.0"
14
14
  requires-python = ">=3.8"
15
15
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
16
16
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -1,6 +1,7 @@
1
1
  import networkx as nx
2
2
  import numpy as np
3
3
  from numba import njit, prange
4
+ import os
4
5
 
5
6
  from .maxcut_tfim_util import compute_cut, compute_energy, convert_bool_to_uint, get_cut, get_cut_base, make_G_m_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick
6
7
 
@@ -59,13 +60,14 @@ def local_repulsion_choice(G_m, repulsion_base, n, m, s):
59
60
 
60
61
 
61
62
  @njit(parallel=True)
62
- def sample_measurement(G_m, shots, thresholds, repulsion_base, is_spin_glass):
63
- shots = max(1, shots >> 1)
63
+ def sample_measurement(G_m, shots, thread_count, thresholds, repulsion_base, is_spin_glass):
64
+ shot_segment = (max(1, shots >> 1) + thread_count - 1) // thread_count
65
+ shots = shot_segment * thread_count
64
66
  n = len(G_m)
65
67
 
66
- solutions = np.empty((shots, n), dtype=np.bool_)
67
- energies = np.empty(shots, dtype=dtype)
68
-
68
+ solutions = np.empty((thread_count, n), dtype=np.bool_)
69
+ energies = np.full(thread_count, np.finfo(dtype).min, dtype=dtype)
70
+
69
71
  best_solution = solutions[0]
70
72
  best_energy = -float("inf")
71
73
 
@@ -73,23 +75,35 @@ def sample_measurement(G_m, shots, thresholds, repulsion_base, is_spin_glass):
73
75
  while improved:
74
76
  improved = False
75
77
  if is_spin_glass:
76
- for s in prange(shots):
77
- # First dimension: Hamming weight
78
- m = sample_mag(thresholds)
79
-
80
- # Second dimension: permutation within Hamming weight
81
- sample = local_repulsion_choice(G_m, repulsion_base, n, m, s)
82
- solutions[s] = sample
83
- energies[s] = compute_energy(sample, G_m, n)
78
+ for i in prange(thread_count):
79
+ s_offset = i * shot_segment
80
+ for j in range(shot_segment):
81
+ s = s_offset + j
82
+
83
+ # First dimension: Hamming weight
84
+ m = sample_mag(thresholds)
85
+
86
+ # Second dimension: permutation within Hamming weight
87
+ sample = local_repulsion_choice(G_m, repulsion_base, n, m, s)
88
+ energy = compute_energy(sample, G_m, n)
89
+
90
+ if energy > energies[i]:
91
+ solutions[i], energies[i] = sample, energy
84
92
  else:
85
- for s in prange(shots):
86
- # First dimension: Hamming weight
87
- m = sample_mag(thresholds)
93
+ for i in prange(thread_count):
94
+ s_offset = i * shot_segment
95
+ for j in range(shot_segment):
96
+ s = s_offset + j
97
+
98
+ # First dimension: Hamming weight
99
+ m = sample_mag(thresholds)
88
100
 
89
- # Second dimension: permutation within Hamming weight
90
- sample = local_repulsion_choice(G_m, repulsion_base, n, m, s)
91
- solutions[s] = sample
92
- energies[s] = compute_cut(sample, G_m, n)
101
+ # Second dimension: permutation within Hamming weight
102
+ sample = local_repulsion_choice(G_m, repulsion_base, n, m, s)
103
+ energy = compute_cut(sample, G_m, n)
104
+
105
+ if energy > energies[i]:
106
+ solutions[i], energies[i] = sample, energy
93
107
 
94
108
  best_index = np.argmax(energies)
95
109
  energy = energies[best_index]
@@ -164,14 +178,14 @@ def init_J_and_z(G_m, repulsion_base):
164
178
 
165
179
 
166
180
  @njit
167
- def cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
181
+ def cpu_footer(shots, thread_count, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
168
182
  J_eff, degrees = init_J_and_z(G_m, repulsion_base)
169
183
  hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
170
184
 
171
185
  degrees = None
172
186
  J_eff = None
173
187
 
174
- best_solution, best_value = sample_measurement(G_m, shots, hamming_prob, repulsion_base, is_spin_glass)
188
+ best_solution, best_value = sample_measurement(G_m, shots, thread_count, hamming_prob, repulsion_base, is_spin_glass)
175
189
 
176
190
  bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
177
191
 
@@ -297,7 +311,9 @@ def maxcut_tfim(
297
311
  is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE
298
312
 
299
313
  if not is_opencl:
300
- bit_string, best_value, partition = cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
314
+ thread_count = os.cpu_count() ** 2
315
+
316
+ bit_string, best_value, partition = cpu_footer(shots, thread_count, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
301
317
 
302
318
  if best_value < 0.0:
303
319
  # Best cut is trivial partition, all/empty
@@ -1,6 +1,7 @@
1
1
  import networkx as nx
2
2
  import numpy as np
3
3
  from numba import njit, prange
4
+ import os
4
5
 
5
6
  from .maxcut_tfim_util import binary_search, compute_cut_sparse, compute_energy_sparse, convert_bool_to_uint, get_cut, get_cut_base, make_G_m_csr_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick, to_scipy_sparse_upper_triangular
6
7
 
@@ -69,13 +70,14 @@ def local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s):
69
70
 
70
71
 
71
72
  @njit(parallel=True)
72
- def sample_measurement(G_data, G_rows, G_cols, shots, thresholds, repulsion_base, is_spin_glass):
73
- shots = max(1, shots >> 1)
73
+ def sample_measurement(G_data, G_rows, G_cols, shots, thread_count, thresholds, repulsion_base, is_spin_glass):
74
+ shot_segment = (max(1, shots >> 1) + thread_count - 1) // thread_count
75
+ shots = shot_segment * thread_count
74
76
  n = G_rows.shape[0] - 1
75
77
 
76
- solutions = np.empty((shots, n), dtype=np.bool_)
77
- energies = np.empty(shots, dtype=dtype)
78
-
78
+ solutions = np.empty((thread_count, n), dtype=np.bool_)
79
+ energies = np.full(thread_count, np.finfo(dtype).min, dtype=dtype)
80
+
79
81
  best_solution = solutions[0]
80
82
  best_energy = -float("inf")
81
83
 
@@ -83,23 +85,35 @@ def sample_measurement(G_data, G_rows, G_cols, shots, thresholds, repulsion_base
83
85
  while improved:
84
86
  improved = False
85
87
  if is_spin_glass:
86
- for s in prange(shots):
87
- # First dimension: Hamming weight
88
- m = sample_mag(thresholds)
89
-
90
- # Second dimension: permutation within Hamming weight
91
- sample = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
92
- solutions[s] = sample
93
- energies[s] = compute_energy_sparse(sample, G_data, G_rows, G_cols, n)
88
+ for i in prange(thread_count):
89
+ s_offset = i * shot_segment
90
+ for j in range(shot_segment):
91
+ s = s_offset + j
92
+
93
+ # First dimension: Hamming weight
94
+ m = sample_mag(thresholds)
95
+
96
+ # Second dimension: permutation within Hamming weight
97
+ sample = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
98
+ energy = compute_energy_sparse(sample, G_data, G_rows, G_cols, n)
99
+
100
+ if energy > energies[i]:
101
+ solutions[i], energies[i] = sample, energy
94
102
  else:
95
- for s in prange(shots):
96
- # First dimension: Hamming weight
97
- m = sample_mag(thresholds)
103
+ for i in prange(thread_count):
104
+ s_offset = i * shot_segment
105
+ for j in range(shot_segment):
106
+ s = s_offset + j
107
+
108
+ # First dimension: Hamming weight
109
+ m = sample_mag(thresholds)
98
110
 
99
- # Second dimension: permutation within Hamming weight
100
- sample = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
101
- solutions[s] = sample
102
- energies[s] = compute_cut_sparse(sample, G_data, G_rows, G_cols, n)
111
+ # Second dimension: permutation within Hamming weight
112
+ sample = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
113
+ energy = compute_cut_sparse(sample, G_data, G_rows, G_cols, n)
114
+
115
+ if energy > energies[i]:
116
+ solutions[i], energies[i] = sample, energy
103
117
 
104
118
  best_index = np.argmax(energies)
105
119
  energy = energies[best_index]
@@ -109,7 +123,7 @@ def sample_measurement(G_data, G_rows, G_cols, shots, thresholds, repulsion_base
109
123
  improved = True
110
124
 
111
125
  if is_spin_glass:
112
- best_energy = compute_cut_sparse(best_solution, G_data, G_rows, G_cols, n)
126
+ best_energy = compute_cut_sparse(sample, G_data, G_rows, G_cols, n)
113
127
 
114
128
  return best_solution, best_energy
115
129
 
@@ -174,10 +188,10 @@ def init_J_and_z(G_m, repulsion_base):
174
188
 
175
189
 
176
190
  @njit
177
- def cpu_footer(J_eff, degrees, shots, quality, n_qubits, G_data, G_rows, G_cols, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
191
+ def cpu_footer(J_eff, degrees, shots, thread_count, quality, n_qubits, G_data, G_rows, G_cols, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
178
192
  hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
179
193
 
180
- best_solution, best_value = sample_measurement(G_data, G_rows, G_cols, shots, hamming_prob, repulsion_base, is_spin_glass)
194
+ best_solution, best_value = sample_measurement(G_data, G_rows, G_cols, shots, thread_count, hamming_prob, repulsion_base, is_spin_glass)
181
195
 
182
196
  bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
183
197
 
@@ -312,7 +326,9 @@ def maxcut_tfim_sparse(
312
326
  is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE
313
327
 
314
328
  if not is_opencl:
315
- bit_string, best_value, partition = cpu_footer(J_eff, degrees, shots, quality, n_qubits, G_m.data, G_m.indptr, G_m.indices, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
329
+ thread_count = os.cpu_count() ** 2
330
+
331
+ bit_string, best_value, partition = cpu_footer(J_eff, degrees, shots, thread_count, quality, n_qubits, G_m.data, G_m.indptr, G_m.indices, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
316
332
 
317
333
  if best_value < 0.0:
318
334
  # Best cut is trivial partition, all/empty
@@ -1,6 +1,7 @@
1
1
  import networkx as nx
2
2
  import numpy as np
3
3
  from numba import njit, prange
4
+ import os
4
5
 
5
6
  from .maxcut_tfim_util import compute_cut_streaming, compute_energy_streaming, get_cut, get_cut_base, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick
6
7
 
@@ -52,11 +53,12 @@ def local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s):
52
53
 
53
54
 
54
55
  @njit(parallel=True)
55
- def sample_measurement(G_func, nodes, shots, thresholds, n, repulsion_base, is_spin_glass):
56
- shots = max(1, shots >> 1)
56
+ def sample_measurement(G_func, nodes, shots, thread_count, thresholds, n, repulsion_base, is_spin_glass):
57
+ shot_segment = (max(1, shots >> 1) + thread_count - 1) // thread_count
58
+ shots = shot_segment * thread_count
57
59
 
58
- solutions = np.empty((shots, n), dtype=np.bool_)
59
- energies = np.empty(shots, dtype=dtype)
60
+ solutions = np.empty((thread_count, n), dtype=np.bool_)
61
+ energies = np.full(thread_count, np.finfo(dtype).min, dtype=dtype)
60
62
 
61
63
  best_solution = solutions[0]
62
64
  best_energy = -float("inf")
@@ -65,23 +67,35 @@ def sample_measurement(G_func, nodes, shots, thresholds, n, repulsion_base, is_s
65
67
  while improved:
66
68
  improved = False
67
69
  if is_spin_glass:
68
- for s in prange(shots):
69
- # First dimension: Hamming weight
70
- m = sample_mag(thresholds)
71
-
72
- # Second dimension: permutation within Hamming weight
73
- sample = local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s)
74
- solutions[s] = sample
75
- energies[s] = compute_energy_streaming(sample, G_func, nodes, n)
70
+ for i in prange(thread_count):
71
+ s_offset = i * shot_segment
72
+ for j in range(shot_segment):
73
+ s = s_offset + j
74
+
75
+ # First dimension: Hamming weight
76
+ m = sample_mag(thresholds)
77
+
78
+ # Second dimension: permutation within Hamming weight
79
+ sample = local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s)
80
+ energy = compute_energy_streaming(sample, G_func, nodes, n)
81
+
82
+ if energy > energies[i]:
83
+ solutions[i], energies[i] = sample, energy
76
84
  else:
77
- for s in prange(shots):
78
- # First dimension: Hamming weight
79
- m = sample_mag(thresholds)
85
+ for i in prange(thread_count):
86
+ s_offset = i * shot_segment
87
+ for j in range(shot_segment):
88
+ s = s_offset + j
80
89
 
81
- # Second dimension: permutation within Hamming weight
82
- sample = local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s)
83
- solutions[s] = sample
84
- energies[s] = compute_cut_streaming(sample, G_func, nodes, n)
90
+ # First dimension: Hamming weight
91
+ m = sample_mag(thresholds)
92
+
93
+ # Second dimension: permutation within Hamming weight
94
+ sample = local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s)
95
+ energy = compute_cut_streaming(sample, G_func, nodes, n)
96
+
97
+ if energy > energies[i]:
98
+ solutions[i], energies[i] = sample, energy
85
99
 
86
100
  best_index = np.argmax(energies)
87
101
  energy = energies[best_index]
@@ -91,7 +105,7 @@ def sample_measurement(G_func, nodes, shots, thresholds, n, repulsion_base, is_s
91
105
  improved = True
92
106
 
93
107
  if is_spin_glass:
94
- best_energy = compute_cut_streaming(best_solution, G_func, nodes, n)
108
+ best_energy = compute_cut_streaming(sample, G_func, nodes, n)
95
109
 
96
110
  return best_solution, best_energy
97
111
 
@@ -132,14 +146,14 @@ def find_G_min(G_func, nodes, n_nodes):
132
146
 
133
147
 
134
148
  @njit
135
- def cpu_footer(shots, quality, n_qubits, G_min, G_func, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
149
+ def cpu_footer(shots, thread_count, quality, n_qubits, G_min, G_func, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
136
150
  J_eff, degrees = init_J_and_z(G_func, nodes, G_min, repulsion_base)
137
151
  hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
138
152
 
139
153
  degrees = None
140
154
  J_eff = None
141
155
 
142
- best_solution, best_value = sample_measurement(G_func, nodes, shots, hamming_prob, n_qubits, repulsion_base, is_spin_glass)
156
+ best_solution, best_value = sample_measurement(G_func, nodes, shots, thread_count, hamming_prob, n_qubits, repulsion_base, is_spin_glass)
143
157
 
144
158
  bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
145
159
 
@@ -191,7 +205,9 @@ def maxcut_tfim_streaming(
191
205
 
192
206
  G_min = find_G_min(G_func, nodes, n_qubits)
193
207
 
194
- bit_string, best_value, partition = cpu_footer(shots, quality, n_qubits, G_min, G_func, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
208
+ thread_count = os.cpu_count() ** 2
209
+
210
+ bit_string, best_value, partition = cpu_footer(shots, thread_count, quality, n_qubits, G_min, G_func, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
195
211
 
196
212
  if best_value < 0.0:
197
213
  # Best cut is trivial partition, all/empty
@@ -484,11 +484,8 @@ def bit_pick(weights, used, n):
484
484
 
485
485
  @njit
486
486
  def gray_code_next(state, curr_idx):
487
- if curr_idx == 0:
488
- return state.copy()
489
-
490
- prev = curr_idx - 1
491
- curr = curr_idx
487
+ prev = curr_idx
488
+ curr = curr_idx + 1
492
489
  prev = prev ^ (prev >> 1)
493
490
  curr = curr ^ (curr >> 1)
494
491
  diff = prev ^ curr
@@ -48,7 +48,7 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_m):
48
48
  n = len(best_theta)
49
49
 
50
50
  states = np.empty((n, n), dtype=np.bool_)
51
- energies = np.zeros(n, dtype=dtype)
51
+ energies = np.full(n, np.finfo(dtype).min, dtype=dtype)
52
52
 
53
53
  if is_spin_glass:
54
54
  for i in prange(n):
@@ -82,18 +82,46 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_m):
82
82
  return best_energy, best_state
83
83
 
84
84
 
85
+ @njit(parallel=True)
86
+ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_m, n, is_spin_glass):
87
+ seed_count = thread_count * gray_seed_multiple
88
+
89
+ seeds = np.empty((seed_count, n), dtype=np.bool_)
90
+ energies = np.empty(seed_count, dtype=dtype)
91
+
92
+ if is_spin_glass:
93
+ for i in prange(seed_count):
94
+ seed = gray_mutation(i, best_theta)
95
+ energies[i] = compute_energy(seed, G_m, n)
96
+ seeds[i] = seed
97
+ else:
98
+ for i in prange(seed_count):
99
+ seed = gray_mutation(i, best_theta)
100
+ energies[i] = compute_cut(seed, G_m, n)
101
+ seeds[i] = seed
102
+
103
+ indices = np.argsort(energies)[::-1]
104
+
105
+ best_seeds = np.empty((thread_count, n), dtype=np.bool_)
106
+ best_energies = np.empty(thread_count, dtype=dtype)
107
+
108
+ for i in prange(thread_count):
109
+ idx = indices[i]
110
+ best_seeds[i] = seeds[idx]
111
+ best_energies[i] = energies[idx]
112
+
113
+ return best_seeds, best_energies
114
+
85
115
  @njit(parallel=True)
86
116
  def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_m):
87
117
  n = len(best_theta)
88
- thread_iterations = gray_iterations // thread_count
89
- gray_iterations = (gray_iterations + thread_count - 1) // thread_count
118
+ thread_iterations = (gray_iterations + thread_count - 1) // thread_count
90
119
 
91
120
  states = np.empty((thread_count, n), dtype=np.bool_)
92
- energies = np.zeros(thread_count, dtype=dtype)
121
+ energies = np.full(thread_count, np.finfo(dtype).min, dtype=dtype)
93
122
 
94
123
  if is_spin_glass:
95
124
  for i in prange(thread_count):
96
- i_offset = i * thread_iterations
97
125
  iterator = iterators[i]
98
126
  for curr_idx in range(thread_iterations):
99
127
  gray_code_next(iterator, curr_idx)
@@ -102,7 +130,6 @@ def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count,
102
130
  states[i], energies[i] = iterator.copy(), energy
103
131
  else:
104
132
  for i in prange(thread_count):
105
- i_offset = i * thread_iterations
106
133
  iterator = iterators[i]
107
134
  for curr_idx in range(thread_iterations):
108
135
  gray_code_next(iterator, curr_idx)
@@ -128,7 +155,8 @@ def spin_glass_solver(
128
155
  anneal_h=None,
129
156
  repulsion_base=None,
130
157
  is_log=False,
131
- gray_iterations=None
158
+ gray_iterations=None,
159
+ gray_seed_multiple=None
132
160
  ):
133
161
  dtype = opencl_context.dtype
134
162
  nodes = None
@@ -171,6 +199,9 @@ def spin_glass_solver(
171
199
  if gray_iterations is None:
172
200
  gray_iterations = n_qubits * os.cpu_count()
173
201
 
202
+ if gray_seed_multiple is None:
203
+ gray_seed_multiple = os.cpu_count()
204
+
174
205
  max_energy = compute_energy(best_theta, G_m, n_qubits) if is_spin_glass else cut_value
175
206
 
176
207
  thread_count = os.cpu_count() ** 2
@@ -195,7 +226,15 @@ def spin_glass_solver(
195
226
  continue
196
227
 
197
228
  # Gray code with default O(n^3)
198
- iterators = np.array([gray_mutation(i, best_theta) for i in range(thread_count)])
229
+ iterators, energies = pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_m, n_qubits, is_spin_glass)
230
+ best_i = np.argmax(energies)
231
+ energy, state = energies[best_i], iterators[best_i]
232
+ if energy > max_energy:
233
+ max_energy = energy
234
+ best_theta = state
235
+ improved = True
236
+ continue
237
+
199
238
  energy, state = run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_m)
200
239
  if energy > max_energy:
201
240
  max_energy = energy
@@ -49,7 +49,7 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols):
49
49
  n = len(best_theta)
50
50
 
51
51
  states = np.empty((n, n), dtype=np.bool_)
52
- energies = np.zeros(n, dtype=dtype)
52
+ energies = np.full(n, np.finfo(dtype).min, dtype=dtype)
53
53
 
54
54
  if is_spin_glass:
55
55
  for i in prange(n):
@@ -83,18 +83,46 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols):
83
83
  return best_energy, best_state
84
84
 
85
85
 
86
+ @njit(parallel=True)
87
+ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_data, G_rows, G_cols, n, is_spin_glass):
88
+ seed_count = thread_count * gray_seed_multiple
89
+
90
+ seeds = np.empty((seed_count, n), dtype=np.bool_)
91
+ energies = np.empty(seed_count, dtype=dtype)
92
+
93
+ if is_spin_glass:
94
+ for i in prange(seed_count):
95
+ seed = gray_mutation(i, best_theta)
96
+ energies[i] = compute_energy_sparse(seed, G_data, G_rows, G_cols, n)
97
+ seeds[i] = seed
98
+ else:
99
+ for i in prange(seed_count):
100
+ seed = gray_mutation(i, best_theta)
101
+ energies[i] = compute_cut_sparse(seed, G_data, G_rows, G_cols, n)
102
+ seeds[i] = seed
103
+
104
+ indices = np.argsort(energies)[::-1]
105
+
106
+ best_seeds = np.empty((thread_count, n), dtype=np.bool_)
107
+ best_energies = np.empty(thread_count, dtype=dtype)
108
+
109
+ for i in prange(thread_count):
110
+ idx = indices[i]
111
+ best_seeds[i] = seeds[idx]
112
+ best_energies[i] = energies[idx]
113
+
114
+ return best_seeds, best_energies
115
+
86
116
  @njit(parallel=True)
87
117
  def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_data, G_rows, G_cols):
88
118
  n = len(best_theta)
89
- thread_iterations = gray_iterations // thread_count
90
- gray_iterations = (gray_iterations + thread_count - 1) // thread_count
119
+ thread_iterations = (gray_iterations + thread_count - 1) // thread_count
91
120
 
92
121
  states = np.empty((thread_count, n), dtype=np.bool_)
93
- energies = np.zeros(thread_count, dtype=dtype)
122
+ energies = np.full(thread_count, np.finfo(dtype).min, dtype=dtype)
94
123
 
95
124
  if is_spin_glass:
96
125
  for i in prange(thread_count):
97
- i_offset = i * thread_iterations
98
126
  iterator = iterators[i]
99
127
  for curr_idx in range(thread_iterations):
100
128
  gray_code_next(iterator, curr_idx)
@@ -103,7 +131,6 @@ def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count,
103
131
  states[i], energies[i] = iterator.copy(), energy
104
132
  else:
105
133
  for i in prange(thread_count):
106
- i_offset = i * thread_iterations
107
134
  iterator = iterators[i]
108
135
  for curr_idx in range(thread_iterations):
109
136
  gray_code_next(iterator, curr_idx)
@@ -129,7 +156,8 @@ def spin_glass_solver_sparse(
129
156
  anneal_h=None,
130
157
  repulsion_base=None,
131
158
  is_log=False,
132
- gray_iterations=None
159
+ gray_iterations=None,
160
+ gray_seed_multiple=None
133
161
  ):
134
162
  dtype = opencl_context.dtype
135
163
  nodes = None
@@ -172,6 +200,9 @@ def spin_glass_solver_sparse(
172
200
  if gray_iterations is None:
173
201
  gray_iterations = n_qubits * os.cpu_count()
174
202
 
203
+ if gray_seed_multiple is None:
204
+ gray_seed_multiple = os.cpu_count()
205
+
175
206
  max_energy = compute_energy_sparse(best_theta, G_m.data, G_m.indptr, G_m.indices, n_qubits) if is_spin_glass else cut_value
176
207
 
177
208
  thread_count = os.cpu_count() ** 2
@@ -196,7 +227,15 @@ def spin_glass_solver_sparse(
196
227
  continue
197
228
 
198
229
  # Gray code with default O(n^3)
199
- iterators = np.array([gray_mutation(i, best_theta) for i in range(thread_count)])
230
+ iterators, energies = pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_m.data, G_m.indptr, G_m.indices, n_qubits, is_spin_glass)
231
+ best_i = np.argmax(energies)
232
+ energy, state = energies[best_i], iterators[best_i]
233
+ if energy > max_energy:
234
+ max_energy = energy
235
+ best_theta = state
236
+ improved = True
237
+ continue
238
+
200
239
  energy, state = run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_m.data, G_m.indptr, G_m.indices)
201
240
  if energy > max_energy:
202
241
  max_energy = energy
@@ -41,7 +41,7 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_func, nodes):
41
41
  n = len(best_theta)
42
42
 
43
43
  states = np.empty((n, n), dtype=np.bool_)
44
- energies = np.zeros(n, dtype=dtype)
44
+ energies = np.full(n, np.finfo(dtype).min, dtype=dtype)
45
45
 
46
46
  if is_spin_glass:
47
47
  for i in prange(n):
@@ -75,18 +75,46 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_func, nodes):
75
75
  return best_energy, best_state
76
76
 
77
77
 
78
+ @njit(parallel=True)
79
+ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_func, nodes, n, is_spin_glass):
80
+ seed_count = thread_count * gray_seed_multiple
81
+
82
+ seeds = np.empty((seed_count, n), dtype=np.bool_)
83
+ energies = np.empty(seed_count, dtype=dtype)
84
+
85
+ if is_spin_glass:
86
+ for i in prange(seed_count):
87
+ seed = gray_mutation(i, best_theta)
88
+ energies[i] = compute_energy_streaming(seed, G_func, nodes, n)
89
+ seeds[i] = seed
90
+ else:
91
+ for i in prange(seed_count):
92
+ seed = gray_mutation(i, best_theta)
93
+ energies[i] = compute_cut_streaming(seed, G_func, nodes, n)
94
+ seeds[i] = seed
95
+
96
+ indices = np.argsort(energies)[::-1]
97
+
98
+ best_seeds = np.empty((thread_count, n), dtype=np.bool_)
99
+ best_energies = np.empty(thread_count, dtype=dtype)
100
+
101
+ for i in prange(thread_count):
102
+ idx = indices[i]
103
+ best_seeds[i] = seeds[idx]
104
+ best_energies[i] = energies[idx]
105
+
106
+ return best_seeds, best_energies
107
+
78
108
  @njit(parallel=True)
79
109
  def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_func, nodes):
80
110
  n = len(best_theta)
81
- thread_iterations = gray_iterations // thread_count
82
- gray_iterations = (gray_iterations + thread_count - 1) // thread_count
111
+ thread_iterations = (gray_iterations + thread_count - 1) // thread_count
83
112
 
84
113
  states = np.empty((thread_count, n), dtype=np.bool_)
85
- energies = np.zeros(thread_count, dtype=dtype)
114
+ energies = np.full(thread_count, np.finfo(dtype).min, dtype=dtype)
86
115
 
87
116
  if is_spin_glass:
88
117
  for i in prange(thread_count):
89
- i_offset = i * thread_iterations
90
118
  iterator = iterators[i]
91
119
  for curr_idx in range(thread_iterations):
92
120
  gray_code_next(iterator, curr_idx)
@@ -95,8 +123,8 @@ def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count,
95
123
  states[i], energies[i] = iterator.copy(), energy
96
124
  else:
97
125
  for i in prange(thread_count):
98
- i_offset = i * thread_iterations
99
126
  iterator = iterators[i]
127
+ r_iterator = iterator.copy()
100
128
  for curr_idx in range(thread_iterations):
101
129
  gray_code_next(iterator, curr_idx)
102
130
  energy = compute_cut_streaming(iterator, G_func, nodes, n)
@@ -122,7 +150,8 @@ def spin_glass_solver_streaming(
122
150
  anneal_h=None,
123
151
  repulsion_base=None,
124
152
  is_log=False,
125
- gray_iterations=None
153
+ gray_iterations=None,
154
+ gray_seed_multiple=None
126
155
  ):
127
156
  dtype = opencl_context.dtype
128
157
  n_qubits = len(nodes)
@@ -155,6 +184,9 @@ def spin_glass_solver_streaming(
155
184
  if gray_iterations is None:
156
185
  gray_iterations = n_qubits * os.cpu_count()
157
186
 
187
+ if gray_seed_multiple is None:
188
+ gray_seed_multiple = os.cpu_count()
189
+
158
190
  max_energy = compute_energy_streaming(best_theta, G_func, nodes, n_qubits) if is_spin_glass else cut_value
159
191
 
160
192
  thread_count = os.cpu_count() ** 2
@@ -179,7 +211,15 @@ def spin_glass_solver_streaming(
179
211
  continue
180
212
 
181
213
  # Gray code with default O(n^3)
182
- iterators = np.array([gray_mutation(i, best_theta) for i in range(thread_count)])
214
+ iterators, energies = pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_func, nodes, n_qubits, is_spin_glass)
215
+ best_i = np.argmax(energies)
216
+ energy, state = energies[best_i], iterators[best_i]
217
+ if energy > max_energy:
218
+ max_energy = energy
219
+ best_theta = state
220
+ improved = True
221
+ continue
222
+
183
223
  energy, state = run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_func, nodes)
184
224
  if energy > max_energy:
185
225
  max_energy = energy
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.0.1
3
+ Version: 9.1.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
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
7
7
 
8
8
  setup(
9
9
  name='pyqrackising',
10
- version='9.0.1',
10
+ version='9.1.0',
11
11
  author='Dan Strano',
12
12
  author_email='stranoj@gmail.com',
13
13
  description='Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)',
File without changes
File without changes
File without changes