PyQrackIsing 9.0.1__tar.gz → 9.1.1__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.1}/PKG-INFO +1 -1
  2. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/README.md +2 -2
  3. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyproject.toml +1 -1
  4. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/maxcut_tfim.py +39 -23
  5. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/maxcut_tfim_sparse.py +40 -24
  6. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/maxcut_tfim_streaming.py +39 -23
  7. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/maxcut_tfim_util.py +2 -5
  8. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/spin_glass_solver.py +68 -31
  9. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/spin_glass_solver_sparse.py +68 -31
  10. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/spin_glass_solver_streaming.py +69 -31
  11. {pyqrackising-9.0.1 → pyqrackising-9.1.1/pyqrackising.egg-info}/PKG-INFO +1 -1
  12. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/setup.py +1 -1
  13. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/LICENSE.md +0 -0
  14. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/MANIFEST.in +0 -0
  15. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/__init__.py +0 -0
  16. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  17. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/generate_tfim_samples.py +0 -0
  18. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/kernels.cl +0 -0
  19. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/tfim_magnetization.py +0 -0
  20. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/tfim_square_magnetization.py +0 -0
  21. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/tsp.py +0 -0
  22. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising/tsp_maxcut.py +0 -0
  23. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
  24. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
  25. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising.egg-info/not-zip-safe +0 -0
  26. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/pyqrackising.egg-info/top_level.txt +0 -0
  27. {pyqrackising-9.0.1 → pyqrackising-9.1.1}/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.1
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.1"
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
@@ -44,36 +44,33 @@ def run_single_bit_flips(best_theta, is_spin_glass, G_m):
44
44
 
45
45
 
46
46
  @njit(parallel=True)
47
- def run_double_bit_flips(best_theta, is_spin_glass, G_m):
47
+ def run_double_bit_flips(best_theta, is_spin_glass, G_m, combos):
48
48
  n = len(best_theta)
49
+ combo_count = (n * (n - 1)) >> 1
49
50
 
50
- states = np.empty((n, n), dtype=np.bool_)
51
- energies = np.zeros(n, dtype=dtype)
51
+ states = np.empty((combo_count, n), dtype=np.bool_)
52
+ energies = np.empty(combo_count, dtype=dtype)
52
53
 
53
54
  if is_spin_glass:
54
- for i in prange(n):
55
+ for c in prange(combo_count):
55
56
  state = best_theta.copy()
56
- state[i] = not state[i]
57
- for j in range(n):
58
- if i == j:
59
- continue
60
- state[j] = not state[j]
61
- energy = compute_energy(state, G_m, n)
62
- if energy > energies[i]:
63
- states[i], energies[i] = state.copy(), energy
64
- state[j] = not state[j]
57
+ c2 = c << 1
58
+ b = combos[c2]
59
+ state[b] = not state[b]
60
+ b = combos[c2 + 1]
61
+ state[b] = not state[b]
62
+
63
+ states[c], energies[c] = state, compute_energy(state, G_m, n)
65
64
  else:
66
- for i in prange(n):
65
+ for c in prange(combo_count):
67
66
  state = best_theta.copy()
68
- state[i] = not state[i]
69
- for j in range(n):
70
- if i == j:
71
- continue
72
- state[j] = not state[j]
73
- energy = compute_cut(state, G_m, n)
74
- if energy > energies[i]:
75
- states[i], energies[i] = state.copy(), energy
76
- state[j] = not state[j]
67
+ c2 = c << 1
68
+ b = combos[c2]
69
+ state[b] = not state[b]
70
+ b = combos[c2 + 1]
71
+ state[b] = not state[b]
72
+
73
+ states[c], energies[c] = state, compute_cut(state, G_m, n)
77
74
 
78
75
  best_index = np.argmax(energies)
79
76
  best_energy = energies[best_index]
@@ -82,18 +79,46 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_m):
82
79
  return best_energy, best_state
83
80
 
84
81
 
82
+ @njit(parallel=True)
83
+ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_m, n, is_spin_glass):
84
+ seed_count = thread_count * gray_seed_multiple
85
+
86
+ seeds = np.empty((seed_count, n), dtype=np.bool_)
87
+ energies = np.empty(seed_count, dtype=dtype)
88
+
89
+ if is_spin_glass:
90
+ for i in prange(seed_count):
91
+ seed = gray_mutation(i, best_theta)
92
+ energies[i] = compute_energy(seed, G_m, n)
93
+ seeds[i] = seed
94
+ else:
95
+ for i in prange(seed_count):
96
+ seed = gray_mutation(i, best_theta)
97
+ energies[i] = compute_cut(seed, G_m, n)
98
+ seeds[i] = seed
99
+
100
+ indices = np.argsort(energies)[::-1]
101
+
102
+ best_seeds = np.empty((thread_count, n), dtype=np.bool_)
103
+ best_energies = np.empty(thread_count, dtype=dtype)
104
+
105
+ for i in prange(thread_count):
106
+ idx = indices[i]
107
+ best_seeds[i] = seeds[idx]
108
+ best_energies[i] = energies[idx]
109
+
110
+ return best_seeds, best_energies
111
+
85
112
  @njit(parallel=True)
