PyQrackIsing 1.10.3__tar.gz → 1.10.5__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.10.3/PyQrackIsing.egg-info → pyqrackising-1.10.5}/PKG-INFO +1 -1
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/PyQrackIsing/_pyqrack_ising.cpp +13 -14
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/PyQrackIsing/generate_tfim_samples.py +3 -1
- pyqrackising-1.10.5/PyQrackIsing/maxcut_tfim.py +234 -0
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/PyQrackIsing/spin_glass_solver.py +16 -10
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/PyQrackIsing/tsp_symmetric.py +14 -5
- {pyqrackising-1.10.3 → pyqrackising-1.10.5/PyQrackIsing.egg-info}/PKG-INFO +1 -1
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/pyproject.toml +1 -1
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/setup.py +1 -1
- pyqrackising-1.10.3/PyQrackIsing/maxcut_tfim.py +0 -131
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/CMakeLists.txt +0 -0
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/LICENSE.md +0 -0
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/MANIFEST.in +0 -0
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/PyQrackIsing/__init__.py +0 -0
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/PyQrackIsing/tfim_magnetization.py +0 -0
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/PyQrackIsing/tfim_square_magnetization.py +0 -0
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/PyQrackIsing.egg-info/not-zip-safe +0 -0
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/PyQrackIsing.egg-info/top_level.txt +0 -0
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/README.md +0 -0
- {pyqrackising-1.10.3 → pyqrackising-1.10.5}/setup.cfg +0 -0
|
@@ -27,7 +27,7 @@ std::random_device rd = std::random_device{};
|
|
|
27
27
|
std::mt19937 rng(rd());
|
|
28
28
|
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
std::vector<double> probability_by_hamming_weight(double J, double h, double z, double theta, double t, size_t n_qubits)
|
|
31
31
|
{
|
|
32
32
|
// critical angle
|
|
33
33
|
const double theta_c = std::asin(std::max(-1.0, std::min(1.0, (std::abs(z * J) >= (std::numeric_limits<double>::epsilon() / 2)) ? std::abs(h) / (z * J) : (J > 0 ? 1.0 : -1.0))));
|
|
@@ -74,16 +74,20 @@ static inline std::string int_to_bitstring(BigInteger integer, size_t length) {
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
std::vector<double> maxcut_hamming_cdf(size_t n_qubits, std::vector<double> J_func, std::vector<double> degrees, int mult_log2) {
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
if (!n_qubits) {
|
|
78
|
+
return std::vector<double>();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const size_t n_steps = n_qubits << mult_log2;
|
|
82
|
+
const size_t shots = n_qubits << mult_log2;
|
|
79
83
|
const double delta_t = 1.0 / (n_steps << (mult_log2 >> 1));
|
|
80
84
|
const double h_mult = (1 << (mult_log2 >> 1)) / (n_steps * delta_t);
|
|
81
85
|
std::vector<double> hamming_prob(n_qubits - 1U, 0.0);
|
|
82
86
|
|
|
83
|
-
for (
|
|
87
|
+
for (size_t step = 0; step < n_steps; ++step) {
|
|
84
88
|
double t = step * delta_t;
|
|
85
89
|
double tm1 = (step - 1) * delta_t;
|
|
86
|
-
for (
|
|
90
|
+
for (size_t q = 0; q < n_qubits; ++q) {
|
|
87
91
|
const double& z = degrees[q];
|
|
88
92
|
const double J_eff = J_func[q];
|
|
89
93
|
const double h_t = h_mult * t;
|
|
@@ -286,15 +290,10 @@ double tfim_square_magnetization(double J, double h, double z, double theta, dou
|
|
|
286
290
|
|
|
287
291
|
PYBIND11_MODULE(tfim_sampler, m) {
|
|
288
292
|
m.doc() = "PyQrackIsing TFIM sample generator";
|
|
289
|
-
m.def("_generate_tfim_samples", &generate_tfim_samples_cpp,
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
m.def("_tfim_magnetization", &tfim_magnetization,
|
|
293
|
-
py::arg("J"), py::arg("h"), py::arg("z"), py::arg("theta"),
|
|
294
|
-
py::arg("t"), py::arg("n_qubits"));
|
|
295
|
-
m.def("_tfim_square_magnetization", &tfim_square_magnetization,
|
|
296
|
-
py::arg("J"), py::arg("h"), py::arg("z"), py::arg("theta"),
|
|
297
|
-
py::arg("t"), py::arg("n_qubits"));
|
|
293
|
+
m.def("_generate_tfim_samples", &generate_tfim_samples_cpp, "Generate measurement samples from globally-uniform TFIM");
|
|
294
|
+
m.def("_tfim_magnetization", &tfim_magnetization, "Magnetization expectation value from globally-uniform TFIM");
|
|
295
|
+
m.def("_tfim_square_magnetization", &tfim_square_magnetization, "Square magnetization expectation value from globally-uniform TFIM");
|
|
298
296
|
m.def("_maxcut_hamming_cdf", &maxcut_hamming_cdf, "Adiabatic TFIM Hamming weight probability density function");
|
|
297
|
+
m.def("_probability_by_hamming_weight", &probability_by_hamming_weight, "Trace probability from globally-uniform TFIM by Hamming weight");
|
|
299
298
|
}
|
|
300
299
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import tfim_sampler
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
def generate_tfim_samples(
|
|
4
|
+
def generate_tfim_samples(
|
|
5
|
+
J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100
|
|
6
|
+
):
|
|
5
7
|
return [int(s) for s in tfim_sampler._generate_tfim_samples(J, h, z, theta, t, n_qubits, shots)]
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import networkx as nx
|
|
2
|
+
import numpy as np
|
|
3
|
+
from numba import njit, prange
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@njit
|
|
7
|
+
def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
|
|
8
|
+
# critical angle
|
|
9
|
+
theta_c = np.arcsin(
|
|
10
|
+
max(
|
|
11
|
+
-1.0,
|
|
12
|
+
min(
|
|
13
|
+
1.0,
|
|
14
|
+
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h) / (z * J)),
|
|
15
|
+
),
|
|
16
|
+
)
|
|
17
|
+
)
|
|
18
|
+
delta_theta = theta - theta_c
|
|
19
|
+
bias = np.zeros(n_qubits + 1)
|
|
20
|
+
|
|
21
|
+
if np.isclose(abs(h), 0.0):
|
|
22
|
+
bias[0] = 1.0
|
|
23
|
+
elif np.isclose(abs(J), 0.0):
|
|
24
|
+
bias.fill(1.0 / (n_qubits + 1.0))
|
|
25
|
+
else:
|
|
26
|
+
sin_delta = np.sin(delta_theta)
|
|
27
|
+
omega = 1.5 * np.pi
|
|
28
|
+
t2 = 1.0
|
|
29
|
+
p = (
|
|
30
|
+
pow(2.0, abs(J / h) - 1.0)
|
|
31
|
+
* (1.0 + sin_delta * np.cos(J * omega * t + theta) / (1.0 + np.sqrt(t / t2)))
|
|
32
|
+
- 0.5
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
if p >= 1024:
|
|
36
|
+
bias[0] = 1.0
|
|
37
|
+
else:
|
|
38
|
+
tot_n = 0.0
|
|
39
|
+
for q in range(n_qubits + 1):
|
|
40
|
+
n = 1.0 / ((n_qubits + 1) * pow(2.0, p * q))
|
|
41
|
+
bias[q] = n
|
|
42
|
+
tot_n += n
|
|
43
|
+
for q in range(n_qubits + 1):
|
|
44
|
+
bias[q] /= tot_n
|
|
45
|
+
|
|
46
|
+
if J > 0.0:
|
|
47
|
+
bias = bias[::-1]
|
|
48
|
+
|
|
49
|
+
return bias
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@njit(parallel=True)
|
|
53
|
+
def maxcut_hamming_cdf(n_qubits, J_func, degrees, mult_log2):
|
|
54
|
+
if n_qubits == 0:
|
|
55
|
+
return np.empty(0, dtype=np.float64)
|
|
56
|
+
|
|
57
|
+
n_steps = n_qubits << mult_log2
|
|
58
|
+
shots = n_qubits << mult_log2
|
|
59
|
+
delta_t = 1.0 / (n_steps << (mult_log2 >> 1))
|
|
60
|
+
h_mult = (1 << (mult_log2 >> 1)) / (n_steps * delta_t)
|
|
61
|
+
hamming_prob = np.zeros(n_qubits - 1)
|
|
62
|
+
|
|
63
|
+
for step in range(n_steps):
|
|
64
|
+
t = step * delta_t
|
|
65
|
+
tm1 = (step - 1) * delta_t
|
|
66
|
+
for q in prange(n_qubits):
|
|
67
|
+
z = degrees[q]
|
|
68
|
+
J_eff = J_func[q]
|
|
69
|
+
h_t = h_mult * t
|
|
70
|
+
bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, t, n_qubits)
|
|
71
|
+
if step == 0:
|
|
72
|
+
for i in range(len(hamming_prob)):
|
|
73
|
+
hamming_prob[i] += bias[i + 1]
|
|
74
|
+
continue
|
|
75
|
+
|
|
76
|
+
last_bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, tm1, n_qubits)
|
|
77
|
+
for i in range(len(hamming_prob)):
|
|
78
|
+
hamming_prob[i] += bias[i + 1] - last_bias[i + 1]
|
|
79
|
+
|
|
80
|
+
tot_prob = sum(hamming_prob)
|
|
81
|
+
for i in prange(len(hamming_prob)):
|
|
82
|
+
hamming_prob[i] /= tot_prob
|
|
83
|
+
|
|
84
|
+
tot_prob = 0.0
|
|
85
|
+
for i in range(len(hamming_prob)):
|
|
86
|
+
tot_prob += hamming_prob[i]
|
|
87
|
+
hamming_prob[i] = tot_prob
|
|
88
|
+
hamming_prob[-1] = 1.0
|
|
89
|
+
|
|
90
|
+
return hamming_prob
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
# Written by Elara (OpenAI custom GPT)
|
|
94
|
+
def local_repulsion_choice(nodes, adjacency, degrees, weights, n, m):
|
|
95
|
+
"""
|
|
96
|
+
Pick m nodes (bit positions) out of n with repulsion bias:
|
|
97
|
+
- High-degree nodes are already less likely
|
|
98
|
+
- After choosing a node, its neighbors' probabilities are further reduced
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
# Base weights: inverse degree
|
|
102
|
+
# degrees = np.array([len(adjacency.get(i, [])) for i in range(n)], dtype=np.float64)
|
|
103
|
+
# weights = 1.0 / (degrees + 1.0)
|
|
104
|
+
weights = weights.copy()
|
|
105
|
+
|
|
106
|
+
chosen = []
|
|
107
|
+
available = set(range(len(nodes)))
|
|
108
|
+
|
|
109
|
+
for _ in range(m):
|
|
110
|
+
if not available:
|
|
111
|
+
break
|
|
112
|
+
|
|
113
|
+
# Normalize weights over remaining nodes
|
|
114
|
+
sub_weights = np.array([weights[i] for i in available], dtype=np.float64)
|
|
115
|
+
sub_weights /= sub_weights.sum()
|
|
116
|
+
sub_nodes = list(available)
|
|
117
|
+
|
|
118
|
+
# Sample one node
|
|
119
|
+
idx = np.random.choice(len(sub_nodes), p=sub_weights)
|
|
120
|
+
node = sub_nodes[idx]
|
|
121
|
+
chosen.append(node)
|
|
122
|
+
|
|
123
|
+
# Remove node from available
|
|
124
|
+
available.remove(node)
|
|
125
|
+
|
|
126
|
+
# Repulsion: penalize neighbors
|
|
127
|
+
for nbr in adjacency.get(nodes[node], []):
|
|
128
|
+
idx = nodes.index(nbr)
|
|
129
|
+
if idx in available:
|
|
130
|
+
weights[idx] *= 0.5 # halve neighbor's weight (tunable!)
|
|
131
|
+
|
|
132
|
+
# Build integer mask
|
|
133
|
+
mask = 0
|
|
134
|
+
for pos in chosen:
|
|
135
|
+
mask |= 1 << pos
|
|
136
|
+
|
|
137
|
+
return mask
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def evaluate_cut_edges(samples, edge_keys, edge_values):
|
|
141
|
+
best_value = float("-inf")
|
|
142
|
+
best_solution = None
|
|
143
|
+
best_cut_edges = None
|
|
144
|
+
|
|
145
|
+
for state in samples:
|
|
146
|
+
cut_edges = []
|
|
147
|
+
cut_value = 0
|
|
148
|
+
for i in range(len(edge_values)):
|
|
149
|
+
k = i << 1
|
|
150
|
+
u, v = edge_keys[k], edge_keys[k + 1]
|
|
151
|
+
if ((state >> u) & 1) != ((state >> v) & 1):
|
|
152
|
+
cut_value += edge_values[i]
|
|
153
|
+
|
|
154
|
+
if cut_value > best_value:
|
|
155
|
+
best_value = cut_value
|
|
156
|
+
best_solution = state
|
|
157
|
+
|
|
158
|
+
return best_solution, float(best_value)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
# By Gemini (Google Search AI)
|
|
162
|
+
def int_to_bitstring(integer, length):
|
|
163
|
+
return (bin(integer)[2:].zfill(length))[::-1]
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def maxcut_tfim(
|
|
167
|
+
G,
|
|
168
|
+
quality=12,
|
|
169
|
+
shots=None,
|
|
170
|
+
):
|
|
171
|
+
# Number of qubits/nodes
|
|
172
|
+
nodes = list(G.nodes())
|
|
173
|
+
n_qubits = len(nodes)
|
|
174
|
+
|
|
175
|
+
if n_qubits == 0:
|
|
176
|
+
return "", 0, ([], [])
|
|
177
|
+
|
|
178
|
+
if shots is None:
|
|
179
|
+
# Number of measurement shots
|
|
180
|
+
shots = n_qubits << quality
|
|
181
|
+
|
|
182
|
+
J_eff = np.array(
|
|
183
|
+
[
|
|
184
|
+
-sum(edge_attributes.get("weight", 1.0) for _, edge_attributes in G.adj[n].items())
|
|
185
|
+
for n in nodes
|
|
186
|
+
],
|
|
187
|
+
dtype=np.float64,
|
|
188
|
+
)
|
|
189
|
+
degrees = np.array(
|
|
190
|
+
[
|
|
191
|
+
sum(abs(edge_attributes.get("weight", 1.0)) for _, edge_attributes in G.adj[n].items())
|
|
192
|
+
for n in nodes
|
|
193
|
+
],
|
|
194
|
+
dtype=np.float64,
|
|
195
|
+
)
|
|
196
|
+
# thresholds = tfim_sampler._maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
|
|
197
|
+
thresholds = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
|
|
198
|
+
G_dict = nx.to_dict_of_lists(G)
|
|
199
|
+
J_max = max(J_eff)
|
|
200
|
+
weights = 1.0 / (1.0 + (J_max - J_eff))
|
|
201
|
+
samples = []
|
|
202
|
+
for s in range(shots):
|
|
203
|
+
# First dimension: Hamming weight
|
|
204
|
+
mag_prob = np.random.random()
|
|
205
|
+
m = 0
|
|
206
|
+
while thresholds[m] < mag_prob:
|
|
207
|
+
m += 1
|
|
208
|
+
m += 1
|
|
209
|
+
# Second dimension: permutation within Hamming weight
|
|
210
|
+
samples.append(local_repulsion_choice(nodes, G_dict, degrees, weights, n_qubits, m))
|
|
211
|
+
|
|
212
|
+
# We only need unique instances
|
|
213
|
+
samples = list(set(samples))
|
|
214
|
+
|
|
215
|
+
edge_keys = []
|
|
216
|
+
edge_values = []
|
|
217
|
+
for u, v, data in G.edges(data=True):
|
|
218
|
+
edge_keys.append(nodes.index(u))
|
|
219
|
+
edge_keys.append(nodes.index(v))
|
|
220
|
+
edge_values.append(data.get("weight", 1.0))
|
|
221
|
+
|
|
222
|
+
best_solution, best_value = evaluate_cut_edges(samples, edge_keys, edge_values)
|
|
223
|
+
|
|
224
|
+
bit_string = int_to_bitstring(best_solution, n_qubits)
|
|
225
|
+
bit_list = list(bit_string)
|
|
226
|
+
l, r = [], []
|
|
227
|
+
for i in range(len(bit_list)):
|
|
228
|
+
b = bit_list[i] == "1"
|
|
229
|
+
if b:
|
|
230
|
+
r.append(nodes[i])
|
|
231
|
+
else:
|
|
232
|
+
l.append(nodes[i])
|
|
233
|
+
|
|
234
|
+
return bit_string, best_value, (l, r)
|
|
@@ -45,7 +45,7 @@ def bootstrap(theta, edge_keys, edge_values, k, indices_array):
|
|
|
45
45
|
energies = np.empty(n)
|
|
46
46
|
j = 0
|
|
47
47
|
for i in prange(n):
|
|
48
|
-
energies[i] = bootstrap_worker(theta, edge_keys, edge_values, indices_array[j:j+k])
|
|
48
|
+
energies[i] = bootstrap_worker(theta, edge_keys, edge_values, indices_array[j : j + k])
|
|
49
49
|
j += k
|
|
50
50
|
|
|
51
51
|
return energies
|
|
@@ -57,7 +57,13 @@ def int_to_bitstring(integer, length):
|
|
|
57
57
|
|
|
58
58
|
|
|
59
59
|
def spin_glass_solver(G, quality=1, best_guess=None):
|
|
60
|
-
|
|
60
|
+
nodes = list(G.nodes())
|
|
61
|
+
n_qubits = len(nodes)
|
|
62
|
+
|
|
63
|
+
if n_qubits == 0:
|
|
64
|
+
return "", 0, ([], []), 0
|
|
65
|
+
|
|
66
|
+
bitstring = ""
|
|
61
67
|
if isinstance(best_guess, str):
|
|
62
68
|
bitstring = best_guess
|
|
63
69
|
elif isinstance(best_guess, int):
|
|
@@ -66,9 +72,7 @@ def spin_glass_solver(G, quality=1, best_guess=None):
|
|
|
66
72
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
67
73
|
else:
|
|
68
74
|
bitstring, _, _ = maxcut_tfim(G, quality=max(0, quality - 1))
|
|
69
|
-
best_theta = [
|
|
70
|
-
nodes = list(G.nodes())
|
|
71
|
-
n_qubits = len(nodes)
|
|
75
|
+
best_theta = [b == "1" for b in list(bitstring)]
|
|
72
76
|
|
|
73
77
|
edge_keys = []
|
|
74
78
|
edge_values = []
|
|
@@ -87,13 +91,15 @@ def spin_glass_solver(G, quality=1, best_guess=None):
|
|
|
87
91
|
|
|
88
92
|
theta = best_theta.copy()
|
|
89
93
|
|
|
90
|
-
combos = list(
|
|
94
|
+
combos = list(
|
|
95
|
+
item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
|
|
96
|
+
)
|
|
91
97
|
energies = bootstrap(theta, edge_keys, edge_values, k, combos)
|
|
92
98
|
|
|
93
99
|
energy = min(energies)
|
|
94
100
|
if energy < min_energy:
|
|
95
101
|
index_match = np.random.choice(np.where(energies == energy)[0])
|
|
96
|
-
indices = combos[(index_match * k):((index_match + 1) * k)]
|
|
102
|
+
indices = combos[(index_match * k) : ((index_match + 1) * k)]
|
|
97
103
|
min_energy = energy
|
|
98
104
|
for i in indices:
|
|
99
105
|
best_theta[i] = not best_theta[i]
|
|
@@ -101,16 +107,16 @@ def spin_glass_solver(G, quality=1, best_guess=None):
|
|
|
101
107
|
break
|
|
102
108
|
|
|
103
109
|
sample = 0
|
|
104
|
-
bitstring =
|
|
110
|
+
bitstring = ""
|
|
105
111
|
l, r = [], []
|
|
106
112
|
for i in range(len(best_theta)):
|
|
107
113
|
b = best_theta[i]
|
|
108
114
|
if b:
|
|
109
|
-
bitstring +=
|
|
115
|
+
bitstring += "1"
|
|
110
116
|
r.append(nodes[i])
|
|
111
117
|
sample |= 1 << i
|
|
112
118
|
else:
|
|
113
|
-
bitstring +=
|
|
119
|
+
bitstring += "0"
|
|
114
120
|
l.append(nodes[i])
|
|
115
121
|
|
|
116
122
|
cut_value = evaluate_cut_edges(sample, edge_keys, edge_values)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from .spin_glass_solver import spin_glass_solver
|
|
2
2
|
import networkx as nx
|
|
3
3
|
|
|
4
|
+
|
|
4
5
|
def get_best_stitch(adjacency, terminals_a, terminals_b, is_cyclic):
|
|
5
6
|
best_weight = float("inf")
|
|
6
7
|
best_edge = None
|
|
@@ -24,7 +25,7 @@ def tsp_symmetric(G, quality=2, is_cyclic=True, start_node=None):
|
|
|
24
25
|
if G.number_of_nodes() == 1:
|
|
25
26
|
return ([0], 0)
|
|
26
27
|
if G.number_of_nodes() == 2:
|
|
27
|
-
return ([0, 1], G[0][1][
|
|
28
|
+
return ([0, 1], G[0][1]["weight"])
|
|
28
29
|
|
|
29
30
|
nodes = list(G.nodes())
|
|
30
31
|
|
|
@@ -38,7 +39,7 @@ def tsp_symmetric(G, quality=2, is_cyclic=True, start_node=None):
|
|
|
38
39
|
while (len(a) == 0) or (len(b) == 0):
|
|
39
40
|
bitstring, _, _, _ = spin_glass_solver(G, quality=quality)
|
|
40
41
|
for idx, bit in enumerate(bitstring):
|
|
41
|
-
if bit ==
|
|
42
|
+
if bit == "1":
|
|
42
43
|
b.append(nodes[idx])
|
|
43
44
|
else:
|
|
44
45
|
a.append(nodes[idx])
|
|
@@ -54,9 +55,17 @@ def tsp_symmetric(G, quality=2, is_cyclic=True, start_node=None):
|
|
|
54
55
|
G_b.add_edge(b.index(u), b.index(v), weight=weight)
|
|
55
56
|
continue
|
|
56
57
|
|
|
57
|
-
sol_a =
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
sol_a = (
|
|
59
|
+
tsp_symmetric(G_a, quality=quality, is_cyclic=False)
|
|
60
|
+
if len(a) > 2
|
|
61
|
+
else (([0, 1], G_a[0][1]["weight"]) if len(a) == 2 else ([0], 0))
|
|
62
|
+
)
|
|
63
|
+
sol_b = (
|
|
64
|
+
tsp_symmetric(G_b, quality=quality, is_cyclic=False)
|
|
65
|
+
if len(b) > 2
|
|
66
|
+
else (([0, 1], G_b[0][1]["weight"]) if len(b) == 2 else ([0], 0))
|
|
67
|
+
)
|
|
68
|
+
sol_weight = sol_a[1] + sol_b[1]
|
|
60
69
|
|
|
61
70
|
path_a = [a[idx] for idx in sol_a[0]]
|
|
62
71
|
path_b = [b[idx] for idx in sol_b[0]]
|
|
@@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
|
|
|
11
11
|
|
|
12
12
|
[project]
|
|
13
13
|
name = "PyQrackIsing"
|
|
14
|
-
version = "1.10.
|
|
14
|
+
version = "1.10.5"
|
|
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.10.
|
|
54
|
+
version='1.10.5',
|
|
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)',
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import networkx as nx
|
|
2
|
-
import numpy as np
|
|
3
|
-
import tfim_sampler
|
|
4
|
-
from numba import njit
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
# Written by Elara (OpenAI custom GPT)
|
|
8
|
-
def local_repulsion_choice(nodes, adjacency, degrees, weights, n, m):
|
|
9
|
-
"""
|
|
10
|
-
Pick m nodes (bit positions) out of n with repulsion bias:
|
|
11
|
-
- High-degree nodes are already less likely
|
|
12
|
-
- After choosing a node, its neighbors' probabilities are further reduced
|
|
13
|
-
"""
|
|
14
|
-
|
|
15
|
-
# Base weights: inverse degree
|
|
16
|
-
# degrees = np.array([len(adjacency.get(i, [])) for i in range(n)], dtype=np.float64)
|
|
17
|
-
# weights = 1.0 / (degrees + 1.0)
|
|
18
|
-
weights = weights.copy()
|
|
19
|
-
|
|
20
|
-
chosen = []
|
|
21
|
-
available = set(range(len(nodes)))
|
|
22
|
-
|
|
23
|
-
for _ in range(m):
|
|
24
|
-
if not available:
|
|
25
|
-
break
|
|
26
|
-
|
|
27
|
-
# Normalize weights over remaining nodes
|
|
28
|
-
sub_weights = np.array([weights[i] for i in available], dtype=np.float64)
|
|
29
|
-
sub_weights /= sub_weights.sum()
|
|
30
|
-
sub_nodes = list(available)
|
|
31
|
-
|
|
32
|
-
# Sample one node
|
|
33
|
-
idx = np.random.choice(len(sub_nodes), p=sub_weights)
|
|
34
|
-
node = sub_nodes[idx]
|
|
35
|
-
chosen.append(node)
|
|
36
|
-
|
|
37
|
-
# Remove node from available
|
|
38
|
-
available.remove(node)
|
|
39
|
-
|
|
40
|
-
# Repulsion: penalize neighbors
|
|
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!)
|
|
45
|
-
|
|
46
|
-
# Build integer mask
|
|
47
|
-
mask = 0
|
|
48
|
-
for pos in chosen:
|
|
49
|
-
mask |= (1 << pos)
|
|
50
|
-
|
|
51
|
-
return mask
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
def evaluate_cut_edges(samples, edge_keys, edge_values):
|
|
55
|
-
best_value = float("-inf")
|
|
56
|
-
best_solution = None
|
|
57
|
-
best_cut_edges = None
|
|
58
|
-
|
|
59
|
-
for state in samples:
|
|
60
|
-
cut_edges = []
|
|
61
|
-
cut_value = 0
|
|
62
|
-
for i in range(len(edge_values)):
|
|
63
|
-
k = i << 1
|
|
64
|
-
u, v = edge_keys[k], edge_keys[k + 1]
|
|
65
|
-
if ((state >> u) & 1) != ((state >> v) & 1):
|
|
66
|
-
cut_value += edge_values[i]
|
|
67
|
-
|
|
68
|
-
if cut_value > best_value:
|
|
69
|
-
best_value = cut_value
|
|
70
|
-
best_solution = state
|
|
71
|
-
|
|
72
|
-
return best_solution, float(best_value)
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
# By Gemini (Google Search AI)
|
|
76
|
-
def int_to_bitstring(integer, length):
|
|
77
|
-
return (bin(integer)[2:].zfill(length))[::-1]
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
def maxcut_tfim(
|
|
81
|
-
G,
|
|
82
|
-
quality = 12,
|
|
83
|
-
shots = None,
|
|
84
|
-
):
|
|
85
|
-
# Number of qubits/nodes
|
|
86
|
-
nodes = list(G.nodes())
|
|
87
|
-
n_qubits = len(nodes)
|
|
88
|
-
|
|
89
|
-
if shots is None:
|
|
90
|
-
# Number of measurement shots
|
|
91
|
-
shots = n_qubits << quality
|
|
92
|
-
|
|
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)
|
|
95
|
-
thresholds = tfim_sampler._maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
|
|
96
|
-
G_dict = nx.to_dict_of_lists(G)
|
|
97
|
-
J_max = max(J_eff)
|
|
98
|
-
weights = 1.0 / (1.0 + (J_max - J_eff))
|
|
99
|
-
samples = []
|
|
100
|
-
for s in range(shots):
|
|
101
|
-
# First dimension: Hamming weight
|
|
102
|
-
mag_prob = np.random.random()
|
|
103
|
-
m = 0
|
|
104
|
-
while thresholds[m] < mag_prob:
|
|
105
|
-
m += 1
|
|
106
|
-
m += 1
|
|
107
|
-
# Second dimension: permutation within Hamming weight
|
|
108
|
-
samples.append(local_repulsion_choice(nodes, G_dict, degrees, weights, n_qubits, m))
|
|
109
|
-
|
|
110
|
-
# We only need unique instances
|
|
111
|
-
samples = list(set(samples))
|
|
112
|
-
|
|
113
|
-
edge_keys = []
|
|
114
|
-
edge_values = []
|
|
115
|
-
for u, v, data in G.edges(data=True):
|
|
116
|
-
edge_keys.append(nodes.index(u))
|
|
117
|
-
edge_keys.append(nodes.index(v))
|
|
118
|
-
edge_values.append(data.get("weight", 1.0))
|
|
119
|
-
|
|
120
|
-
best_solution, best_value = evaluate_cut_edges(samples, edge_keys, edge_values)
|
|
121
|
-
|
|
122
|
-
bit_list = list(int_to_bitstring(best_solution, n_qubits))
|
|
123
|
-
l, r = [], []
|
|
124
|
-
for i in range(len(bit_list)):
|
|
125
|
-
b = (bit_list[i] == '1')
|
|
126
|
-
if b:
|
|
127
|
-
r.append(nodes[i])
|
|
128
|
-
else:
|
|
129
|
-
l.append(nodes[i])
|
|
130
|
-
|
|
131
|
-
return bit_list, best_value, (l, r)
|
|
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
|
|
File without changes
|