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.
- {pyqrackising-9.7.6/pyqrackising.egg-info → pyqrackising-9.7.8}/PKG-INFO +1 -1
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/README.md +1 -1
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyproject.toml +1 -1
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/generate_tfim_samples.py +34 -20
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/kernels.cl +92 -60
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/spin_glass_solver.py +8 -11
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/spin_glass_solver_sparse.py +5 -5
- {pyqrackising-9.7.6 → pyqrackising-9.7.8/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/setup.py +1 -1
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/LICENSE.md +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/MANIFEST.in +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/__init__.py +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/otoc.py +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/tsp.py +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-9.7.6 → pyqrackising-9.7.8}/setup.cfg +0 -0
|
@@ -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
|
-
[](https://doi.org/10.5281/zenodo.17618663) [](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.
|
|
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)
|
|
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] =
|
|
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
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
-
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
)
|
|
171
|
-
|
|
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
|
-
|
|
185
|
+
if s == count:
|
|
186
|
+
break
|
|
187
|
+
if s == count:
|
|
175
188
|
break
|
|
176
|
-
|
|
177
|
-
samples
|
|
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
|
|
981
|
-
|
|
982
|
-
|
|
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
|
|
987
|
-
const
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
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
|
|
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
|
|
1005
|
-
const
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
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 >
|
|
1019
|
-
best_energy
|
|
1034
|
+
if (energy > ZERO_R1) {
|
|
1035
|
+
best_energy += energy;
|
|
1020
1036
|
} else {
|
|
1021
|
-
theta_local[
|
|
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
|
|
1063
|
-
|
|
1064
|
-
|
|
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
|
|
1069
|
-
const
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
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
|
|
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
|
|
1087
|
-
const
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
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 >
|
|
1101
|
-
best_energy
|
|
1132
|
+
if (energy > ZERO_R1) {
|
|
1133
|
+
best_energy += energy;
|
|
1102
1134
|
} else {
|
|
1103
|
-
theta_local[
|
|
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
|
|
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
|
|
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 <=
|
|
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
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
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
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
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
|
|
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
|
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name='pyqrackising',
|
|
10
|
-
version='9.7.
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|