PyQrackIsing 5.3.1__tar.gz → 5.3.3__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-5.3.1/pyqrackising.egg-info → pyqrackising-5.3.3}/PKG-INFO +1 -1
  2. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyproject.toml +1 -1
  3. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/maxcut_tfim.py +71 -70
  4. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/maxcut_tfim_sparse.py +69 -69
  5. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/maxcut_tfim_streaming.py +69 -69
  6. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/maxcut_tfim_util.py +31 -0
  7. {pyqrackising-5.3.1 → pyqrackising-5.3.3/pyqrackising.egg-info}/PKG-INFO +1 -1
  8. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/setup.py +1 -1
  9. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/CMakeLists.txt +0 -0
  10. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/LICENSE.md +0 -0
  11. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/MANIFEST.in +0 -0
  12. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/README.md +0 -0
  13. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/__init__.py +0 -0
  14. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/_pyqrack_ising.cpp +0 -0
  15. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/generate_tfim_samples.py +0 -0
  16. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/kernels.cl +0 -0
  17. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/spin_glass_solver.py +0 -0
  18. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/spin_glass_solver_sparse.py +0 -0
  19. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  20. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/tfim_magnetization.py +0 -0
  21. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/tfim_square_magnetization.py +0 -0
  22. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising/tsp.py +0 -0
  23. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising.egg-info/SOURCES.txt +0 -0
  24. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising.egg-info/dependency_links.txt +0 -0
  25. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising.egg-info/not-zip-safe +0 -0
  26. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/pyqrackising.egg-info/top_level.txt +0 -0
  27. {pyqrackising-5.3.1 → pyqrackising-5.3.3}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.3.1
3
+ Version: 5.3.3
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.3.1"
15
+ version = "5.3.3"
16
16
  requires-python = ">=3.8"
17
17
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
18
18
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -4,7 +4,7 @@ import numpy as np
4
4
  import os
5
5
  from numba import njit, prange
6
6
 
7
- from .maxcut_tfim_util import init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
7
+ from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, low_width, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
8
8
 
9
9
  IS_OPENCL_AVAILABLE = True
10
10
  try:
@@ -78,7 +78,8 @@ def compute_energy(sample, G_m, n_qubits):
78
78
 
79
79
 
80
80
  @njit(parallel=True)
81
- def sample_for_solution(G_m, shots, thresholds, degrees_sum, J_eff, n):
81
+ def sample_for_solution(G_m, shots, thresholds, J_eff):
82
+ n = len(G_m)
82
83
  max_weight = G_m.max()
83
84
  weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
84
85
 
@@ -128,6 +129,36 @@ def init_J_and_z(G_m):
128
129
  return J_eff, degrees
129
130
 
130
131
 
