PyQrackIsing 1.11.0__tar.gz → 1.13.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 (21) hide show
  1. {pyqrackising-1.11.0/PyQrackIsing.egg-info → pyqrackising-1.13.0}/PKG-INFO +1 -1
  2. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/PyQrackIsing/maxcut_tfim.py +141 -21
  3. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/PyQrackIsing/spin_glass_solver.py +2 -2
  4. {pyqrackising-1.11.0 → pyqrackising-1.13.0/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  5. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/README.md +4 -4
  6. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/pyproject.toml +2 -1
  7. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/setup.py +1 -1
  8. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/CMakeLists.txt +0 -0
  9. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/LICENSE.md +0 -0
  10. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/MANIFEST.in +0 -0
  11. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/PyQrackIsing/__init__.py +0 -0
  12. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  13. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/PyQrackIsing/generate_tfim_samples.py +0 -0
  14. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/PyQrackIsing/tfim_magnetization.py +0 -0
  15. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  16. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/PyQrackIsing/tsp_symmetric.py +0 -0
  17. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  18. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  19. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  20. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/PyQrackIsing.egg-info/top_level.txt +0 -0
  21. {pyqrackising-1.11.0 → pyqrackising-1.13.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.11.0
3
+ Version: 1.13.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
@@ -1,16 +1,88 @@
1
+ import math
1
2
  import networkx as nx
2
3
  import numpy as np
3
4
  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(J, h, z, theta, t, n_qubits, bias):
15
+ # critical angle
16
+ theta_c = np.arcsin(
17
+ max(
18
+ -1.0,
19
+ min(
20
+ 1.0,
21
+ (1.0 if J > 0.0 else -1.0) if abs(z * J) <= (2 ** (-54)) else (abs(h) / (z * J)),
22
+ ),
23
+ )
24
+ )
25
+
26
+ p = (
27
+ pow(2.0, abs(J / h) - 1.0)
28
+ * (1.0 + math.sin(theta - theta_c) * math.cos(1.5 * math.pi * J * t + theta) / (1.0 + math.sqrt(t)))
29
+ - 0.5
30
+ )
31
+
32
+ if (p * n_qubits) >= 1024:
33
+ for i in range(n_qubits - 1):
34
+ bias[i] = 0.0
35
+ return
36
+
37
+ tot_n = 1.0 + 1.0 / pow(2.0, p * n_qubits)
38
+ for q in range(1, n_qubits):
39
+ n = 1.0 / pow(2.0, p * q)
40
+ bias[q - 1] = n
41
+ tot_n += n
42
+ for q in range(n_qubits - 1):
43
+ bias[q] /= tot_n
44
+
45
+ if J > 0.0:
46
+ end = n_qubits - 2
47
+ for i in range((n_qubits - 1) // 2):
48
+ tmp = bias[i]
49
+ bias[i] = bias[end - i]
50
+ bias[end - i] = tmp
51
+
52
+ @cuda.jit
53
+ def cuda_maxcut_hamming_cdf(n_qubits, n_steps, delta_t, tot_t, h_mult, J_func, degrees, theta, hamming_prob):
54
+ qc = cuda.blockIdx.x * cuda.blockDim.x + cuda.threadIdx.x
55
+ step = qc // n_qubits
56
+ q = qc % n_qubits
57
+
58
+ if step > n_steps:
59
+ return
60
+
61
+ J_eff = J_func[q]
62
+ if abs(J_eff) <= (2 ** (-54)):
63
+ return
64
+
65
+ z = degrees[q]
66
+ theta_eff = theta[q]
67
+ t = step * delta_t
68
+ tm1 = (step - 1) * delta_t
69
+ h_t = h_mult * (tot_t - t)
70
+ n_bias = n_qubits - 1
71
+
72
+ bias = cuda.local.array(shape=(8192,), dtype=types.float64)
73
+ cuda_probability_by_hamming_weight(J_eff, h_t, z, theta_eff, t, n_qubits, bias)
74
+ last_bias = cuda.local.array(shape=(8192,), dtype=types.float64)
75
+ cuda_probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_qubits, last_bias)
76
+ for i in range(n_qubits - 1):
77
+ hamming_prob[i] += bias[i] - last_bias[i]
78
+
79
+ except:
80
+ IS_CUDA_AVAILABLE = False
4
81
 
5
82
 
6
83
  @njit
7
84
  def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
8
- if np.isclose(abs(J), 0.0):
9
- return np.full(n_qubits - 1, 1.0 / (n_qubits - 1.0))
10
-
11
85
  bias = np.zeros(n_qubits - 1)
12
- if np.isclose(abs(h), 0.0):
13
- return bias
14
86
 
15
87
  # critical angle
16
88
  theta_c = np.arcsin(
@@ -35,7 +107,7 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
35
107
  tot_n = 1.0 + 1.0 / pow(2.0, p * n_qubits)
36
108
  for q in range(1, n_qubits):
37
109
  n = 1.0 / pow(2.0, p * q)
38
- bias[q] = n
110
+ bias[q - 1] = n
39
111
  tot_n += n
40
112
  bias /= tot_n
41
113
 
@@ -46,16 +118,16 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
46
118
 
47
119
 
48
120
  @njit(parallel=True)
49
- def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality):
121
+ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
50
122
  if n_qubits < 2:
51
123
  return np.empty(0, dtype=np.float64)
52
124
 
53
- n_steps = n_qubits << (quality << 1)
54
- delta_t = 1.0 / (n_steps << quality)
55
- tot_t = (n_steps - 1) * delta_t
56
- h_mult = (1 << quality) / tot_t
125
+ n_steps = 1 << quality
126
+ delta_t = 1.0 / n_steps
127
+ tot_t = n_steps * delta_t
128
+ h_mult = 32.0 / tot_t
57
129
  n_bias = n_qubits - 1
58
- hamming_prob = np.full(n_bias, 1 / n_bias)
130
+
59
131
  theta = np.zeros(n_qubits)
60
132
  for q in prange(n_qubits):
61
133
  J = J_func[q]
@@ -73,13 +145,15 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality):
73
145
  for qc in prange(n_qubits, n_steps * n_qubits):
74
146
  step = qc // n_qubits
75
147
  q = qc % n_qubits
76
- t = step * delta_t
77
- tm1 = (step - 1) * delta_t
78
- z = degrees[q]
79
148
  J_eff = J_func[q]
149
+ if np.isclose(abs(J_eff), 0.0):
150
+ continue
151
+ z = degrees[q]
80
152
  theta_eff = theta[q]
153
+ t = step * delta_t
154
+ tm1 = (step - 1) * delta_t
81
155
  h_t = h_mult * (tot_t - t)
82
- bias = probability_by_hamming_weight(J_eff, h_mult, z, theta_eff, t, n_qubits)
156
+ bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, t, n_qubits)
83
157
  last_bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_qubits)
