PyQrackIsing 1.27.1__tar.gz → 1.28.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 (22) hide show
  1. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/MANIFEST.in +1 -0
  2. {pyqrackising-1.27.1/PyQrackIsing.egg-info → pyqrackising-1.28.0}/PKG-INFO +1 -1
  3. pyqrackising-1.28.0/PyQrackIsing/kernels.cl +70 -0
  4. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/PyQrackIsing/maxcut_tfim.py +46 -88
  5. {pyqrackising-1.27.1 → pyqrackising-1.28.0/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  6. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/PyQrackIsing.egg-info/SOURCES.txt +1 -0
  7. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/pyproject.toml +1 -1
  8. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/setup.py +1 -1
  9. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/CMakeLists.txt +0 -0
  10. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/LICENSE.md +0 -0
  11. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/PyQrackIsing/__init__.py +0 -0
  12. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  13. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/PyQrackIsing/generate_tfim_samples.py +0 -0
  14. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/PyQrackIsing/spin_glass_solver.py +0 -0
  15. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/PyQrackIsing/tfim_magnetization.py +0 -0
  16. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  17. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/PyQrackIsing/tsp_symmetric.py +0 -0
  18. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  19. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  20. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/PyQrackIsing.egg-info/top_level.txt +0 -0
  21. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/README.md +0 -0
  22. {pyqrackising-1.27.1 → pyqrackising-1.28.0}/setup.cfg +0 -0
@@ -1,2 +1,3 @@
1
1
  include CMakeLists.txt
2
2
  include PyQrackIsing/_pyqrack_ising.cpp
3
+ include PyQrackIsing/kernels.cl
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.27.1
3
+ Version: 1.28.0
4
4
  Summary: Near-ideal closed-form solutions for transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -0,0 +1,70 @@
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
+
@@ -1,83 +1,14 @@
1
1
  import math
2
2
  import networkx as nx
3
3
  import numpy as np
4
+ import os
4
5
  from numba import njit, prange
5
- IS_CUDA_AVAILABLE = True
6
- try:
7
- from numba import cuda, types
8
- from numba.core.errors import NumbaPerformanceWarning
9
- import warnings
10
-
11
- warnings.simplefilter('ignore', category=NumbaPerformanceWarning)
12
-
13
- @cuda.jit(device=True)
14
- def cuda_probability_by_hamming_weight(q, J, h, z, theta, t, n_qubits):
15
- # critical angle
16
- theta_c = np.arcsin(max(-1.0, min(1.0, abs(h) / (z * J))))
17
-
18
- p = (
19
- pow(2.0, abs(J / h) - 1.0)
20
- * (1.0 + math.sin(theta - theta_c) * math.cos(1.5 * math.pi * J * t + theta) / (1.0 + math.sqrt(t)))
21
- - 0.5
22
- )
23
-
24
- if (p * (n_qubits + 2.0)) >= 1024.0:
25
- return 0.0
26
-
27
- result = (pow(2.0, (n_qubits + 2.0) * p) - 1.0) * pow(2.0, -((n_qubits + 1.0) * p) - p * q) / (pow(2.0, p) - 1.0)
28
6
 
29
- if math.isnan(result) or math.isinf(result):
30
- return 0.0
31
-
32
- return result
33
-
34
- @cuda.jit
35
- def cuda_maxcut_hamming_cdf_wide(delta_t, tot_t, h_mult, J_func, degrees, theta, hamming_prob):
36
- step = cuda.blockIdx.x
37
- qi = cuda.blockIdx.y
38
- J_eff = J_func[qi]
39
- z = degrees[qi]
40
- if abs(z * J_eff) <= (2 ** (-52)):
41
- return
42
-
43
- n_qubits = cuda.gridDim.y
44
- theta_eff = theta[qi]
45
- t = step * delta_t
46
- tm1 = (step - 1) * delta_t
47
- h_t = h_mult * (tot_t - t)
48
-
49
- qo = cuda.threadIdx.x
50
- if J_eff > 0.0:
51
- qo = cuda.blockDim.x - (1 + qo)
52
- diff = cuda_probability_by_hamming_weight(qo, J_eff, h_t, z, theta_eff, t, n_qubits)
53
- diff -= cuda_probability_by_hamming_weight(qo, J_eff, h_t, z, theta_eff, tm1, n_qubits)
54
- hamming_prob[qo] += diff
55
-
56
- @cuda.jit
57
- def cuda_maxcut_hamming_cdf(n_qubits, delta_t, tot_t, h_mult, J_func, degrees, theta, hamming_prob):
58
- step = cuda.blockIdx.x // n_qubits
59
- qi = cuda.blockIdx.x % n_qubits
60
- J_eff = J_func[qi]
61
- z = degrees[qi]
62
- if abs(z * J_eff) <= (2 ** (-52)):
63
- return
64
-
65
- theta_eff = theta[qi]
66
- t = step * delta_t
67
- tm1 = (step - 1) * delta_t
68
- h_t = h_mult * (tot_t - t)
69
-
70
- n_threads = cuda.blockDim.x
71
- qo = cuda.threadIdx.x
72
- while qo < n_qubits:
73
- _qo = (n_qubits - (1 + qo)) if J_eff > 0.0 else qo
74
- diff = cuda_probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, t, n_qubits)
75
- diff -= cuda_probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, tm1, n_qubits)
76
- hamming_prob[_qo] += diff
77
- qo += n_threads
78
-
79
- except:
80
- IS_CUDA_AVAILABLE = False
7
+ IS_OPENCL_AVAILABLE = True
8
+ try:
9
+ import pyopencl as cl
10
+ except ImportError:
11
+ IS_OPENCL_AVAILABLE = False
81
12
 