86
113
  def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_m):
87
114
  n = len(best_theta)
88
- thread_iterations = gray_iterations // thread_count
89
- gray_iterations = (gray_iterations + thread_count - 1) // thread_count
115
+ thread_iterations = (gray_iterations + thread_count - 1) // thread_count
90
116
 
91
117
  states = np.empty((thread_count, n), dtype=np.bool_)
92
- energies = np.zeros(thread_count, dtype=dtype)
118
+ energies = np.full(thread_count, np.finfo(dtype).min, dtype=dtype)
93
119
 
94
120
  if is_spin_glass:
95
121
  for i in prange(thread_count):
96
- i_offset = i * thread_iterations
97
122
  iterator = iterators[i]
98
123
  for curr_idx in range(thread_iterations):
99
124
  gray_code_next(iterator, curr_idx)
@@ -102,7 +127,6 @@ def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count,
102
127
  states[i], energies[i] = iterator.copy(), energy
103
128
  else:
104
129
  for i in prange(thread_count):
105
- i_offset = i * thread_iterations
106
130
  iterator = iterators[i]
107
131
  for curr_idx in range(thread_iterations):
108
132
  gray_code_next(iterator, curr_idx)
@@ -128,7 +152,8 @@ def spin_glass_solver(
128
152
  anneal_h=None,
129
153
  repulsion_base=None,
130
154
  is_log=False,
131
- gray_iterations=None
155
+ gray_iterations=None,
156
+ gray_seed_multiple=None
132
157
  ):
133
158
  dtype = opencl_context.dtype
134
159
  nodes = None
@@ -171,8 +196,13 @@ def spin_glass_solver(
171
196
  if gray_iterations is None:
172
197
  gray_iterations = n_qubits * os.cpu_count()
173
198
 
199
+ if gray_seed_multiple is None:
200
+ gray_seed_multiple = os.cpu_count()
201
+
174
202
  max_energy = compute_energy(best_theta, G_m, n_qubits) if is_spin_glass else cut_value
175
203
 
204
+ combos2 = np.array(list(itertools.combinations(range(n_qubits), 2))).flatten()
205
+
176
206
  thread_count = os.cpu_count() ** 2
177
207
  improved = True
178
208
  while improved:
@@ -187,7 +217,7 @@ def spin_glass_solver(
187
217
  continue
188
218
 
189
219
  # Double bit flips with O(n^3)
190
- energy, state = run_double_bit_flips(best_theta, is_spin_glass, G_m)
220
+ energy, state = run_double_bit_flips(best_theta, is_spin_glass, G_m, combos2)
191
221
  if energy > max_energy:
192
222
  max_energy = energy
193
223
  best_theta = state
@@ -195,7 +225,14 @@ def spin_glass_solver(
195
225
  continue
196
226
 
197
227
  # Gray code with default O(n^3)
198
- iterators = np.array([gray_mutation(i, best_theta) for i in range(thread_count)])
228
+ iterators, energies = pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_m, n_qubits, is_spin_glass)
229
+ energy, state = energies[0], iterators[0]
230
+ if energy > max_energy:
231
+ max_energy = energy
232
+ best_theta = state
233
+ improved = True
234
+ continue
235
+
199
236
  energy, state = run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_m)
200
237
  if energy > max_energy:
201
238
  max_energy = energy
@@ -45,36 +45,33 @@ def run_single_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols):
45
45
 
46
46
 
47
47
  @njit(parallel=True)
48
- def run_double_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols):
48
+ def run_double_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols, combos):
49
49
  n = len(best_theta)
50
+ combo_count = (n * (n - 1)) >> 1
50
51
 
51
- states = np.empty((n, n), dtype=np.bool_)
52
- energies = np.zeros(n, dtype=dtype)
52
+ states = np.empty((combo_count, n), dtype=np.bool_)
53
+ energies = np.empty(combo_count, dtype=dtype)
53
54
 
54
55
  if is_spin_glass:
