PyQrackIsing 9.7.6__tar.gz → 9.7.8__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.6/pyqrackising.egg-info → pyqrackising-9.7.8}/PKG-INFO +1 -1
  2. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/README.md +1 -1
  3. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyproject.toml +1 -1
  4. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/generate_tfim_samples.py +34 -20
  5. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/kernels.cl +92 -60
  6. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/spin_glass_solver.py +8 -11
  7. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/spin_glass_solver_sparse.py +5 -5
  8. {pyqrackising-9.7.6 → pyqrackising-9.7.8/pyqrackising.egg-info}/PKG-INFO +1 -1
  9. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/setup.py +1 -1
  10. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/LICENSE.md +0 -0
  11. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/MANIFEST.in +0 -0
  12. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/__init__.py +0 -0
  13. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  14. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/maxcut_tfim.py +0 -0
  15. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  16. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  17. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/maxcut_tfim_util.py +0 -0
  18. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/otoc.py +0 -0
  19. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  20. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/tfim_magnetization.py +0 -0
  21. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/tfim_square_magnetization.py +0 -0
  22. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/tsp.py +0 -0
  23. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/tsp_maxcut.py +0 -0
  24. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising.egg-info/SOURCES.txt +0 -0
  25. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-9.7.6 → pyqrackising-9.7.8}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.7.6