132
+ @njit
133
+ def cpu_footer(shots, quality, n_qubits, G_m, nodes):
134
+ J_eff, degrees = init_J_and_z(G_m)
135
+ hamming_prob = init_thresholds(n_qubits)
136
+
137
+ maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
138
+
139
+ best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, J_eff)
140
+
141
+ bit_string, l, r = get_cut(best_solution, nodes)
142
+
143
+ return bit_string, best_value, (l, r)
144
+
145
+
146
+ @njit
147
+ def gpu_footer(shots, n_qubits, G_m, J_eff, hamming_prob, nodes):
148
+ hamming_prob /= hamming_prob.sum()
149
+ tot_prob = 0.0
150
+ for i in range(n_qubits - 1):
151
+ tot_prob += hamming_prob[i]
152
+ hamming_prob[i] = tot_prob
153
+ hamming_prob[-1] = 2.0
154
+
155
+ best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, J_eff)
156
+
157
+ bit_string, l, r = get_cut(best_solution, nodes)
158
+
159
+ return bit_string, best_value, (l, r)
160
+
161
+
131
162
  def maxcut_tfim(
132
163
  G,
133
164
  quality=None,
@@ -146,18 +177,7 @@ def maxcut_tfim(
146
177
  G_m = G
147
178
 
148
179
  if n_qubits < 3:
149
- if n_qubits == 0:
150
- return "", 0, ([], [])
151
-
152
- if n_qubits == 1:
153
- return "0", 0, (nodes, [])
154
-
155
- if n_qubits == 2:
156
- weight = G_m[0, 1]
157
- if weight < 0.0:
158
- return "00", 0, (nodes, [])
159
-
160
- return "01", weight, ([nodes[0]], [nodes[1]])
180
+ return low_width(G_m, nodes, n_qubits)
161
181
 
162
182
  if quality is None:
163
183
  quality = 8
@@ -166,64 +186,45 @@ def maxcut_tfim(
166
186
  # Number of measurement shots
167
187
  shots = n_qubits << quality
168
188
 
169
- n_steps = 1 << quality
189
+ n_steps = 2 << quality
170
190
  grid_size = n_steps * n_qubits
171
191
 
192
+ if not (IS_OPENCL_AVAILABLE and grid_size >= 128):
193
+ return cpu_footer(shots, quality, n_qubits, G_m, nodes)
194
+
172
195
  J_eff, degrees = init_J_and_z(G_m)
173
196
  hamming_prob = init_thresholds(n_qubits)
174
197
 
175
- if IS_OPENCL_AVAILABLE and grid_size >= 128:
176
- delta_t = 1.0 / n_steps
177
- tot_t = 2.0 * n_steps * delta_t
178
- h_mult = 2.0 / tot_t
179
- theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
180
- args = np.empty(3, dtype=np.float32)
181
- args[0] = delta_t
182
- args[1] = tot_t
183
- args[2] = h_mult
184
-
185
- # Warp size is 32:
186
- group_size = n_qubits - 1
187
- if group_size > 256:
188
- group_size = 256
189
- grid_dim = n_steps * n_qubits * group_size
190
-
191
- # Move to GPU
192
- mf = cl.mem_flags
193
- args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
194
- J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
195
- deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
196
- theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
197
- ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
198
-
199
- # Kernel execution
200
- opencl_context.maxcut_hamming_cdf_kernel(
201
- opencl_context.queue, (grid_dim,), (group_size,),
202
- np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
203
- )
204
-
205
- # Fetch results
206
- cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
207
-
208
- hamming_prob /= hamming_prob.sum()
209
- tot_prob = 0.0
210
- for i in range(n_qubits - 1):
211
- tot_prob += hamming_prob[i]
212
- hamming_prob[i] = tot_prob
213
- hamming_prob[-1] = 2.0
214
- else:
215
- maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
216
-
217
- best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, degrees.sum(), J_eff, n_qubits)
218
-
219
- bit_string = ""
220
- l, r = [], []
221
- for i in range(len(best_solution)):
222
- if best_solution[i]:
223
- bit_string += "1"
224
- r.append(nodes[i])
225
- else:
226
- bit_string += "0"
227
- l.append(nodes[i])
228
-
229
- return bit_string, best_value, (l, r)
198
+ delta_t = 1.0 / n_steps
199
+ tot_t = 2.0 * n_steps * delta_t
200
+ h_mult = 2.0 / tot_t
201
+ theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
202
+ args = np.empty(3, dtype=np.float32)
203
+ args[0] = delta_t
204
+ args[1] = tot_t
205
+ args[2] = h_mult
206
+
207
+ # Warp size is 32:
208
+ group_size = n_qubits - 1
209
+ if group_size > 256:
210
+ group_size = 256
211
+ grid_dim = n_steps * n_qubits * group_size
212
+
213
+ # Move to GPU
214
+ mf = cl.mem_flags
215
+ args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
216
+ J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
217
+ deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
218
+ theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
219
+ ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
220
+
221
+ # Kernel execution
222
+ opencl_context.maxcut_hamming_cdf_kernel(
223
+ opencl_context.queue, (grid_dim,), (group_size,),
224
+ np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
225
+ )
226
+
227
+ # Fetch results
228
+ cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
229
+
230
+ return gpu_footer(shots, n_qubits, G_m, J_eff, hamming_prob, nodes)
@@ -4,7 +4,7 @@ import numpy as np
4
4
  import os
5
5
  from numba import njit, prange
6
6
 
7
- from .maxcut_tfim_util import init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
7
+ from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, low_width, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
8
8
 
9
9
  IS_OPENCL_AVAILABLE = True
10
10
  try:
@@ -175,6 +175,36 @@ def init_J_and_z(G_data, G_rows, G_cols):
175
175
  return J_eff, degrees
176
176
 
177
177
 
178
+ @njit
179
+ def cpu_footer(shots, quality, n_qubits, G_data, G_rows, G_cols, nodes):
180
+ J_eff, degrees = init_J_and_z(G_data, G_rows, G_cols)
181
+ hamming_prob = init_thresholds(n_qubits)
182
+
183
+ maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
184
+
185
+ best_solution, best_value = sample_for_solution(G_data, G_rows, G_cols, shots, hamming_prob, J_eff)
186
+
187
+ bit_string, l, r = get_cut(best_solution, nodes)
188
+
189
+ return bit_string, best_value, (l, r)
190
+
191
+
192
+ @njit
193
+ def gpu_footer(shots, n_qubits, G_data, G_rows, G_cols, J_eff, hamming_prob, nodes):
194
+ hamming_prob /= hamming_prob.sum()
195
+ tot_prob = 0.0
196
+ for i in range(n_qubits - 1):
197
+ tot_prob += hamming_prob[i]
198
+ hamming_prob[i] = tot_prob
199
+ hamming_prob[-1] = 2.0
200
+
201
+ best_solution, best_value = sample_for_solution(G_data, G_rows, G_cols, shots, hamming_prob, J_eff)
202
+
203
+ bit_string, l, r = get_cut(best_solution, nodes)
204
+
205
+ return bit_string, best_value, (l, r)
206
+
207
+
178
208
  def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
179
209
  lil = lil_matrix((n_nodes, n_nodes), dtype=np.float32)
180
210
  for u in range(n_nodes):
@@ -205,18 +235,7 @@ def maxcut_tfim_sparse(
205
235
  G_m = G
206
236
 
207
237
  if n_qubits < 3:
208
- if n_qubits == 0:
209
- return "", 0, ([], [])
210
-
211
- if n_qubits == 1:
212
- return "0", 0, (nodes, [])
213
-
214
- if n_qubits == 2:
215
- weight = G_m[0, 1]
216
- if weight < 0.0:
217
- return "00", 0, (nodes, [])
218
-
219
- return "01", weight, ([nodes[0]], [nodes[1]])
238
+ return low_width(G_m, nodes, n_qubits)
220
239
 
221
240
  if quality is None:
222
241
  quality = 8
@@ -225,64 +244,45 @@ def maxcut_tfim_sparse(
225
244
  # Number of measurement shots
226
245
  shots = n_qubits << quality
227
246
 
228
- n_steps = 1 << quality
247
+ n_steps = 2 << quality
229
248
  grid_size = n_steps * n_qubits
230
249
 
250
+ if not (IS_OPENCL_AVAILABLE and grid_size >= 128):
251
+ return cpu_footer(shots, quality, n_qubits, G_m.data, G_m.indptr, G_m.indices, nodes)
252
+
231
253
  J_eff, degrees = init_J_and_z(G_m.data, G_m.indptr, G_m.indices)
232
254
  hamming_prob = init_thresholds(n_qubits)
233
255
 
234
- if IS_OPENCL_AVAILABLE and grid_size >= 128:
235
- delta_t = 1.0 / n_steps
236
- tot_t = 2.0 * n_steps * delta_t
237
- h_mult = 2.0 / tot_t
238
- theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
239
- args = np.empty(3, dtype=np.float32)
240
- args[0] = delta_t
241
- args[1] = tot_t
242
- args[2] = h_mult
243
-
244
- # Warp size is 32:
245
- group_size = n_qubits - 1
246
- if group_size > 256:
247
- group_size = 256
248
- grid_dim = n_steps * n_qubits * group_size
249
-
250
- # Move to GPU
251
- mf = cl.mem_flags
252
- args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
253
- J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
254
- deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
255
- theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
256
- ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
257
-
258
- # Kernel execution
259
- opencl_context.maxcut_hamming_cdf_kernel(
260
- opencl_context.queue, (grid_dim,), (group_size,),
261
- np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
262
- )
263
-
264
- # Fetch results
265
- cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
266
-
267
- hamming_prob /= hamming_prob.sum()
268
- tot_prob = 0.0
269
- for i in range(n_qubits - 1):
270
- tot_prob += hamming_prob[i]
271
- hamming_prob[i] = tot_prob
272
- hamming_prob[-1] = 2.0
273
- else:
274
- maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
275
-
276
- best_solution, best_value = sample_for_solution(G_m.data, G_m.indptr, G_m.indices, shots, hamming_prob, J_eff)
277
-
278
- bit_string = ""
279
- l, r = [], []
280
- for i in range(len(best_solution)):
281
- if best_solution[i]:
282
- bit_string += "1"
283
- r.append(nodes[i])
284
- else:
285
- bit_string += "0"
286
- l.append(nodes[i])
287
-
288
- return bit_string, best_value, (l, r)
256
+ delta_t = 1.0 / n_steps
257
+ tot_t = 2.0 * n_steps * delta_t
258
+ h_mult = 2.0 / tot_t
259
+ theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
260
+ args = np.empty(3, dtype=np.float32)
261
+ args[0] = delta_t
262
+ args[1] = tot_t
263
+ args[2] = h_mult
264
+
265
+ # Warp size is 32:
266
+ group_size = n_qubits - 1
267
+ if group_size > 256:
268
+ group_size = 256
269
+ grid_dim = n_steps * n_qubits * group_size
270
+
271
+ # Move to GPU
272
+ mf = cl.mem_flags
273
+ args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
274
+ J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
275
+ deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
276
+ theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
277
+ ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
278
+
279
+ # Kernel execution
280
+ opencl_context.maxcut_hamming_cdf_kernel(
281
+ opencl_context.queue, (grid_dim,), (group_size,),
282
+ np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
283
+ )
284
+
285
+ # Fetch results
286
+ cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
287
+
288
+ return gpu_footer(shots, n_qubits, G_m.data, G_m.indptr, G_m.indices, J_eff, hamming_prob, nodes)
@@ -4,7 +4,7 @@ import numpy as np
4
4
  import os
5
5
  from numba import njit, prange
6
6
 
7
- from .maxcut_tfim_util import init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
7
+ from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, low_width, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
8
8
 
9
9
  IS_OPENCL_AVAILABLE = True
10
10
  try:
@@ -135,6 +135,36 @@ def init_J_and_z(G_func, G_func_args_tuple, nodes):
135
135
  return J_eff, degrees, G_max
136
136
 
137
137
 
138
+ @njit
139
+ def cpu_footer(shots, quality, n_qubits, G_func, G_func_args_tuple, nodes):
140
+ J_eff, degrees, G_max = init_J_and_z(G_func, G_func_args_tuple, nodes)
141
+ hamming_prob = init_thresholds(n_qubits)
142
+
143
+ maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
144
+
145
+ best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob, degrees.sum(), J_eff, n_qubits)
146
+
147
+ bit_string, l, r = get_cut(best_solution, nodes)
148
+
149
+ return bit_string, best_value, (l, r)
150
+
151
+
152
+ @njit
153
+ def gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max, J_eff, degrees, hamming_prob):
154
+ hamming_prob /= hamming_prob.sum()
155
+ tot_prob = 0.0
156
+ for i in range(n_qubits - 1):
157
+ tot_prob += hamming_prob[i]
158
+ hamming_prob[i] = tot_prob
159
+ hamming_prob[-1] = 2.0
160
+
161
+ best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob, degrees.sum(), J_eff, n_qubits)
162
+
163
+ bit_string, l, r = get_cut(best_solution, nodes)
164
+
165
+ return bit_string, best_value, (l, r)
166
+
167
+
138
168
  def maxcut_tfim_streaming(
139
169
  G_func,
140
170
  nodes,
@@ -145,18 +175,7 @@ def maxcut_tfim_streaming(
145
175
  n_qubits = len(nodes)
146
176
 
147
177
  if n_qubits < 3:
148
- if n_qubits == 0:
149
- return "", 0, ([], [])
150
-
151
- if n_qubits == 1:
152
- return "0", 0, (nodes, [])
153
-
154
- if n_qubits == 2:
155
- weight = G_func((nodes[0], nodes[1]), G_func_args_tuple)
156
- if weight < 0.0:
157
- return "00", 0, (nodes, [])
158
-
159
- return "01", weight, ([nodes[0]], [nodes[1]])
178
+ return low_width(G_m, nodes, n_qubits)
160
179
 
161
180
  if quality is None:
162
181
  quality = 8
@@ -165,64 +184,45 @@ def maxcut_tfim_streaming(
165
184
  # Number of measurement shots
166
185
  shots = n_qubits << quality
167
186
 
168
- n_steps = 1 << quality
187
+ n_steps = 2 << quality
169
188
  grid_size = n_steps * n_qubits
170
189
 
190
+ if not (IS_OPENCL_AVAILABLE and grid_size >= 128):
191
+ return cpu_footer(shots, quality, n_qubits, G_func, G_func_args_tuple, nodes)
192
+
171
193
  J_eff, degrees, G_max = init_J_and_z(G_func, G_func_args_tuple, nodes)
172
194
  hamming_prob = init_thresholds(n_qubits)
173
195
 
174
- if IS_OPENCL_AVAILABLE and grid_size >= 128:
175
- delta_t = 1.0 / n_steps
176
- tot_t = 2.0 * n_steps * delta_t
177
- h_mult = 2.0 / tot_t
178
- theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
179
- args = np.empty(3, dtype=np.float32)
180
- args[0] = delta_t
181
- args[1] = tot_t
182
- args[2] = h_mult
183
-
184
- # Warp size is 32:
185
- group_size = n_qubits - 1
186
- if group_size > 256:
187
- group_size = 256
188
- grid_dim = n_steps * n_qubits * group_size
189
-
190
- # Move to GPU
191
- mf = cl.mem_flags
192
- args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
193
- J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
194
- deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
195
- theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
196
- ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
197
-
198
- # Kernel execution
199
- opencl_context.maxcut_hamming_cdf_kernel(
200
- opencl_context.queue, (grid_dim,), (group_size,),
201
- np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
202
- )
203
-
204
- # Fetch results
205
- cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
206
-
207
- hamming_prob /= hamming_prob.sum()
208
- tot_prob = 0.0
209
- for i in range(n_qubits - 1):
210
- tot_prob += hamming_prob[i]
211
- hamming_prob[i] = tot_prob
212
- hamming_prob[-1] = 2.0
213
- else:
214
- maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
215
-
216
- best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob, degrees.sum(), J_eff, n_qubits)
217
-
218
- bit_string = ""
219
- l, r = [], []
220
- for i in range(len(best_solution)):
221
- if best_solution[i]:
222
- bit_string += "1"
223
- r.append(nodes[i])
224
- else:
225
- bit_string += "0"
226
- l.append(nodes[i])
227
-
228
- return bit_string, best_value, (l, r)
196
+ delta_t = 1.0 / n_steps
197
+ tot_t = 2.0 * n_steps * delta_t
198
+ h_mult = 2.0 / tot_t
199
+ theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
200
+ args = np.empty(3, dtype=np.float32)
201
+ args[0] = delta_t
202
+ args[1] = tot_t
203
+ args[2] = h_mult
204
+
205
+ # Warp size is 32:
206
+ group_size = n_qubits - 1
207
+ if group_size > 256:
208
+ group_size = 256
209
+ grid_dim = n_steps * n_qubits * group_size
210
+
211
+ # Move to GPU
212
+ mf = cl.mem_flags
213
+ args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
214
+ J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
215
+ deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
216
+ theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
217
+ ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
218
+
219
+ # Kernel execution
220
+ opencl_context.maxcut_hamming_cdf_kernel(
221
+ opencl_context.queue, (grid_dim,), (group_size,),
222
+ np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
223
+ )
224
+
225
+ # Fetch results
226
+ cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
227
+
228
+ return gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max, J_eff, degrees, hamming_prob)
@@ -5,6 +5,21 @@ import os
5
5
  from numba import njit, prange
6
6
 
7
7
 
8
+ @njit
9
+ def get_cut(solution, nodes):
10
+ bit_string = ""
11
+ l, r = [], []
12
+ for i in range(len(solution)):
13
+ if solution[i]:
14
+ bit_string += "1"
15
+ r.append(nodes[i])
16
+ else:
17
+ bit_string += "0"
18
+ l.append(nodes[i])
19
+
20
+ return bit_string, l, r
21
+
22
+
8
23
  @njit(parallel=True)
9
24
  def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
10
25
  theta = np.empty(n_qubits, dtype=np.float32)
@@ -45,6 +60,22 @@ def init_thresholds(n_qubits):
45
60
  return thresholds
46
61
 
47
62
 
63
+ @njit
64
+ def low_width(G_m, nodes, n_qubits):
65
+ if n_qubits == 0:
66
+ return "", 0, ([], [])
67
+
68
+ if n_qubits == 1:
69
+ return "0", 0, (nodes, [])
70
+
71
+ if n_qubits == 2:
72
+ weight = G_m[0, 1]
73
+ if weight < 0.0:
74
+ return "00", 0, (nodes, [])
75
+
76
+ return "01", weight, ([nodes[0]], [nodes[1]])
77
+
78
+
48
79
  @njit(parallel=True)
49
80
  def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
50
81
  if n_qubits < 2:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.3.1
3
+ Version: 5.3.3
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.3.1',
54
+ version='5.3.3',
55
55
  author='Dan Strano',
56
56
  author_email='stranoj@gmail.com',
57
57
  description='Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)',
File without changes
File without changes
File without changes
File without changes