PyQrackIsing 1.10.4__tar.gz → 1.10.6__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.4/PyQrackIsing.egg-info → pyqrackising-1.10.6}/PKG-INFO +1 -1
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/PyQrackIsing/_pyqrack_ising.cpp +3 -57
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/PyQrackIsing/generate_tfim_samples.py +3 -1
- pyqrackising-1.10.6/PyQrackIsing/maxcut_tfim.py +238 -0
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/PyQrackIsing/spin_glass_solver.py +11 -9
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/PyQrackIsing/tsp_symmetric.py +14 -5
- {pyqrackising-1.10.4 → pyqrackising-1.10.6/PyQrackIsing.egg-info}/PKG-INFO +1 -1
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/pyproject.toml +1 -1
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/setup.py +1 -1
- pyqrackising-1.10.4/PyQrackIsing/maxcut_tfim.py +0 -135
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/CMakeLists.txt +0 -0
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/LICENSE.md +0 -0
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/MANIFEST.in +0 -0
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/PyQrackIsing/__init__.py +0 -0
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/PyQrackIsing/tfim_magnetization.py +0 -0
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/PyQrackIsing/tfim_square_magnetization.py +0 -0
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/PyQrackIsing.egg-info/not-zip-safe +0 -0
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/PyQrackIsing.egg-info/top_level.txt +0 -0
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/README.md +0 -0
- {pyqrackising-1.10.4 → pyqrackising-1.10.6}/setup.cfg +0 -0
|
@@ -73,53 +73,6 @@ static inline std::string int_to_bitstring(BigInteger integer, size_t length) {
|
|
|
73
73
|
return s;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
std::vector<double> maxcut_hamming_cdf(size_t n_qubits, std::vector<double> J_func, std::vector<double> degrees, int mult_log2) {
|
|
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;
|
|
83
|
-
const double delta_t = 1.0 / (n_steps << (mult_log2 >> 1));
|
|
84
|
-
const double h_mult = (1 << (mult_log2 >> 1)) / (n_steps * delta_t);
|
|
85
|
-
std::vector<double> hamming_prob(n_qubits - 1U, 0.0);
|
|
86
|
-
|
|
87
|
-
for (size_t step = 0; step < n_steps; ++step) {
|
|
88
|
-
double t = step * delta_t;
|
|
89
|
-
double tm1 = (step - 1) * delta_t;
|
|
90
|
-
for (size_t q = 0; q < n_qubits; ++q) {
|
|
91
|
-
const double& z = degrees[q];
|
|
92
|
-
const double J_eff = J_func[q];
|
|
93
|
-
const double h_t = h_mult * t;
|
|
94
|
-
auto bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, t, n_qubits);
|
|
95
|
-
if (step == 0) {
|
|
96
|
-
for (size_t i = 0U; i < hamming_prob.size(); ++i) {
|
|
97
|
-
hamming_prob[i] += bias[i + 1U];
|
|
98
|
-
}
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
auto last_bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, tm1, n_qubits);
|
|
102
|
-
for (size_t i = 0U; i < hamming_prob.size(); ++i) {
|
|
103
|
-
hamming_prob[i] += bias[i + 1U] - last_bias[i + 1U];
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
double tot_prob = std::accumulate(hamming_prob.begin(), hamming_prob.end(), 0.0);
|
|
109
|
-
for (auto& x : hamming_prob) {
|
|
110
|
-
x /= tot_prob;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
tot_prob = 0.0;
|
|
114
|
-
for (size_t i = 0U; i < hamming_prob.size(); ++i) {
|
|
115
|
-
tot_prob += hamming_prob[i];
|
|
116
|
-
hamming_prob[i] = tot_prob;
|
|
117
|
-
}
|
|
118
|
-
hamming_prob.back() = 1.0;
|
|
119
|
-
|
|
120
|
-
return hamming_prob;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
76
|
static inline double closeness_like_bits(BigInteger perm, size_t n_rows, size_t n_cols) {
|
|
124
77
|
std::string bits = int_to_bitstring(perm, n_rows * n_cols);
|
|
125
78
|
double like_count = 0.0;
|
|
@@ -290,15 +243,8 @@ double tfim_square_magnetization(double J, double h, double z, double theta, dou
|
|
|
290
243
|
|
|
291
244
|
PYBIND11_MODULE(tfim_sampler, m) {
|
|
292
245
|
m.doc() = "PyQrackIsing TFIM sample generator";
|
|
293
|
-
m.def("_generate_tfim_samples", &generate_tfim_samples_cpp,
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
m.def("_tfim_magnetization", &tfim_magnetization,
|
|
297
|
-
py::arg("J"), py::arg("h"), py::arg("z"), py::arg("theta"),
|
|
298
|
-
py::arg("t"), py::arg("n_qubits"));
|
|
299
|
-
m.def("_tfim_square_magnetization", &tfim_square_magnetization,
|
|
300
|
-
py::arg("J"), py::arg("h"), py::arg("z"), py::arg("theta"),
|
|
301
|
-
py::arg("t"), py::arg("n_qubits"));
|
|
302
|
-
m.def("_maxcut_hamming_cdf", &maxcut_hamming_cdf, "Adiabatic TFIM Hamming weight probability density function");
|
|
246
|
+
m.def("_generate_tfim_samples", &generate_tfim_samples_cpp, "Generate measurement samples from globally-uniform TFIM");
|
|
247
|
+
m.def("_tfim_magnetization", &tfim_magnetization, "Magnetization expectation value from globally-uniform TFIM");
|
|
248
|
+
m.def("_tfim_square_magnetization", &tfim_square_magnetization, "Square magnetization expectation value from globally-uniform TFIM");
|
|
303
249
|
}
|
|
304
250
|
|
|
@@ -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,238 @@
|
|
|
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 < 2:
|
|
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
|
+
tot_t = (n_steps - 1) * delta_t
|
|
61
|
+
h_mult = (1 << (mult_log2 >> 1)) / tot_t
|
|
62
|
+
n_bias = n_qubits - 1
|
|
63
|
+
hamming_prob = np.zeros(n_bias)
|
|
64
|
+
|
|
65
|
+
for q in prange(n_qubits):
|
|
66
|
+
z = degrees[q]
|
|
67
|
+
J_eff = J_func[q]
|
|
68
|
+
bias = probability_by_hamming_weight(J_eff, h_mult, z, 0.0, 0, n_qubits)
|
|
69
|
+
for i in range(n_bias):
|
|
70
|
+
hamming_prob[i] += bias[i + 1]
|
|
71
|
+
|
|
72
|
+
for step in range(1, n_steps):
|
|
73
|
+
t = step * delta_t
|
|
74
|
+
tm1 = (step - 1) * delta_t
|
|
75
|
+
for q in prange(n_qubits):
|
|
76
|
+
z = degrees[q]
|
|
77
|
+
J_eff = J_func[q]
|
|
78
|
+
h_t = h_mult * (tot_t - t)
|
|
79
|
+
bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, t, n_qubits)
|
|
80
|
+
last_bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, tm1, n_qubits)
|
|
81
|
+
for i in range(n_bias):
|
|
82
|
+
hamming_prob[i] += bias[i + 1] - last_bias[i + 1]
|
|
83
|
+
|
|
84
|
+
tot_prob = sum(hamming_prob)
|
|
85
|
+
for i in prange(n_bias):
|
|
86
|
+
hamming_prob[i] /= tot_prob
|
|
87
|
+
|
|
88
|
+
tot_prob = 0.0
|
|
89
|
+
for i in range(n_bias):
|
|
90
|
+
tot_prob += hamming_prob[i]
|
|
91
|
+
hamming_prob[i] = tot_prob
|
|
92
|
+
hamming_prob[-1] = 1.0
|
|
93
|
+
|
|
94
|
+
return hamming_prob
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
# Written by Elara (OpenAI custom GPT)
|
|
98
|
+
def local_repulsion_choice(nodes, adjacency, degrees, weights, n, m):
|
|
99
|
+
"""
|
|
100
|
+
Pick m nodes (bit positions) out of n with repulsion bias:
|
|
101
|
+
- High-degree nodes are already less likely
|
|
102
|
+
- After choosing a node, its neighbors' probabilities are further reduced
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
# Base weights: inverse degree
|
|
106
|
+
# degrees = np.array([len(adjacency.get(i, [])) for i in range(n)], dtype=np.float64)
|
|
107
|
+
# weights = 1.0 / (degrees + 1.0)
|
|
108
|
+
weights = weights.copy()
|
|
109
|
+
|
|
110
|
+
chosen = []
|
|
111
|
+
available = set(range(len(nodes)))
|
|
112
|
+
|
|
113
|
+
for _ in range(m):
|
|
114
|
+
if not available:
|
|
115
|
+
break
|
|
116
|
+
|
|
117
|
+
# Normalize weights over remaining nodes
|
|
118
|
+
sub_weights = np.array([weights[i] for i in available], dtype=np.float64)
|
|
119
|
+
sub_weights /= sub_weights.sum()
|
|
120
|
+
sub_nodes = list(available)
|
|
121
|
+
|
|
122
|
+
# Sample one node
|
|
123
|
+
idx = np.random.choice(len(sub_nodes), p=sub_weights)
|
|
124
|
+
node = sub_nodes[idx]
|
|
125
|
+
chosen.append(node)
|
|
126
|
+
|
|
127
|
+
# Remove node from available
|
|
128
|
+
available.remove(node)
|
|
129
|
+
|
|
130
|
+
# Repulsion: penalize neighbors
|
|
131
|
+
for nbr in adjacency.get(nodes[node], []):
|
|
132
|
+
idx = nodes.index(nbr)
|
|
133
|
+
if idx in available:
|
|
134
|
+
weights[idx] *= 0.5 # halve neighbor's weight (tunable!)
|
|
135
|
+
|
|
136
|
+
# Build integer mask
|
|
137
|
+
mask = 0
|
|
138
|
+
for pos in chosen:
|
|
139
|
+
mask |= 1 << pos
|
|
140
|
+
|
|
141
|
+
return mask
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def evaluate_cut_edges(samples, edge_keys, edge_values):
|
|
145
|
+
best_value = float("-inf")
|
|
146
|
+
best_solution = None
|
|
147
|
+
best_cut_edges = None
|
|
148
|
+
|
|
149
|
+
for state in samples:
|
|
150
|
+
cut_edges = []
|
|
151
|
+
cut_value = 0
|
|
152
|
+
for i in range(len(edge_values)):
|
|
153
|
+
k = i << 1
|
|
154
|
+
u, v = edge_keys[k], edge_keys[k + 1]
|
|
155
|
+
if ((state >> u) & 1) != ((state >> v) & 1):
|
|
156
|
+
cut_value += edge_values[i]
|
|
157
|
+
|
|
158
|
+
if cut_value > best_value:
|
|
159
|
+
best_value = cut_value
|
|
160
|
+
best_solution = state
|
|
161
|
+
|
|
162
|
+
return best_solution, float(best_value)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
# By Gemini (Google Search AI)
|
|
166
|
+
def int_to_bitstring(integer, length):
|
|
167
|
+
return (bin(integer)[2:].zfill(length))[::-1]
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def maxcut_tfim(
|
|
171
|
+
G,
|
|
172
|
+
quality=12,
|
|
173
|
+
shots=None,
|
|
174
|
+
):
|
|
175
|
+
# Number of qubits/nodes
|
|
176
|
+
nodes = list(G.nodes())
|
|
177
|
+
n_qubits = len(nodes)
|
|
178
|
+
|
|
179
|
+
if n_qubits == 0:
|
|
180
|
+
return "", 0, ([], [])
|
|
181
|
+
|
|
182
|
+
if shots is None:
|
|
183
|
+
# Number of measurement shots
|
|
184
|
+
shots = n_qubits << quality
|
|
185
|
+
|
|
186
|
+
J_eff = np.array(
|
|
187
|
+
[
|
|
188
|
+
-sum(edge_attributes.get("weight", 1.0) for _, edge_attributes in G.adj[n].items())
|
|
189
|
+
for n in nodes
|
|
190
|
+
],
|
|
191
|
+
dtype=np.float64,
|
|
192
|
+
)
|
|
193
|
+
degrees = np.array(
|
|
194
|
+
[
|
|
195
|
+
sum(abs(edge_attributes.get("weight", 1.0)) for _, edge_attributes in G.adj[n].items())
|
|
196
|
+
for n in nodes
|
|
197
|
+
],
|
|
198
|
+
dtype=np.float64,
|
|
199
|
+
)
|
|
200
|
+
# thresholds = tfim_sampler._maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
|
|
201
|
+
thresholds = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
|
|
202
|
+
G_dict = nx.to_dict_of_lists(G)
|
|
203
|
+
J_max = max(J_eff)
|
|
204
|
+
weights = 1.0 / (1.0 + (J_max - J_eff))
|
|
205
|
+
samples = []
|
|
206
|
+
for s in range(shots):
|
|
207
|
+
# First dimension: Hamming weight
|
|
208
|
+
mag_prob = np.random.random()
|
|
209
|
+
m = 0
|
|
210
|
+
while thresholds[m] < mag_prob:
|
|
211
|
+
m += 1
|
|
212
|
+
m += 1
|
|
213
|
+
# Second dimension: permutation within Hamming weight
|
|
214
|
+
samples.append(local_repulsion_choice(nodes, G_dict, degrees, weights, n_qubits, m))
|
|
215
|
+
|
|
216
|
+
# We only need unique instances
|
|
217
|
+
samples = list(set(samples))
|
|
218
|
+
|
|
219
|
+
edge_keys = []
|
|
220
|
+
edge_values = []
|
|
221
|
+
for u, v, data in G.edges(data=True):
|
|
222
|
+
edge_keys.append(nodes.index(u))
|
|
223
|
+
edge_keys.append(nodes.index(v))
|
|
224
|
+
edge_values.append(data.get("weight", 1.0))
|
|
225
|
+
|
|
226
|
+
best_solution, best_value = evaluate_cut_edges(samples, edge_keys, edge_values)
|
|
227
|
+
|
|
228
|
+
bit_string = int_to_bitstring(best_solution, n_qubits)
|
|
229
|
+
bit_list = list(bit_string)
|
|
230
|
+
l, r = [], []
|
|
231
|
+
for i in range(len(bit_list)):
|
|
232
|
+
b = bit_list[i] == "1"
|
|
233
|
+
if b:
|
|
234
|
+
r.append(nodes[i])
|
|
235
|
+
else:
|
|
236
|
+
l.append(nodes[i])
|
|
237
|
+
|
|
238
|
+
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
|
|
@@ -61,9 +61,9 @@ def spin_glass_solver(G, quality=1, best_guess=None):
|
|
|
61
61
|
n_qubits = len(nodes)
|
|
62
62
|
|
|
63
63
|
if n_qubits == 0:
|
|
64
|
-
return
|
|
64
|
+
return "", 0, ([], []), 0
|
|
65
65
|
|
|
66
|
-
bitstring =
|
|
66
|
+
bitstring = ""
|
|
67
67
|
if isinstance(best_guess, str):
|
|
68
68
|
bitstring = best_guess
|
|
69
69
|
elif isinstance(best_guess, int):
|
|
@@ -72,7 +72,7 @@ def spin_glass_solver(G, quality=1, best_guess=None):
|
|
|
72
72
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
73
73
|
else:
|
|
74
74
|
bitstring, _, _ = maxcut_tfim(G, quality=max(0, quality - 1))
|
|
75
|
-
best_theta = [
|
|
75
|
+
best_theta = [b == "1" for b in list(bitstring)]
|
|
76
76
|
|
|
77
77
|
edge_keys = []
|
|
78
78
|
edge_values = []
|
|
@@ -91,13 +91,15 @@ def spin_glass_solver(G, quality=1, best_guess=None):
|
|
|
91
91
|
|
|
92
92
|
theta = best_theta.copy()
|
|
93
93
|
|
|
94
|
-
combos = list(
|
|
94
|
+
combos = list(
|
|
95
|
+
item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
|
|
96
|
+
)
|
|
95
97
|
energies = bootstrap(theta, edge_keys, edge_values, k, combos)
|
|
96
98
|
|
|
97
99
|
energy = min(energies)
|
|
98
100
|
if energy < min_energy:
|
|
99
101
|
index_match = np.random.choice(np.where(energies == energy)[0])
|
|
100
|
-
indices = combos[(index_match * k):((index_match + 1) * k)]
|
|
102
|
+
indices = combos[(index_match * k) : ((index_match + 1) * k)]
|
|
101
103
|
min_energy = energy
|
|
102
104
|
for i in indices:
|
|
103
105
|
best_theta[i] = not best_theta[i]
|
|
@@ -105,16 +107,16 @@ def spin_glass_solver(G, quality=1, best_guess=None):
|
|
|
105
107
|
break
|
|
106
108
|
|
|
107
109
|
sample = 0
|
|
108
|
-
bitstring =
|
|
110
|
+
bitstring = ""
|
|
109
111
|
l, r = [], []
|
|
110
112
|
for i in range(len(best_theta)):
|
|
111
113
|
b = best_theta[i]
|
|
112
114
|
if b:
|
|
113
|
-
bitstring +=
|
|
115
|
+
bitstring += "1"
|
|
114
116
|
r.append(nodes[i])
|
|
115
117
|
sample |= 1 << i
|
|
116
118
|
else:
|
|
117
|
-
bitstring +=
|
|
119
|
+
bitstring += "0"
|
|
118
120
|
l.append(nodes[i])
|
|
119
121
|
|
|
120
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.6"
|
|
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.6',
|
|
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,135 +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 n_qubits == 0:
|
|
90
|
-
return '', 0, ([], [])
|
|
91
|
-
|
|
92
|
-
if shots is None:
|
|
93
|
-
# Number of measurement shots
|
|
94
|
-
shots = n_qubits << quality
|
|
95
|
-
|
|
96
|
-
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)
|
|
97
|
-
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)
|
|
98
|
-
thresholds = tfim_sampler._maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
|
|
99
|
-
G_dict = nx.to_dict_of_lists(G)
|
|
100
|
-
J_max = max(J_eff)
|
|
101
|
-
weights = 1.0 / (1.0 + (J_max - J_eff))
|
|
102
|
-
samples = []
|
|
103
|
-
for s in range(shots):
|
|
104
|
-
# First dimension: Hamming weight
|
|
105
|
-
mag_prob = np.random.random()
|
|
106
|
-
m = 0
|
|
107
|
-
while thresholds[m] < mag_prob:
|
|
108
|
-
m += 1
|
|
109
|
-
m += 1
|
|
110
|
-
# Second dimension: permutation within Hamming weight
|
|
111
|
-
samples.append(local_repulsion_choice(nodes, G_dict, degrees, weights, n_qubits, m))
|
|
112
|
-
|
|
113
|
-
# We only need unique instances
|
|
114
|
-
samples = list(set(samples))
|
|
115
|
-
|
|
116
|
-
edge_keys = []
|
|
117
|
-
edge_values = []
|
|
118
|
-
for u, v, data in G.edges(data=True):
|
|
119
|
-
edge_keys.append(nodes.index(u))
|
|
120
|
-
edge_keys.append(nodes.index(v))
|
|
121
|
-
edge_values.append(data.get("weight", 1.0))
|
|
122
|
-
|
|
123
|
-
best_solution, best_value = evaluate_cut_edges(samples, edge_keys, edge_values)
|
|
124
|
-
|
|
125
|
-
bit_string = int_to_bitstring(best_solution, n_qubits)
|
|
126
|
-
bit_list = list(bit_string)
|
|
127
|
-
l, r = [], []
|
|
128
|
-
for i in range(len(bit_list)):
|
|
129
|
-
b = (bit_list[i] == '1')
|
|
130
|
-
if b:
|
|
131
|
-
r.append(nodes[i])
|
|
132
|
-
else:
|
|
133
|
-
l.append(nodes[i])
|
|
134
|
-
|
|
135
|
-
return bit_string, 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
|