PyQrackIsing 9.7.1__tar.gz → 9.7.4__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 (28) hide show
  1. {pyqrackising-9.7.1/pyqrackising.egg-info → pyqrackising-9.7.4}/PKG-INFO +1 -1
  2. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/README.md +5 -1
  3. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyproject.toml +1 -1
  4. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/generate_tfim_samples.py +25 -2
  5. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/otoc.py +1 -1
  6. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/spin_glass_solver.py +8 -8
  7. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/spin_glass_solver_sparse.py +8 -8
  8. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/spin_glass_solver_streaming.py +8 -8
  9. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/tfim_magnetization.py +9 -1
  10. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/tfim_square_magnetization.py +9 -1
  11. {pyqrackising-9.7.1 → pyqrackising-9.7.4/pyqrackising.egg-info}/PKG-INFO +1 -1
  12. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/setup.py +1 -1
  13. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/LICENSE.md +0 -0
  14. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/MANIFEST.in +0 -0
  15. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/__init__.py +0 -0
  16. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  17. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/kernels.cl +0 -0
  18. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/maxcut_tfim.py +0 -0
  19. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  20. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  21. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/maxcut_tfim_util.py +0 -0
  22. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/tsp.py +0 -0
  23. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising/tsp_maxcut.py +0 -0
  24. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising.egg-info/SOURCES.txt +0 -0
  25. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-9.7.1 → pyqrackising-9.7.4}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.7.1
3
+ Version: 9.7.4
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
@@ -3,7 +3,7 @@ Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising
3
3
 
