Skip to content

Commit da9fc4e

Browse files
Reuse energy value
1 parent c645864 commit da9fc4e

3 files changed

Lines changed: 12 additions & 9 deletions

File tree

pyqrackising/solve_maxcut_exact.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ def solve_maxcut_exact(
266266
return "01", weight, ([nodes[0]], [nodes[1]]), -weight, True
267267

268268
bitstring = ""
269-
cut_value = 0.0
269+
cut_value = None
270+
energy_value = None
270271
if isinstance(best_guess, str):
271272
bitstring = best_guess
272273
elif isinstance(best_guess, int):
@@ -287,15 +288,15 @@ def solve_maxcut_exact(
287288
if gray_seed_multiple is not None: kwargs["gray_seed_multiple"] = gray_seed_multiple
288289
kwargs["is_maxcut_gpu"] = is_maxcut_gpu
289290
t0 = time.monotonic()
290-
bitstring, cut_value, _, _ = spin_glass_solver(
291+
bitstring, cut_value, _, energy_value = spin_glass_solver(
291292
G_m, is_spin_glass=is_spin_glass, **kwargs
292293
)
293294
if verbose:
294295
print(f"Heuristic value: {cut_value:.6f} ({time.monotonic()-t0:.3f}s)")
295296

296297
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
297298
if is_spin_glass:
298-
max_energy = compute_energy(best_theta, G_m, n_qubits)
299+
max_energy = compute_energy(best_theta, G_m, n_qubits) if energy_value is None else energy_value
299300
elif cut_value is None:
300301
max_energy = compute_cut(best_theta, G_m, n_qubits)
301302
else:

pyqrackising/solve_maxcut_exact_sparse.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ def solve_maxcut_exact_sparse(
267267
G_cols = np.asarray(G_m.indices, dtype=np.int64)
268268

269269
bitstring = ""
270-
cut_value = 0.0
270+
cut_value = None
271+
energy_value = None
271272
if isinstance(best_guess, str):
272273
bitstring = best_guess
273274
elif isinstance(best_guess, int):
@@ -288,15 +289,15 @@ def solve_maxcut_exact_sparse(
288289
if gray_seed_multiple is not None: kwargs["gray_seed_multiple"] = gray_seed_multiple
289290
kwargs["is_maxcut_gpu"] = is_maxcut_gpu
290291
t0 = time.monotonic()
291-
bitstring, cut_value, _, _ = spin_glass_solver_sparse(
292+
bitstring, cut_value, _, energy_value = spin_glass_solver_sparse(
292293
G_m, is_spin_glass=is_spin_glass, **kwargs
293294
)
294295
if verbose:
295296
print(f"Heuristic value: {cut_value:.6f} ({time.monotonic()-t0:.3f}s)")
296297

297298
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
298299
if is_spin_glass:
299-
max_energy = compute_energy_sparse(best_theta, G_m.data, G_m.indptr, G_m.indices, n_qubits)
300+
max_energy = compute_energy_sparse(best_theta, G_m.data, G_m.indptr, G_m.indices, n_qubits) if energy_value is None else energy_value
300301
elif cut_value is None:
301302
max_energy = compute_cut_sparse(best_theta, G_m.data, G_m.indptr, G_m.indices, n_qubits)
302303
else:

pyqrackising/solve_maxcut_exact_streaming.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ def solve_maxcut_exact_streaming(
238238
return "01", weight, ([nodes[0]], [nodes[1]]), -weight, True
239239

240240
bitstring = ""
241-
cut_value = 0.0
241+
cut_value = None
242+
energy_value = None
242243
if isinstance(best_guess, str):
243244
bitstring = best_guess
244245
elif isinstance(best_guess, int):
@@ -258,15 +259,15 @@ def solve_maxcut_exact_streaming(
258259
if gray_iterations is not None: kwargs["gray_iterations"] = gray_iterations
259260
if gray_seed_multiple is not None: kwargs["gray_seed_multiple"] = gray_seed_multiple
260261
t0 = time.monotonic()
261-
bitstring, cut_value, _, _ = spin_glass_solver_streaming(
262+
bitstring, cut_value, _, energy_value = spin_glass_solver_streaming(
262263
G_func, nodes, is_spin_glass=is_spin_glass, **kwargs
263264
)
264265
if verbose:
265266
print(f"Heuristic value: {cut_value:.6f} ({time.monotonic()-t0:.3f}s)")
266267

267268
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
268269
if is_spin_glass:
269-
max_energy = compute_energy_streaming(best_theta, G_func, nodes, n_qubits)
270+
max_energy = compute_energy_streaming(best_theta, G_func, nodes, n_qubits) if energy_value is None else energy_value
270271
elif cut_value is None:
271272
max_energy = compute_cut_streaming(best_theta, G_func, nodes, n_qubits)
272273
else:

0 commit comments

Comments
 (0)