PyQrackIsing 5.3.3__tar.gz → 5.4.1__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-5.3.3/pyqrackising.egg-info → pyqrackising-5.4.1}/PKG-INFO +1 -1
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/README.md +4 -4
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyproject.toml +1 -1
- pyqrackising-5.4.1/pyqrackising/kernels.cl +217 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising/maxcut_tfim.py +33 -21
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising/maxcut_tfim_sparse.py +36 -23
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising/maxcut_tfim_streaming.py +33 -21
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising/maxcut_tfim_util.py +12 -33
- pyqrackising-5.4.1/pyqrackising/spin_glass_solver.py +217 -0
- pyqrackising-5.4.1/pyqrackising/spin_glass_solver_sparse.py +236 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising/spin_glass_solver_streaming.py +26 -32
- pyqrackising-5.4.1/pyqrackising/spin_glass_solver_util.py +21 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising.egg-info/SOURCES.txt +1 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/setup.py +1 -1
- pyqrackising-5.3.3/pyqrackising/kernels.cl +0 -70
- pyqrackising-5.3.3/pyqrackising/spin_glass_solver.py +0 -144
- pyqrackising-5.3.3/pyqrackising/spin_glass_solver_sparse.py +0 -158
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/CMakeLists.txt +0 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/LICENSE.md +0 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/MANIFEST.in +0 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising/tsp.py +0 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-5.3.3 → pyqrackising-5.4.1}/setup.cfg +0 -0
|
@@ -50,10 +50,10 @@ from pyqrackising import maxcut_tfim
|
|
|
50
50
|
import networkx as nx
|
|
51
51
|
|
|
52
52
|
G = nx.petersen_graph()
|
|
53
|
-
best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=
|
|
53
|
+
best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=2)
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
We also provide `maxcut_tfim_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs). The (integer) `quality` setting is optional, with a default value of `
|
|
56
|
+
We also provide `maxcut_tfim_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs). The (integer) `quality` setting is optional, with a default value of `3`, 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.)
|
|
57
57
|
|
|
58
58
|
Based on a combination of the TFIM-inspired MAXCUT solver and another technique for finding ground-state energy in quantum chemistry that we call the _"binary Clifford eigensolver,"_ we also provide an (approximate) spin glass ground-state solver:
|
|
59
59
|
```py
|
|
@@ -73,10 +73,10 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
|
|
|
73
73
|
|
|
74
74
|
|
|
75
75
|
G = generate_spin_glass_graph(n_nodes=64, seed=42)
|
|
76
|
-
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=
|
|
76
|
+
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=2, best_guess=None)
|
|
77
77
|
# solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=6)[0])
|
|
78
78
|
```
|
|
79
|
-
We also provide `spin_glass_solver_sparse(G)`, for `scipy` **upper-triangular** CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `
|
|
79
|
+
We also provide `spin_glass_solver_sparse(G)`, for `scipy` **upper-triangular** CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `3`. `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)` with passed-through `quality` 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.
|
|
80
80
|
|
|
81
81
|
From the `spin_glass_solver()`, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
|
|
82
82
|
```py
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "pyqrackising"
|
|
15
|
-
version = "5.
|
|
15
|
+
version = "5.4.1"
|
|
16
16
|
requires-python = ">=3.8"
|
|
17
17
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
18
18
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// By Google Search AI
|
|
2
|
+
inline void AtomicAdd_g_f(volatile __global float *source, const float operand) {
|
|
3
|
+
union {
|
|
4
|
+
unsigned int intVal;
|
|
5
|
+
float floatVal;
|
|
6
|
+
} oldVal, newVal;
|
|
7
|
+
|
|
8
|
+
do {
|
|
9
|
+
oldVal.floatVal = *source;
|
|
10
|
+
newVal.floatVal = oldVal.floatVal + operand;
|
|
11
|
+
// Use atomic_cmpxchg to atomically update the value
|
|
12
|
+
} while (atomic_cmpxchg((volatile __global unsigned int*)source, oldVal.intVal, newVal.intVal) != oldVal.intVal);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// By Elara (custom OpenAI GPT)
|
|
16
|
+
inline float probability_by_hamming_weight(
|
|
17
|
+
int q, float J, float h, float z, float theta, float t, int n_qubits
|
|
18
|
+
) {
|
|
19
|
+
float ratio = fabs(h) / (z * J);
|
|
20
|
+
if (ratio > 1.0f) ratio = 1.0f;
|
|
21
|
+
float theta_c = asin(ratio);
|
|
22
|
+
|
|
23
|
+
float p = pow(2.0f, fabs(J / h) - 1.0f)
|
|
24
|
+
* (1.0f + sin(theta - theta_c) * cos(1.5f * M_PI_F * J * t + theta) / (1.0f + sqrt(t)))
|
|
25
|
+
- 0.5f;
|
|
26
|
+
|
|
27
|
+
if ((p * (n_qubits + 2.0f)) >= 1024.0f) return 0.0f;
|
|
28
|
+
|
|
29
|
+
float numerator = pow(2.0f, (n_qubits + 2.0f) * p) - 1.0f;
|
|
30
|
+
float denominator = pow(2.0f, p) - 1.0f;
|
|
31
|
+
float result = numerator * pow(2.0f, -((n_qubits + 1.0f) * p) - p * q) / denominator;
|
|
32
|
+
|
|
33
|
+
if (isnan(result) || isinf(result)) return 0.0f;
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
__kernel void maxcut_hamming_cdf(
|
|
38
|
+
int n_qubits,
|
|
39
|
+
__constant int* degrees,
|
|
40
|
+
__constant float* args,
|
|
41
|
+
__constant float* J_func,
|
|
42
|
+
__constant float* theta,
|
|
43
|
+
__global float* hamming_prob
|
|
44
|
+
) {
|
|
45
|
+
const float delta_t = args[0U];
|
|
46
|
+
const float tot_t = args[1U];
|
|
47
|
+
const float h_mult = args[2U];
|
|
48
|
+
const int step = get_group_id(0) / n_qubits;
|
|
49
|
+
const int qi = get_group_id(0) % n_qubits;
|
|
50
|
+
const float J_eff = J_func[qi];
|
|
51
|
+
const float z = degrees[qi];
|
|
52
|
+
if (fabs(z * J_eff) <= (pow(2.0f, -52))) return;
|
|
53
|
+
|
|
54
|
+
const float theta_eff = theta[qi];
|
|
55
|
+
const float t = step * delta_t;
|
|
56
|
+
const float tm1 = (step - 1) * delta_t;
|
|
57
|
+
const float h_t = h_mult * (tot_t - t);
|
|
58
|
+
|
|
59
|
+
const int n_threads = get_local_size(0);
|
|
60
|
+
|
|
61
|
+
for (int qo = get_local_id(0); qo < n_qubits; qo += n_threads) {
|
|
62
|
+
int _qo = (J_eff > 0.0f) ? (n_qubits - (1 + qo)) : qo;
|
|
63
|
+
float diff = probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, t, n_qubits);
|
|
64
|
+
diff -= probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, tm1, n_qubits);
|
|
65
|
+
AtomicAdd_g_f(&(hamming_prob[_qo]), diff);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
float bootstrap_worker(__constant char* theta, __global double* G_m, __constant int* indices, const int k, const int n) {
|
|
70
|
+
double energy = 0.0;
|
|
71
|
+
const size_t n_st = (size_t)n;
|
|
72
|
+
for (int u = 0; u < n; ++u) {
|
|
73
|
+
const size_t u_offset = u * n_st;
|
|
74
|
+
bool u_bit = theta[u];
|
|
75
|
+
for (int x = 0; x < k; ++x) {
|
|
76
|
+
if (indices[x] == u) {
|
|
77
|
+
u_bit = !u_bit;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
for (int v = u + 1; v < n; ++v) {
|
|
82
|
+
const double val = G_m[u_offset + v];
|
|
83
|
+
bool v_bit = theta[v];
|
|
84
|
+
for (int x = 0; x < k; ++x) {
|
|
85
|
+
if (indices[x] == v) {
|
|
86
|
+
v_bit = !v_bit;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
energy += (u_bit == v_bit) ? val : -val;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return (float)energy;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
__kernel void bootstrap(
|
|
98
|
+
__global double* G_m,
|
|
99
|
+
__constant char* best_theta,
|
|
100
|
+
__constant int* indices_array,
|
|
101
|
+
__constant int* args, // args[0] = n, args[1] = k
|
|
102
|
+
__global float* min_energy_ptr, // output: per-group min energy
|
|
103
|
+
__global int* min_index_ptr, // output: per-group best index (i)
|
|
104
|
+
__local float* loc_energy, // local memory buffer
|
|
105
|
+
__local int* loc_index // local memory buffer
|
|
106
|
+
) {
|
|
107
|
+
const int n = args[0];
|
|
108
|
+
const int k = args[1];
|
|
109
|
+
const int combo_count = args[2];
|
|
110
|
+
const int i = get_global_id(0);
|
|
111
|
+
|
|
112
|
+
float energy = INFINITY;
|
|
113
|
+
|
|
114
|
+
if (i < combo_count) {
|
|
115
|
+
const int j = i * k;
|
|
116
|
+
// Compute energy for this combination
|
|
117
|
+
energy = bootstrap_worker(best_theta, G_m, indices_array + j, k, n);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const int lt_id = get_local_id(0);
|
|
121
|
+
const int lt_size = get_local_size(0);
|
|
122
|
+
|
|
123
|
+
// Initialize local memory
|
|
124
|
+
loc_energy[lt_id] = energy;
|
|
125
|
+
loc_index[lt_id] = i;
|
|
126
|
+
|
|
127
|
+
// Reduce within workgroup
|
|
128
|
+
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
129
|
+
barrier(CLK_LOCAL_MEM_FENCE);
|
|
130
|
+
if ((lt_id < offset) && (loc_energy[lt_id + offset] < loc_energy[lt_id])) {
|
|
131
|
+
loc_energy[lt_id] = loc_energy[lt_id + offset];
|
|
132
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Write out per-group result
|
|
137
|
+
if (lt_id == 0) {
|
|
138
|
+
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
139
|
+
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
float bootstrap_worker_sparse(__constant char* theta, __global double* G_data, __global ulong* G_rows, __global ulong* G_cols, __constant int* indices, const int k, const int n) {
|
|
144
|
+
double energy = 0.0;
|
|
145
|
+
for (int u = 0; u < n; ++u) {
|
|
146
|
+
bool u_bit = theta[u];
|
|
147
|
+
for (int x = 0; x < k; ++x) {
|
|
148
|
+
if (indices[x] == u) {
|
|
149
|
+
u_bit = !u_bit;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const size_t mCol = G_rows[u + 1];
|
|
154
|
+
for (int col = G_rows[u]; col < mCol; ++col) {
|
|
155
|
+
const int v = G_cols[col];
|
|
156
|
+
const double val = G_data[col];
|
|
157
|
+
bool v_bit = theta[v];
|
|
158
|
+
for (int x = 0; x < k; ++x) {
|
|
159
|
+
if (indices[x] == v) {
|
|
160
|
+
v_bit = !v_bit;
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
energy += (u_bit == v_bit) ? val : -val;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return (float)energy;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
__kernel void bootstrap_sparse(
|
|
172
|
+
__global double* G_data,
|
|
173
|
+
__global ulong* G_rows,
|
|
174
|
+
__global ulong* G_cols,
|
|
175
|
+
__constant char* best_theta,
|
|
176
|
+
__constant int* indices_array,
|
|
177
|
+
__constant int* args, // args[0] = n, args[1] = k
|
|
178
|
+
__global float* min_energy_ptr, // output: per-group min energy
|
|
179
|
+
__global int* min_index_ptr, // output: per-group best index (i)
|
|
180
|
+
__local float* loc_energy, // local memory buffer
|
|
181
|
+
__local int* loc_index // local memory buffer
|
|
182
|
+
) {
|
|
183
|
+
const int n = args[0];
|
|
184
|
+
const int k = args[1];
|
|
185
|
+
const int combo_count = args[2];
|
|
186
|
+
const int i = get_global_id(0);
|
|
187
|
+
|
|
188
|
+
float energy = INFINITY;
|
|
189
|
+
|
|
190
|
+
if (i < combo_count) {
|
|
191
|
+
const int j = i * k;
|
|
192
|
+
// Compute energy for this combination
|
|
193
|
+
energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const int lt_id = get_local_id(0);
|
|
197
|
+
const int lt_size = get_local_size(0);
|
|
198
|
+
|
|
199
|
+
// Initialize local memory
|
|
200
|
+
loc_energy[lt_id] = energy;
|
|
201
|
+
loc_index[lt_id] = i;
|
|
202
|
+
|
|
203
|
+
// Reduce within workgroup
|
|
204
|
+
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
205
|
+
barrier(CLK_LOCAL_MEM_FENCE);
|
|
206
|
+
if ((lt_id < offset) && (loc_energy[lt_id + offset] < loc_energy[lt_id])) {
|
|
207
|
+
loc_energy[lt_id] = loc_energy[lt_id + offset];
|
|
208
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Write out per-group result
|
|
213
|
+
if (lt_id == 0) {
|
|
214
|
+
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
215
|
+
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
216
|
+
}
|
|
217
|
+
}
|
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
|
4
4
|
import os
|
|
5
5
|
from numba import njit, prange
|
|
6
6
|
|
|
7
|
-
from .maxcut_tfim_util import get_cut, init_theta, init_thresholds,
|
|
7
|
+
from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -26,45 +26,46 @@ def local_repulsion_choice(G_m, max_weight, weights, n, m):
|
|
|
26
26
|
|
|
27
27
|
weights = weights.copy()
|
|
28
28
|
chosen = np.zeros(m, dtype=np.int32) # store chosen indices
|
|
29
|
-
|
|
30
|
-
mask = np.zeros(n, dtype=np.bool_)
|
|
29
|
+
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
31
30
|
chosen_count = 0
|
|
32
31
|
|
|
33
32
|
for _ in range(m):
|
|
34
33
|
# Count available
|
|
35
34
|
total_w = 0.0
|
|
36
35
|
for i in range(n):
|
|
37
|
-
if
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
break
|
|
36
|
+
if used[i]:
|
|
37
|
+
continue
|
|
38
|
+
total_w += weights[i]
|
|
41
39
|
|
|
42
40
|
# Normalize & sample
|
|
43
41
|
r = np.random.rand()
|
|
44
42
|
cum = 0.0
|
|
45
43
|
node = -1
|
|
46
44
|
for i in range(n):
|
|
47
|
-
if
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
45
|
+
if used[i]:
|
|
46
|
+
continue
|
|
47
|
+
cum += weights[i]
|
|
48
|
+
if (total_w * r) < cum:
|
|
49
|
+
node = i
|
|
50
|
+
break
|
|
52
51
|
|
|
53
52
|
if node == -1:
|
|
54
|
-
|
|
53
|
+
node = 0
|
|
54
|
+
while used[node]:
|
|
55
|
+
node += 1
|
|
55
56
|
|
|
56
57
|
# Select node
|
|
57
58
|
chosen[chosen_count] = node
|
|
58
59
|
chosen_count += 1
|
|
59
|
-
|
|
60
|
-
mask[node] = True
|
|
60
|
+
used[node] = True
|
|
61
61
|
|
|
62
62
|
# Repulsion: penalize neighbors
|
|
63
63
|
for nbr in range(n):
|
|
64
|
-
if
|
|
65
|
-
|
|
64
|
+
if used[nbr]:
|
|
65
|
+
continue
|
|
66
|
+
weights[nbr] *= max(2e-7, 1 - G_m[node, nbr] / max_weight)
|
|
66
67
|
|
|
67
|
-
return
|
|
68
|
+
return used
|
|
68
69
|
|
|
69
70
|
|
|
70
71
|
@njit
|
|
@@ -81,7 +82,7 @@ def compute_energy(sample, G_m, n_qubits):
|
|
|
81
82
|
def sample_for_solution(G_m, shots, thresholds, J_eff):
|
|
82
83
|
n = len(G_m)
|
|
83
84
|
max_weight = G_m.max()
|
|
84
|
-
weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
|
|
85
|
+
weights = (1.0 / (1.0 + (2 ** -52) - J_eff)).astype(np.float64)
|
|
85
86
|
|
|
86
87
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
87
88
|
energies = np.empty(shots, dtype=np.float32)
|
|
@@ -177,10 +178,21 @@ def maxcut_tfim(
|
|
|
177
178
|
G_m = G
|
|
178
179
|
|
|
179
180
|
if n_qubits < 3:
|
|
180
|
-
|
|
181
|
+
if n_qubits == 0:
|
|
182
|
+
return "", 0, ([], [])
|
|
183
|
+
|
|
184
|
+
if n_qubits == 1:
|
|
185
|
+
return "0", 0, (nodes, [])
|
|
186
|
+
|
|
187
|
+
if n_qubits == 2:
|
|
188
|
+
weight = G_m[0, 1]
|
|
189
|
+
if weight < 0.0:
|
|
190
|
+
return "00", 0, (nodes, [])
|
|
191
|
+
|
|
192
|
+
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
181
193
|
|
|
182
194
|
if quality is None:
|
|
183
|
-
quality =
|
|
195
|
+
quality = 3
|
|
184
196
|
|
|
185
197
|
if shots is None:
|
|
186
198
|
# Number of measurement shots
|
|
@@ -3,8 +3,9 @@ import networkx as nx
|
|
|
3
3
|
import numpy as np
|
|
4
4
|
import os
|
|
5
5
|
from numba import njit, prange
|
|
6
|
+
from scipy.sparse import lil_matrix, csr_matrix
|
|
6
7
|
|
|
7
|
-
from .maxcut_tfim_util import get_cut, init_theta, init_thresholds,
|
|
8
|
+
from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
9
|
|
|
9
10
|
IS_OPENCL_AVAILABLE = True
|
|
10
11
|
try:
|
|
@@ -45,55 +46,56 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, m):
|
|
|
45
46
|
|
|
46
47
|
weights = weights.copy()
|
|
47
48
|
chosen = np.zeros(m, dtype=np.int32) # store chosen indices
|
|
48
|
-
|
|
49
|
-
mask = np.zeros(n, dtype=np.bool_)
|
|
49
|
+
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
50
50
|
chosen_count = 0
|
|
51
51
|
|
|
52
52
|
for _ in range(m):
|
|
53
53
|
# Count available
|
|
54
54
|
total_w = 0.0
|
|
55
55
|
for i in range(n):
|
|
56
|
-
if
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
break
|
|
56
|
+
if used[i]:
|
|
57
|
+
continue
|
|
58
|
+
total_w += weights[i]
|
|
60
59
|
|
|
61
60
|
# Normalize & sample
|
|
62
61
|
r = np.random.rand()
|
|
63
62
|
cum = 0.0
|
|
64
63
|
node = -1
|
|
65
64
|
for i in range(n):
|
|
66
|
-
if
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
65
|
+
if used[i]:
|
|
66
|
+
continue
|
|
67
|
+
cum += weights[i]
|
|
68
|
+
if (total_w * r) < cum:
|
|
69
|
+
node = i
|
|
70
|
+
break
|
|
71
71
|
|
|
72
72
|
if node == -1:
|
|
73
|
-
|
|
73
|
+
node = 0
|
|
74
|
+
while used[node]:
|
|
75
|
+
node += 1
|
|
74
76
|
|
|
75
77
|
# Select node
|
|
76
78
|
chosen[chosen_count] = node
|
|
77
79
|
chosen_count += 1
|
|
78
|
-
|
|
79
|
-
mask[node] = True
|
|
80
|
+
used[node] = True
|
|
80
81
|
|
|
81
82
|
# Repulsion: penalize neighbors
|
|
82
83
|
for j in range(G_rows[node], G_rows[node + 1]):
|
|
83
84
|
nbr = G_cols[j]
|
|
84
|
-
if
|
|
85
|
-
|
|
85
|
+
if used[nbr]:
|
|
86
|
+
continue
|
|
87
|
+
weights[nbr] *= max(2e-7, 1 - G_data[j] / max_weight)
|
|
86
88
|
|
|
87
89
|
for nbr in range(node):
|
|
88
|
-
if
|
|
90
|
+
if used[nbr]:
|
|
89
91
|
continue
|
|
90
92
|
start = G_rows[nbr]
|
|
91
93
|
end = G_rows[nbr + 1]
|
|
92
94
|
j = binary_search(G_cols[start:end], node) + start
|
|
93
95
|
if j < end:
|
|
94
|
-
weights[nbr] *=
|
|
96
|
+
weights[nbr] *= max(2e-7, 1 - G_data[j] / max_weight)
|
|
95
97
|
|
|
96
|
-
return
|
|
98
|
+
return used
|
|
97
99
|
|
|
98
100
|
|
|
99
101
|
@njit
|
|
@@ -112,7 +114,7 @@ def compute_energy(sample, G_data, G_rows, G_cols):
|
|
|
112
114
|
def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds, J_eff):
|
|
113
115
|
n = G_rows.shape[0] - 1
|
|
114
116
|
max_weight = G_data.max()
|
|
115
|
-
weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
|
|
117
|
+
weights = (1.0 / (1.0 + (2 ** -52) - J_eff)).astype(np.float64)
|
|
116
118
|
|
|
117
119
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
118
120
|
energies = np.empty(shots, dtype=np.float32)
|
|
@@ -235,10 +237,21 @@ def maxcut_tfim_sparse(
|
|
|
235
237
|
G_m = G
|
|
236
238
|
|
|
237
239
|
if n_qubits < 3:
|
|
238
|
-
|
|
240
|
+
if n_qubits == 0:
|
|
241
|
+
return "", 0, ([], [])
|
|
242
|
+
|
|
243
|
+
if n_qubits == 1:
|
|
244
|
+
return "0", 0, (nodes, [])
|
|
245
|
+
|
|
246
|
+
if n_qubits == 2:
|
|
247
|
+
weight = G_m[0, 1]
|
|
248
|
+
if weight < 0.0:
|
|
249
|
+
return "00", 0, (nodes, [])
|
|
250
|
+
|
|
251
|
+
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
239
252
|
|
|
240
253
|
if quality is None:
|
|
241
|
-
quality =
|
|
254
|
+
quality = 3
|
|
242
255
|
|
|
243
256
|
if shots is None:
|
|
244
257
|
# Number of measurement shots
|
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
|
4
4
|
import os
|
|
5
5
|
from numba import njit, prange
|
|
6
6
|
|
|
7
|
-
from .maxcut_tfim_util import get_cut, init_theta, init_thresholds,
|
|
7
|
+
from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -26,45 +26,46 @@ def local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights
|
|
|
26
26
|
|
|
27
27
|
weights = weights.copy()
|
|
28
28
|
chosen = np.zeros(m, dtype=np.int32) # store chosen indices
|
|
29
|
-
|
|
30
|
-
mask = np.zeros(n, dtype=np.bool_)
|
|
29
|
+
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
31
30
|
chosen_count = 0
|
|
32
31
|
|
|
33
32
|
for _ in range(m):
|
|
34
33
|
# Count available
|
|
35
34
|
total_w = 0.0
|
|
36
35
|
for i in range(n):
|
|
37
|
-
if
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
break
|
|
36
|
+
if used[i]:
|
|
37
|
+
continue
|
|
38
|
+
total_w += weights[i]
|
|
41
39
|
|
|
42
40
|
# Normalize & sample
|
|
43
41
|
r = np.random.rand()
|
|
44
42
|
cum = 0.0
|
|
45
43
|
node = -1
|
|
46
44
|
for i in range(n):
|
|
47
|
-
if
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
45
|
+
if used[i]:
|
|
46
|
+
continue
|
|
47
|
+
cum += weights[i]
|
|
48
|
+
if (total_w * r) < cum:
|
|
49
|
+
node = i
|
|
50
|
+
break
|
|
52
51
|
|
|
53
52
|
if node == -1:
|
|
54
|
-
|
|
53
|
+
node = 0
|
|
54
|
+
while used[node]:
|
|
55
|
+
node += 1
|
|
55
56
|
|
|
56
57
|
# Select node
|
|
57
58
|
chosen[chosen_count] = node
|
|
58
59
|
chosen_count += 1
|
|
59
|
-
|
|
60
|
-
mask[node] = True
|
|
60
|
+
used[node] = True
|
|
61
61
|
|
|
62
62
|
# Repulsion: penalize neighbors
|
|
63
63
|
for nbr in range(n):
|
|
64
|
-
if
|
|
65
|
-
|
|
64
|
+
if used[nbr]:
|
|
65
|
+
continue
|
|
66
|
+
weights[nbr] *= max(2e-7, 1 - G_func((nodes[node], nodes[nbr]), G_func_args_tuple) / max_weight)
|
|
66
67
|
|
|
67
|
-
return
|
|
68
|
+
return used
|
|
68
69
|
|
|
69
70
|
|
|
70
71
|
@njit
|
|
@@ -79,7 +80,7 @@ def compute_energy(sample, G_func, G_func_args_tuple, nodes, n_qubits):
|
|
|
79
80
|
|
|
80
81
|
@njit(parallel=True)
|
|
81
82
|
def sample_for_solution(G_func, G_func_args_tuple, nodes, max_weight, shots, thresholds, degrees_sum, J_eff, n):
|
|
82
|
-
weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
|
|
83
|
+
weights = (1.0 / (1.0 + (2 ** -52) - J_eff)).astype(np.float64)
|
|
83
84
|
|
|
84
85
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
85
86
|
energies = np.empty(shots, dtype=np.float32)
|
|
@@ -175,10 +176,21 @@ def maxcut_tfim_streaming(
|
|
|
175
176
|
n_qubits = len(nodes)
|
|
176
177
|
|
|
177
178
|
if n_qubits < 3:
|
|
178
|
-
|
|
179
|
+
if n_qubits == 0:
|
|
180
|
+
return "", 0, ([], [])
|
|
181
|
+
|
|
182
|
+
if n_qubits == 1:
|
|
183
|
+
return "0", 0, (nodes, [])
|
|
184
|
+
|
|
185
|
+
if n_qubits == 2:
|
|
186
|
+
weight = G_func((nodes[0], nodes[1]), G_func_args_tuple)
|
|
187
|
+
if weight < 0.0:
|
|
188
|
+
return "00", 0, (nodes, [])
|
|
189
|
+
|
|
190
|
+
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
179
191
|
|
|
180
192
|
if quality is None:
|
|
181
|
-
quality =
|
|
193
|
+
quality = 3
|
|
182
194
|
|
|
183
195
|
if shots is None:
|
|
184
196
|
# Number of measurement shots
|