55
- for i in prange(n):
56
+ for c in prange(combo_count):
56
57
  state = best_theta.copy()
57
- state[i] = not state[i]
58
- for j in range(n):
59
- if i == j:
60
- continue
61
- state[j] = not state[j]
62
- energy = compute_energy_sparse(state, G_data, G_rows, G_cols, n)
63
- if energy > energies[i]:
64
- states[i], energies[i] = state.copy(), energy
65
- state[j] = not state[j]
58
+ c2 = c << 1
59
+ b = combos[c2]
60
+ state[b] = not state[b]
61
+ b = combos[c2 + 1]
62
+ state[b] = not state[b]
63
+
64
+ states[c], energies[c] = state, compute_energy_sparse(state, G_data, G_rows, G_cols, n)
66
65
  else:
67
- for i in prange(n):
66
+ for c in prange(combo_count):
68
67
  state = best_theta.copy()
69
- state[i] = not state[i]
70
- for j in range(n):
71
- if i == j:
72
- continue
73
- state[j] = not state[j]
74
- energy = compute_cut_sparse(state, G_data, G_rows, G_cols, n)
75
- if energy > energies[i]:
76
- states[i], energies[i] = state.copy(), energy
77
- state[j] = not state[j]
68
+ c2 = c << 1
69
+ b = combos[c2]
70
+ state[b] = not state[b]
71
+ b = combos[c2 + 1]
72
+ state[b] = not state[b]
73
+
74
+ states[c], energies[c] = state, compute_cut_sparse(state, G_data, G_rows, G_cols, n)
78
75
 
79
76
  best_index = np.argmax(energies)
80
77
  best_energy = energies[best_index]
@@ -83,18 +80,46 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols):
83
80
  return best_energy, best_state
84
81
 
85
82
 
83
+ @njit(parallel=True)
84
+ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_data, G_rows, G_cols, n, is_spin_glass):
85
+ seed_count = thread_count * gray_seed_multiple
86
+
87
+ seeds = np.empty((seed_count, n), dtype=np.bool_)
88
+ energies = np.empty(seed_count, dtype=dtype)
89
+
90
+ if is_spin_glass:
91
+ for i in prange(seed_count):
92
+ seed = gray_mutation(i, best_theta)
93
+ energies[i] = compute_energy_sparse(seed, G_data, G_rows, G_cols, n)
94
+ seeds[i] = seed
95
+ else:
96
+ for i in prange(seed_count):
97
+ seed = gray_mutation(i, best_theta)
98
+ energies[i] = compute_cut_sparse(seed, G_data, G_rows, G_cols, n)
99
+ seeds[i] = seed
100
+
101
+ indices = np.argsort(energies)[::-1]
102
+
103
+ best_seeds = np.empty((thread_count, n), dtype=np.bool_)
104
+ best_energies = np.empty(thread_count, dtype=dtype)
105
+
106
+ for i in prange(thread_count):
107
+ idx = indices[i]
108
+ best_seeds[i] = seeds[idx]
109
+ best_energies[i] = energies[idx]
110
+
111
+ return best_seeds, best_energies
112
+
86
113
  @njit(parallel=True)
87
114
  def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_data, G_rows, G_cols):
88
115
  n = len(best_theta)
89
- thread_iterations = gray_iterations // thread_count
90
- gray_iterations = (gray_iterations + thread_count - 1) // thread_count
116
+ thread_iterations = (gray_iterations + thread_count - 1) // thread_count
91
117
 
92
118
  states = np.empty((thread_count, n), dtype=np.bool_)
93
- energies = np.zeros(thread_count, dtype=dtype)
119
+ energies = np.full(thread_count, np.finfo(dtype).min, dtype=dtype)
94
120
 
95
121
  if is_spin_glass:
96
122
  for i in prange(thread_count):
97
- i_offset = i * thread_iterations
98
123
  iterator = iterators[i]
99
124
  for curr_idx in range(thread_iterations):
100
125
  gray_code_next(iterator, curr_idx)
@@ -103,7 +128,6 @@ def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count,
103
128
  states[i], energies[i] = iterator.copy(), energy
104
129
  else:
105
130
  for i in prange(thread_count):
106
- i_offset = i * thread_iterations
107
131
  iterator = iterators[i]
108
132
  for curr_idx in range(thread_iterations):
109
133
  gray_code_next(iterator, curr_idx)
@@ -129,7 +153,8 @@ def spin_glass_solver_sparse(
129
153
  anneal_h=None,
130
154
  repulsion_base=None,
131
155
  is_log=False,
132
- gray_iterations=None
156
+ gray_iterations=None,
157
+ gray_seed_multiple=None
133
158
  ):
