PyQrackIsing 5.4.0__tar.gz → 5.4.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 (29) hide show
  1. {pyqrackising-5.4.0/pyqrackising.egg-info → pyqrackising-5.4.1}/PKG-INFO +1 -1
  2. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyproject.toml +1 -1
  3. pyqrackising-5.4.1/pyqrackising/kernels.cl +217 -0
  4. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/maxcut_tfim_util.py +9 -3
  5. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/spin_glass_solver.py +85 -5
  6. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/spin_glass_solver_sparse.py +89 -5
  7. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/spin_glass_solver_streaming.py +5 -5
  8. {pyqrackising-5.4.0 → pyqrackising-5.4.1/pyqrackising.egg-info}/PKG-INFO +1 -1
  9. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/setup.py +1 -1
  10. pyqrackising-5.4.0/pyqrackising/kernels.cl +0 -70
  11. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/CMakeLists.txt +0 -0
  12. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/LICENSE.md +0 -0
  13. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/MANIFEST.in +0 -0
  14. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/README.md +0 -0
  15. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/__init__.py +0 -0
  16. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
  17. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/generate_tfim_samples.py +0 -0
  18. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/maxcut_tfim.py +0 -0
  19. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  20. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  21. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/spin_glass_solver_util.py +0 -0
  22. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/tfim_magnetization.py +0 -0
  23. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/tfim_square_magnetization.py +0 -0
  24. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising/tsp.py +0 -0
  25. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
  26. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
  27. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising.egg-info/not-zip-safe +0 -0
  28. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/pyqrackising.egg-info/top_level.txt +0 -0
  29. {pyqrackising-5.4.0 → pyqrackising-5.4.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.4.0
3
+ Version: 5.4.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
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "pyqrackising"
15
- version = "5.4.0"
15
+ version = "5.4.1"
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"}
@@ -0,0 +1,217 @@
1
+ // By Google Search AI
2
+ inline void AtomicAdd_g_f(volatile __global float *source, const float operand) {
3
+ union {
4
+ unsigned int intVal;
5
+ float floatVal;
6
+ } oldVal, newVal;
7
+
8
+ do {
9
+ oldVal.floatVal = *source;
10
+ newVal.floatVal = oldVal.floatVal + operand;
11
+ // Use atomic_cmpxchg to atomically update the value
12
+ } while (atomic_cmpxchg((volatile __global unsigned int*)source, oldVal.intVal, newVal.intVal) != oldVal.intVal);
13
+ }
14
+
15
+ // By Elara (custom OpenAI GPT)
16
+ inline float probability_by_hamming_weight(
17
+ int q, float J, float h, float z, float theta, float t, int n_qubits
18
+ ) {
19
+ float ratio = fabs(h) / (z * J);
20
+ if (ratio > 1.0f) ratio = 1.0f;
21
+ float theta_c = asin(ratio);
22
+
23
+ float p = pow(2.0f, fabs(J / h) - 1.0f)
24
+ * (1.0f + sin(theta - theta_c) * cos(1.5f * M_PI_F * J * t + theta) / (1.0f + sqrt(t)))
25
+ - 0.5f;
26
+
27
+ if ((p * (n_qubits + 2.0f)) >= 1024.0f) return 0.0f;
28
+
29
+ float numerator = pow(2.0f, (n_qubits + 2.0f) * p) - 1.0f;
30
+ float denominator = pow(2.0f, p) - 1.0f;
31
+ float result = numerator * pow(2.0f, -((n_qubits + 1.0f) * p) - p * q) / denominator;
32
+
33
+ if (isnan(result) || isinf(result)) return 0.0f;
34
+ return result;
35
+ }
36
+
37
+ __kernel void maxcut_hamming_cdf(
38
+ int n_qubits,
39
+ __constant int* degrees,
40
+ __constant float* args,
41
+ __constant float* J_func,
42
+ __constant float* theta,
43
+ __global float* hamming_prob
44
+ ) {
45
+ const float delta_t = args[0U];
46
+ const float tot_t = args[1U];
47
+ const float h_mult = args[2U];
48
+ const int step = get_group_id(0) / n_qubits;
49
+ const int qi = get_group_id(0) % n_qubits;
50
+ const float J_eff = J_func[qi];
51
+ const float z = degrees[qi];
52
+ if (fabs(z * J_eff) <= (pow(2.0f, -52))) return;
53
+
54
+ const float theta_eff = theta[qi];
55
+ const float t = step * delta_t;
56
+ const float tm1 = (step - 1) * delta_t;
57
+ const float h_t = h_mult * (tot_t - t);
58
+
59
+ const int n_threads = get_local_size(0);
60
+
61
+ for (int qo = get_local_id(0); qo < n_qubits; qo += n_threads) {
62
+ int _qo = (J_eff > 0.0f) ? (n_qubits - (1 + qo)) : qo;
63
+ float diff = probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, t, n_qubits);
64
+ diff -= probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, tm1, n_qubits);
65
+ AtomicAdd_g_f(&(hamming_prob[_qo]), diff);
66
+ }
67
+ }
68
+
69
+ float bootstrap_worker(__constant char* theta, __global double* G_m, __constant int* indices, const int k, const int n) {
70
+ double energy = 0.0;
71
+ const size_t n_st = (size_t)n;
72
+ for (int u = 0; u < n; ++u) {
73
+ const size_t u_offset = u * n_st;
74
+ bool u_bit = theta[u];
75
+ for (int x = 0; x < k; ++x) {
76
+ if (indices[x] == u) {
77
+ u_bit = !u_bit;
78
+ break;
79
+ }
80
+ }
81
+ for (int v = u + 1; v < n; ++v) {
82
+ const double val = G_m[u_offset + v];
83
+ bool v_bit = theta[v];
84
+ for (int x = 0; x < k; ++x) {
85
+ if (indices[x] == v) {
86
+ v_bit = !v_bit;
87
+ break;
88
+ }
89
+ }
90
+ energy += (u_bit == v_bit) ? val : -val;
91
+ }
92
+ }
93
+
94
+ return (float)energy;
95
+ }
96
+
97
+ __kernel void bootstrap(
98
+ __global double* G_m,
99
+ __constant char* best_theta,
100
+ __constant int* indices_array,
101
+ __constant int* args, // args[0] = n, args[1] = k
102
+ __global float* min_energy_ptr, // output: per-group min energy
103
+ __global int* min_index_ptr, // output: per-group best index (i)
104
+ __local float* loc_energy, // local memory buffer
105
+ __local int* loc_index // local memory buffer
106
+ ) {
107
+ const int n = args[0];
108
+ const int k = args[1];
109
+ const int combo_count = args[2];
110
+ const int i = get_global_id(0);
111
+
112
+ float energy = INFINITY;
113
+
114
+ if (i < combo_count) {
115
+ const int j = i * k;
116
+ // Compute energy for this combination
117
+ energy = bootstrap_worker(best_theta, G_m, indices_array + j, k, n);
118
+ }
119
+
120
+ const int lt_id = get_local_id(0);
121
+ const int lt_size = get_local_size(0);
122
+
123
+ // Initialize local memory
124
+ loc_energy[lt_id] = energy;
125
+ loc_index[lt_id] = i;
126
+
127
+ // Reduce within workgroup
128
+ for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
129
+ barrier(CLK_LOCAL_MEM_FENCE);
130
+ if ((lt_id < offset) && (loc_energy[lt_id + offset] < loc_energy[lt_id])) {
131
+ loc_energy[lt_id] = loc_energy[lt_id + offset];
132
+ loc_index[lt_id] = loc_index[lt_id + offset];
133
+ }
134
+ }
135
+
136
+ // Write out per-group result
137
+ if (lt_id == 0) {
138
+ min_energy_ptr[get_group_id(0)] = loc_energy[0];
139
+ min_index_ptr[get_group_id(0)] = loc_index[0];
140
+ }
141
+ }
142
+
143
+ float bootstrap_worker_sparse(__constant char* theta, __global double* G_data, __global ulong* G_rows, __global ulong* G_cols, __constant int* indices, const int k, const int n) {
144
+ double energy = 0.0;
145
+ for (int u = 0; u < n; ++u) {
146
+ bool u_bit = theta[u];
147
+ for (int x = 0; x < k; ++x) {
148
+ if (indices[x] == u) {
149
+ u_bit = !u_bit;
150
+ break;
151
+ }
152
+ }
153
+ const size_t mCol = G_rows[u + 1];
154
+ for (int col = G_rows[u]; col < mCol; ++col) {
155
+ const int v = G_cols[col];
156
+ const double val = G_data[col];
157
+ bool v_bit = theta[v];
158
+ for (int x = 0; x < k; ++x) {
159
+ if (indices[x] == v) {
160
+ v_bit = !v_bit;
161
+ break;
162
+ }
163
+ }
164
+ energy += (u_bit == v_bit) ? val : -val;
165
+ }
166
+ }
167
+
168
+ return (float)energy;
169
+ }
170
+
171
+ __kernel void bootstrap_sparse(
172
+ __global double* G_data,
173
+ __global ulong* G_rows,
174
+ __global ulong* G_cols,
175
+ __constant char* best_theta,
176
+ __constant int* indices_array,
177
+ __constant int* args, // args[0] = n, args[1] = k
178
+ __global float* min_energy_ptr, // output: per-group min energy
179
+ __global int* min_index_ptr, // output: per-group best index (i)
180
+ __local float* loc_energy, // local memory buffer
181
+ __local int* loc_index // local memory buffer
182
+ ) {
183
+ const int n = args[0];
184
+ const int k = args[1];
185
+ const int combo_count = args[2];
186
+ const int i = get_global_id(0);
187
+
188
+ float energy = INFINITY;
189
+
190
+ if (i < combo_count) {
191
+ const int j = i * k;
192
+ // Compute energy for this combination
193
+ energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n);
194
+ }
195
+
196
+ const int lt_id = get_local_id(0);
197
+ const int lt_size = get_local_size(0);
198
+
199
+ // Initialize local memory
200
+ loc_energy[lt_id] = energy;
201
+ loc_index[lt_id] = i;
202
+
203
+ // Reduce within workgroup
204
+ for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
205
+ barrier(CLK_LOCAL_MEM_FENCE);
206
+ if ((lt_id < offset) && (loc_energy[lt_id + offset] < loc_energy[lt_id])) {
207
+ loc_energy[lt_id] = loc_energy[lt_id + offset];
208
+ loc_index[lt_id] = loc_index[lt_id + offset];
209
+ }
210
+ }
211
+
212
+ // Write out per-group result
213
+ if (lt_id == 0) {
214
+ min_energy_ptr[get_group_id(0)] = loc_energy[0];
215
+ min_index_ptr[get_group_id(0)] = loc_index[0];
216
+ }
217
+ }
@@ -133,16 +133,20 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
133
133
 