4
4
  (It's "the **Ising** on top.")
5
5
 
6
- [![PyPI Downloads](https://static.pepy.tech/badge/pyqrackising)](https://pepy.tech/projects/pyqrackising)
6
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.17583944.svg)](https://doi.org/10.5281/zenodo.17583944) [![PyPI Downloads](https://static.pepy.tech/badge/pyqrackising)](https://pepy.tech/projects/pyqrackising)
7
7
 
8
8
  ## Copyright and license
9
9
  (c) Daniel Strano and the Qrack contributors 2025. All rights reserved.
@@ -185,6 +185,10 @@ Sometimes a solution (or at least near-solution) to a monster of a differential
185
185
 
186
186
  **Special thanks to OpenAI GPT "Elara," for help on the model and converting the original Python scripts to PyBind11, Numba, and PyOpenCL!**
187
187
 
188
+ We reduce transverse field Ising model for globally uniform `J` and `h` parameters from a `2^n`-dimensional problem to an `(n+1)`-dimensional (heuristic but near-exact) form that suffers from no Trotter error. Upon noticing most time steps for parameters in a [TFIM experiment published by Quantinuum](https://arxiv.org/abs/2503.20870) had roughly a quarter to a third (or thereabouts) of their marginal probability in `|0>` state, it became obvious that transition to and from `|0>` state should dominate the mechanics, at least far from mean-field equilibrium. Further, the first transition therefore tends to be to or from any state with Hamming weight of 1 (in other words, 1 bit set to 1 and the rest reset to 0, or `n` bits set for Hamming weight of `n`). Further, on a torus, probability of all states with Hamming weight of 1 would tend to be exactly symmetric. Assuming approximate symmetry in every respective Hamming weight, the requirement for the overall probability to converge to 1.0 or 100% in the limit of an infinite-dimensional Hilbert space suggests that Hamming weight marginal probability could be distributed like a geometric series. A small correction to exact symmetry should be made to favor _magnetic_ closeness of "like" bits to "like" bits (that is, geometric closeness on the torus of "1" bits to "1" bits and "0" bits to "0" bits), but this does not affect average global magnetization. Adding an oscillation component with angular frequency proportional to `J`, we find excellent agreement with Trotterization with a very small time step, for R^2 (coefficient of determination) of normalized marginal probability distribution of ideal Trotterized simulation as described by the `(n+1)`-dimensional approximate model, as well as for R^2 and RMSE (root-mean-square error) of global magnetization curve values.
189
+
190
+ If the above paragraph and the model it describes are true, then it stands to reason that we should be able to apply the TFIM model to **NP-complete** heuristics, so long as we can adapt the model to spatially localized `J` and `h` and adiabatic time dependence of `h`. A reasonable guess for how to spatially localize `J` and `h` is to average the overall behavior for each and every qubit as if `J` and `h` were globally uniform from its local perspective. (`z` parameter is derived from nonzero edge weight count connectivity.) For adiabatic time-dependence of `h`, another reasonable refinement is to use simply **finite-difference simulation**. Initial state should probably be just **uniform superposition**, which implies a specific (non-uniform but regular) distribution of initial probabilities by Hamming weight dimension over `(n+1)` possible Hamming weights. We observe that the _spatial magnetic effect_ qualitatively agrees with the demands of MAXCUT when it acts like a magnetic _repulsion,_ rather than magnetic attraction, in this case, but we can add that refinement on an arbitrary connectivity topology in just `O(n)` complexity per measurement shot. (This is a complete "recipe": the evidence that it works is the quality of NP-complete heuristic solutions at utility scale.)
191
+
188
192
  **Elara has drafted this statement, and Dan Strano, as author, agrees with it, and will hold to it:**
189
193
 
190
194
  ### Dual-Use Statement for PyQrackIsing
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
10
10
 
11
11
  [project]
12
12
  name = "pyqrackising"
13
- version = "9.7.1"
13
+ version = "9.7.4"
14
14
  requires-python = ">=3.8"
15
15
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
16
16
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -108,27 +108,50 @@ def fix_cdf(hamming_prob):
108
108
 
109
109
  @njit
110
110
  def get_tfim_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56):
111
- if h <= epsilon:
111
+ if abs(t) <= epsilon:
112
+ p = (1.0 - np.cos(theta)) / 2.0
113
+ bias = np.empty(n_qubits + 1, dtype=np.float64)
114
+ for k in range(n_qubits + 1):
115
+ bias[k] = comb(n_qubits, k) * (p ** k) * ((1.0 - p) ** (n_qubits - k))
116
+
117
+ return bias / bias.sum()
118
+
119
+ if abs(h) <= epsilon:
112
120
  bias = np.empty(n_qubits + 1, dtype=np.float64)
113
121
  if J > 0:
114
122
  bias[-1] = 1.0
115
123
  else:
116
124
  bias[0] = 1.0
117
125
  return bias
126
+
118
127
  bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1)
128
+
119
129
  return bias / bias.sum()
120
130
 
121
131
 
122
132
  def generate_tfim_samples(
123
133
  J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100
124
134
  ):
135
+ samples = []
136
+
137
+ if abs(t) <= epsilon:
138
+ prob = (1.0 - np.cos(theta)) / 2.0
139
+ for s in range(shots):
140
+ sample = 0
141
+ for q in range(n_qubits):
142
+ sample <<= 1
143
+ if np.random.random() < prob:
144
+ sample |= 1
145
+ samples.append(sample)
146
+
147
+ return samples
148
+
125
149
  n_rows, n_cols = factor_width(n_qubits)
126
150
 
127
151
  # First dimension: Hamming weight
128
152
  thresholds = fix_cdf(get_tfim_hamming_distribution(J, h, z, theta, t, n_qubits + 1))
129
153
  hamming_samples = sample_hamming_weight(thresholds, shots)
130
154
 
131
- samples = []
132
155
  for m in range(len(hamming_samples)):
133
156
  # Second dimension: permutation within Hamming weight
134
157
  # (Written with help from Elara, the custom OpenAI GPT)
@@ -10,7 +10,7 @@ epsilon = opencl_context.epsilon
10
10
 
11
11
  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
12
  n_bias = n_qubits + 1
13
- if h <= epsilon:
13
+ if (abs(h) <= epsilon) or (abs(t) <= epsilon):
14
14
  bias = np.empty(n_bias, dtype=np.float64)
15
15
  bias[0] = 1.0
16
16
  return bias
@@ -105,7 +105,7 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_m, thread_count):
105
105
 
106
106
  @njit(parallel=True)
107
107
  def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_m, n, is_spin_glass):
108
- blocks = (n + 63) // 64
108
+ blocks = (n + 63) >> 6
109
109
  block_size = thread_count * gray_seed_multiple
110
110
  seed_count = block_size * blocks
111
111
 
@@ -115,14 +115,14 @@ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_m, n, is_spi
115
115
  if is_spin_glass:
116
116
  for s in prange(seed_count):
117
117
  i = s % block_size