134
159
  dtype = opencl_context.dtype
135
160
  nodes = None
@@ -172,8 +197,13 @@ def spin_glass_solver_sparse(
172
197
  if gray_iterations is None:
173
198
  gray_iterations = n_qubits * os.cpu_count()
174
199
 
200
+ if gray_seed_multiple is None:
201
+ gray_seed_multiple = os.cpu_count()
202
+
175
203
  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
204
 
205
+ combos2 = np.array(list(itertools.combinations(range(n_qubits), 2))).flatten()
206
+
177
207
  thread_count = os.cpu_count() ** 2
178
208
  improved = True
179
209
  while improved:
@@ -188,7 +218,7 @@ def spin_glass_solver_sparse(
188
218
  continue
189
219
 
190
220
  # Double bit flips with O(n^3)
191
- energy, state = run_double_bit_flips(best_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices)
221
+ energy, state = run_double_bit_flips(best_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices, combos2)
192
222
  if energy > max_energy:
193
223
  max_energy = energy
194
224
  best_theta = state
@@ -196,7 +226,14 @@ def spin_glass_solver_sparse(
196
226
  continue
197
227
 
198
228
  # Gray code with default O(n^3)
199
- 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.data, G_m.indptr, G_m.indices, n_qubits, is_spin_glass)
230
+ energy, state = energies[0], iterators[0]
231
+ if energy > max_energy:
232
+ max_energy = energy
233
+ best_theta = state
234
+ improved = True
235
+ continue
236
+
200
237
  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
238
  if energy > max_energy:
202
239
  max_energy = energy
@@ -37,36 +37,33 @@ def run_single_bit_flips(best_theta, is_spin_glass, G_func, nodes):
37
37
 
38
38
 
39
39
  @njit(parallel=True)
40
- def run_double_bit_flips(best_theta, is_spin_glass, G_func, nodes):
40
+ def run_double_bit_flips(best_theta, is_spin_glass, G_func, nodes, combos):
41
41
  n = len(best_theta)
42
+ combo_count = (n * (n - 1)) >> 1
42
43
 
43
- states = np.empty((n, n), dtype=np.bool_)
44
- energies = np.zeros(n, dtype=dtype)
44
+ states = np.empty((combo_count, n), dtype=np.bool_)
45
+ energies = np.empty(combo_count, dtype=dtype)
45
46
 
46
47
  if is_spin_glass:
47
- for i in prange(n):
48
+ for c in prange(combo_count):
48
49
  state = best_theta.copy()
49
- state[i] = not state[i]
50
- for j in range(n):
51
- if i == j:
52
- continue
53
- state[j] = not state[j]
54
- energy = compute_energy_streaming(state, G_func, nodes, n)
55
- if energy > energies[i]:
56
- states[i], energies[i] = state.copy(), energy
57
- state[j] = not state[j]
50
+ c2 = c << 1
51
+ b = combos[c2]
52
+ state[b] = not state[b]
53
+ b = combos[c2 + 1]
54
+ state[b] = not state[b]
55
+
56
+ states[c], energies[c] = state, compute_energy_streaming(state, G_func, nodes, n)
58
57
  else:
59
- for i in prange(n):
58
+ for c in prange(combo_count):
60
59
  state = best_theta.copy()
61
- state[i] = not state[i]
62
- for j in range(n):
63
- if i == j:
64
- continue
65
- state[j] = not state[j]
66
- energy = compute_cut_streaming(state, G_func, nodes, n)
67
- if energy > energies[i]:
68
- states[i], energies[i] = state.copy(), energy
69
- state[j] = not state[j]
60
+ c2 = c << 1
61
+ b = combos[c2]
62
+ state[b] = not state[b]
63
+ b = combos[c2 + 1]
64
+ state[b] = not state[b]
65
+
66
+ states[c], energies[c] = state, compute_cut_streaming(state, G_func, nodes, n)
70
67
 
71
68
  best_index = np.argmax(energies)
72
69
  best_energy = energies[best_index]
@@ -75,18 +72,46 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_func, nodes):
75
72
  return best_energy, best_state
76
73
 
77
74
 
75
+ @njit(parallel=True)
76
+ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_func, nodes, n, is_spin_glass):
77
+ seed_count = thread_count * gray_seed_multiple
78
+
79
+ seeds = np.empty((seed_count, n), dtype=np.bool_)
80
+ energies = np.empty(seed_count, dtype=dtype)
81
+
82
+ if is_spin_glass:
83
+ for i in prange(seed_count):
84
+ seed = gray_mutation(i, best_theta)
85
+ energies[i] = compute_energy_streaming(seed, G_func, nodes, n)
86
+ seeds[i] = seed
87
+ else:
88
+ for i in prange(seed_count):
89
+ seed = gray_mutation(i, best_theta)
90
+ energies[i] = compute_cut_streaming(seed, G_func, nodes, n)
91
+ seeds[i] = seed
92
+
93
+ indices = np.argsort(energies)[::-1]
94
+
95
+ best_seeds = np.empty((thread_count, n), dtype=np.bool_)
96
+ best_energies = np.empty(thread_count, dtype=dtype)
97
+
98
+ for i in prange(thread_count):
99
+ idx = indices[i]
100
+ best_seeds[i] = seeds[idx]
101
+ best_energies[i] = energies[idx]
102
+
103
+ return best_seeds, best_energies
104
+
78
105
  @njit(parallel=True)
