PyQrackIsing 9.16.8__tar.gz → 9.16.9__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 (36) hide show
  1. {pyqrackising-9.16.8/pyqrackising.egg-info → pyqrackising-9.16.9}/PKG-INFO +1 -1
  2. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyproject.toml +1 -1
  3. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/maxcut_tfim_util.py +13 -14
  4. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/otoc.py +62 -73
  5. {pyqrackising-9.16.8 → pyqrackising-9.16.9/pyqrackising.egg-info}/PKG-INFO +1 -1
  6. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/setup.py +1 -1
  7. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/LICENSE.md +0 -0
  8. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/MANIFEST.in +0 -0
  9. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/README.md +0 -0
  10. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/__init__.py +0 -0
  11. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  12. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/generate_tfim_samples.py +0 -0
  13. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/kernels.cl +0 -0
  14. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/maxcut_tfim.py +0 -0
  15. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  16. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  17. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/solve_maxcut_bnb.py +0 -0
  18. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/solve_maxcut_bnb_sparse.py +0 -0
  19. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/solve_maxcut_bnb_streaming.py +0 -0
  20. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/solve_maxcut_exact.py +0 -0
  21. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/solve_maxcut_exact_sparse.py +0 -0
  22. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/solve_maxcut_exact_streaming.py +0 -0
  23. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/spin_glass_solver.py +0 -0
  24. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/spin_glass_solver_hybrid.py +0 -0
  25. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/spin_glass_solver_sparse.py +0 -0
  26. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  27. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/tfim_magnetization.py +0 -0
  28. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/tfim_square_magnetization.py +0 -0
  29. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/tsp.py +0 -0
  30. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising/tsp_maxcut.py +0 -0
  31. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising.egg-info/SOURCES.txt +0 -0
  32. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising.egg-info/dependency_links.txt +0 -0
  33. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising.egg-info/not-zip-safe +0 -0
  34. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising.egg-info/requires.txt +0 -0
  35. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/pyqrackising.egg-info/top_level.txt +0 -0
  36. {pyqrackising-9.16.8 → pyqrackising-9.16.9}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.16.8
3
+ Version: 9.16.9
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 @@ build-backend = "setuptools.build_meta"
7
7
 
8
8
  [project]
9
9
  name = "pyqrackising"
10
- version = "9.16.8"
10
+ version = "9.16.9"
11
11
  requires-python = ">=3.8"
12
12
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
13
13
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -559,17 +559,17 @@ def init_thresholds(n_qubits):
559
559
  n_bias = n_qubits + 1
560
560
  thresholds = np.empty(n_bias, dtype=np.float64)
561
561
  normalizer = 0
562
- for q in range(n_qubits >> 1):
562
+ for q in range(n_bias >> 1):
563
563
  normalizer += math.comb(n_qubits, q) << 1
564
- if n_qubits & 1:
564
+ if n_bias & 1:
565
565
  normalizer += math.comb(n_qubits, n_qubits >> 1)
566
- p = 1
567
- for q in range(n_qubits >> 1):
566
+ p = 1.0
567
+ for q in range(n_bias >> 1):
568
568
  val = p / normalizer
569
569
  thresholds[q] = val
570
570
  thresholds[n_bias - (q + 1)] = val
571
571
  p = math.comb(n_qubits, q + 1)
572
- if n_qubits & 1:
572
+ if n_bias & 1:
573
573
  thresholds[n_qubits >> 1] = p / normalizer
574
574
 
575
575
  return thresholds
@@ -624,7 +624,7 @@ def _apply_time_envelope(bias, t, n_qubits):
624
624
 
625
625
 
626
626
  @njit(cache=True)
627
- def probability_by_hamming_weight(J, h, z, theta, t, n_bias, normalized=True, omega=1.5 * np.pi):
627
+ def probability_by_hamming_weight(J, h, z, theta, t, n_bias, omega=1.5 * np.pi):
628
628
  zJ = z * J
629
629
  theta_c = ((np.pi if J > 0 else -np.pi) / 2) if abs(zJ) < epsilon else np.arcsin(max(-1.0, min(1.0, h / zJ)))
630
630
 
