PyQrackIsing 9.11.0__tar.gz → 9.11.2__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 (30) hide show
  1. {pyqrackising-9.11.0/pyqrackising.egg-info → pyqrackising-9.11.2}/PKG-INFO +1 -1
  2. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/README.md +1 -1
  3. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyproject.toml +1 -1
  4. pyqrackising-9.11.2/pyqrackising/generate_tfim_samples.py +244 -0
  5. {pyqrackising-9.11.0 → pyqrackising-9.11.2/pyqrackising.egg-info}/PKG-INFO +1 -1
  6. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/setup.py +1 -1
  7. pyqrackising-9.11.0/pyqrackising/generate_tfim_samples.py +0 -202
  8. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/LICENSE.md +0 -0
  9. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/MANIFEST.in +0 -0
  10. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/__init__.py +0 -0
  11. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  12. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/kernels.cl +0 -0
  13. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/maxcut_tfim.py +0 -0
  14. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  15. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  16. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/maxcut_tfim_util.py +0 -0
  17. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/otoc.py +0 -0
  18. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/spin_glass_solver.py +0 -0
  19. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/spin_glass_solver_sparse.py +0 -0
  20. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  21. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/tfim_magnetization.py +0 -0
  22. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/tfim_square_magnetization.py +0 -0
  23. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/tsp.py +0 -0
  24. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising/tsp_maxcut.py +0 -0
  25. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising.egg-info/SOURCES.txt +0 -0
  26. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising.egg-info/dependency_links.txt +0 -0
  27. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising.egg-info/not-zip-safe +0 -0
  28. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising.egg-info/requires.txt +0 -0
  29. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/pyqrackising.egg-info/top_level.txt +0 -0
  30. {pyqrackising-9.11.0 → pyqrackising-9.11.2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.11.0
3
+ Version: 9.11.2
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.17635216.svg)](https://doi.org/10.5281/zenodo.17635216) [![PyPI Downloads](https://static.pepy.tech/badge/pyqrackising)](https://pepy.tech/projects/pyqrackising)
6
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.18706638.svg)](https://doi.org/10.5281/zenodo.18706638) [![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.
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
7
7
 
8
8
  [project]
9
9
  name = "pyqrackising"
10
- version = "9.11.0"
10
+ version = "9.11.2"
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"}
@@ -0,0 +1,244 @@
1
+ from .maxcut_tfim_util import probability_by_hamming_weight, sample_mag, opencl_context
2
+ import itertools
3
+ import math
4
+ import random
5
+ import sys
6
+ import numpy as np
7
+ from numba import njit
8
+
9
+ from collections import Counter
10
+
11
+
12
+ epsilon = opencl_context.epsilon
13
+
14
+
15
+ # Fixed-Hamming-weight Kawasaki swaps with acceptance probability, by Elara
16
+ def random_fixed_hamming_state(n_qubits, h):
17
+ bits = np.zeros(n_qubits, dtype=np.bool_)
18
+ bits[:h] = True
19
+ np.random.shuffle(bits)
20
+ state = 0
21
+ for b in bits:
22
+ state <<= 1
23
+ if b:
24
+ state |= 1
25
+
26
+ return state
27
+
28
+
29
+ def build_neighbors(n_rows, n_cols):
30
+ L = n_rows * n_cols
31
+ neighbors = [[] for _ in range(L)]
32
+
33
+ for i in range(n_rows):
34
+ for j in range(n_cols):
35
+ idx = i * n_cols + j
36
+
37
+ right = i * n_cols + ((j + 1) % n_cols)
38
+ left = i * n_cols + ((j - 1) % n_cols)
39
+ down = ((i + 1) % n_rows) * n_cols + j
40
+ up = ((i - 1) % n_rows) * n_cols + j
41
+
42
+ neighbors[idx] = [right, left, down, up]
43
+
44
+ return neighbors
45
+
46
+
47
+ def count_like_edges(state, neighbors):
48
+ like = 0
49
+ visited = set()
50
+
51
+ for i, nbrs in enumerate(neighbors):
52
+ si = (state >> i) & 1
53
+ for j in nbrs:
54
+ if (j, i) in visited:
55
+ continue
56
+ sj = (state >> j) & 1
57
+ if si == sj:
58
+ like += 1
59
+ visited.add((i, j))
60
+
61
+ return like
62
+
63
+
64
+ def delta_like_edges(state, i, j, neighbors):
65
+ si = (state >> i) & 1
66
+ sj = (state >> j) & 1
67
+
68
+ delta = 0
69
+
70
+ for idx in [i, j]:
71
+ s_old = (state >> idx) & 1
72
+
73
+ for nbr in neighbors[idx]:
74
+ if nbr == i or nbr == j:
75
+ continue
76
+
77
+ s_nbr = (state >> nbr) & 1
78
+
79
+ old_like = s_old == s_nbr
80
+ new_spin = sj if idx == i else si
81
+ new_like = new_spin == s_nbr
82
+
83
+ delta += int(new_like) - int(old_like)
84
+
85
+ # handle edge between i and j if neighbors
86
+ if j in neighbors[i]:
87
+ old_like = si == sj
88
+ new_like = sj == si
89
+ delta += int(new_like) - int(old_like)
90
+
91
+ return delta
92
+
93
+
94
+ def sample_fixed_hamming_weight(h_weight, count, n_rows, n_cols, burnin=10):
95
+ if count == 0:
96
+ return []
97
+
98
+ n_qubits = n_rows * n_cols
99
+ neighbors = build_neighbors(n_rows, n_cols)
100
+
101
+ like_edges = 0
102
+ while like_edges == 0:
103
+ state = random_fixed_hamming_state(n_qubits, h_weight)
104
+ like_edges = count_like_edges(state, neighbors)
105
+
106
+ samples = []
107
+
108
+ burn = burnin * n_qubits
109
+ thinning = n_qubits
110
+
111
+ ones = set([i for i in range(n_qubits) if (state >> i) & 1])
112
+ zeros = set([i for i in range(n_qubits) if not (state >> i) & 1])
113
+
114
+ for step in range(burn + thinning * count):
115
+ # choose random 1-bit and 0-bit
116
+
117
+ i = random.choice(tuple(ones))
118
+ j = random.choice(tuple(zeros))
119
+
120
+ delta = delta_like_edges(state, i, j, neighbors)
121
+ new_like = like_edges + delta
122
+
123
+ if new_like > 0:
124
+ accept_prob = new_like / like_edges
125
+ if np.random.random() < accept_prob:
126
+ state ^= 1 << i
127
+ ones.remove(i)
128
+ zeros.add(i)
129
+
130
+ state ^= 1 << j
131
+ zeros.remove(j)
132
+ ones.add(j)
133
+
134
+ like_edges = new_like
135
+
136
+ if step >= burn and (step - burn) % thinning == 0:
137
+ samples.append(state)
138
+
139
+ return samples
140
+
141
+
142
+ @njit
143
+ def factor_width(width, is_transpose=False):
144
+ col_len = math.floor(math.sqrt(width))
145
+ while ((width // col_len) * col_len) != width:
146
+ col_len -= 1
147
+ row_len = width // col_len
148
+
149
+ return (col_len, row_len) if is_transpose else (row_len, col_len)
150
+
151
+
152
+ @njit
153
+ def comb(n, k):
154
+ if (k < 0) or (k > n):
155
+ return 0
156
+ if (k == 0) or (k == n):
157
+ return 1
158
+ res = 1
159
+ for i in range(1, k + 1):
160
+ res = res * (n - k + i) // i
161
+ return res
162
+
163
+
164
+ @njit
165
+ def fix_cdf(hamming_prob):
166
+ tot_prob = 0.0
167
+ n_bias = len(hamming_prob)
168
+ cum_prob = np.empty(n_bias, dtype=np.float64)
169
+ for i in range(n_bias):
170
+ tot_prob += hamming_prob[i]
171
+ cum_prob[i] = tot_prob
172
+ cum_prob[-1] = 1.0
173
+
174
+ return cum_prob
175
+
176
+
177
+ @njit
178
+ def get_tfim_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, omega=1.5 * np.pi):
179
+ if abs(t) <= epsilon:
180
+ p = (1.0 - np.cos(theta)) / 2.0
181
+ bias = np.empty(n_qubits + 1, dtype=np.float64)
182
+ for k in range(n_qubits + 1):
183
+ bias[k] = comb(n_qubits, k) * (p**k) * ((1.0 - p) ** (n_qubits - k))
184
+
185
+ return bias / bias.sum()
186
+
187
+ if abs(h) <= epsilon:
188
+ bias = np.empty(n_qubits + 1, dtype=np.float64)
189
+ if J > 0:
190
+ bias[-1] = 1.0
191
+ else:
192
+ bias[0] = 1.0
193
+ return bias
194
+
195
+ bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1, normalized=True, omega=omega)
196
+
197
+ return bias / bias.sum()
198
+
199
+
200
+ def generate_tfim_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100):
201
+ samples = []
202
+
203
+ if abs(t) <= epsilon:
204
+ prob = (1.0 - np.cos(theta)) / 2.0
205
+ for s in range(shots):
206
+ sample = 0
207
+ for q in range(n_qubits):
208
+ sample <<= 1
209
+ if np.random.random() < prob:
210
+ sample |= 1
211
+ samples.append(sample)
212
+
213
+ return samples
214
+
215
+ n_rows, n_cols = factor_width(n_qubits)
216
+
217
+ # First dimension: Hamming weight
218
+ bias = get_tfim_hamming_distribution(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits)
219
+ counts = np.random.multinomial(shots, bias)
220
+
221
+ samples += [0] * counts[0]
222
+ samples += [(1 << n_qubits) - 1] * counts[-1]
223
+ if n_qubits > 1:
224
+ samples += [int(1 << np.random.randint(n_qubits)) for _ in range(counts[1])]
225
+ if n_qubits > 2:
226
+ mask = (1 << n_qubits) - 1
227
+ samples += [(mask ^ int(1 << np.random.randint(n_qubits))) for _ in range(counts[-2])]
228
+
229
+ # Second dimension: magnetic localization
230
+ for h_weight in range(2, len(bias) - 2):
231
+ samples += sample_fixed_hamming_weight(h_weight, counts[h_weight], n_rows, n_cols)
232
+
233
+ np.random.shuffle(samples)
234
+
235
+ return samples
236
+
237
+
238
+ def generate_fermi_hubbard_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100, omega=1.5 * np.pi):
239
+ shots_x, shots_y, shots_z = np.random.multinomial(shots, [1 / 3, 1 / 3, 1 / 3])
240
+ return (
241
+ generate_tfim_samples(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, shots=shots_z)
242
+ + generate_tfim_samples(J=-h, h=-J, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_x)
243
+ + generate_tfim_samples(J=J, h=h, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_y)
244
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.11.0
3
+ Version: 9.11.2
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.11.0",
10
+ version="9.11.2",
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)",
@@ -1,202 +0,0 @@
1
- from .maxcut_tfim_util import probability_by_hamming_weight, sample_mag, opencl_context
2
- import itertools
3
- import math
4
- import sys
5
- import numpy as np
6
- from numba import njit
7
-
8
- from collections import Counter
9
-
10
-
11
- epsilon = opencl_context.epsilon
12
-
13
-
14
- @njit
15
- def factor_width(width, is_transpose=False):
16
- col_len = math.floor(math.sqrt(width))
17
- while ((width // col_len) * col_len) != width:
18
- col_len -= 1
19
- row_len = width // col_len
20
-
21
- return (col_len, row_len) if is_transpose else (row_len, col_len)
22
-
23
-
24
- # By Gemini (Google Search AI)
25
- def int_to_bitstring(integer, length):
26
- return bin(integer)[2:].zfill(length)
27
-
28
-
29
- # Drafted by Elara (OpenAI custom GPT), improved by Dan Strano
30
- def closeness_like_bits(perm, n_rows, n_cols):
31
- """
32
- Compute closeness-of-like-bits metric C(state) for a given bitstring on an LxL toroidal grid.
33
-
34
- Parameters:
35
- perm: integer representing basis state, bit-length n_rows * n_cols
36
- n_rows: row count of torus
37
- n_cols: column count of torus
38
-
39
- Returns:
40
- normalized_closeness: float, in [-1, +1]
41
- +1 means all neighbors are like-like, -1 means all neighbors are unlike
42
- """
43
- # reshape the bitstring into LxL grid
44
- bitstring = list(int_to_bitstring(perm, n_rows * n_cols))
45
- grid = np.array(bitstring).reshape((n_rows, n_cols))
46
- total_edges = 0
47
- like_count = 0
48
-
49
- # iterate over each site, count neighbors (right and down to avoid double-count)
50
- for i in range(n_rows):
51
- for j in range(n_cols):
52
- s = grid[i, j]
53
-
54
- # right neighbor (wrap around)
55
- s_right = grid[i, (j + 1) % n_cols]
56
- like_count += 1 if s == s_right else -1
57
- total_edges += 1
58
-
59
- # down neighbor (wrap around)
60
- s_down = grid[(i + 1) % n_rows, j]
61
- like_count += 1 if s == s_down else -1
62
- total_edges += 1
63
-
64
- # normalize
65
- normalized_closeness = like_count / total_edges
66
- return normalized_closeness
67
-
68
-
69
- @njit
70
- def comb(n, k):
71
- if (k < 0) or (k > n):
72
- return 0
73
- if (k == 0) or (k == n):
74
- return 1
75
- res = 1
76
- for i in range(1, k + 1):
77
- res = res * (n - k + i) // i
78
- return res
79
-
80
-
81
- @njit
82
- def expected_closeness_weight(n_rows, n_cols, hamming_weight):
83
- L = n_rows * n_cols
84
- same_pairs = comb(hamming_weight, 2) + comb(L - hamming_weight, 2)
85
- total_pairs = comb(L, 2)
86
- mu_k = same_pairs / total_pairs
87
- return 2.0 * mu_k - 1.0
88
-
89
-
90
- @njit
91
- def sample_hamming_weight(thresholds, shots):
92
- hamming_samples = np.zeros(shots, dtype=np.int32)
93
- for s in range(shots):
94
- hamming_samples[s] = sample_mag(thresholds)
95
-
96
- return hamming_samples
97
-
98
-
99
- @njit
100
- def fix_cdf(hamming_prob):
101
- tot_prob = 0.0
102
- n_bias = len(hamming_prob)
103
- cum_prob = np.empty(n_bias, dtype=np.float64)
104
- for i in range(n_bias):
105
- tot_prob += hamming_prob[i]
106
- cum_prob[i] = tot_prob
107
- cum_prob[-1] = 1.0
108
-
109
- return cum_prob
110
-
111
-
112
- @njit
113
- def get_tfim_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, omega=1.5 * np.pi):
114
- if abs(t) <= epsilon:
115
- p = (1.0 - np.cos(theta)) / 2.0
116
- bias = np.empty(n_qubits + 1, dtype=np.float64)
117
- for k in range(n_qubits + 1):
118
- bias[k] = comb(n_qubits, k) * (p**k) * ((1.0 - p) ** (n_qubits - k))
119
-
120
- return bias / bias.sum()
121
-
122
- if abs(h) <= epsilon:
123
- bias = np.empty(n_qubits + 1, dtype=np.float64)
124
- if J > 0:
125
- bias[-1] = 1.0
126
- else:
127
- bias[0] = 1.0
128
- return bias
129
-
130
- bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1, normalized=True, omega=omega)
131
-
132
- return bias / bias.sum()
133
-
134
-
135
- def generate_tfim_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100, omega=1.5 * np.pi):
136
- samples = []
137
-
138
- if abs(t) <= epsilon:
139
- prob = (1.0 - np.cos(theta)) / 2.0
140
- for s in range(shots):
141
- sample = 0
142
- for q in range(n_qubits):
143
- sample <<= 1
144
- if np.random.random() < prob:
145
- sample |= 1
146
- samples.append(sample)
147
-
148
- return samples
149
-
150
- n_rows, n_cols = factor_width(n_qubits)
151
-
152
- # First dimension: Hamming weight
153
- bias = get_tfim_hamming_distribution(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, omega=omega)
154
- thresholds = fix_cdf(bias)
155
- hamming_samples = dict(Counter(sample_hamming_weight(thresholds, shots)))
156
-
157
- for h_weight, count in hamming_samples.items():
158
- if h_weight == 0:
159
- samples += count * [0]
160
- continue
161
-
162
- if h_weight == n_qubits:
163
- samples += count * [(1 << n_qubits) - 1]
164
- continue
165
-
166
- rands = [np.random.random() for _ in range(count)]
167
- rands.sort()
168
- state_int = 0
169
- tot_prob = 0
170
- s = 0
171
- # How closely grouped are "like" bits to "like"?
172
- expected_closeness = expected_closeness_weight(n_rows, n_cols, h_weight)
173
- h_weight_combos = math.comb(n_qubits, h_weight)
174
- for combo in itertools.combinations(range(n_qubits), h_weight):
175
- state_int = sum(1 << pos for pos in combo)
176
- # When we add all "closeness" possibilities for the particular Hamming weight, we should maintain the (n+1) mean probability dimensions.
177
- normed_closeness = (1 + closeness_like_bits(state_int, n_rows, n_cols)) / (1 + expected_closeness)
178
- # Use a normalized weighted average that favors the (n+1)-dimensional model at later times.
179
- # 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.
180
- tot_prob += normed_closeness / h_weight_combos
181
- while rands[s] <= tot_prob:
182
- samples.append(state_int)
183
- s += 1
184
- if s == count:
185
- break
186
- if s == count:
187
- break
188
- if s < count:
189
- samples += (count - s) * [state_int]
190
-
191
- np.random.shuffle(samples)
192
-
193
- return samples
194
-
195
-
196
- def generate_fermi_hubbard_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100, omega=1.5 * np.pi):
197
- shots_x, shots_y, shots_z = np.random.multinomial(shots, [1 / 3, 1 / 3, 1 / 3])
198
- return (
199
- generate_tfim_samples(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, shots=shots_z)
200
- + generate_tfim_samples(J=-h, h=-J, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_x)
201
- + generate_tfim_samples(J=J, h=h, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_y)
202
- )
File without changes
File without changes
File without changes