118
- offset = (s // block_size) * 64
118
+ offset = (s // block_size) << 6
119
119
  seed = gray_mutation(i, best_theta, offset)
120
120
  energies[s] = compute_energy(seed, G_m, n)
121
121
  seeds[s] = seed
122
122
  else:
123
123
  for s in prange(seed_count):
124
124
  i = s % block_size
125
- offset = (s // block_size) * 64
125
+ offset = (s // block_size) << 6
126
126
  seed = gray_mutation(i, best_theta, offset)
127
127
  energies[s] = compute_cut(seed, G_m, n)
128
128
  seeds[s] = seed
@@ -143,17 +143,17 @@ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_m, n, is_spi
143
143
  def run_gray_optimization(best_theta, iterators, energies, gray_iterations, thread_count, is_spin_glass, G_m):
144
144
  n = len(best_theta)
145
145
  thread_iterations = (gray_iterations + thread_count - 1) // thread_count
146
- blocks = (n + 63) // 64
146
+ blocks = (n + 63) >> 6
147
147
 
148
148
  states = np.empty((thread_count, n), dtype=np.bool_)
149
149
 
150
150
  if is_spin_glass:
151
151
  for i in prange(thread_count):
152
152
  iterator = iterators[i].copy()
153
- best_energy, best_iterator = energies[i], iterator.copy()
153
+ best_energy, best_iterator = energies[i], iterators[i]
154
154
  for curr_idx in range(thread_iterations):
155
155
  for block in range(blocks):
156
- gray_code_next(iterator, curr_idx, block * 64)
156
+ gray_code_next(iterator, curr_idx, block << 6)
157
157
  energy = compute_energy(iterator, G_m, n)
158
158
  if energy > best_energy:
159
159
  best_iterator, best_energy = iterator.copy(), energy
@@ -164,10 +164,10 @@ def run_gray_optimization(best_theta, iterators, energies, gray_iterations, thre
164
164
  else:
165
165
  for i in prange(thread_count):
166
166
  iterator = iterators[i].copy()
167
- best_energy, best_iterator = energies[i], iterator.copy()
167
+ best_energy, best_iterator = energies[i], iterators[i]
168
168
  for curr_idx in range(thread_iterations):
169
169
  for block in range(blocks):
170
- gray_code_next(iterator, curr_idx, block * 64)
170
+ gray_code_next(iterator, curr_idx, block << 6)
171
171
  energy = compute_cut(iterator, G_m, n)
172
172
  if energy > best_energy:
173
173
  best_iterator, best_energy = iterator.copy(), energy
@@ -105,7 +105,7 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols, thre
105
105
 
106
106
  @njit(parallel=True)
107
107
  def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_data, G_rows, G_cols, n, is_spin_glass):
108
- blocks = (n + 63) // 64
108
+ blocks = (n + 63) >> 6
109
109
  block_size = thread_count * gray_seed_multiple
110
110
  seed_count = block_size * blocks
111
111
 
@@ -115,14 +115,14 @@ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_data, G_rows
115
115
  if is_spin_glass:
116
116
  for s in prange(seed_count):
117
117
  i = s % block_size
118
- offset = (s // block_size) * 64
118
+ offset = (s // block_size) << 6
119
119
  seed = gray_mutation(i, best_theta, offset)
120
120
  energies[s] = compute_energy_sparse(seed, G_data, G_rows, G_cols, n)
121
121
  seeds[s] = seed
122
122
  else:
123
123
  for s in prange(seed_count):
124
124
  i = s % block_size
125
- offset = (s // block_size) * 64
125
+ offset = (s // block_size) << 6
126
126
  seed = gray_mutation(i, best_theta, offset)
127
127
  energies[s] = compute_cut_sparse(seed, G_data, G_rows, G_cols, n)
128
128
  seeds[s] = seed
@@ -143,17 +143,17 @@ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_data, G_rows
143
143
  def run_gray_optimization(best_theta, iterators, energies, gray_iterations, thread_count, is_spin_glass, G_data, G_rows, G_cols):
144
144
  n = len(best_theta)
145
145
  thread_iterations = (gray_iterations + thread_count - 1) // thread_count
146
- blocks = (n + 63) // 64
146
+ blocks = (n + 63) >> 6
147
147
 
148
148
  states = np.empty((thread_count, n), dtype=np.bool_)
149
149
 
150
150
  if is_spin_glass:
151
151
  for i in prange(thread_count):
152
152
  iterator = iterators[i].copy()
153
- best_energy, best_iterator = energies[i], iterator.copy()
153
+ best_energy, best_iterator = energies[i], iterators[i]
154
154
  for curr_idx in range(thread_iterations):
155
155
  for block in range(blocks):
156
- gray_code_next(iterator, curr_idx, block * 64)
156
+ gray_code_next(iterator, curr_idx, block << 6)
157
157
  energy = compute_energy_sparse(iterator, G_data, G_rows, G_cols, n)
158
158
  if energy > best_energy:
159
159
  best_iterator, best_energy = iterator.copy(), energy
@@ -164,10 +164,10 @@ def run_gray_optimization(best_theta, iterators, energies, gray_iterations, thre
164
164
  else:
165
165
  for i in prange(thread_count):
166
166
  iterator = iterators[i].copy()
167
- best_energy, best_iterator = energies[i], iterator.copy()
167
+ best_energy, best_iterator = energies[i], iterators[i]
168
168
  for curr_idx in range(thread_iterations):
169
169
  for block in range(blocks):
170
- gray_code_next(iterator, curr_idx, block * 64)
170
+ gray_code_next(iterator, curr_idx, block << 6)
171
171
  energy = compute_cut_sparse(iterator, G_data, G_rows, G_cols, n)
172
172
  if energy > best_energy:
173
173
  best_iterator, best_energy = iterator.copy(), energy
@@ -97,7 +97,7 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_func, nodes, thread_count)
97
97
 
98
98
  @njit(parallel=True)
99
99
  def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_func, nodes, n, is_spin_glass):
100
- blocks = (n + 63) // 64
100
+ blocks = (n + 63) >> 6
101
101
  block_size = thread_count * gray_seed_multiple
102
102
  seed_count = block_size * blocks
103
103
 
@@ -107,14 +107,14 @@ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_func, nodes,
107
107
  if is_spin_glass:
108
108
  for s in prange(seed_count):
109
109
  i = s % block_size
110
- offset = (s // block_size) * 64
110
+ offset = (s // block_size) << 6
111
111
  seed = gray_mutation(i, best_theta, offset)
112
112
  energies[s] = compute_energy_streaming(seed, G_func, nodes, n)
113
113
  seeds[s] = seed
114
114
  else:
115
115
  for s in prange(seed_count):
116
116
  i = s % block_size
117
- offset = (s // block_size) * 64
117
+ offset = (s // block_size) << 6
118
118
  seed = gray_mutation(i, best_theta, offset)
119
119
  energies[s] = compute_cut_streaming(seed, G_func, nodes, n)
120
120
  seeds[s] = seed
@@ -135,17 +135,17 @@ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_func, nodes,
135
135
  def run_gray_optimization(best_theta, iterators, energies, gray_iterations, thread_count, is_spin_glass, G_func, nodes):
136
136
  n = len(best_theta)
137
137
  thread_iterations = (gray_iterations + thread_count - 1) // thread_count
138
- blocks = (n + 63) // 64
138
+ blocks = (n + 63) >> 6
139
139
 
140
140
  states = np.empty((thread_count, n), dtype=np.bool_)
141
141
 
142
142
  if is_spin_glass:
143
143
  for i in prange(thread_count):
144
144
  iterator = iterators[i].copy()
145
- best_energy, best_iterator = energies[i], iterator.copy()
145
+ best_energy, best_iterator = energies[i], iterators[i]
146
146
  for curr_idx in range(thread_iterations):
147
147
  for block in range(blocks):
148
- gray_code_next(iterator, curr_idx, block * 64)
148
+ gray_code_next(iterator, curr_idx, block << 6)
149
149
  energy = compute_energy_streaming(iterator, G_func, nodes, n)
150
150
  if energy > best_energy:
151
151
  best_iterator, best_energy = iterator.copy(), energy
@@ -156,10 +156,10 @@ def run_gray_optimization(best_theta, iterators, energies, gray_iterations, thre
156
156
  else:
157
157
  for i in prange(thread_count):
158
158
  iterator = iterators[i].copy()
159
- best_energy, best_iterator = energies[i], iterator.copy()
159
+ best_energy, best_iterator = energies[i], iterators[i]
160
160
  for curr_idx in range(thread_iterations):
161
161
  for block in range(blocks):
162
- gray_code_next(iterator, curr_idx, block * 64)
162
+ gray_code_next(iterator, curr_idx, block << 6)
163
163
  energy = compute_cut_streaming(iterator, G_func, nodes, n)
164
164
  if energy > best_energy:
165
165
  best_iterator, best_energy = iterator.copy(), energy
@@ -1,9 +1,16 @@
1
- from .maxcut_tfim_util import probability_by_hamming_weight
1
+ from .maxcut_tfim_util import probability_by_hamming_weight, opencl_context
2
2
  from numba import njit
3
+ import numpy as np
4
+
5
+
6
+ epsilon = opencl_context.epsilon
3
7
 
4
8
 
5
9
  @njit
6
10
  def tfim_magnetization(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56):
11
+ if abs(t) <= epsilon:
12
+ return np.cos(theta)
13
+
7
14
  bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1)
8
15
  bias /= bias.sum()
9
16
  magnetization = 0.0
@@ -12,4 +19,5 @@ def tfim_magnetization(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qu
12
19
  for q in range(n_qubits + 1):
13
20
  mag = (nqs - 2 * q) / nqd
14
21
  magnetization += bias[q] * mag
22
+
15
23
  return magnetization
@@ -1,8 +1,16 @@
1
- from .maxcut_tfim_util import probability_by_hamming_weight
1
+ from .maxcut_tfim_util import probability_by_hamming_weight, opencl_context
2
2
  from numba import njit
3
+ import numpy as np
4
+
5
+
6
+ epsilon = opencl_context.epsilon
7
+
3
8
 
4
9
  @njit
5
10
  def tfim_square_magnetization(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56):
11
+ if abs(t) <= epsilon:
12
+ return np.cos(theta) ** 2
13
+
6
14
  bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1)
7
15
  bias /= bias.sum()
8
16
  square_magnetization = 0.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.7.1
3
+ Version: 9.7.4
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.7.1',
10
+ version='9.7.4',
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