134
134
 
135
135
  class OpenCLContext:
136
- def __init__(self, a, c, q, k):
136
+ def __init__(self, a, c, q, m, b, s):
137
137
  self.IS_OPENCL_AVAILABLE = a
138
138
  self.ctx = c
139
139
  self.queue = q
140
- self.maxcut_hamming_cdf_kernel = k
140
+ self.maxcut_hamming_cdf_kernel = m
141
+ self.bootstrap_kernel = b
142
+ self.bootstrap_sparse_kernel = s
141
143
 
142
144
  IS_OPENCL_AVAILABLE = True
143
145
  ctx = None
144
146
  queue = None
145
147
  maxcut_hamming_cdf_kernel = None
148
+ bootstrap_kernel = None
149
+ bootstrap_sparse_kernel = None
146
150
  try:
147
151
  import pyopencl as cl
148
152
 
@@ -154,8 +158,10 @@ try:
154
158
  kernel_src = open(os.path.dirname(os.path.abspath(__file__)) + "/kernels.cl").read()
155
159
  program = cl.Program(ctx, kernel_src).build()
156
160
  maxcut_hamming_cdf_kernel = program.maxcut_hamming_cdf
161
+ bootstrap_kernel = program.bootstrap
162
+ bootstrap_sparse_kernel = program.bootstrap_sparse
157
163
  except ImportError:
158
164
  IS_OPENCL_AVAILABLE = False
159
165
  print("PyOpenCL not installed. (If you have any OpenCL accelerator devices with available ICDs, you might want to optionally install pyopencl.)")
160
166
 
161
- opencl_context = OpenCLContext(IS_OPENCL_AVAILABLE, ctx, queue, maxcut_hamming_cdf_kernel)
167
+ opencl_context = OpenCLContext(IS_OPENCL_AVAILABLE, ctx, queue, maxcut_hamming_cdf_kernel, bootstrap_kernel, bootstrap_sparse_kernel)
@@ -1,4 +1,5 @@
1
1
  from .maxcut_tfim import maxcut_tfim
2
+ from .maxcut_tfim_util import opencl_context
2
3
  from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
3
4
  import itertools
4
5
  import networkx as nx
@@ -7,6 +8,13 @@ from numba import njit, prange
7
8
  import os
8
9
 
9
10
 
11
+ IS_OPENCL_AVAILABLE = True
12
+ try:
13
+ import pyopencl as cl
14
+ except ImportError:
15
+ IS_OPENCL_AVAILABLE = False
16
+
17
+
10
18
  @njit
11
19
  def evaluate_cut_edges(theta_bits, G_m):
12
20
  n_qubits = len(G_m)
