PyQrackIsing 1.8.4__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.
- {pyqrackising-1.8.4/PyQrackIsing.egg-info → pyqrackising-1.9.0}/PKG-INFO +1 -1
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/PyQrackIsing/__init__.py +1 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/PyQrackIsing/maxcut_tfim.py +26 -16
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/PyQrackIsing/spin_glass_solver.py +15 -11
- pyqrackising-1.9.0/PyQrackIsing/tsp_symmetric.py +70 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0/PyQrackIsing.egg-info}/PKG-INFO +1 -1
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/PyQrackIsing.egg-info/SOURCES.txt +1 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/README.md +30 -3
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/pyproject.toml +1 -1
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/setup.py +1 -1
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/CMakeLists.txt +0 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/LICENSE.md +0 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/MANIFEST.in +0 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/PyQrackIsing/generate_tfim_samples.py +0 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/PyQrackIsing/tfim_magnetization.py +0 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/PyQrackIsing/tfim_square_magnetization.py +0 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/PyQrackIsing.egg-info/not-zip-safe +0 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/PyQrackIsing.egg-info/top_level.txt +0 -0
- {pyqrackising-1.8.4 → pyqrackising-1.9.0}/setup.cfg +0 -0
|
@@ -5,7 +5,7 @@ from numba import njit
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
# Written by Elara (OpenAI custom GPT)
|
|
8
|
-
def local_repulsion_choice(adjacency, degrees, weights, n, m):
|
|
8
|
+
def local_repulsion_choice(nodes, adjacency, degrees, weights, n, m):
|
|
9
9
|
"""
|
|
10
10
|
Pick m nodes (bit positions) out of n with repulsion bias:
|
|
11
11
|
- High-degree nodes are already less likely
|
|
@@ -18,7 +18,7 @@ def local_repulsion_choice(adjacency, degrees, weights, n, m):
|
|
|
18
18
|
weights = weights.copy()
|
|
19
19
|
|
|
20
20
|
chosen = []
|
|
21
|
-
available = set(range(
|
|
21
|
+
available = set(range(len(nodes)))
|
|
22
22
|
|
|
23
23
|
for _ in range(m):
|
|
24
24
|
if not available:
|
|
@@ -38,9 +38,10 @@ def local_repulsion_choice(adjacency, degrees, weights, n, m):
|
|
|
38
38
|
available.remove(node)
|
|
39
39
|
|
|
40
40
|
# Repulsion: penalize neighbors
|
|
41
|
-
for nbr in adjacency.get(node, []):
|
|
42
|
-
|
|
43
|
-
|
|
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!)
|
|
44
45
|
|
|
45
46
|
# Build integer mask
|
|
46
47
|
mask = 0
|
|
@@ -62,15 +63,13 @@ def evaluate_cut_edges(samples, edge_keys, edge_values):
|
|
|
62
63
|
k = i << 1
|
|
63
64
|
u, v = edge_keys[k], edge_keys[k + 1]
|
|
64
65
|
if ((state >> u) & 1) != ((state >> v) & 1):
|
|
65
|
-
cut_edges.append((u, v))
|
|
66
66
|
cut_value += edge_values[i]
|
|
67
67
|
|
|
68
68
|
if cut_value > best_value:
|
|
69
69
|
best_value = cut_value
|
|
70
70
|
best_solution = state
|
|
71
|
-
best_cut_edges = cut_edges
|
|
72
71
|
|
|
73
|
-
return float(best_value)
|
|
72
|
+
return best_solution, float(best_value)
|
|
74
73
|
|
|
75
74
|
|
|
76
75
|
# By Gemini (Google Search AI)
|
|
@@ -84,13 +83,15 @@ def maxcut_tfim(
|
|
|
84
83
|
shots = None,
|
|
85
84
|
):
|
|
86
85
|
# Number of qubits/nodes
|
|
87
|
-
|
|
86
|
+
nodes = list(G.nodes())
|
|
87
|
+
n_qubits = len(nodes)
|
|
88
|
+
|
|
88
89
|
if shots is None:
|
|
89
90
|
# Number of measurement shots
|
|
90
91
|
shots = n_qubits << quality
|
|
91
92
|
|
|
92
|
-
J_eff = np.array([-sum(edge_attributes.get('weight', 1.0) for _, edge_attributes in G.adj[
|
|
93
|
-
degrees = np.array([sum(abs(edge_attributes.get('weight', 1.0)) for _, edge_attributes in G.adj[
|
|
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)
|
|
94
95
|
thresholds = tfim_sampler._maxcut_hamming_cdf(J_eff, degrees, quality)
|
|
95
96
|
G_dict = nx.to_dict_of_lists(G)
|
|
96
97
|
J_max = max(J_eff)
|
|
@@ -104,15 +105,24 @@ def maxcut_tfim(
|
|
|
104
105
|
m += 1
|
|
105
106
|
m += 1
|
|
106
107
|
# Second dimension: permutation within Hamming weight
|
|
107
|
-
samples.append(local_repulsion_choice(G_dict, degrees, weights, n_qubits, m))
|
|
108
|
+
samples.append(local_repulsion_choice(nodes, G_dict, degrees, weights, n_qubits, m))
|
|
108
109
|
|
|
109
110
|
edge_keys = []
|
|
110
111
|
edge_values = []
|
|
111
112
|
for u, v, data in G.edges(data=True):
|
|
112
|
-
edge_keys.append(u)
|
|
113
|
-
edge_keys.append(v)
|
|
113
|
+
edge_keys.append(nodes.index(u))
|
|
114
|
+
edge_keys.append(nodes.index(v))
|
|
114
115
|
edge_values.append(data.get("weight", 1.0))
|
|
115
116
|
|
|
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])
|
|
117
127
|
|
|
118
|
-
return best_value,
|
|
128
|
+
return bit_list, best_value, (l, r)
|
|
@@ -6,16 +6,14 @@ import os
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
def evaluate_cut_edges(state, edge_keys, edge_values):
|
|
9
|
-
cut_edges = []
|
|
10
9
|
cut_value = 0
|
|
11
10
|
for i in range(len(edge_values)):
|
|
12
11
|
k = i << 1
|
|
13
12
|
u, v = edge_keys[k], edge_keys[k + 1]
|
|
14
13
|
if ((state >> u) & 1) != ((state >> v) & 1):
|
|
15
|
-
cut_edges.append((u, v))
|
|
16
14
|
cut_value += edge_values[i]
|
|
17
15
|
|
|
18
|
-
return float(cut_value)
|
|
16
|
+
return float(cut_value)
|
|
19
17
|
|
|
20
18
|
|
|
21
19
|
@njit
|
|
@@ -67,15 +65,16 @@ def spin_glass_solver(G, quality=2, best_guess=None):
|
|
|
67
65
|
elif isinstance(best_guess, list):
|
|
68
66
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
69
67
|
else:
|
|
70
|
-
|
|
68
|
+
bitstring, _, _ = maxcut_tfim(G, quality=0)
|
|
71
69
|
best_theta = [ b == '1' for b in list(bitstring)]
|
|
72
|
-
|
|
70
|
+
nodes = list(G.nodes())
|
|
71
|
+
n_qubits = len(nodes)
|
|
73
72
|
|
|
74
73
|
edge_keys = []
|
|
75
74
|
edge_values = []
|
|
76
75
|
for u, v, data in G.edges(data=True):
|
|
77
|
-
edge_keys.append(u)
|
|
78
|
-
edge_keys.append(v)
|
|
76
|
+
edge_keys.append(nodes.index(u))
|
|
77
|
+
edge_keys.append(nodes.index(v))
|
|
79
78
|
edge_values.append(data.get("weight", 1.0))
|
|
80
79
|
|
|
81
80
|
min_energy = compute_energy(best_theta, edge_keys, edge_values)
|
|
@@ -102,13 +101,18 @@ def spin_glass_solver(G, quality=2, best_guess=None):
|
|
|
102
101
|
break
|
|
103
102
|
|
|
104
103
|
sample = 0
|
|
105
|
-
bitstring =
|
|
104
|
+
bitstring = ''
|
|
105
|
+
l, r = [], []
|
|
106
106
|
for i in range(len(best_theta)):
|
|
107
107
|
b = best_theta[i]
|
|
108
|
-
bitstring += '1' if b else '0'
|
|
109
108
|
if b:
|
|
109
|
+
bitstring += '1'
|
|
110
|
+
r.append(nodes[i])
|
|
110
111
|
sample |= 1 << i
|
|
112
|
+
else:
|
|
113
|
+
bitstring += '0'
|
|
114
|
+
l.append(nodes[i])
|
|
111
115
|
|
|
112
|
-
cut_value
|
|
116
|
+
cut_value = evaluate_cut_edges(sample, edge_keys, edge_values)
|
|
113
117
|
|
|
114
|
-
return float(cut_value),
|
|
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)
|
|
@@ -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
|
-
|
|
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
|
-
|
|
73
|
-
#
|
|
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.
|
|
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.
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|