82
13
 
83
14
  @njit
@@ -387,32 +318,59 @@ def maxcut_tfim(
387
318
  grid_size = n_steps * n_qubits
388
319
 
389
320
  J_eff, degrees = init_J_and_z(G_m)
390
- thresholds = init_thresholds(n_qubits)
321
+ hamming_prob = init_thresholds(n_qubits)
322
+
323
+ if IS_OPENCL_AVAILABLE and grid_size >= 128:
324
+ # Pick a device (GPU if available)
325
+ ctx = cl.create_some_context()
326
+ queue = cl.CommandQueue(ctx)
327
+
328
+ # Load and build OpenCL kernels
329
+ kernel_src = open(os.path.dirname(os.path.abspath(__file__)) + "/kernels.cl").read()
330
+ program = cl.Program(ctx, kernel_src).build()
391
331
 
392
- if IS_CUDA_AVAILABLE and cuda.is_available() and grid_size >= 128:
393
332
  delta_t = 1.0 / n_steps
394
333
  tot_t = 2.0 * n_steps * delta_t
395
334
  h_mult = 2.0 / tot_t
396
335
  theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
336
+ args = np.empty(3, dtype=np.float32)
337
+ args[0] = delta_t
338
+ args[1] = tot_t
339
+ args[2] = h_mult
397
340
 
398
341
  # Warp size is 32:
399
342
  group_size = n_qubits - 1
400
- grid_dims = (n_steps, n_qubits)
401
- if group_size <= 256:
402
- cuda_maxcut_hamming_cdf_wide[grid_dims, group_size](delta_t, tot_t, h_mult, J_eff, degrees, theta, thresholds)
403
- else:
404
- cuda_maxcut_hamming_cdf[n_steps * n_qubits, 128](n_qubits, delta_t, tot_t, h_mult, J_eff, degrees, theta, thresholds)
343
+ if group_size > 256:
344
+ group_size = 256
345
+ grid_dim = n_steps * n_qubits
346
+
347
+ # Move to GPU
348
+ mf = cl.mem_flags
349
+ args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
350
+ J_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
351
+ deg_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
352
+ theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
353
+ ham_buf = cl.Buffer(ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
354
+
355
+ # Kernel execution
356
+ program.maxcut_hamming_cdf(
357
+ queue, (grid_dim,), (group_size,),
358
+ np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
359
+ )
360
+
361
+ # Fetch results
362
+ cl.enqueue_copy(queue, hamming_prob, ham_buf)
405
363
 
406
- thresholds /= thresholds.sum()
364
+ hamming_prob /= hamming_prob.sum()
407
365
  tot_prob = 0.0
408
366
  for i in range(n_qubits - 1):
409
- tot_prob += thresholds[i]
410
- thresholds[i] = tot_prob
411
- thresholds[-1] = 2.0
367
+ tot_prob += hamming_prob[i]
368
+ hamming_prob[i] = tot_prob
369
+ hamming_prob[-1] = 2.0
412
370
  else:
413
- maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, thresholds)
371
+ maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
414
372
 
415
- best_solution, best_value = sample_for_solution(G_m, shots, thresholds, degrees, J_eff, n_qubits)
373
+ best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, degrees, J_eff, n_qubits)
416
374
 
417
375
  bit_string = "".join(["1" if b else "0" for b in best_solution])
418
376
  bit_list = list(bit_string)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.27.1
3
+ Version: 1.28.0
4
4
  Summary: Near-ideal closed-form solutions for transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -7,6 +7,7 @@ setup.py
7
7
  PyQrackIsing/__init__.py
8
8
  PyQrackIsing/_pyqrack_ising.cpp
9
9
  PyQrackIsing/generate_tfim_samples.py
10
+ PyQrackIsing/kernels.cl
10
11
  PyQrackIsing/maxcut_tfim.py
11
12
  PyQrackIsing/spin_glass_solver.py
12
13
  PyQrackIsing/tfim_magnetization.py
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "PyQrackIsing"
15
- version = "1.27.1"
15
+ version = "1.28.0"
16
16
  requires-python = ">=3.8"
17
17
  description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
18
18
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='PyQrackIsing',
54
- version='1.27.1',
54
+ version='1.28.0',
55
55
  author='Dan Strano',
56
56
  author_email='stranoj@gmail.com',
57
57
  description='Near-ideal closed-form solutions for transverse field Ising model (TFIM)',
File without changes
File without changes
File without changes