11from .maxcut_tfim import maxcut_tfim
2- from .maxcut_tfim_util import compute_cut , compute_energy , get_cut , gray_code_next , gray_mutation , heuristic_threshold , int_to_bitstring , make_G_m_buf , make_best_theta_buf , make_best_theta_buf_64 , opencl_context , setup_opencl
2+ from .maxcut_tfim_util import compute_cut , compute_cut_diff , compute_cut_diff_2 , compute_energy , compute_energy_diff , compute_energy_diff_2 , get_cut , gray_code_next , gray_mutation , heuristic_threshold , int_to_bitstring , make_G_m_buf , make_best_theta_buf , make_best_theta_buf_64 , opencl_context , setup_opencl
33import networkx as nx
44import numpy as np
55from numba import njit , prange
@@ -28,12 +28,12 @@ def run_single_bit_flips(best_theta, is_spin_glass, G_m):
2828 for i in prange (n ):
2929 state = best_theta .copy ()
3030 state [i ] = not state [i ]
31- energies [i ] = compute_energy ( state , G_m , n )
31+ energies [i ] = compute_energy_diff ( i , state , G_m , n )
3232 else :
3333 for i in prange (n ):
3434 state = best_theta .copy ()
3535 state [i ] = not state [i ]
36- energies [i ] = compute_cut ( state , G_m , n )
36+ energies [i ] = compute_cut_diff ( i , state , G_m , n )
3737
3838 best_index = np .argmax (energies )
3939 best_energy = energies [best_index ]
@@ -71,7 +71,7 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_m, thread_count):
7171 state [i ] = not state [i ]
7272 state [j ] = not state [j ]
7373
74- states [t ], energies [t ] = state , compute_energy ( state , G_m , n )
74+ states [t ], energies [t ] = state , compute_energy_diff_2 ( i , j , state , G_m , n )
7575
7676 s += thread_batch
7777 else :
@@ -93,7 +93,7 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_m, thread_count):
9393 state [i ] = not state [i ]
9494 state [j ] = not state [j ]
9595
96- states [t ], energies [t ] = state , compute_cut ( state , G_m , n )
96+ states [t ], energies [t ] = state , compute_cut_diff_2 ( i , j , state , G_m , n )
9797
9898 s += thread_batch
9999
@@ -184,7 +184,7 @@ def run_gray_optimization(best_theta, iterators, energies, gray_iterations, thre
184184 return best_energy , best_state
185185
186186
187- def run_bit_flips_opencl (is_double , n , kernel , best_energy , theta , theta_buf , G_m_buf , is_segmented , local_size , global_size , args_buf , local_energy_buf , local_index_buf , max_energy_host , max_index_host , max_energy_buf , max_index_buf ):
187+ def run_bit_flips_opencl (is_double , n , kernel , theta , theta_buf , G_m_buf , is_segmented , local_size , global_size , args_buf , local_energy_buf , local_index_buf , max_energy_host , max_index_host , max_energy_buf , max_index_buf ):
188188 queue = opencl_context .queue
189189
190190 # Set kernel args
@@ -227,9 +227,7 @@ def run_bit_flips_opencl(is_double, n, kernel, best_energy, theta, theta_buf, G_
227227
228228 if energy <= 0.0 :
229229 # No improvement: we can exit early
230- return best_energy , theta
231-
232- energy += best_energy
230+ return 0.0 , theta
233231
234232 # We need the best index
235233 queue .finish ()
@@ -425,23 +423,23 @@ def spin_glass_solver(
425423 # Single bit flips with O(n^2)
426424 if is_opencl :
427425 theta_buf = make_best_theta_buf (best_theta )
428- energy , state = run_bit_flips_opencl (False , n_qubits , single_bit_flips_kernel , max_energy , best_theta , theta_buf , G_m_buf , is_segmented , * opencl_args )
426+ energy , state = run_bit_flips_opencl (False , n_qubits , single_bit_flips_kernel , best_theta , theta_buf , G_m_buf , is_segmented , * opencl_args )
429427 else :
430428 energy , state = run_single_bit_flips (best_theta , is_spin_glass , G_m )
431- if energy > max_energy :
432- max_energy = energy
429+ if energy > 0.0 :
430+ max_energy + = energy
433431 best_theta = state
434432 improved = True
435433 continue
436434
437435 # Double bit flips with O(n^3)
438436 if is_opencl :
439437 # theta_buf has not changed
440- energy , state = run_bit_flips_opencl (True , n_qubits , double_bit_flips_kernel , max_energy , best_theta , theta_buf , G_m_buf , is_segmented , * opencl_args )
438+ energy , state = run_bit_flips_opencl (True , n_qubits , double_bit_flips_kernel , best_theta , theta_buf , G_m_buf , is_segmented , * opencl_args )
441439 else :
442440 energy , state = run_double_bit_flips (best_theta , is_spin_glass , G_m , thread_count )
443- if energy > max_energy :
444- max_energy = energy
441+ if energy > 0.0 :
442+ max_energy + = energy
445443 best_theta = state
446444 improved = True
447445 continue
@@ -472,23 +470,23 @@ def spin_glass_solver(
472470 # Single bit flips with O(n^2)
473471 if is_opencl :
474472 theta_buf = make_best_theta_buf (reheat_theta )
475- energy , state = run_bit_flips_opencl (False , n_qubits , single_bit_flips_kernel , max_energy , reheat_theta , theta_buf , G_m_buf , is_segmented , * opencl_args )
473+ energy , state = run_bit_flips_opencl (False , n_qubits , single_bit_flips_kernel , reheat_theta , theta_buf , G_m_buf , is_segmented , * opencl_args )
476474 else :
477475 energy , state = run_single_bit_flips (reheat_theta , is_spin_glass , G_m )
478- if energy > max_energy :
479- max_energy = energy
476+ if energy > 0.0 :
477+ max_energy + = energy
480478 best_theta = state
481479 improved = True
482480 continue
483481
484482 # Double bit flips with O(n^3)
485483 if is_opencl :
486484 # theta_buf has not changed
487- energy , state = run_bit_flips_opencl (True , n_qubits , double_bit_flips_kernel , max_energy , reheat_theta , theta_buf , G_m_buf , is_segmented , * opencl_args )
485+ energy , state = run_bit_flips_opencl (True , n_qubits , double_bit_flips_kernel , reheat_theta , theta_buf , G_m_buf , is_segmented , * opencl_args )
488486 else :
489487 energy , state = run_double_bit_flips (reheat_theta , is_spin_glass , G_m , thread_count )
490- if energy > max_energy :
491- max_energy = energy
488+ if energy > 0.0 :
489+ max_energy + = energy
492490 best_theta = state
493491 improved = True
494492
0 commit comments