84
158
  for i in range(n_bias):
85
159
  hamming_prob[i] += bias[i] - last_bias[i]
@@ -93,8 +167,6 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality):
93
167
  hamming_prob[i] = tot_prob
94
168
  hamming_prob[-1] = 1.0
95
169
 
96
- return hamming_prob
97
-
98
170
 
99
171
  # Written by Elara (OpenAI custom GPT)
100
172
  def local_repulsion_choice(nodes, adjacency, degrees, weights, n, m):
@@ -171,7 +243,7 @@ def int_to_bitstring(integer, length):
171
243
 
172
244
  def maxcut_tfim(
173
245
  G,
174
- quality=4,
246
+ quality=8,
175
247
  shots=None,
176
248
  ):
177
249
  # Number of qubits/nodes
@@ -186,7 +258,7 @@ def maxcut_tfim(
186
258
 
187
259
  if shots is None:
188
260
  # Number of measurement shots
189
- shots = n_qubits << (quality << 1)
261
+ shots = n_qubits << quality
190
262
 
191
263
  J_eff = np.array(
192
264
  [
@@ -203,7 +275,55 @@ def maxcut_tfim(
203
275
  dtype=np.float64,
204
276
  )
205
277
  # thresholds = tfim_sampler._maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
206
- thresholds = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
278
+ n_bias = n_qubits - 1
279
+ tot_prob = 2.0
280
+ thresholds = np.zeros(n_bias)
281
+ for q in range(1, n_qubits // 2):
282
+ n = math.comb(n_qubits, q)
283
+ thresholds[q - 1] = n
284
+ thresholds[n_bias - q] = n
285
+ tot_prob += n << 1
286
+ if n_qubits & 1:
287
+ q = n_qubits // 2
288
+ n = math.comb(n_qubits, q)
289
+ thresholds[q - 1] = n
290
+ tot_prob += n
291
+ thresholds /= tot_prob
292
+
293
+ n_steps = 1 << quality
294
+ grid_size = (n_steps * n_qubits + 31) // 32
295
+
296
+ if IS_CUDA_AVAILABLE and cuda.is_available() and grid_size >= 256 and (n_qubits <= 8192):
297
+ delta_t = 1.0 / n_steps
298
+ tot_t = n_steps * delta_t
299
+ h_mult = 32.0 / tot_t
300
+
301
+ theta = np.zeros(n_qubits)
302
+ for q in range(n_qubits):
303
+ J = J_eff[q]
304
+ z = degrees[q]
305
+ theta[q] = np.arcsin(
306
+ max(
307
+ -1.0,
308
+ min(
309
+ 1.0,
310
+ (1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
311
+ ),
312
+ )
313
+ )
314
+
315
+ cuda_maxcut_hamming_cdf[grid_size, 256](n_qubits, n_steps, delta_t, tot_t, h_mult, J_eff, degrees, theta, thresholds)
316
+
317
+ tot_prob = sum(thresholds)
318
+ thresholds /= tot_prob
319
+
320
+ tot_prob = 0.0
321
+ for i in range(n_bias):
322
+ tot_prob += thresholds[i]
323
+ thresholds[i] = tot_prob
324
+ thresholds[-1] = 1.0
325
+ else:
326
+ maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, thresholds)
207
327
  G_dict = nx.to_dict_of_lists(G)
208
328
  J_max = max(J_eff)
209
329
  weights = 1.0 / (1.0 + (J_max - J_eff))
@@ -56,7 +56,7 @@ def int_to_bitstring(integer, length):
56
56
  return (bin(integer)[2:].zfill(length))[::-1]
57
57
 
58
58
 
59
- def spin_glass_solver(G, quality=4, best_guess=None):
59
+ def spin_glass_solver(G, quality=5, best_guess=None):
60
60
  nodes = list(G.nodes())
61
61
  n_qubits = len(nodes)
62
62
 
@@ -88,7 +88,7 @@ def spin_glass_solver(G, quality=4, best_guess=None):
88
88
  improved = True
89
89
  while improved:
90
90
  improved = False
91
- for k in range(1, max(2, quality - 2)):
91
+ for k in range(1, max(3, quality - 2)):
92
92
  if n_qubits < k:
93
93
  break
94
94
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.11.0
3
+ Version: 1.13.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
@@ -49,7 +49,7 @@ G = nx.petersen_graph()
49
49
  best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=8)
50
50
  ```
51
51
 
52
- The (integer) `quality` setting is optional, with a default value of `4`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
52
+ The (integer) `quality` setting is optional, with a default value of `8`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
53
53
 
54
54
  Based on a combination of the TFIM-inspired MAXCUT solver and another technique for finding ground-state energy in quantum chemistry that we call the _"binary Clifford eigensolver,"_ we also provide an (approximate) spin glass ground-state solver:
55
55
  ```py
@@ -69,10 +69,10 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
69
69
 
70
70
 
71
71
  G = generate_spin_glass_graph(n_nodes=64, seed=42)
72
- solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=8, best_guess=None)
72
+ solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=5, best_guess=None)
73
73
  # solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=8)[0])
74
74
  ```
75
- The (integer) `quality` setting is optional, with a default value of `4`. `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. 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.
75
+ The (integer) `quality` setting is optional, with a default value of `5`. `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. 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.
76
76
 
77
77
  From the `spin_glass_solver()`, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
78
78
  ```py
@@ -93,7 +93,7 @@ def generate_tsp_graph(n_nodes=64, seed=None):
93
93
 
94
94
  n_nodes = 128
95
95
  G = generate_tsp_graph(n_nodes=n_nodes, seed=42)
96
- circuit, path_length = tsp_symmetric(G, quality=8, is_cyclic=True, start_node=None)
96
+ circuit, path_length = tsp_symmetric(G, quality=1, is_cyclic=True, start_node=None)
97
97
 
98
98
  print(f"Node count: {n_nodes}")
99
99
  print(f"Path: {circuit}")
@@ -6,12 +6,13 @@ requires = [
6
6
  "networkx",
7
7
  "numpy",
8
8
  "numba",
9
+ "numba-cuda",
9
10
  ]
10
11
  build-backend = "setuptools.build_meta"
11
12
 
12
13
  [project]
13
14
  name = "PyQrackIsing"
14
- version = "1.11.0"
15
+ version = "1.13.0"
15
16
  requires-python = ">=3.8"
16
17
  description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
17
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.11.0',
54
+ version='1.13.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