3
+ Version: 9.7.8
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
- [![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)
6
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.17618663.svg)](https://doi.org/10.5281/zenodo.17618663) [![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.
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
10
10
 
11
11
  [project]
12
12
  name = "pyqrackising"
13
- version = "9.7.6"
13
+ version = "9.7.8"
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"}
@@ -5,6 +5,8 @@ import sys
5
5
  import numpy as np
6
6
  from numba import njit
7
7
 
8
+ from collections import Counter
9
+
8
10
 
9
11
  epsilon = opencl_context.epsilon
10
12
 
@@ -89,7 +91,7 @@ def expected_closeness_weight(n_rows, n_cols, hamming_weight):
89
91
  def sample_hamming_weight(thresholds, shots):
90
92
  hamming_samples = np.zeros(shots, dtype=np.int32)
91
93
  for s in range(shots):
92
- hamming_samples[s] = sample_mag(thresholds) + 1
94
+ hamming_samples[s] = sample_mag(thresholds)
93
95
 
94
96
  return hamming_samples
95
97
 
@@ -101,7 +103,7 @@ def fix_cdf(hamming_prob):
101
103
  for i in range(n_bias):
102
104
  tot_prob += hamming_prob[i]
103
105
  cum_prob[i] = tot_prob
104
- cum_prob[-1] = 2.0
106
+ cum_prob[-1] = 1.0
105
107
 
106
108
  return cum_prob
107
109
 
@@ -149,32 +151,44 @@ def generate_tfim_samples(
149
151
  n_rows, n_cols = factor_width(n_qubits)
150
152
 
151
153
  # First dimension: Hamming weight
152
- thresholds = fix_cdf(get_tfim_hamming_distribution(J, h, z, theta, t, n_qubits + 1))
153
- hamming_samples = sample_hamming_weight(thresholds, shots)
154
-
155
- for m in range(len(hamming_samples)):
156
- # Second dimension: permutation within Hamming weight
157
- # (Written with help from Elara, the custom OpenAI GPT)
158
- hs = hamming_samples[m]
159
- rands = []
160
- for s in range(hs):
161
- rands.append(np.random.random())
154
+ bias = get_tfim_hamming_distribution(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits)
155
+ thresholds = fix_cdf(bias)
156
+ hamming_samples = dict(Counter(sample_hamming_weight(thresholds, shots)))
157
+
158
+ for h_weight, count in hamming_samples.items():
159
+ if h_weight == 0:
160
+ samples += count * [0]
161
+ continue
162
+
163
+ if h_weight == n_qubits:
164
+ samples += count * [(1 << n_qubits) - 1]
165
+ continue
166
+
167
+ rands = [np.random.random() for _ in range(count)]
162
168
  rands.sort()
163
169
  state_int = 0
164
170
  tot_prob = 0
165
171
  s = 0
166
- for combo in itertools.combinations(range(n_qubits), m):
172
+ # How closely grouped are "like" bits to "like"?
173
+ expected_closeness = expected_closeness_weight(n_rows, n_cols, h_weight)
174
+ h_weight_combos = math.comb(n_qubits, h_weight)
175
+ for combo in itertools.combinations(range(n_qubits), h_weight):
167
176
  state_int = sum(1 << pos for pos in combo)
168
- tot_prob += (1.0 + closeness_like_bits(state_int, n_rows, n_cols)) / (
169
- 1.0 + expected_closeness_weight(n_rows, n_cols, m)
170
- )
171
- while (s < hs) and (rands[s] <= tot_prob):
177
+ # When we add all "closeness" possibilities for the particular Hamming weight, we should maintain the (n+1) mean probability dimensions.
178
+ normed_closeness = (1 + closeness_like_bits(state_int, n_rows, n_cols)) / (1 + expected_closeness)
179
+ # Use a normalized weighted average that favors the (n+1)-dimensional model at later times.
180
+ # The (n+1)-dimensional marginal probability is the product of a function of Hamming weight and "closeness," split among all basis states with that specific Hamming weight.
181
+ tot_prob += normed_closeness / h_weight_combos
182
+ while (rands[s] <= tot_prob):
172
183
  samples.append(state_int)
173
184
  s += 1
174
- if s == hs:
185
+ if s == count:
186
+ break
187
+ if s == count:
175
188
  break
176
- for r in range(hs - s):
177
- samples.append(state_int)
189
+ if s < count:
190
+ samples += (count - s) * [state_int]
191
+
178
192
 
179
193
  np.random.shuffle(samples)
180
194
 
@@ -977,48 +977,64 @@ __kernel void gray(
977
977
  }
978
978
 
979
979
  // Initialize different seed per thread
980
- const int seed = i ^ (i >> 1);
981
- for (int b = 0; b < 64; ++b) {
982
- theta_local[last_block] ^= (seed >> (63U - b)) << b;
980
+ const ulong seed = i ^ (i >> 1);
981
+ int b;
982
+ for (b = 0; b < 64; ++b) {
983
+ if (seed >> (63U - b)) {
984
+ break;
985
+ }
983
986
  }
987
+ theta_local[last_block] ^= (seed >> (63U - b)) << b;
988
+
989
+ const size_t b_offset = b * n;
990
+ const bool b_bit = get_local_bit(theta_local, b);
984
991
 
985
992
  real1 best_energy = ZERO_R1;
986
- for (uint u = 0; u < n; u++) {
987
- const size_t u_offset = u * n;
988
- const bool u_bit = get_local_bit(theta_local, u);
989
- for (uint v = u + 1; v < n; v++) {
990
- const bool v_bit = get_local_bit(theta_local, v);
991
- const real1 val = G_m[u_offset + v];
992
- if (u_bit != v_bit) {
993
- best_energy += val;
994
- } else if (is_spin_glass) {
995
- best_energy -= val;
996
- }
993
+ for (uint v = 0; v < b; v++) {
994
+ const bool v_bit = get_local_bit(theta_local, v);
995
+ real1 val = G_m[b_offset + v];
996
+ if (is_spin_glass) {
997
+ val *= 2;
998
+ }
999
+ best_energy += (b_bit != v_bit) ? val : -val;
1000
+ }
1001
+ for (uint v = b + 1; v < n; v++) {
1002
+ const bool v_bit = get_local_bit(theta_local, v);
1003
+ real1 val = G_m[b_offset + v];
1004
+ if (is_spin_glass) {
1005
+ val *= 2;
997
1006
  }
1007
+ best_energy += (b_bit != v_bit) ? val : -val;
998
1008
  }
999
1009
 
1000
1010
  for (; i < gray_iterations; i += max_i) {
1001
1011
  for (int block = 0; block < blocks; ++block) {
1002
- const size_t flip_bit = gray_code_next(theta_local, i, block << 6U);
1012
+ const size_t k = gray_code_next(theta_local, i, block << 6U);
1013
+ const size_t k_offset = k * n;
1014
+ const bool k_bit = get_local_bit(theta_local, k);
1015
+
1003
1016
  real1 energy = ZERO_R1;
1004
- for (uint u = 0; u < n; u++) {
1005
- const size_t u_offset = u * n;
1006
- const bool u_bit = get_local_bit(theta_local, u);
1007
- for (uint v = u + 1; v < n; v++) {
1008
- const bool v_bit = get_local_bit(theta_local, v);
1009
- const real1 val = G_m[u_offset + v];
1010
- if (u_bit != v_bit) {
1011
- energy += val;
1012
- } else if (is_spin_glass) {
1013
- energy -= val;
1014
- }
1017
+ for (uint v = 0; v < k; v++) {
1018
+ const bool v_bit = get_local_bit(theta_local, v);
1019
+ real1 val = G_m[k_offset + v];
1020
+ if (is_spin_glass) {
1021
+ val *= 2;
1015
1022
  }
1023
+ energy += (k_bit != v_bit) ? val : -val;
1024
+ }
1025
+ for (uint v = k + 1; v < n; v++) {
1026
+ const bool v_bit = get_local_bit(theta_local, v);
1027
+ real1 val = G_m[k_offset + v];
1028
+ if (is_spin_glass) {
1029
+ val *= 2;
1030
+ }
1031
+ energy += (k_bit != v_bit) ? val : -val;
1016
1032
  }
1017
1033
 
1018
- if (energy > best_energy) {
1019
- best_energy = energy;
1034
+ if (energy > ZERO_R1) {
1035
+ best_energy += energy;
1020
1036
  } else {
1021
- theta_local[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
1037
+ theta_local[k >> 6U] ^= 1UL << (k & 63U);
1022
1038
  }
1023
1039
  }
1024
1040
  }
@@ -1059,48 +1075,64 @@ __kernel void gray_segmented(
1059
1075
  }
1060
1076
 
1061
1077
  // Initialize different seed per thread
1062
- const int seed = i ^ (i >> 1);
1063
- for (int b = 0; b < 64; ++b) {
1064
- theta_local[last_block] ^= (seed >> (63U - b)) << b;
1078
+ const ulong seed = i ^ (i >> 1);
1079
+ int b;
1080
+ for (b = 0; b < 64; ++b) {
1081
+ if (seed >> (63U - b)) {
1082
+ break;
1083
+ }
1065
1084
  }
1085
+ theta_local[last_block] ^= (seed >> (63U - b)) << b;
1086
+
1087
+ const size_t b_offset = b * n;
1088
+ const bool b_bit = get_local_bit(theta_local, b);
1066
1089
 
1067
1090
  real1 best_energy = ZERO_R1;
1068
- for (uint u = 0; u < n; u++) {
1069
- const size_t u_offset = u * n;
1070
- const bool u_bit = get_local_bit(theta_local, u);
1071
- for (uint v = u + 1; v < n; v++) {
1072
- const bool v_bit = get_local_bit(theta_local, v);
1073
- const real1 val = get_G_m(G_m, u_offset + v, segment_size);
1074
- if (u_bit != v_bit) {
1075
- best_energy += val;
1076
- } else if (is_spin_glass) {
1077
- best_energy -= val;
1078
- }
1091
+ for (uint v = 0; v < b; v++) {
1092
+ const bool v_bit = get_local_bit(theta_local, v);
1093
+ real1 val = get_G_m(G_m, b_offset + v, segment_size);
1094
+ if (is_spin_glass) {
1095
+ val *= 2;
1079
1096
  }
1097
+ best_energy += (b_bit != v_bit) ? val : -val;
1098
+ }
1099
+ for (uint v = b + 1; v < n; v++) {
1100
+ const bool v_bit = get_local_bit(theta_local, v);
1101
+ real1 val = get_G_m(G_m, b_offset + v, segment_size);
1102
+ if (is_spin_glass) {
1103
+ val *= 2;
1104
+ }
1105
+ best_energy += (b_bit != v_bit) ? val : -val;
1080
1106
  }
1081
1107
 
1082
1108
  for (; i < gray_iterations; i += max_i) {
1083
1109
  for (int block = 0; block < blocks; ++block) {
1084
- const size_t flip_bit = gray_code_next(theta_local, i, block << 6U);
1110
+ const size_t k = gray_code_next(theta_local, i, block << 6U);
1111
+ const size_t k_offset = k * n;
1112
+ const bool k_bit = get_local_bit(theta_local, k);
1113
+
1085
1114
  real1 energy = ZERO_R1;
1086
- for (uint u = 0; u < n; u++) {
1087
- const size_t u_offset = u * n;
1088
- const bool u_bit = get_local_bit(theta_local, u);
1089
- for (uint v = u + 1; v < n; v++) {
1090
- const bool v_bit = get_local_bit(theta_local, v);
1091
- const real1 val = get_G_m(G_m, u_offset + v, segment_size);
1092
- if (u_bit != v_bit) {
1093
- energy += val;
1094
- } else if (is_spin_glass) {
1095
- energy -= val;
1096
- }
1115
+ for (uint v = 0; v < k; v++) {
1116
+ const bool v_bit = get_local_bit(theta_local, v);
1117
+ real1 val = get_G_m(G_m, k_offset + v, segment_size);
1118
+ if (is_spin_glass) {
1119
+ val *= 2;
1120
+ }
1121
+ energy += (k_bit != v_bit) ? val : -val;
1122
+ }
1123
+ for (uint v = k + 1; v < n; v++) {
1124
+ const bool v_bit = get_local_bit(theta_local, v);
1125
+ real1 val = get_G_m(G_m, k_offset + v, segment_size);
1126
+ if (is_spin_glass) {
1127
+ val *= 2;
1097
1128
  }
1129
+ energy += (k_bit != v_bit) ? val : -val;
1098
1130
  }
1099
1131
 
1100
- if (energy > best_energy) {
1101
- best_energy = energy;
1132
+ if (energy > ZERO_R1) {
1133
+ best_energy += energy;
1102
1134
  } else {
1103
- theta_local[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
1135
+ theta_local[k >> 6U] ^= 1UL << (k & 63U);
1104
1136
  }
1105
1137
  }
1106
1138
  }
@@ -1137,7 +1169,7 @@ __kernel void gray_sparse(
1137
1169
  }
1138
1170
 
1139
1171
  // Initialize different seed per thread
1140
- const int seed = i ^ (i >> 1);
1172
+ const ulong seed = i ^ (i >> 1);
1141
1173
  for (int b = 0; b < 64; ++b) {
1142
1174
  theta_local[last_block] ^= (seed >> (63U - b)) << b;
1143
1175
  }
@@ -1223,7 +1255,7 @@ __kernel void gray_sparse_segmented(
1223
1255
  }
1224
1256
 
1225
1257
  // Initialize different seed per thread
1226
- const int seed = i ^ (i >> 1);
1258
+ const ulong seed = i ^ (i >> 1);
1227
1259
  for (int b = 0; b < 64; ++b) {
1228
1260
  theta_local[last_block] ^= (seed >> (63U - b)) << b;
1229
1261
  }
@@ -295,10 +295,12 @@ def run_gray_search_opencl(n, kernel, best_energy, theta, theta_buf, G_m_buf, is
295
295
  best_x = np.argmax(max_energy_host)
296
296
  energy = max_energy_host[best_x]
297
297
 
298
- if energy <= best_energy:
298
+ if energy <= 0.0:
299
299
  # No improvement: we can exit early
300
300
  return best_energy, theta
301
301
 
302
+ energy += best_energy
303
+
302
304
  # We need the best index
303
305
  queue.finish()
304
306
 
@@ -447,11 +449,6 @@ def spin_glass_solver(
447
449
  if is_opencl and (n_qubits <= gnl):
448
450
  theta_buf_64 = make_best_theta_buf_64(best_theta)
449
451
  energy, state = run_gray_search_opencl(n_qubits, gray_kernel, max_energy, best_theta, theta_buf_64, G_m_buf, is_segmented, *gray_args)
450
- if energy > max_energy:
451
- max_energy = energy
452
- best_theta = state
453
- improved = True
454
- continue
455
452
  else:
456
453
  # Gray code with default O(n^3)
457
454
  iterators, energies = pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_m, n_qubits, is_spin_glass)
@@ -463,11 +460,11 @@ def spin_glass_solver(
463
460
  continue
464
461
 
465
462
  energy, state = run_gray_optimization(best_theta, iterators, energies, gray_iterations, thread_count, is_spin_glass, G_m)
466
- if energy > max_energy:
467
- max_energy = energy
468
- best_theta = state
469
- improved = True
470
- continue
463
+ if energy > max_energy:
464
+ max_energy = energy
465
+ best_theta = state
466
+ improved = True
467
+ continue
471
468
 
472
469
  # Post-reheat phase
473
470
  reheat_theta = state
@@ -462,11 +462,11 @@ def spin_glass_solver_sparse(
462
462
  continue
463
463
 
464
464
  energy, state = run_gray_optimization(best_theta, iterators, energies, gray_iterations, thread_count, is_spin_glass, G_m.data, G_m.indptr, G_m.indices)
465
- if energy > max_energy:
466
- max_energy = energy
467
- best_theta = state
468
- improved = True
469
- continue
465
+ if energy > max_energy:
466
+ max_energy = energy
467
+ best_theta = state
468
+ improved = True
469
+ continue
470
470
 
471
471
  # Post-reheat phase
472
472
  reheat_theta = state
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.7.6
3
+ Version: 9.7.8
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.6',
10
+ version='9.7.8',
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