PyQrackIsing 1.23.1__tar.gz → 1.24.1__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 (21) hide show
  1. {pyqrackising-1.23.1/PyQrackIsing.egg-info → pyqrackising-1.24.1}/PKG-INFO +1 -1
  2. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/PyQrackIsing/maxcut_tfim.py +1 -1
  3. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/PyQrackIsing/tsp_symmetric.py +98 -68
  4. {pyqrackising-1.23.1 → pyqrackising-1.24.1/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  5. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/README.md +2 -2
  6. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/pyproject.toml +1 -1
  7. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/setup.py +1 -1
  8. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/CMakeLists.txt +0 -0
  9. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/LICENSE.md +0 -0
  10. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/MANIFEST.in +0 -0
  11. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/PyQrackIsing/__init__.py +0 -0
  12. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  13. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/PyQrackIsing/generate_tfim_samples.py +0 -0
  14. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/PyQrackIsing/spin_glass_solver.py +0 -0
  15. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/PyQrackIsing/tfim_magnetization.py +0 -0
  16. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  17. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  18. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  19. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  20. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/PyQrackIsing.egg-info/top_level.txt +0 -0
  21. {pyqrackising-1.23.1 → pyqrackising-1.24.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.23.1
3
+ Version: 1.24.1
4
4
  Summary: Near-ideal closed-form solutions for transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -107,7 +107,7 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
107
107
  n_bias = n_qubits - 1
108
108
 
109
109
  theta = np.empty(n_qubits, dtype=np.float64)
110
- for q in prange(n_qubits):
110
+ for q in range(n_qubits):
111
111
  J = J_func[q]
112
112
  z = degrees[q]
113
113
  theta[q] = np.arcsin(
@@ -4,6 +4,12 @@ from numba import njit
4
4
  import numpy as np
5
5
 
6
6
 
7
+
8
+ # By Gemini (Google Search AI)
9
+ def int_to_bitstring(integer, length):
10
+ return (bin(integer)[2:].zfill(length))[::-1]
11
+
12
+
7
13
  # two_opt() and targeted_three_opt() written by Elara (OpenAI ChatGPT instance)
8
14
  @njit
9
15
  def path_length(path, G_m):
@@ -13,6 +19,7 @@ def path_length(path, G_m):
13
19
 
14
20
  return tot_len
15
21
 
22
+
16
23
  @njit
17
24
  def two_opt(path, G):
18
25
  improved = True
@@ -34,6 +41,7 @@ def two_opt(path, G):
34
41
  path = best_path
35
42
  return best_path, best_dist
36
43
 
44
+
37
45
  @njit
38
46
  def targeted_three_opt(path, W, k_neighbors=20):
39
47
  """
@@ -120,45 +128,9 @@ def targeted_three_opt(path, W, k_neighbors=20):
120
128
 
121
129
  return best_path, best_dist
122
130
 
123
- def tsp_symmetric(G, quality=1, shots=None, correction_quality=2, is_2_opt=True, is_3_opt=True, k_neighbors=20, is_cyclic=True, multi_start=1):
124
- nodes = None
125
- n_nodes = 0
126
- G_m = None
127
- if isinstance(G, nx.Graph):
128
- nodes = list(G.nodes())
129
- n_nodes = len(nodes)
130
- G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
131
- else:
132
- n_nodes = len(G)
133
- nodes = list(range(n_nodes))
134
- G_m = G
135
-
136
- if n_nodes == 0:
137
- return ([], 0)
138
- if n_nodes == 1:
139
- return ([nodes[0]], 0)
140
- if n_nodes == 2:
141
- if is_cyclic:
142
- return ([nodes[0], nodes[1], nodes[0]], 2 * G_m[0, 1])
143
- else:
144
- return ([nodes[0], nodes[1]], G_m[0, 1])
145
-
146
- a = []
147
- b = []
148
- best_energy = float("inf")
149
- for _ in range(multi_start):
150
- energy = 0.0
151
- _a = []
152
- _b = []
153
- while (len(_a) == 0) or (len(_b) == 0):
154
- bits = ''
155
- _, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots, correction_quality=correction_quality)
156
- _a = list(bits[0])
157
- _b = list(bits[1])
158
- if energy < best_energy:
159
- best_energy = energy
160
- a, b = _a, _b
161
131
 
132
+ @njit
133
+ def init_G_a_b(G_m, a, b):
162
134
  n_a_nodes = len(a)
163
135
  n_b_nodes = len(b)
164
136
  G_a = np.zeros((n_a_nodes, n_a_nodes), dtype=np.float64)
@@ -174,47 +146,41 @@ def tsp_symmetric(G, quality=1, shots=None, correction_quality=2, is_2_opt=True,
174
146
  continue
175
147
  G_b[i, j] = G_m[b[i], b[j]]
176
148
 
177
- sol_a = tsp_symmetric(G_a, quality=quality, correction_quality=correction_quality, is_cyclic=False, is_2_opt=False, is_3_opt=False, multi_start=multi_start)
178
- sol_b = tsp_symmetric(G_b, quality=quality, correction_quality=correction_quality, is_cyclic=False, is_2_opt=False, is_3_opt=False, multi_start=multi_start)
179
-
180
- path_a = [a[x] for x in sol_a[0]]
181
- path_b = [b[x] for x in sol_b[0]]
149
+ return G_a, G_b
182
150
 
183
- sol_weight = sol_a[1] + sol_b[1]
184
151
 
185
- single = None
152
+ @njit
153
+ def stitch(G_m, path_a, path_b, sol_weight):
186
154
  is_single_a = len(path_a) == 1
187
155
  is_single_b = len(path_b) == 1
156
+ best_path = [0]
157
+ best_weight = 0.0
158
+ if is_single_a or is_single_b:
159
+ singlet = 0
160
+ bulk = [0]
161
+ if is_single_a:
162
+ singlet = path_a[0]
163
+ bulk = path_b
164
+ elif is_single_b:
165
+ singlet = path_b[0]
166
+ bulk = path_a
188
167
 
189
- if is_single_a and is_single_b:
190
- return (path_a + path_b, sol_weight + G_m[path_a[0], path_b[0]])
191
-
192
- singlet = None
193
- bulk = None
194
- if is_single_a:
195
- singlet = path_a[0]
196
- bulk = path_b
197
- elif is_single_b:
198
- singlet = path_b[0]
199
- bulk = path_a
200
-
201
- if not singlet is None:
202
168
  best_weight = G_m[singlet, bulk[0]]
203
169
  best_path = [singlet] + bulk
204
170
  weight = G_m[singlet, bulk[-1]]
205
171
  if weight < best_weight:
206
172
  best_weight = weight
207
173
  best_path = bulk + [singlet]
208
- for i in range(len(bulk) - 1):
174
+ for i in range(1, len(bulk)):
209
175
  weight = (
210
- G_m[singlet, bulk[i]] +
211
- G_m[singlet, bulk[i + 1]] -
212
- G_m[bulk[i], bulk[i + 1]]
176
+ G_m[singlet, bulk[i - 1]] +
177
+ G_m[singlet, bulk[i]] -
178
+ G_m[bulk[i - 1], bulk[i]]
213
179
  )
214
180
  if weight < best_weight:
215
181
  best_weight = weight
216
182
  best_path = bulk.copy()
217
- best_path.insert(i + 1, singlet)
183
+ best_path.insert(i, singlet)
218
184
  else:
219
185
  terminals_a = [path_a[0], path_a[-1]]
220
186
  terminals_b = [path_b[0], path_b[-1]]
@@ -227,21 +193,85 @@ def tsp_symmetric(G, quality=1, shots=None, correction_quality=2, is_2_opt=True,
227
193
  if weight < best_weight:
228
194
  best_weight = weight
229
195
  best_path = path_b + path_a
230
- for i in range(len(path_b) - 1):
196
+ for i in range(1, len(path_b)):
231
197
  weight = (
232
- G_m[terminals_a[0], path_b[i]] +
233
- G_m[terminals_a[1], path_b[i + 1]] -
234
- G_m[path_b[i], path_b[i + 1]]
198
+ G_m[terminals_a[0], path_b[i - 1]] +
199
+ G_m[terminals_a[1], path_b[i]] -
200
+ G_m[path_b[i - 1], path_b[i]]
235
201
  )
236
202
  if weight < best_weight:
237
203
  best_weight = weight
238
204
  best_path = path_b.copy()
239
- best_path[i + 1:i + 1] = path_a
205
+ best_path[i:i] = path_a
240
206
  path_a.reverse()
241
207
  terminals_a.reverse()
242
208
  path_a, path_b = path_b, path_a
243
209
  terminals_a, terminals_b = terminals_b, terminals_a
244
210
 
211
+ return best_path, best_weight
212
+
213
+
214
+ def tsp_symmetric(G, quality=1, shots=None, correction_quality=2, monte_carlo=False, is_2_opt=True, is_3_opt=True, k_neighbors=20, is_cyclic=True, multi_start=1):
215
+ nodes = None
216
+ n_nodes = 0
217
+ G_m = None
218
+ if isinstance(G, nx.Graph):
219
+ nodes = list(G.nodes())
220
+ n_nodes = len(nodes)
221
+ G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
222
+ else:
223
+ n_nodes = len(G)
224
+ nodes = list(range(n_nodes))
225
+ G_m = G
226
+
227
+ if n_nodes == 0:
228
+ return ([], 0)
229
+ if n_nodes == 1:
230
+ return ([nodes[0]], 0)
231
+ if n_nodes == 2:
232
+ if is_cyclic:
233
+ return ([nodes[0], nodes[1], nodes[0]], 2 * G_m[0, 1])
234
+ else:
235
+ return ([nodes[0], nodes[1]], G_m[0, 1])
236
+
237
+ a = []
238
+ b = []
239
+ best_energy = float("inf")
240
+ for _ in range(multi_start):
241
+ energy = 0.0
242
+ _a = []
243
+ _b = []
244
+ while (len(_a) == 0) or (len(_b) == 0):
245
+ bits = ([], [])
246
+ if monte_carlo:
247
+ for i in range(n_nodes):
248
+ if np.random.random() < 0.5:
249
+ bits[0].append(i)
250
+ else:
251
+ bits[1].append(i)
252
+ else:
253
+ _, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots, correction_quality=correction_quality)
254
+ _a = list(bits[0])
255
+ _b = list(bits[1])
256
+ if energy < best_energy:
257
+ best_energy = energy
258
+ a, b = _a, _b
259
+
260
+ G_a, G_b = init_G_a_b(G_m, a, b)
261
+
262
+ sol_a = tsp_symmetric(G_a, quality=quality, correction_quality=correction_quality, monte_carlo=monte_carlo, is_cyclic=False, is_2_opt=False, is_3_opt=False, multi_start=multi_start)
263
+ sol_b = tsp_symmetric(G_b, quality=quality, correction_quality=correction_quality, monte_carlo=monte_carlo, is_cyclic=False, is_2_opt=False, is_3_opt=False, multi_start=multi_start)
264
+
265
+ path_a = [a[x] for x in sol_a[0]]
266
+ path_b = [b[x] for x in sol_b[0]]
267
+
268
+ sol_weight = sol_a[1] + sol_b[1]
269
+
270
+ if (len(path_a) == 1) and (len(path_b) == 1):
271
+ return (path_a + path_b, sol_weight + G_m[path_a[0], path_b[0]])
272
+
273
+ best_path, best_weight = stitch(G_m, path_a, path_b, sol_weight)
274
+
245
275
  if is_2_opt or is_3_opt:
246
276
  cycle_node = best_path[0]
247
277
  best_path += [cycle_node]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.23.1
3
+ Version: 1.24.1
4
4
  Summary: Near-ideal closed-form solutions for transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -93,13 +93,13 @@ def generate_tsp_graph(n_nodes=64, seed=None):
93
93
 
94
94
  n_nodes = 128
95
95
  G = generate_tsp_graph(n_nodes=n_nodes, seed=42)
96
- circuit, path_length = tsp_symmetric(G, quality=1, correction_quality=2, is_cyclic=True, multi_start=1, is_2_opt=True, is_3_opt=True, k_neighbors=20)
96
+ circuit, path_length = tsp_symmetric(G, is_monte_carlo=False, quality=1, correction_quality=2, is_cyclic=True, multi_start=1, is_2_opt=True, is_3_opt=True, k_neighbors=20)
97
97
 
98
98
  print(f"Node count: {n_nodes}")
99
99
  print(f"Path: {circuit}")
100
100
  print(f"Path length: {path_length}")
101
101
  ```
102
- We only provide a solver for the symmetric version of the TSP (i.e., the distance from "A" to "B" is considered the same as from "B" to "A"). `multi_start` controls how many stochastic repeats of MAXCUT are tried to select the best result, at every level of recursion.
102
+ We only provide a solver for the symmetric version of the TSP (i.e., the distance from "A" to "B" is considered the same as from "B" to "A"). `is_monte_carlo=True` switches out the MAXCUT-based heuristic for pure Monte Carlo recursive bipartitioning. `multi_start` controls how many stochastic repeats of MAXCUT are tried to select the best result, at every level of recursion.
103
103
 
104
104
  ## About
105
105
  Transverse field Ising model (TFIM) is the basis of most claimed algorithmic "quantum advantage," circa 2025, with the notable exception of Shor's integer factoring algorithm.
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "PyQrackIsing"
15
- version = "1.23.1"
15
+ version = "1.24.1"
16
16
  requires-python = ">=3.8"
17
17
  description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
18
18
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='PyQrackIsing',
54
- version='1.23.1',
54
+ version='1.24.1',
55
55
  author='Dan Strano',
56
56
  author_email='stranoj@gmail.com',
57
57
  description='Near-ideal closed-form solutions for transverse field Ising model (TFIM)',
File without changes
File without changes
File without changes