PyQrackIsing 9.16.7__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.7/pyqrackising.egg-info → pyqrackising-9.16.9}/PKG-INFO +1 -1
  2. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyproject.toml +1 -1
  3. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/maxcut_tfim_util.py +13 -14
  4. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/otoc.py +68 -75
  5. {pyqrackising-9.16.7 → pyqrackising-9.16.9/pyqrackising.egg-info}/PKG-INFO +1 -1
  6. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/setup.py +1 -1
  7. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/LICENSE.md +0 -0
  8. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/MANIFEST.in +0 -0
  9. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/README.md +0 -0
  10. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/__init__.py +0 -0
  11. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  12. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/generate_tfim_samples.py +0 -0
  13. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/kernels.cl +0 -0
  14. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/maxcut_tfim.py +0 -0
  15. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  16. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  17. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/solve_maxcut_bnb.py +0 -0
  18. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/solve_maxcut_bnb_sparse.py +0 -0
  19. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/solve_maxcut_bnb_streaming.py +0 -0
  20. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/solve_maxcut_exact.py +0 -0
  21. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/solve_maxcut_exact_sparse.py +0 -0
  22. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/solve_maxcut_exact_streaming.py +0 -0
  23. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/spin_glass_solver.py +0 -0
  24. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/spin_glass_solver_hybrid.py +0 -0
  25. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/spin_glass_solver_sparse.py +0 -0
  26. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  27. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/tfim_magnetization.py +0 -0
  28. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/tfim_square_magnetization.py +0 -0
  29. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/tsp.py +0 -0
  30. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising/tsp_maxcut.py +0 -0
  31. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising.egg-info/SOURCES.txt +0 -0
  32. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising.egg-info/dependency_links.txt +0 -0
  33. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising.egg-info/not-zip-safe +0 -0
  34. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising.egg-info/requires.txt +0 -0
  35. {pyqrackising-9.16.7 → pyqrackising-9.16.9}/pyqrackising.egg-info/top_level.txt +0 -0
  36. {pyqrackising-9.16.7 → 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.7
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.7"
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,65 +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
+
32
50
  diff_z = np.zeros(n_bias, dtype=np.float64)
33
- diff_z[0] = n_qubits
51
+ diff_x = np.zeros(n_bias, dtype=np.float64)
52
+
34
53
  for pauli_string in pauli_strings:
35
54
  pauli_string = list(pauli_string)
36
55
  if len(pauli_string) != n_qubits:
37
56
  raise ValueError("OTOCS pauli_string must be same length as n_qubits! (Use 'I' for qubits that aren't changed.)")
38
57
 
39
- term_signal = 0.5 * pauli_string.count("X") + pauli_string.count("Z") + 1.5 * pauli_string.count("Y")
40
- if term_signal == 0:
41
- continue
42
-
43
- signal_frac -= term_signal
44
-
45
- fwd = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1)
46
- rev = probability_by_hamming_weight(-J, -h, z, theta + np.pi, t, n_qubits + 1)
47
- diff_theta = rev - fwd
48
-
49
- phi = theta + np.pi / 2
50
- fwd = probability_by_hamming_weight(-h, -J, z, phi, t, n_qubits + 1)
51
- rev = probability_by_hamming_weight(h, J, z, phi - np.pi, t, n_qubits + 1)
52
- diff_phi = rev - fwd
53
-
54
58
  for b in pauli_string:
55
59
  match b:
56
60
  case "X":
57
- diff_z += diff_phi
61
+ diff_z += diff_theta
58
62
  case "Z":
59
- diff_x += diff_theta
63
+ diff_x += diff_phi
60
64
  case "Y":
61
- diff_z += diff_phi
62
- diff_x += diff_theta
65
+ diff_z += diff_theta
66
+ diff_x += diff_phi
63
67
  case _:
64
68
  pass
65
69
 
66
- # Normalize:
67
- diff_z /= diff_z.sum()
68
- diff_x /= diff_x.sum()
69
-
70
- signal_frac = 2**signal_frac
71
- diff_z = signal_frac * diff_z + (1 - signal_frac) * diff_x
72
-
73
- # Normalize:
74
- 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()
75
79
 
76
- return diff_z
80
+ return z_basis
77
81
 
78
82
 