@@ -41,8 +49,8 @@ def bootstrap_worker(theta, G_m, indices):
41
49
 
42
50
 
43
51
  @njit(parallel=True)
44
- def bootstrap(best_theta, G_m, k, indices_array, min_energy):
45
- n = best_theta.shape[0]
52
+ def bootstrap(best_theta, G_m, indices_array, k, min_energy):
53
+ n = len(indices_array) // k
46
54
  energies = np.empty(n, dtype=np.float32)
47
55
  for i in prange(n):
48
56
  j = i * k
@@ -59,6 +67,75 @@ def bootstrap(best_theta, G_m, k, indices_array, min_energy):
59
67
  return min_energy
60
68
 
61
69
 
70
+ def run_bootstrap_opencl(best_theta, G_m_np, indices_array_np, k, min_energy):
71
+ ctx = opencl_context.ctx
72
+ queue = opencl_context.queue
73
+ bootstrap_kernel = opencl_context.bootstrap_kernel
74
+
75
+ n = best_theta.shape[0]
76
+ combo_count = len(indices_array_np) // k
77
+
78
+ best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
79
+
80
+ # Args: [n, k, combo_count]
81
+ args_np = np.array([n, k, combo_count], dtype=np.int32)
82
+
83
+ # Buffers
84
+ mf = cl.mem_flags
85
+ best_theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=best_theta_np)
86
+ G_m_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m_np)
87
+ indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
88
+ args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
89
+
90
+ # Allocate min_energy and min_index result buffers per workgroup
91
+ min_energy_host = np.empty(combo_count, dtype=np.float32)
92
+ min_index_host = np.empty(combo_count, dtype=np.int32)
93
+
94
+ min_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_energy_host.nbytes)
95
+ min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
96
+
97
+ # Local memory allocation (1 float per work item)
98
+ local_size = min(64, n)
99
+ global_size = ((combo_count + local_size - 1) // local_size) * local_size
100
+ local_energy_buf = cl.LocalMemory(np.dtype(np.float32).itemsize * local_size)
101
+ local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
102
+
103
+ # Set kernel args
104
+ bootstrap_kernel.set_args(
105
+ G_m_buf,
106
+ best_theta_buf,
107
+ indices_buf,
108
+ args_buf,
109
+ min_energy_buf,
110
+ min_index_buf,
111
+ local_energy_buf,
112
+ local_index_buf
113
+ )
114
+
115
+ cl.enqueue_nd_range_kernel(queue, bootstrap_kernel, (global_size,), (local_size,))
116
+
117
+ # Read results
118
+ cl.enqueue_copy(queue, min_energy_host, min_energy_buf)
119
+ cl.enqueue_copy(queue, min_index_host, min_index_buf)
120
+ queue.finish()
121
+
122
+ # Find global minimum
123
+ min_idx = np.argmin(min_energy_host)
124
+ energy = min_energy_host[min_idx]
125
+ if min_energy < energy:
126
+ return min_energy
127
+
128
+ best_i = min_index_host[min_idx]
129
+
130
+ flip_index_start = best_i * k
131
+ indices_to_flip = indices_array_np[flip_index_start : flip_index_start + k]
132
+
133
+ for i in indices_to_flip:
134
+ best_theta[i] = not best_theta[i]
135
+
136
+ return energy
137
+
138
+
62
139
  def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
63
140
  nodes = None
64
141
  n_qubits = 0
@@ -113,14 +190,17 @@ def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
113
190
 
114
191
  combos = []
115
192
  if len(combos_list) < k:
116
- combos = list(
193
+ combos = np.array(list(
117
194
  item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
118
- )
195
+ ))
119
196
  combos_list.append(combos)
120
197
  else:
121
198
  combos = combos_list[k - 1]
122
199
 
123
- energy = bootstrap(best_theta, G_m, k, combos, min_energy)
200
+ if IS_OPENCL_AVAILABLE:
201
+ energy = run_bootstrap_opencl(best_theta, G_m, combos, k, min_energy)
202
+ else:
203
+ energy = bootstrap(best_theta, G_m, combos, k, min_energy)
124
204
 
125
205
  if energy < min_energy:
126
206
  min_energy = energy
@@ -1,4 +1,5 @@
1
1
  from .maxcut_tfim_sparse import maxcut_tfim_sparse
2
+ from .maxcut_tfim_util import opencl_context
2
3
  from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
3
4
  import itertools
4
5
  import networkx as nx
@@ -8,6 +9,13 @@ import os
8
9
  from scipy.sparse import lil_matrix, csr_matrix
9
10
 
10
11
 
12
+ IS_OPENCL_AVAILABLE = True
13
+ try:
14
+ import pyopencl as cl
15
+ except ImportError:
16
+ IS_OPENCL_AVAILABLE = False
17
+
18
+
11
19
  @njit
12
20
  def evaluate_cut_edges(theta_bits, G_data, G_rows, G_cols):
13
21
  n = G_rows.shape[0] - 1
@@ -44,8 +52,8 @@ def bootstrap_worker(theta, G_data, G_rows, G_cols, indices):
44
52
 
45
53
 
46
54
  @njit(parallel=True)
47
- def bootstrap(best_theta, G_data, G_rows, G_cols, k, indices_array, min_energy):
48
- n = best_theta.shape[0]
55
+ def bootstrap(best_theta, G_data, G_rows, G_cols, indices_array, k, min_energy):
56
+ n = len(indices_array) // k
49
57
  energies = np.empty(n, dtype=np.float32)
50
58
  for i in prange(n):
51
59
  j = i * k
@@ -62,6 +70,79 @@ def bootstrap(best_theta, G_data, G_rows, G_cols, k, indices_array, min_energy):
62
70
  return min_energy
63
71
 
64
72
 
73
+ def run_bootstrap_opencl(best_theta, G_data_np, G_rows_np, G_cols_np, indices_array_np, k, min_energy):
74
+ ctx = opencl_context.ctx
75
+ queue = opencl_context.queue
76
+ bootstrap_kernel = opencl_context.bootstrap_sparse_kernel
77
+
78
+ n = best_theta.shape[0]
79
+ combo_count = len(indices_array_np) // k
80
+
81
+ best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
82
+
83
+ # Args: [n, k, combo_count]
84
+ args_np = np.array([n, k, combo_count], dtype=np.int32)
85
+
86
+ # Buffers
87
+ mf = cl.mem_flags
88
+ best_theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=best_theta_np)
89
+ G_data_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_data_np)
90
+ G_rows_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_rows_np.astype(np.uint64))
91
+ G_cols_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_cols_np.astype(np.uint64))
92
+ indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
93
+ args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
94
+
95
+ # Allocate min_energy and min_index result buffers per workgroup
96
+ min_energy_host = np.empty(combo_count, dtype=np.float32)
97
+ min_index_host = np.empty(combo_count, dtype=np.int32)
98
+
99
+ min_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_energy_host.nbytes)
100
+ min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
101
+
102
+ # Local memory allocation (1 float per work item)
103
+ local_size = min(64, n)
104
+ global_size = ((combo_count + local_size - 1) // local_size) * local_size
105
+ local_energy_buf = cl.LocalMemory(np.dtype(np.float32).itemsize * local_size)
106
+ local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
107
+
108
+ # Set kernel args
109
+ bootstrap_kernel.set_args(
110
+ G_data_buf,
111
+ G_rows_buf,
112
+ G_cols_buf,
113
+ best_theta_buf,
114
+ indices_buf,
115
+ args_buf,
116
+ min_energy_buf,
117
+ min_index_buf,
118
+ local_energy_buf,
119
+ local_index_buf
120
+ )
121
+
122
+ cl.enqueue_nd_range_kernel(queue, bootstrap_kernel, (global_size,), (local_size,))
123
+
124
+ # Read results
125
+ cl.enqueue_copy(queue, min_energy_host, min_energy_buf)
126
+ cl.enqueue_copy(queue, min_index_host, min_index_buf)
127
+ queue.finish()
128
+
129
+ # Find global minimum
130
+ min_idx = np.argmin(min_energy_host)
131
+ energy = min_energy_host[min_idx]
132
+ if min_energy < energy:
133
+ return min_energy
134
+
135
+ best_i = min_index_host[min_idx]
136
+
137
+ flip_index_start = best_i * k
138
+ indices_to_flip = indices_array_np[flip_index_start : flip_index_start + k]
139
+
140
+ for i in indices_to_flip:
141
+ best_theta[i] = not best_theta[i]
142
+
143
+ return energy
144
+
145
+
65
146
  def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
66
147
  lil = lil_matrix((n_nodes, n_nodes), dtype=np.float64)
67
148
  for u in range(n_nodes):
@@ -128,14 +209,17 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
128
209
 
129
210
  combos = []
130
211
  if len(combos_list) < k:
131
- combos = list(
212
+ combos = np.array(list(
132
213
  item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
133
- )
214
+ ))
134
215
  combos_list.append(combos)
135
216
  else:
136
217
  combos = combos_list[k - 1]
137
218
 
138
- energy = bootstrap(best_theta, G_m.data, G_m.indptr, G_m.indices, k, combos, min_energy)
219
+ if IS_OPENCL_AVAILABLE:
220
+ energy = run_bootstrap_opencl(best_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, min_energy)
221
+ else:
222
+ energy = bootstrap(best_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, min_energy)
139
223
 
140
224
  if energy < min_energy:
141
225
  min_energy = energy
@@ -42,8 +42,8 @@ def bootstrap_worker(theta, G_func, G_func_args_tuple, nodes, indices):
42
42
 
43
43
 
44
44
  @njit(parallel=True)
45
- def bootstrap(best_theta, G_func, G_func_args_tuple, nodes, k, indices_array, min_energy):
46
- n = best_theta.shape[0]
45
+ def bootstrap(best_theta, G_func, G_func_args_tuple, nodes, indices_array, k, min_energy):
46
+ n = len(indices_array) // k
47
47
  energies = np.empty(n, dtype=np.float32)
48
48
  for i in prange(n):
49
49
  j = i * k
@@ -111,14 +111,14 @@ def spin_glass_solver_streaming(
111
111
 
112
112
  combos = []
113
113
  if len(combos_list) < k:
114
- combos = list(
114
+ combos = np.array(list(
115
115
  item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
116
- )
116
+ ))
117
117
  combos_list.append(combos)
118
118
  else:
119
119
  combos = combos_list[k - 1]
120
120
 
121
- energy = bootstrap(best_theta, G_func, G_func_args_tuple, nodes, k, combos, min_energy)
121
+ energy = bootstrap(best_theta, G_func, G_func_args_tuple, nodes, combos, k, min_energy)
122
122
 
123
123
  if energy < min_energy:
124
124
  min_energy = energy
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.4.0
3
+ Version: 5.4.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
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='pyqrackising',
54
- version='5.4.0',
54
+ version='5.4.1',
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)',
@@ -1,70 +0,0 @@
1
- // By Google Search AI
2
- inline void AtomicAdd_g_f(volatile __global float *source, const float operand) {
3
- union {
4
- unsigned int intVal;
5
- float floatVal;
6
- } oldVal, newVal;
7
-
8
- do {
9
- oldVal.floatVal = *source;
10
- newVal.floatVal = oldVal.floatVal + operand;
11
- // Use atomic_cmpxchg to atomically update the value
12
- } while (atomic_cmpxchg((volatile __global unsigned int*)source, oldVal.intVal, newVal.intVal) != oldVal.intVal);
13
- }
14
-
15
- // By Elara (custom OpenAI GPT)
16
- inline float probability_by_hamming_weight(
17
- int q, float J, float h, float z, float theta, float t, int n_qubits
18
- ) {
19
- float ratio = fabs(h) / (z * J);
20
- if (ratio > 1.0f) ratio = 1.0f;
21
- float theta_c = asin(ratio);
22
-
23
- float p = pow(2.0f, fabs(J / h) - 1.0f)
24
- * (1.0f + sin(theta - theta_c) * cos(1.5f * M_PI_F * J * t + theta) / (1.0f + sqrt(t)))
25
- - 0.5f;
26
-
27
- if ((p * (n_qubits + 2.0f)) >= 1024.0f) return 0.0f;
28
-
29
- float numerator = pow(2.0f, (n_qubits + 2.0f) * p) - 1.0f;
30
- float denominator = pow(2.0f, p) - 1.0f;
31
- float result = numerator * pow(2.0f, -((n_qubits + 1.0f) * p) - p * q) / denominator;
32
-
33
- if (isnan(result) || isinf(result)) return 0.0f;
34
- return result;
35
- }
36
-
37
- __kernel void maxcut_hamming_cdf(
38
- int n_qubits,
39
- __constant int* degrees,
40
- __constant float* args,
41
- __constant float* J_func,
42
- __constant float* theta,
43
- __global float* hamming_prob
44
- ) {
45
- float delta_t = args[0];
46
- float tot_t = args[1];
47
- float h_mult = args[2];
48
- int step = get_group_id(0) / n_qubits;
49
- int qi = get_group_id(0) % n_qubits;
50
- float J_eff = J_func[qi];
51
- float z = degrees[qi];
52
- if (fabs(z * J_eff) <= (pow(2.0f, -52))) return;
53
-
54
- float theta_eff = theta[qi];
55
- float t = step * delta_t;
56
- float tm1 = (step - 1) * delta_t;
57
- float h_t = h_mult * (tot_t - t);
58
-
59
- int qo = get_local_id(0);
60
- int n_threads = get_local_size(0);
61
-
62
- while (qo < n_qubits) {
63
- int _qo = (J_eff > 0.0f) ? (n_qubits - (1 + qo)) : qo;
64
- float diff = probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, t, n_qubits);
65
- diff -= probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, tm1, n_qubits);
66
- AtomicAdd_g_f(&(hamming_prob[_qo]), diff);
67
- qo += n_threads;
68
- }
69
- }
70
-
File without changes
File without changes
File without changes
File without changes