@@ -641,10 +641,7 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_bias, normalized=True, om
641
641
  print("[WARN]: probability_by_hamming_weight() went below maximum precision.")
642
642
 
643
643
  if J > 0.0:
644
- return bias[::-1]
645
-
646
- if normalized:
647
- bias /= bias.sum()
644
+ bias = bias[::-1]
648
645
 
649
646
  # Apply time-dependent envelope: maximum differential at small t,
650
647
  # recovers original distribution as t -> inf.
@@ -670,11 +667,13 @@ def maxcut_hamming_cdf(hamming_prob, n_qubits, J_func, degrees, quality, tot_t,
670
667
  t = step * delta_t
671
668
  tm1 = (step - 1) * delta_t
672
669
  h_t = h_mult * (tot_t - t)
673
- bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, t, n_bias, False, omega)
674
- last_bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_bias, False, omega)
675
- for i in range(n_bias):
676
- hamming_prob[i] += bias[i] - last_bias[i]
670
+ bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, t, n_bias, omega)
671
+ last_bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_bias, omega)
672
+ hamming_prob += bias - last_bias
677
673
 
674
+ p_min = hamming_prob.min()
675
+ if p_min < 0:
676
+ hamming_prob -= p_min
678
677
  hamming_prob /= hamming_prob.sum() - (hamming_prob[0] + hamming_prob[-1])
679
678
  tot_prob = 0.0
680
679
  n_bias -= 2
@@ -1,4 +1,4 @@
1
- from .maxcut_tfim_util import probability_by_hamming_weight, sample_mag, opencl_context
1
+ from .maxcut_tfim_util import init_thresholds, probability_by_hamming_weight, sample_mag, opencl_context
2
2
  import math
3
3
  from numba import njit
4
4
  import numpy as np
@@ -8,6 +8,29 @@ import sys
8
8
  epsilon = opencl_context.epsilon
9
9
 
10
10
 
11
+ def hadamard(dist):
12
+ """Krawtchouk/MacWilliams transform -- the bitwise Hadamard transform
13
+ restricted to Hamming-weight-symmetric distributions. Built via the
14
+ stable 3-term Krawtchouk recurrence rather than the closed-form
15
+ alternating sum of binomial coefficients, which suffers catastrophic
16
+ cancellation for larger n_qubits.
17
+
18
+ Provided by (Anthropic) Claude."""
19
+ n = len(dist) - 1
20
+ k_arr = np.arange(n + 1)
21
+ K_prev = np.ones(n + 1) # K_0(x) = 1
22
+ result = np.empty(n + 1)
23
+ result[0] = np.dot(dist, K_prev)
24
+ if n >= 1:
25
+ K_curr = n - 2 * k_arr # K_1(x) = n - 2x
26
+ result[1] = np.dot(dist, K_curr)
27
+ for j in range(1, n):
28
+ K_next = ((n - 2 * k_arr) * K_curr - (n - j + 1) * K_prev) / (j + 1)
29
+ result[j + 1] = np.dot(dist, K_next)
30
+ K_prev, K_curr = K_curr, K_next
31
+ return result / (2 ** n)
32
+
33
+
11
34
  def get_otoc_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.0, t=5, n_qubits=65, pauli_strings=["X" + "I" * 64]):
12
35
  n_bias = n_qubits + 1
13
36
  if (abs(h) <= epsilon) or (abs(t) <= epsilon):
@@ -15,62 +38,46 @@ def get_otoc_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.0, t=5, n_qubits=6
15
38
  bias[0] = 1.0
16
39
  return bias
17
40
 
