PyQrackIsing 1.8.3__tar.gz → 1.9.0__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.8.3/PyQrackIsing.egg-info → pyqrackising-1.9.0}/PKG-INFO +1 -1
  2. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/PyQrackIsing/__init__.py +1 -0
  3. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/PyQrackIsing/maxcut_tfim.py +39 -19
  4. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/PyQrackIsing/spin_glass_solver.py +15 -12
  5. pyqrackising-1.9.0/PyQrackIsing/tsp_symmetric.py +70 -0
  6. {pyqrackising-1.8.3 → pyqrackising-1.9.0/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  7. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/PyQrackIsing.egg-info/SOURCES.txt +1 -0
  8. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/README.md +30 -3
  9. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/pyproject.toml +1 -1
  10. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/setup.py +1 -1
  11. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/CMakeLists.txt +0 -0
  12. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/LICENSE.md +0 -0
  13. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/MANIFEST.in +0 -0
  14. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  15. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/PyQrackIsing/generate_tfim_samples.py +0 -0
  16. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/PyQrackIsing/tfim_magnetization.py +0 -0
  17. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  18. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  19. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  20. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/PyQrackIsing.egg-info/top_level.txt +0 -0
  21. {pyqrackising-1.8.3 → pyqrackising-1.9.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.8.3
3
+ Version: 1.9.0
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
@@ -3,3 +3,4 @@ from .tfim_magnetization import tfim_magnetization
3
3
  from .tfim_square_magnetization import tfim_square_magnetization
4
4
  from .maxcut_tfim import maxcut_tfim
5
5
  from .spin_glass_solver import spin_glass_solver
6
+ from .tsp_symmetric import tsp_symmetric
@@ -1,10 +1,11 @@
1
1
  import networkx as nx
2
2
  import numpy as np
3
3
  import tfim_sampler
4
+ from numba import njit
4
5
 
5
6
 
6
7
  # Written by Elara (OpenAI custom GPT)
7
- def local_repulsion_choice(adjacency, degrees, weights, n, m):
8
+ def local_repulsion_choice(nodes, adjacency, degrees, weights, n, m):
8
9
  """
9
10
  Pick m nodes (bit positions) out of n with repulsion bias:
10
11
  - High-degree nodes are already less likely
@@ -17,7 +18,7 @@ def local_repulsion_choice(adjacency, degrees, weights, n, m):
17
18
  weights = weights.copy()
18
19
 
19
20
  chosen = []
20
- available = set(range(n))
21
+ available = set(range(len(nodes)))
21
22
 
22
23
  for _ in range(m):
23
24
  if not available:
@@ -37,9 +38,10 @@ def local_repulsion_choice(adjacency, degrees, weights, n, m):
37
38
  available.remove(node)
38
39
 
39
40
  # Repulsion: penalize neighbors
40
- for nbr in adjacency.get(node, []):
41
- if nbr in available:
42
- weights[nbr] *= 0.5 # halve neighbor's weight (tunable!)
41
+ for nbr in adjacency.get(nodes[node], []):
42
+ idx = nodes.index(nbr)
43
+ if idx in available:
44
+ weights[idx] *= 0.5 # halve neighbor's weight (tunable!)
43
45
 
44
46
  # Build integer mask
45
47
  mask = 0
@@ -49,7 +51,7 @@ def local_repulsion_choice(adjacency, degrees, weights, n, m):
49
51
  return mask
50
52
 
51
53
 
52
- def evaluate_cut_edges(samples, G):
54
+ def evaluate_cut_edges(samples, edge_keys, edge_values):
53
55
  best_value = float("-inf")
54
56
  best_solution = None
55
57
  best_cut_edges = None
@@ -57,17 +59,17 @@ def evaluate_cut_edges(samples, G):
57
59
  for state in samples:
58
60
  cut_edges = []
59
61
  cut_value = 0
60
- for u, v, data in G.edges(data=True):
62
+ for i in range(len(edge_values)):
63
+ k = i << 1
64
+ u, v = edge_keys[k], edge_keys[k + 1]
61
65
  if ((state >> u) & 1) != ((state >> v) & 1):
62
- cut_edges.append((u, v))
63
- cut_value += data.get("weight", 1.0)
66
+ cut_value += edge_values[i]
64
67
 
65
68
  if cut_value > best_value:
66
69
  best_value = cut_value
67
70
  best_solution = state
68
- best_cut_edges = cut_edges
69
71
 
70
- return float(best_value), best_solution, best_cut_edges
72
+ return best_solution, float(best_value)
71
73
 
72
74
 
73
75
  # By Gemini (Google Search AI)
@@ -81,13 +83,15 @@ def maxcut_tfim(
81
83
  shots = None,
82
84
  ):
83
85
  # Number of qubits/nodes
84
- n_qubits = G.number_of_nodes()
86
+ nodes = list(G.nodes())
87
+ n_qubits = len(nodes)
88
+
85
89
  if shots is None:
86
90
  # Number of measurement shots
87
91
  shots = n_qubits << quality
88
92
 
89
- J_eff = np.array([-sum(edge_attributes.get('weight', 1.0) for _, edge_attributes in G.adj[i].items()) for i in range(n_qubits)], dtype=np.float64)
90
- degrees = np.array([sum(abs(edge_attributes.get('weight', 1.0)) for _, edge_attributes in G.adj[i].items()) for i in range(n_qubits)], dtype=np.float64)
93
+ J_eff = np.array([-sum(edge_attributes.get('weight', 1.0) for _, edge_attributes in G.adj[n].items()) for n in nodes], dtype=np.float64)
94
+ degrees = np.array([sum(abs(edge_attributes.get('weight', 1.0)) for _, edge_attributes in G.adj[n].items()) for n in nodes], dtype=np.float64)
91
95
  thresholds = tfim_sampler._maxcut_hamming_cdf(J_eff, degrees, quality)
92
96
  G_dict = nx.to_dict_of_lists(G)
93
97
  J_max = max(J_eff)
@@ -101,8 +105,24 @@ def maxcut_tfim(
101
105
  m += 1
102
106
  m += 1
103
107
  # Second dimension: permutation within Hamming weight
104
- samples.append(local_repulsion_choice(G_dict, degrees, weights, n_qubits, m))
105
-
106
- best_value, best_solution, best_cut_edges = evaluate_cut_edges(samples, G)
107
-
108
- return best_value, int_to_bitstring(best_solution, n_qubits), best_cut_edges
108
+ samples.append(local_repulsion_choice(nodes, G_dict, degrees, weights, n_qubits, m))
109
+
110
+ edge_keys = []
111
+ edge_values = []
112
+ for u, v, data in G.edges(data=True):
113
+ edge_keys.append(nodes.index(u))
114
+ edge_keys.append(nodes.index(v))
115
+ edge_values.append(data.get("weight", 1.0))
116
+
117
+ best_solution, best_value = evaluate_cut_edges(samples, edge_keys, edge_values)
118
+
119
+ bit_list = list(int_to_bitstring(best_solution, n_qubits))
120
+ l, r = [], []
121
+ for i in range(len(bit_list)):
122
+ b = (bit_list[i] == '1')
123
+ if b:
124
+ r.append(nodes[i])
125
+ else:
126
+ l.append(nodes[i])
127
+
128
+ return bit_list, best_value, (l, r)
@@ -5,18 +5,15 @@ from numba import njit, prange
5
5
  import os
6
6
 
7
7
 
8
- @njit
9
8
  def evaluate_cut_edges(state, edge_keys, edge_values):
10
- cut_edges = []
11
9
  cut_value = 0
12
10
  for i in range(len(edge_values)):
13
11
  k = i << 1
14
12
  u, v = edge_keys[k], edge_keys[k + 1]
15
13
  if ((state >> u) & 1) != ((state >> v) & 1):
16
- cut_edges.append((u, v))
17
14
  cut_value += edge_values[i]
18
15
 
19
- return float(cut_value), cut_edges
16
+ return float(cut_value)
20
17
 
21
18
 
22
19
  @njit
@@ -68,15 +65,16 @@ def spin_glass_solver(G, quality=2, best_guess=None):
68
65
  elif isinstance(best_guess, list):
69
66
  bitstring = "".join(["1" if b else "0" for b in best_guess])
70
67
  else:
71
- cut_value, bitstring, cut_edges = maxcut_tfim(G, quality=0)
68
+ bitstring, _, _ = maxcut_tfim(G, quality=0)
72
69
  best_theta = [ b == '1' for b in list(bitstring)]
73
- n_qubits = len(best_theta)
70
+ nodes = list(G.nodes())
71
+ n_qubits = len(nodes)
74
72
 
75
73
  edge_keys = []
76
74
  edge_values = []
77
75
  for u, v, data in G.edges(data=True):
78
- edge_keys.append(u)
79
- edge_keys.append(v)
76
+ edge_keys.append(nodes.index(u))
77
+ edge_keys.append(nodes.index(v))
80
78
  edge_values.append(data.get("weight", 1.0))
81
79
 
82
80
  min_energy = compute_energy(best_theta, edge_keys, edge_values)
@@ -103,13 +101,18 @@ def spin_glass_solver(G, quality=2, best_guess=None):
103
101
  break
104
102
 
105
103
  sample = 0
106
- bitstring = ""
104
+ bitstring = ''
105
+ l, r = [], []
107
106
  for i in range(len(best_theta)):
108
107
  b = best_theta[i]
109
- bitstring += '1' if b else '0'
110
108
  if b:
109
+ bitstring += '1'
110
+ r.append(nodes[i])
111
111
  sample |= 1 << i
112
+ else:
113
+ bitstring += '0'
114
+ l.append(nodes[i])
112
115
 
113
- cut_value, cut_edges = evaluate_cut_edges(sample, edge_keys, edge_values)
116
+ cut_value = evaluate_cut_edges(sample, edge_keys, edge_values)
114
117
 
115
- return float(cut_value), bitstring, cut_edges, float(min_energy)
118
+ return bitstring, float(cut_value), (l, r), float(min_energy)
@@ -0,0 +1,70 @@
1
+ from .spin_glass_solver import spin_glass_solver
2
+ import networkx as nx
3
+
4
+ def get_best_stitch(adjacency, terminals_a, terminals_b, is_cyclic):
5
+ best_weight = float("inf")
6
+ best_edge = None
7
+ for a in range(2):
8
+ a_term = terminals_a[a]
9
+ for b in range(2):
10
+ b_term = terminals_b[b]
11
+ weight = adjacency[a_term][b_term]["weight"]
12
+ if is_cyclic:
13
+ n_a_term = terminals_a[0 if a else 1]
14
+ n_b_term = terminals_b[0 if b else 1]
15
+ weight += adjacency[n_a_term][n_b_term]["weight"]
16
+ if weight < best_weight:
17
+ best_weight = weight
18
+ best_edge = (a, b)
19
+
20
+ return best_weight, best_edge
21
+
22
+
23
+ def tsp_symmetric(G, quality=2, is_cyclic=True):
24
+ if G.number_of_nodes() == 1:
25
+ return ([0], 0)
26
+ if G.number_of_nodes() == 2:
27
+ return ([0, 1], G[0][1]['weight'])
28
+
29
+ nodes = list(G.nodes())
30
+
31
+ a = []
32
+ b = []
33
+ while (len(a) == 0) or (len(b) == 0):
34
+ bitstring, _, _, _ = spin_glass_solver(G, quality=quality)
35
+ for idx, bit in enumerate(bitstring):
36
+ if bit == '1':
37
+ b.append(nodes[idx])
38
+ else:
39
+ a.append(nodes[idx])
40
+
41
+ G_a = nx.Graph()
42
+ G_b = nx.Graph()
43
+ for u, v, data in G.edges(data=True):
44
+ weight = data.get("weight", 1.0)
45
+ if (u in a) and (v in a):
46
+ G_a.add_edge(a.index(u), a.index(v), weight=weight)
47
+ continue
48
+ elif (u in b) and (v in b):
49
+ G_b.add_edge(b.index(u), b.index(v), weight=weight)
50
+ continue
51
+
52
+ sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False) if len(a) > 2 else (([0, 1], G_a[0][1]['weight']) if len(a) == 2 else ([0], 0))
53
+ sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False) if len(b) > 2 else (([0, 1], G_b[0][1]['weight']) if len(b) == 2 else ([0], 0))
54
+ sol_weight = sol_a[1] + sol_b[1]
55
+
56
+ path_a = [a[idx] for idx in sol_a[0]]
57
+ path_b = [b[idx] for idx in sol_b[0]]
58
+
59
+ terminals_a = (path_a[0], path_a[-1])
60
+ terminals_b = (path_b[0], path_b[-1])
61
+
62
+ # Find the best edge to stitch "a" to "b"
63
+ best_weight, best_end = get_best_stitch(G, terminals_a, terminals_b, is_cyclic)
64
+
65
+ if best_end[0] == 0:
66
+ path_a.reverse()
67
+ if best_end[1] == 1:
68
+ path_b.reverse()
69
+
70
+ return (path_a + path_b, sol_weight + best_weight)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.8.3
3
+ Version: 1.9.0
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,6 +11,7 @@ PyQrackIsing/maxcut_tfim.py
11
11
  PyQrackIsing/spin_glass_solver.py
12
12
  PyQrackIsing/tfim_magnetization.py
13
13
  PyQrackIsing/tfim_square_magnetization.py
14
+ PyQrackIsing/tsp_symmetric.py
14
15
  PyQrackIsing.egg-info/PKG-INFO
15
16
  PyQrackIsing.egg-info/SOURCES.txt
16
17
  PyQrackIsing.egg-info/dependency_links.txt
@@ -46,7 +46,7 @@ from PyQrackIsing import maxcut_tfim
46
46
  import networkx as nx
47
47
 
48
48
  G = nx.petersen_graph()
49
- best_cut_value, best_solution_bit_string, best_cut_edges = maxcut_tfim(G, quality=12)
49
+ best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=12)
50
50
  ```
51
51
 
52
52
  The (integer) `quality` setting is optional, with a default value of `12`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
@@ -69,11 +69,38 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
69
69
 
70
70
 
71
71
  G = generate_spin_glass_graph(n_nodes=64, seed=42)
72
- cut_value, bitstring, cut_edges, energy = spin_glass_solver(G, quality=2, best_guess=None)
73
- # cut_value, bitstring, cut_edges, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=12)[1])
72
+ solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=2, best_guess=None)
73
+ # solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=12)[1])
74
74
  ```
75
75
  The `quality` setting default is `2`, and overhead grows roughly like the qubit count to the power of `quality` (so `O(n ** 2)` for default). `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). By default, `spin_glass_solver()` uses `maxcut_tfim(G, quality=0)` as `best_guess`, which typically works well, but it could be seeded with higher `maxcut_tfim()` `quality` or Goemans-Williamson, for example. This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
76
76
 
77
+ From the `spin_glass_solver()`, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
78
+ ```py
79
+ from PyQrackIsing import tsp_symmetric
80
+ import networkx as nx
81
+ import numpy as np
82
+
83
+ # Traveling Salesman Problem (normalized to longest segment)
84
+ def generate_tsp_graph(n_nodes=64, seed=None):
85
+ if not (seed is None):
86
+ np.random.seed(seed)
87
+ G = nx.Graph()
88
+ for u in range(n_nodes):
89
+ for v in range(u + 1, n_nodes):
90
+ G.add_edge(u, v, weight=np.random.random())
91
+ return G
92
+
93
+
94
+ n_nodes = 128
95
+ G = generate_tsp_graph(n_nodes=n_nodes, seed=42)
96
+ circuit, path_length = tsp_symmetric(G, quality=2, is_cyclic=True)
97
+
98
+ print(f"Node count: {n_nodes}")
99
+ print(f"Path: {circuit}")
100
+ print(f"Path length: {path_length}")
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"). `is_cyclic` controls whether the path must ultimately complete a circuit, if `True`, or a one-way trip, if `False`.
103
+
77
104
  ## About
78
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.
79
106
 
@@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
11
11
 
12
12
  [project]
13
13
  name = "PyQrackIsing"
14
- version = "1.8.3"
14
+ version = "1.9.0"
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"}
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='PyQrackIsing',
54
- version='1.8.3',
54
+ version='1.9.0',
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