PyQrackIsing 2.0.0__tar.gz → 2.0.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 (22) hide show
  1. {pyqrackising-2.0.0/pyqrackising.egg-info → pyqrackising-2.0.2}/PKG-INFO +1 -1
  2. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyproject.toml +1 -1
  3. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising/_pyqrack_ising.cpp +6 -4
  4. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising/maxcut_tfim.py +9 -15
  5. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising/spin_glass_solver.py +7 -8
  6. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising/tsp_symmetric.py +29 -31
  7. {pyqrackising-2.0.0 → pyqrackising-2.0.2/pyqrackising.egg-info}/PKG-INFO +1 -1
  8. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/setup.py +1 -1
  9. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/CMakeLists.txt +0 -0
  10. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/LICENSE.md +0 -0
  11. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/MANIFEST.in +0 -0
  12. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/README.md +0 -0
  13. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising/__init__.py +0 -0
  14. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising/generate_tfim_samples.py +0 -0
  15. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising/kernels.cl +0 -0
  16. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising/tfim_magnetization.py +0 -0
  17. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising/tfim_square_magnetization.py +0 -0
  18. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising.egg-info/SOURCES.txt +0 -0
  19. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising.egg-info/dependency_links.txt +0 -0
  20. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising.egg-info/not-zip-safe +0 -0
  21. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/pyqrackising.egg-info/top_level.txt +0 -0
  22. {pyqrackising-2.0.0 → pyqrackising-2.0.2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 2.0.0
3
+ Version: 2.0.2
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
@@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
11
11
 
12
12
  [project]
13
13
  name = "pyqrackising"
14
- version = "2.0.0"
14
+ version = "2.0.2"
15
15
  requires-python = ">=3.8"
16
16
  description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
17
17
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -39,17 +39,19 @@ static inline std::vector<double> probability_by_hamming_weight(double J, double
39
39
  std::fill(bias.begin(), bias.end(), 1.0 / (n_qubits + 1.0));
40
40
  } else {
41
41
  const double sin_delta = std::sin(delta_theta);
42
- const double omega = 1.5 * M_PI;
43
- const double t2 = 1.0;
42
+ constexpr double omega = 1.5 * M_PI;
43
+ constexpr double t2 = 1.0;
44
44
  const double p = std::pow(2.0, std::abs(J / h) - 1.0) * (1.0 + sin_delta * std::cos(J * omega * t + theta) / (1.0 + std::sqrt(t / t2))) - 0.5;
45
- if (p >= 1024) {
45
+ if ((p * n_qubits) >= 1024) {
46
46
  bias[0] = 1.0;
47
47
  } else {
48
+ const double factor = std::pow(2.0, -p);
49
+ double n = 1.0;
48
50
  double tot_n = 0.0;
49
51
  for (size_t q = 0U; q <= n_qubits; ++q) {
50
- double n = 1.0 / ((n_qubits + 1) * std::pow(2.0, p * q));
51
52
  bias[q] = n;
52
53
  tot_n += n;
54
+ n *= factor;
53
55
  }
54
56
  for (size_t q = 0U; q <= n_qubits; ++q) {
55
57
  bias[q] /= tot_n;
@@ -28,15 +28,7 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
28
28
  bias = np.empty(n_qubits - 1, dtype=np.float64)
29
29
 
30
30
  # critical angle
31
- theta_c = np.arcsin(
32
- max(
33
- -1.0,
34
- min(
35
- 1.0,
36
- (1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h) / (z * J)),
37
- ),
38
- )
39
- )
31
+ theta_c = np.arcsin(max(-1.0, min(1.0, abs(h) / (z * J))))
40
32
 
41
33
  p = (
42
34
  pow(2.0, abs(J / h) - 1.0)
@@ -48,8 +40,10 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
48
40
  return bias
49
41
 
50
42
  tot_n = 1.0 + 1.0 / pow(2.0, p * n_qubits)
43
+ factor = pow(2.0, -p)
44
+ n = 1.0
51
45
  for q in range(1, n_qubits):
52
- n = 1.0 / pow(2.0, p * q)
46
+ n *= factor
53
47
  bias[q - 1] = n
54
48
  tot_n += n
55
49
  bias /= tot_n
@@ -376,14 +370,14 @@ def maxcut_tfim(
376
370
 
377
371
  best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, degrees, J_eff, n_qubits)
378
372
 
379
- bit_string = "".join(["1" if b else "0" for b in best_solution])
380
- bit_list = list(bit_string)
373
+ bit_string = ""
381
374
  l, r = [], []
382
- for i in range(len(bit_list)):
383
- b = bit_list[i] == "1"
384
- if b:
375
+ for i in range(len(best_solution)):
376
+ if best_solution[i]:
377
+ bit_string += "1"
385
378
  r.append(nodes[i])
386
379
  else:
380
+ bit_string += "0"
387
381
  l.append(nodes[i])
388
382
 
389
383
  return bit_string, best_value, (l, r)
@@ -6,15 +6,16 @@ from numba import njit, prange
6
6
  import os
7
7
 
8
8
 
9
- def evaluate_cut_edges(state, G_m):
9
+ @njit
10
+ def evaluate_cut_edges(theta_bits, G_m):
10
11
  n_qubits = len(G_m)
11
- cut_value = 0
12
+ cut = 0
12
13
  for u in range(n_qubits):
13
14
  for v in range(u + 1, n_qubits):
14
- if ((state >> u) & 1) != ((state >> v) & 1):
15
- cut_value += G_m[u, v]
15
+ if theta_bits[u] != theta_bits[v]:
16
+ cut += G_m[u, v]
16
17
 
17
- return float(cut_value)
18
+ return cut
18
19
 
19
20
 
20
21
  @njit
@@ -122,7 +123,6 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best
122
123
  improved = True
123
124
  break
124
125
 
125
- sample = 0
126
126
  bitstring = ""
127
127
  l, r = [], []
128
128
  for i in range(len(best_theta)):
@@ -130,11 +130,10 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best
130
130
  if b:
131
131
  bitstring += "1"
132
132
  r.append(nodes[i])
133
- sample |= 1 << i
134
133
  else:
135
134
  bitstring += "0"
136
135
  l.append(nodes[i])
137
136
 
138
- cut_value = evaluate_cut_edges(sample, G_m)
137
+ cut_value = evaluate_cut_edges(best_theta, G_m)
139
138
 
140
139
  return bitstring, float(cut_value), (l, r), float(min_energy)
@@ -1,6 +1,6 @@
1
1
  from .spin_glass_solver import spin_glass_solver
2
2
  import networkx as nx
3
- from numba import njit
3
+ from numba import njit, prange
4
4
  import numpy as np
5
5
 
6
6
 
@@ -30,9 +30,7 @@ def one_way_two_opt(path, G):
30
30
  while improved:
31
31
  improved = False
32
32
  for i in range(1, path_len - 1):
33
- for j in range(i+1, path_len + 1):
34
- if j - i == 1: # adjacent edges, skip
35
- continue
33
+ for j in range(i + 2, path_len + 1):
36
34
  new_path = best_path[:]
37
35
  new_path[i:j] = best_path[j-1:i-1:-1]
38
36
  new_dist = path_length(new_path, G)
@@ -53,9 +51,7 @@ def anchored_two_opt(path, G):
53
51
  while improved:
54
52
  improved = False
55
53
  for i in range(1, path_len - 1):
56
- for j in range(i+1, path_len):
57
- if j - i == 1: # adjacent edges, skip
58
- continue
54
+ for j in range(i + 2, path_len):
59
55
  new_path = best_path[:]
60
56
  new_path[i:j] = best_path[j-1:i-1:-1]
61
57
  new_dist = path_length(new_path, G)
@@ -75,9 +71,7 @@ def two_opt(path, G):
75
71
  while improved:
76
72
  improved = False
77
73
  for i in range(1, len(path) - 2):
78
- for j in range(i+1, len(path) - 1):
79
- if j - i == 1: # adjacent edges, skip
80
- continue
74
+ for j in range(i + 2, len(path) - 1):
81
75
  new_path = best_path[:]
82
76
  new_path[i:j] = best_path[j-1:i-1:-1] # reverse segment
83
77
  new_dist = path_length(new_path, G)
@@ -175,18 +169,18 @@ def targeted_three_opt(path, W, k_neighbors=20):
175
169
  return best_path, best_dist
176
170
 
177
171
 
178
- @njit
172
+ @njit(parallel=True)
179
173
  def init_G_a_b(G_m, a, b):
180
174
  n_a_nodes = len(a)
181
175
  n_b_nodes = len(b)
182
176
  G_a = np.zeros((n_a_nodes, n_a_nodes), dtype=np.float64)
183
177
  G_b = np.zeros((n_b_nodes, n_b_nodes), dtype=np.float64)
184
- for i in range(n_a_nodes):
178
+ for i in prange(n_a_nodes):
185
179
  for j in range(n_a_nodes):
186
180
  if i == j:
187
181
  continue
188
182
  G_a[i, j] = G_m[a[i], a[j]]
189
- for i in range(n_b_nodes):
183
+ for i in prange(n_b_nodes):
190
184
  for j in range(n_b_nodes):
191
185
  if i == j:
192
186
  continue
@@ -257,6 +251,19 @@ def stitch(G_m, path_a, path_b, sol_weight):
257
251
  return best_path, best_weight
258
252
 
259
253
 
254
+ def monte_carlo_loop(n_nodes):
255
+ bits = ([], [])
256
+ while (len(bits[0]) == 0) or (len(bits[1]) == 0):
257
+ bits = ([], [])
258
+ for i in range(n_nodes):
259
+ if np.random.random() < 0.5:
260
+ bits[0].append(i)
261
+ else:
262
+ bits[1].append(i)
263
+
264
+ return bits
265
+
266
+
260
267
  def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, correction_quality=2, monte_carlo=False, is_3_opt=True, k_neighbors=20, is_cyclic=True, multi_start=1, is_top_level=True):
261
268
  nodes = None
262
269
  n_nodes = 0
@@ -288,26 +295,17 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, corr
288
295
  b = []
289
296
  c = []
290
297
  if (start_node is None) and (end_node is None):
291
- best_energy = float("inf")
292
- for _ in range(multi_start):
293
- energy = 0.0
294
- _a = []
295
- _b = []
296
- while (len(_a) == 0) or (len(_b) == 0):
298
+ if monte_carlo:
299
+ a, b = monte_carlo_loop(n_nodes)
300
+ else:
301
+ best_energy = float("inf")
302
+ for _ in range(multi_start):
297
303
  bits = ([], [])
298
- if monte_carlo:
299
- for i in range(n_nodes):
300
- if np.random.random() < 0.5:
301
- bits[0].append(i)
302
- else:
303
- bits[1].append(i)
304
- else:
304
+ while (len(bits[0]) == 0) or (len(bits[1]) == 0):
305
305
  _, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots, correction_quality=correction_quality)
306
- _a = list(bits[0])
307
- _b = list(bits[1])
308
- if energy < best_energy:
309
- best_energy = energy
310
- a, b = _a, _b
306
+ if energy < best_energy:
307
+ best_energy = energy
308
+ a, b = bits
311
309
  else:
312
310
  is_cyclic = False
313
311
  a.append(nodes.index(start_node))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 2.0.0
3
+ Version: 2.0.2
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
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='pyqrackising',
54
- version='2.0.0',
54
+ version='2.0.2',
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
File without changes