79
83
  @njit
@@ -100,11 +104,18 @@ def factor_width(width):
100
104
 
101
105
 
102
106
  # Provided by Google search AI
103
- def find_all_str_occurrences(main_string, sub_string):
107
+ def find_all_bit_flips(main_string):
104
108
  indices = []
105
109
  start_index = 0
106
110
  while True:
107
- index = main_string.find(sub_string, start_index)
111
+ index = main_string.find("X", start_index)
112
+ if index == -1:
113
+ break
114
+ indices.append(index)
115
+ start_index = index + 1 # Start searching after the found occurrence
116
+ start_index = 0
117
+ while True:
118
+ index = main_string.find("Y", start_index)
108
119
  if index == -1:
109
120
  break
110
121
  indices.append(index)
@@ -139,39 +150,23 @@ def take_sample(n_qubits, sample, m, inv_dist):
139
150
  return sample
140
151
 
141
152
 
142
- 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):
143
154
  inv_dist = np.zeros(n_qubits, dtype=np.float64)
144
155
  for idx in butterfly_idx_x:
145
- b_row, b_col = divmod(idx, row_len)
146
- for q in range(n_qubits):
147
- q_row, q_col = divmod(q, row_len)
148
- inv_dist[q] += abs(q_row - b_row) + abs(q_col - b_col)
149
- for idx in butterfly_idx_z:
150
156
  b_row, b_col = divmod(idx, row_len)
151
157
  for q in range(n_qubits):
152
158
  q_row, q_col = divmod(q, row_len)
153
159
  inv_dist[q] -= abs(q_row - b_row) + abs(q_col - b_col)
154
- inv_dist = 2 ** (inv_dist / t)
160
+ inv_dist /= t
155
161
 
156
162
  return inv_dist
157
163
 
158
164
 
159
- 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):
160
166
  inv_dist = np.zeros(n_qubits, dtype=np.float64)
161
167
  half_row = row_len >> 1
162
168
  half_col = col_len >> 1
163
169
  for idx in butterfly_idx_x:
164
- b_row, b_col = divmod(idx, row_len)
165
- for q in range(n_qubits):
166
- q_row, q_col = divmod(q, row_len)
167
- row_d = abs(q_row - b_row)
168
- if row_d > half_row:
169
- row_d = row_len - row_d
170
- col_d = abs(q_col - b_col)
171
- if col_d > half_col:
172
- col_d = col_len - col_d
173
- inv_dist[q] += row_d + col_d
174
- for idx in butterfly_idx_z:
175
170
  b_row, b_col = divmod(idx, row_len)
176
171
  for q in range(n_qubits):
177
172
  q_row, q_col = divmod(q, row_len)
@@ -182,7 +177,7 @@ def get_inv_dist(butterfly_idx_x, butterfly_idx_z, n_qubits, row_len, col_len, t
182
177
  if col_d > half_col:
183
178
  col_d = col_len - col_d
184
179
  inv_dist[q] -= row_d + col_d
185
- inv_dist = 2 ** (inv_dist / t)
180
+ inv_dist /= t
186
181
 
187
182
  return inv_dist
188
183
 
@@ -199,19 +194,17 @@ def generate_otoc_samples(
199
194
  is_orbifold=True,
200
195
  ):
201
196
  thresholds = fix_cdf(get_otoc_hamming_distribution(J, h, z, theta, t, n_qubits, pauli_strings))
202
-
203
197
  row_len, col_len = factor_width(n_qubits)
204
198
  inv_dist = np.zeros(n_qubits, dtype=np.float64)
205
- for pauli_string in pauli_strings:
206
- 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:
207
201
  continue
208
- butterfly_idx_x = find_all_str_occurrences(pauli_string, "X")
209
- butterfly_idx_z = find_all_str_occurrences(pauli_string, "Z")
202
+ butterfly_idx_x = find_all_bit_flips(pauli_string)
210
203
  if is_orbifold:
211
- 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)
212
205
  else:
213
- inv_dist += get_willow_inv_dist(butterfly_idx_x, butterfly_idx_z, n_qubits, row_len, col_len, t)
214
- 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
215
208
 
216
209
  qubit_pows = [1 << q for q in range(n_qubits)]
217
210
  samples = []
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.16.7
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.7",
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