18
- diff_x = np.empty(n_bias, dtype=np.float64)
19
- tot_prob = 0
20
- p = 1.0
21
- for q in range(n_qubits >> 1):
22
- diff_x[q] = p
23
- diff_x[n_bias - (q + 1)] = p
24
- tot_prob += 2 * p
25
- p = math.comb(n_qubits, q + 1)
26
- if n_qubits & 1:
27
- diff_x[n_qubits >> 1] = p
28
- tot_prob += p
29
- diff_x *= n_qubits / tot_prob
30
-
31
- signal_frac = 0.0
41
+ fwd = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1)
42
+ rev = probability_by_hamming_weight(-J, -h, z, theta + np.pi, t, n_qubits + 1)
43
+ diff_theta = (rev - fwd) / n_qubits
44
+
45
+ phi = theta + np.pi / 2
46
+ fwd = probability_by_hamming_weight(-h, -J, z, phi, t, n_qubits + 1)
47
+ rev = probability_by_hamming_weight(h, J, z, phi - np.pi, t, n_qubits + 1)
48
+ diff_phi = (rev - fwd) / n_qubits
49
+
50
+ diff_z = np.zeros(n_bias, dtype=np.float64)
51
+ diff_x = np.zeros(n_bias, dtype=np.float64)
52
+
32
53
  for pauli_string in pauli_strings:
33
54
  pauli_string = list(pauli_string)
34
55
  if len(pauli_string) != n_qubits:
35
56
  raise ValueError("OTOCS pauli_string must be same length as n_qubits! (Use 'I' for qubits that aren't changed.)")
36
57
 
37
- term_signal = pauli_string.count("X") + pauli_string.count("Z") + 2 * pauli_string.count("Y")
38
- if term_signal == 0:
39
- continue
40
-
41
- signal_frac -= term_signal
42
-
43
- fwd = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1)
44
- rev = probability_by_hamming_weight(-J, -h, z, theta + np.pi, t, n_qubits + 1)
45
- diff_theta = rev - fwd
46
-
47
- phi = theta + np.pi / 2
48
- fwd = probability_by_hamming_weight(-h, -J, z, phi, t, n_qubits + 1)
49
- rev = probability_by_hamming_weight(h, J, z, phi - np.pi, t, n_qubits + 1)
50
- diff_phi = rev - fwd
51
-
52
58
  for b in pauli_string:
53
59
  match b:
54
60
  case "X":
55
- diff_x += diff_phi
61
+ diff_z += diff_theta
56
62
  case "Z":
57
- diff_x += diff_theta
63
+ diff_x += diff_phi
58
64
  case "Y":
59
- diff_x += diff_theta + diff_phi
65
+ diff_z += diff_theta
66
+ diff_x += diff_phi
60
67
  case _:
61
68
  pass
62
69
 
63
- # Normalize:
64
- diff_x /= diff_x.sum()
65
-
66
- signal_frac = 2**signal_frac
67
- diff_z = (1 - signal_frac) * diff_x
68
- diff_z[0] += signal_frac
69
-
70
- # Normalize:
71
- diff_z /= diff_z.sum()
70
+ x_basis = init_thresholds(n_qubits) + diff_x
71
+ x_min = x_basis.min()
72
+ if x_min < 0:
73
+ x_basis -= x_min
74
+ z_basis = hadamard(x_basis) + diff_z
75
+ z_min = z_basis.min()
76
+ if z_min < 0:
77
+ z_basis -= z_min
78
+ z_basis /= z_basis.sum()
72
79
 
73
- return diff_z
80
+ return z_basis
74
81
 
75
82
 
76
83
  @njit
@@ -97,11 +104,11 @@ def factor_width(width):
97
104
 
98
105
 
99
106
  # Provided by Google search AI
100
- def find_all_str_occurrences(main_string, sub_string):
107
+ def find_all_bit_flips(main_string):
101
108
  indices = []
102
109
  start_index = 0
103
110
  while True:
104
- index = main_string.find(sub_string, start_index)
111
+ index = main_string.find("X", start_index)
105
112
  if index == -1:
106
113
  break
107
114
  indices.append(index)
@@ -143,39 +150,23 @@ def take_sample(n_qubits, sample, m, inv_dist):
143
150
  return sample
144
151
 
145
152
 
146
- def get_willow_inv_dist(butterfly_idx_x, butterfly_idx_z, n_qubits, row_len, col_len, t):
153
+ def get_willow_inv_dist(butterfly_idx_x, n_qubits, row_len, col_len, t):
147
154
  inv_dist = np.zeros(n_qubits, dtype=np.float64)
148
155
  for idx in butterfly_idx_x:
149
- b_row, b_col = divmod(idx, row_len)
150
- for q in range(n_qubits):
151
- q_row, q_col = divmod(q, row_len)
152
- inv_dist[q] += abs(q_row - b_row) + abs(q_col - b_col)
153
- for idx in butterfly_idx_z:
154
156
  b_row, b_col = divmod(idx, row_len)
155
157
  for q in range(n_qubits):
156
158
  q_row, q_col = divmod(q, row_len)
157
159
  inv_dist[q] -= abs(q_row - b_row) + abs(q_col - b_col)
158
- inv_dist = 2 ** (inv_dist / t)
160
+ inv_dist /= t
159
161
 
160
162
  return inv_dist
161
163
 
162
164
 
163
- def get_inv_dist(butterfly_idx_x, butterfly_idx_z, n_qubits, row_len, col_len, t):
165
+ def get_inv_dist(butterfly_idx_x, n_qubits, row_len, col_len, t):
164
166
  inv_dist = np.zeros(n_qubits, dtype=np.float64)
165
167
  half_row = row_len >> 1
166
168
  half_col = col_len >> 1
167
169
  for idx in butterfly_idx_x:
168
- b_row, b_col = divmod(idx, row_len)
169
- for q in range(n_qubits):
170
- q_row, q_col = divmod(q, row_len)
171
- row_d = abs(q_row - b_row)
172
- if row_d > half_row:
173
- row_d = row_len - row_d
174
- col_d = abs(q_col - b_col)
175
- if col_d > half_col:
176
- col_d = col_len - col_d
177
- inv_dist[q] += row_d + col_d
178
- for idx in butterfly_idx_z:
179
170
  b_row, b_col = divmod(idx, row_len)
180
171
  for q in range(n_qubits):
181
172
  q_row, q_col = divmod(q, row_len)
@@ -186,7 +177,7 @@ def get_inv_dist(butterfly_idx_x, butterfly_idx_z, n_qubits, row_len, col_len, t
186
177
  if col_d > half_col:
187
178
  col_d = col_len - col_d
188
179
  inv_dist[q] -= row_d + col_d
189
- inv_dist = 2 ** (inv_dist / t)
180
+ inv_dist /= t
190
181
 
191
182
  return inv_dist
192
183
 
@@ -203,19 +194,17 @@ def generate_otoc_samples(
203
194
  is_orbifold=True,
204
195
  ):
205
196
  thresholds = fix_cdf(get_otoc_hamming_distribution(J, h, z, theta, t, n_qubits, pauli_strings))
206
-
207
197
  row_len, col_len = factor_width(n_qubits)
208
198
  inv_dist = np.zeros(n_qubits, dtype=np.float64)
209
- for pauli_string in pauli_strings:
210
- if (pauli_string.count("X") + pauli_string.count("Y") + pauli_string.count("Z")) == 0:
199
+ for i, pauli_string in enumerate(pauli_strings):
200
+ if pauli_string.count("I") == n_qubits:
211
201
  continue
212
- butterfly_idx_x = find_all_str_occurrences(pauli_string, "X")
213
- butterfly_idx_z = find_all_str_occurrences(pauli_string, "Z")
202
+ butterfly_idx_x = find_all_bit_flips(pauli_string)
214
203
  if is_orbifold:
215
- inv_dist += get_inv_dist(butterfly_idx_x, butterfly_idx_z, n_qubits, row_len, col_len, t)
204
+ inv_dist = 0.5 * inv_dist + get_inv_dist(butterfly_idx_x, n_qubits, row_len, col_len, t)
216
205
  else:
217
- inv_dist += get_willow_inv_dist(butterfly_idx_x, butterfly_idx_z, n_qubits, row_len, col_len, t)
218
- inv_dist /= 2.0
206
+ inv_dist = 0.5 * inv_dist + get_willow_inv_dist(butterfly_idx_x, n_qubits, row_len, col_len, t)
207
+ inv_dist = 2 ** inv_dist
219
208
 
220
209
  qubit_pows = [1 << q for q in range(n_qubits)]
221
210
  samples = []
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.16.8
3
+ Version: 9.16.9
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.16.8",
10
+ version="9.16.9",
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
File without changes