79
106
  def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_func, nodes):
80
107
  n = len(best_theta)
81
- thread_iterations = gray_iterations // thread_count
82
- gray_iterations = (gray_iterations + thread_count - 1) // thread_count
108
+ thread_iterations = (gray_iterations + thread_count - 1) // thread_count
83
109
 
84
110
  states = np.empty((thread_count, n), dtype=np.bool_)
85
- energies = np.zeros(thread_count, dtype=dtype)
111
+ energies = np.full(thread_count, np.finfo(dtype).min, dtype=dtype)
86
112
 
87
113
  if is_spin_glass:
88
114
  for i in prange(thread_count):
89
- i_offset = i * thread_iterations
90
115
  iterator = iterators[i]
91
116
  for curr_idx in range(thread_iterations):
92
117
  gray_code_next(iterator, curr_idx)
@@ -95,8 +120,8 @@ def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count,
95
120
  states[i], energies[i] = iterator.copy(), energy
96
121
  else:
97
122
  for i in prange(thread_count):
98
- i_offset = i * thread_iterations
99
123
  iterator = iterators[i]
124
+ r_iterator = iterator.copy()
100
125
  for curr_idx in range(thread_iterations):
101
126
  gray_code_next(iterator, curr_idx)
102
127
  energy = compute_cut_streaming(iterator, G_func, nodes, n)
@@ -122,7 +147,8 @@ def spin_glass_solver_streaming(
122
147
  anneal_h=None,
123
148
  repulsion_base=None,
124
149
  is_log=False,
125
- gray_iterations=None
150
+ gray_iterations=None,
151
+ gray_seed_multiple=None
126
152
  ):
127
153
  dtype = opencl_context.dtype
128
154
  n_qubits = len(nodes)
@@ -155,8 +181,13 @@ def spin_glass_solver_streaming(
155
181
  if gray_iterations is None:
156
182
  gray_iterations = n_qubits * os.cpu_count()
157
183
 
184
+ if gray_seed_multiple is None:
185
+ gray_seed_multiple = os.cpu_count()
186
+
158
187
  max_energy = compute_energy_streaming(best_theta, G_func, nodes, n_qubits) if is_spin_glass else cut_value
159
188
 
189
+ combos2 = np.array(list(itertools.combinations(range(n_qubits), 2))).flatten()
190
+
160
191
  thread_count = os.cpu_count() ** 2
161
192
  improved = True
162
193
  while improved:
@@ -171,7 +202,7 @@ def spin_glass_solver_streaming(
171
202
  continue
172
203
 
173
204
  # Double bit flips with O(n^3)
174
- energy, state = run_double_bit_flips(best_theta, is_spin_glass, G_func, nodes)
205
+ energy, state = run_double_bit_flips(best_theta, is_spin_glass, G_func, nodes, combos2)
175
206
  if energy > max_energy:
176
207
  max_energy = energy
177
208
  best_theta = state
@@ -179,7 +210,14 @@ def spin_glass_solver_streaming(
179
210
  continue
180
211
 
181
212
  # Gray code with default O(n^3)
182
- iterators = np.array([gray_mutation(i, best_theta) for i in range(thread_count)])
213
+ iterators, energies = pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_func, nodes, n_qubits, is_spin_glass)
214
+ energy, state = energies[0], iterators[0]
215
+ if energy > max_energy:
216
+ max_energy = energy
217
+ best_theta = state
218
+ improved = True
219
+ continue
220
+
183
221
  energy, state = run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_func, nodes)
184
222
  if energy > max_energy:
185
223
  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.1
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.1',
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