PyQrackIsing 9.8.0__tar.gz → 9.9.0__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.
Files changed (28) hide show
  1. {pyqrackising-9.8.0/pyqrackising.egg-info → pyqrackising-9.9.0}/PKG-INFO +1 -1
  2. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/README.md +1 -1
  3. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyproject.toml +1 -1
  4. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/kernels.cl +89 -165
  5. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/maxcut_tfim.py +42 -39
  6. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/maxcut_tfim_sparse.py +5 -5
  7. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/maxcut_tfim_streaming.py +37 -34
  8. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/maxcut_tfim_util.py +54 -98
  9. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/spin_glass_solver.py +71 -120
  10. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/spin_glass_solver_sparse.py +7 -5
  11. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/spin_glass_solver_streaming.py +69 -113
  12. {pyqrackising-9.8.0 → pyqrackising-9.9.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  13. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/setup.py +1 -1
  14. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/LICENSE.md +0 -0
  15. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/MANIFEST.in +0 -0
  16. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/__init__.py +0 -0
  17. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  18. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/generate_tfim_samples.py +0 -0
  19. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/otoc.py +0 -0
  20. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/tfim_magnetization.py +0 -0
  21. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  22. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/tsp.py +0 -0
  23. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising/tsp_maxcut.py +0 -0
  24. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
  25. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-9.8.0 → pyqrackising-9.9.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.8.0
3
+ Version: 9.9.0
4
4
  Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -3,7 +3,7 @@ Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising
3
3
 
4
4
  (It's "the **Ising** on top.")
5
5
 
6
- [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.17625519.svg)](https://doi.org/10.5281/zenodo.17625519) [![PyPI Downloads](https://static.pepy.tech/badge/pyqrackising)](https://pepy.tech/projects/pyqrackising)
6
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.17635216.svg)](https://doi.org/10.5281/zenodo.17635216) [![PyPI Downloads](https://static.pepy.tech/badge/pyqrackising)](https://pepy.tech/projects/pyqrackising)
7
7
 
8
8
  ## Copyright and license
9
9
  (c) Daniel Strano and the Qrack contributors 2025. All rights reserved.
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
10
10
 
11
11
  [project]
12
12
  name = "pyqrackising"
13
- version = "9.8.0"
13
+ version = "9.9.0"
14
14
  requires-python = ">=3.8"
15
15
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
16
16
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -97,25 +97,19 @@ inline bool get_const_bit(__constant uint* theta, const size_t u) {
97
97
  return (theta[u >> 5U] >> (u & 31U)) & 1U;
98
98
  }
99
99
 
100
- real1 single_bit_flip_worker(__constant uint* theta, __global const real1* G_m, const int n, const bool is_spin_glass, const int k) {
100
+ real1 single_bit_flip_worker(__constant uint* theta, __global const real1* G_m, const int n, const int k) {
101
101
  const size_t k_offset = k * (size_t)n;
102
102
  const bool k_bit = !get_const_bit(theta, k);
103
103
  real1 energy = ZERO_R1;
104
104
  for (int v = 0; v < k; ++v) {
105
105
  const bool v_bit = get_const_bit(theta, v);
106
106
  real1 val = G_m[k_offset + v];
107
- if (is_spin_glass) {
108
- val *= 2;
109
- }
110
- energy += (k_bit != v_bit) ? val : -val;
107
+ energy += (k_bit == v_bit) ? -val : val;
111
108
  }
112
109
  for (int v = k + 1; v < n; ++v) {
113
110
  const bool v_bit = get_const_bit(theta, v);
114
111
  real1 val = G_m[k_offset + v];
115
- if (is_spin_glass) {
116
- val *= 2;
117
- }
118
- energy += (k_bit != v_bit) ? val : -val;
112
+ energy += (k_bit == v_bit) ? -val : val;
119
113
  }
120
114
 
121
115
  return energy;
@@ -140,7 +134,10 @@ __kernel void single_bit_flips(
140
134
  int best_i = i;
141
135
 
142
136
  for (; i < n; i += max_i) {
143
- const real1 energy = single_bit_flip_worker(best_theta, G_m, n, is_spin_glass, i);
137
+ real1 energy = single_bit_flip_worker(best_theta, G_m, n, i);
138
+ if (is_spin_glass) {
139
+ energy *= 2.0;
140
+ }
144
141
  if (energy > best_energy) {
145
142
  best_energy = energy;
146
143
  best_i = i;
@@ -150,7 +147,7 @@ __kernel void single_bit_flips(
150
147
  reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
151
148
  }
152
149
 
153
- real1 double_bit_flip_worker(__constant uint* theta, __global const real1* G_m, const int n, const bool is_spin_glass, int k, int l) {
150
+ real1 double_bit_flip_worker(__constant uint* theta, __global const real1* G_m, const int n, int k, int l) {
154
151
  if (l < k) {
155
152
  int t = k;
156
153
  k = l;
@@ -164,41 +161,23 @@ real1 double_bit_flip_worker(__constant uint* theta, __global const real1* G_m,
164
161
  for (int v = 0; v < k; ++v) {
165
162
  const bool v_bit = get_const_bit(theta, v);
166
163
  real1 val = G_m[k_offset + v];
167
- if (is_spin_glass) {
168
- val *= 2;
169
- }
170
- energy += (k_bit != v_bit) ? val : -val;
164
+ energy += (k_bit == v_bit) ? -val : val;
171
165
  val = G_m[l_offset + v];
172
- if (is_spin_glass) {
173
- val *= 2;
174
- }
175
- energy += (l_bit != v_bit) ? val : -val;
166
+ energy += (l_bit == v_bit) ? -val : val;
176
167
  }
177
168
  for (int v = k + 1; v < l; ++v) {
178
169
  const bool v_bit = get_const_bit(theta, v);
179
170
  real1 val = G_m[k_offset + v];
180
- if (is_spin_glass) {
181
- val *= 2;
182
- }
183
- energy += (k_bit != v_bit) ? val : -val;
171
+ energy += (k_bit == v_bit) ? -val : val;
184
172
  val = G_m[l_offset + v];
185
- if (is_spin_glass) {
186
- val *= 2;
187
- }
188
- energy += (l_bit != v_bit) ? val : -val;
173
+ energy += (l_bit == v_bit) ? -val : val;
189
174
  }
190
175
  for (int v = l + 1; v < n; ++v) {
191
176
  const bool v_bit = get_const_bit(theta, v);
192
177
  real1 val = G_m[k_offset + v];
193
- if (is_spin_glass) {
194
- val *= 2;
195
- }
196
- energy += (k_bit != v_bit) ? val : -val;
178
+ energy += (k_bit == v_bit) ? -val : val;
197
179
  val = G_m[l_offset + v];
198
- if (is_spin_glass) {
199
- val *= 2;
200
- }
201
- energy += (l_bit != v_bit) ? val : -val;
180
+ energy += (l_bit == v_bit) ? -val : val;
202
181
  }
203
182
 
204
183
  return energy;
@@ -238,7 +217,10 @@ __kernel void double_bit_flips(
238
217
  }
239
218
  const int l = c + k + 1;
240
219
 
241
- const real1 energy = double_bit_flip_worker(best_theta, G_m, n, is_spin_glass, k, l);
220
+ real1 energy = double_bit_flip_worker(best_theta, G_m, n, k, l);
221
+ if (is_spin_glass) {
222
+ energy *= 2.0;
223
+ }
242
224
  if (energy > best_energy) {
243
225
  best_energy = energy;
244
226
  best_i = i;
@@ -503,25 +485,19 @@ __kernel void calculate_cut_segmented(
503
485
  reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
504
486
  }
505
487
 
506
- real1 single_bit_flip_worker_segmented(__constant uint* theta, __global const real1** G_m, const int n, const int segment_size, const bool is_spin_glass, const int k) {
488
+ real1 single_bit_flip_worker_segmented(__constant uint* theta, __global const real1** G_m, const int n, const int segment_size, const int k) {
507
489
  const size_t k_offset = k * (size_t)n;
508
490
  const bool k_bit = !get_const_bit(theta, k);
509
491
  real1 energy = ZERO_R1;
510
492
  for (int v = 0; v < k; ++v) {
511
493
  const bool v_bit = get_const_bit(theta, v);
512
494
  real1 val = get_G_m(G_m, k_offset + v, segment_size);
513
- if (is_spin_glass) {
514
- val *= 2;
515
- }
516
- energy += (k_bit != v_bit) ? val : -val;
495
+ energy += (k_bit == v_bit) ? -val : val;
517
496
  }
518
497
  for (int v = k + 1; v < n; ++v) {
519
498
  const bool v_bit = get_const_bit(theta, v);
520
499
  real1 val = get_G_m(G_m, k_offset + v, segment_size);
521
- if (is_spin_glass) {
522
- val *= 2;
523
- }
524
- energy += (k_bit != v_bit) ? val : -val;
500
+ energy += (k_bit == v_bit) ? -val : val;
525
501
  }
526
502
 
527
503
  return energy;
@@ -552,7 +528,10 @@ __kernel void single_bit_flips_segmented(
552
528
  int best_i = i;
553
529
 
554
530
  for (; i < n; i += max_i) {
555
- const real1 energy = single_bit_flip_worker_segmented(best_theta, G_m, n, segment_size, is_spin_glass, i);
531
+ real1 energy = single_bit_flip_worker_segmented(best_theta, G_m, n, segment_size, i);
532
+ if (is_spin_glass) {
533
+ energy *= 2.0;
534
+ }
556
535
  if (energy > best_energy) {
557
536
  best_energy = energy;
558
537
  best_i = i;
@@ -562,7 +541,7 @@ __kernel void single_bit_flips_segmented(
562
541
  reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
563
542
  }
564
543
 
565
- real1 double_bit_flip_worker_segmented(__constant uint* theta, __global const real1** G_m, const int n, const int segment_size, const bool is_spin_glass, int k, int l) {
544
+ real1 double_bit_flip_worker_segmented(__constant uint* theta, __global const real1** G_m, const int n, const int segment_size, int k, int l) {
566
545
  if (l < k) {
567
546
  int t = k;
568
547
  k = l;
@@ -576,41 +555,23 @@ real1 double_bit_flip_worker_segmented(__constant uint* theta, __global const re
576
555
  for (int v = 0; v < k; ++v) {
577
556
  const bool v_bit = get_const_bit(theta, v);
578
557
  real1 val = get_G_m(G_m, k_offset + v, segment_size);
579
- if (is_spin_glass) {
580
- val *= 2;
581
- }
582
- energy += (k_bit != v_bit) ? val : -val;
558
+ energy += (k_bit == v_bit) ? -val : val;
583
559
  val = get_G_m(G_m, l_offset + v, segment_size);
584
- if (is_spin_glass) {
585
- val *= 2;
586
- }
587
- energy += (l_bit != v_bit) ? val : -val;
560
+ energy += (l_bit == v_bit) ? -val : val;
588
561
  }
589
562
  for (int v = k + 1; v < l; ++v) {
590
563
  const bool v_bit = get_const_bit(theta, v);
591
564
  real1 val = get_G_m(G_m, k_offset + v, segment_size);
592
- if (is_spin_glass) {
593
- val *= 2;
594
- }
595
- energy += (k_bit != v_bit) ? val : -val;
565
+ energy += (k_bit == v_bit) ? -val : val;
596
566
  val = get_G_m(G_m, l_offset + v, segment_size);
597
- if (is_spin_glass) {
598
- val *= 2;
599
- }
600
- energy += (l_bit != v_bit) ? val : -val;
567
+ energy += (l_bit == v_bit) ? -val : val;
601
568
  }
602
569
  for (int v = l + 1; v < n; ++v) {
603
570
  const bool v_bit = get_const_bit(theta, v);
604
571
  real1 val = get_G_m(G_m, k_offset + v, segment_size);
605
- if (is_spin_glass) {
606
- val *= 2;
607
- }
608
- energy += (k_bit != v_bit) ? val : -val;
572
+ energy += (k_bit == v_bit) ? -val : val;
609
573
  val = get_G_m(G_m, l_offset + v, segment_size);
610
- if (is_spin_glass) {
611
- val *= 2;
612
- }
613
- energy += (l_bit != v_bit) ? val : -val;
574
+ energy += (l_bit == v_bit) ? -val : val;
614
575
  }
615
576
 
616
577
  return energy;
@@ -656,7 +617,10 @@ __kernel void double_bit_flips_segmented(
656
617
  }
657
618
  const int l = c + k + 1;
658
619
 
659
- const real1 energy = double_bit_flip_worker_segmented(best_theta, G_m, n, segment_size, is_spin_glass, k, l);
620
+ real1 energy = double_bit_flip_worker_segmented(best_theta, G_m, n, segment_size, k, l);
621
+ if (is_spin_glass) {
622
+ energy *= 2.0;
623
+ }
660
624
  if (energy > best_energy) {
661
625
  best_energy = energy;
662
626
  best_i = i;
@@ -902,40 +866,11 @@ __kernel void double_bit_flips_sparse_segmented(
902
866
  reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
903
867
  }
904
868
 
905
- void reduce_energy_index_block(real1 energy, int i, int block, __local real1* loc_energy, __local int* loc_index, __local int* loc_block, __global real1* max_energy_ptr, __global int* max_index_ptr, __global int* max_block_ptr) {
906
- const int lt_id = get_local_id(0);
907
- const int lt_size = get_local_size(0);
908
-
909
- loc_energy[lt_id] = energy;
910
- loc_index[lt_id] = i;
911
- loc_block[lt_id] = block;
912
-
913
- // Reduce within workgroup
914
- for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
915
- barrier(CLK_LOCAL_MEM_FENCE);
916
- real1 hid_energy, lid_energy;
917
- if (lt_id < offset) {
918
- hid_energy = loc_energy[lt_id + offset];
919
- lid_energy = loc_energy[lt_id];
920
- if (hid_energy > lid_energy) {
921
- loc_energy[lt_id] = hid_energy;
922
- loc_index[lt_id] = loc_index[lt_id + offset];
923
- loc_block[lt_id] = loc_block[lt_id + offset];
924
- }
925
- }
926
- }
927
-
928
- barrier(CLK_LOCAL_MEM_FENCE);
929
-
930
- // Write out per-group result
931
- if (lt_id == 0) {
932
- max_energy_ptr[get_group_id(0)] = loc_energy[0];
933
- max_index_ptr[get_group_id(0)] = loc_index[0];
934
- max_block_ptr[get_group_id(0)] = loc_block[0];
935
- }
869
+ inline bool get_local_bit(ulong* theta, const size_t u) {
870
+ return (theta[u >> 6U] >> (u & 63U)) & 1U;
936
871
  }
937
872
 
938
- inline bool get_local_bit(ulong* theta, const size_t u) {
873
+ inline bool get_const_long_bit(__constant ulong* theta, const size_t u) {
939
874
  return (theta[u >> 6U] >> (u & 63U)) & 1U;
940
875
  }
941
876
 
@@ -955,6 +890,7 @@ inline size_t gray_code_next(ulong* theta, const size_t curr_idx, const size_t o
955
890
  return flip_bit;
956
891
  }
957
892
 
893
+
958
894
  __kernel void gray(
959
895
  __global const real1* G_m,
960
896
  __constant ulong* theta,
@@ -965,8 +901,9 @@ __kernel void gray(
965
901
  const int n = args[0];
966
902
  const bool is_spin_glass = args[1];
967
903
  const int gray_iterations = args[2];
968
- const int blocks = (n + 63) / 64;
904
+ const int blocks = (n + 63) >> 6U;
969
905
  const int last_block = blocks - 1;
906
+ const int rem = n - (blocks << 6U);
970
907
 
971
908
  int i = get_global_id(0);
972
909
  const int max_i = get_global_size(0);
@@ -978,33 +915,29 @@ __kernel void gray(
978
915
 
979
916
  // Initialize different seed per thread
980
917
  const ulong seed = i ^ (i >> 1);
981
- int b;
982
- for (b = 0; b < 64; ++b) {
983
- if (seed >> (63U - b)) {
984
- break;
918
+ const int max_lcv = (n < 64) ? n : 64;
919
+ for (int b = 0; b < max_lcv; ++b) {
920
+ const bool bit = (seed >> (63U - b)) & 1U;
921
+ if (!bit) {
922
+ continue;
985
923
  }
924
+ const int offset = n - (b + 1);
925
+ theta_local[offset >> 6U] ^= 1 << (offset & 63U);
986
926
  }
987
- theta_local[last_block] ^= (seed >> (63U - b)) << b;
988
-
989
- const size_t b_offset = b * n;
990
- const bool b_bit = get_local_bit(theta_local, b);
991
927
 
992
928
  real1 best_energy = ZERO_R1;
993
- for (uint v = 0; v < b; v++) {
994
- const bool v_bit = get_local_bit(theta_local, v);
995
- real1 val = G_m[b_offset + v];
996
- if (is_spin_glass) {
997
- val *= 2;
998
- }
999
- best_energy += (b_bit != v_bit) ? val : -val;
1000
- }
1001
- for (uint v = b + 1; v < n; v++) {
1002
- const bool v_bit = get_local_bit(theta_local, v);
1003
- real1 val = G_m[b_offset + v];
1004
- if (is_spin_glass) {
1005
- val *= 2;
929
+ for (int u = 0; u < n; ++u) {
930
+ const size_t u_offset = u * n;
931
+ const bool o_u_bit = get_const_long_bit(theta, u);
932
+ const bool n_u_bit = get_local_bit(theta_local, u);
933
+ for (uint v = u + 1; v < n; ++v) {
934
+ const bool o_diff = o_u_bit != get_const_long_bit(theta, v);
935
+ const bool n_diff = n_u_bit != get_local_bit(theta_local, v);
936
+ if (o_diff != n_diff) {
937
+ const real1 val = G_m[u_offset + v];
938
+ best_energy += n_diff ? val : -val;
939
+ }
1006
940
  }
1007
- best_energy += (b_bit != v_bit) ? val : -val;
1008
941
  }
1009
942
 
1010
943
  for (; i < gray_iterations; i += max_i) {
@@ -1017,18 +950,12 @@ __kernel void gray(
1017
950
  for (uint v = 0; v < k; v++) {
1018
951
  const bool v_bit = get_local_bit(theta_local, v);
1019
952
  real1 val = G_m[k_offset + v];
1020
- if (is_spin_glass) {
1021
- val *= 2;
1022
- }
1023
- energy += (k_bit != v_bit) ? val : -val;
953
+ energy += (k_bit == v_bit) ? -val : val;
1024
954
  }
1025
955
  for (uint v = k + 1; v < n; v++) {
1026
956
  const bool v_bit = get_local_bit(theta_local, v);
1027
957
  real1 val = G_m[k_offset + v];
1028
- if (is_spin_glass) {
1029
- val *= 2;
1030
- }
1031
- energy += (k_bit != v_bit) ? val : -val;
958
+ energy += (k_bit == v_bit) ? -val : val;
1032
959
  }
1033
960
 
1034
961
  if (energy > ZERO_R1) {
@@ -1044,6 +971,9 @@ __kernel void gray(
1044
971
  for (int b = 0; b < blocks; ++b) {
1045
972
  theta_out[offset + b] = theta_local[b];
1046
973
  }
974
+ if (!is_spin_glass) {
975
+ best_energy /= 2;
976
+ }
1047
977
  energy_out[i] = best_energy;
1048
978
  }
1049
979
 
@@ -1065,6 +995,7 @@ __kernel void gray_segmented(
1065
995
  const int segment_size = args[3];
1066
996
  const int blocks = (n + 63) / 64;
1067
997
  const int last_block = blocks - 1;
998
+ const int rem = n - (blocks << 6U);
1068
999
 
1069
1000
  int i = get_global_id(0);
1070
1001
  const int max_i = get_global_size(0);
@@ -1076,33 +1007,29 @@ __kernel void gray_segmented(
1076
1007
 
1077
1008
  // Initialize different seed per thread
1078
1009
  const ulong seed = i ^ (i >> 1);
1079
- int b;
1080
- for (b = 0; b < 64; ++b) {
1081
- if (seed >> (63U - b)) {
1082
- break;
1010
+ const int max_lcv = (n < 64) ? n : 64;
1011
+ for (int b = 0; b < max_lcv; ++b) {
1012
+ const bool bit = (seed >> (63U - b)) & 1U;
1013
+ if (!bit) {
1014
+ continue;
1083
1015
  }
1016
+ const int offset = n - (b + 1);
1017
+ theta_local[offset >> 6U] ^= 1 << (offset & 63U);
1084
1018
  }
1085
- theta_local[last_block] ^= (seed >> (63U - b)) << b;
1086
-
1087
- const size_t b_offset = b * n;
1088
- const bool b_bit = get_local_bit(theta_local, b);
1089
1019
 
1090
1020
  real1 best_energy = ZERO_R1;
1091
- for (uint v = 0; v < b; v++) {
1092
- const bool v_bit = get_local_bit(theta_local, v);
1093
- real1 val = get_G_m(G_m, b_offset + v, segment_size);
1094
- if (is_spin_glass) {
1095
- val *= 2;
1096
- }
1097
- best_energy += (b_bit != v_bit) ? val : -val;
1098
- }
1099
- for (uint v = b + 1; v < n; v++) {
1100
- const bool v_bit = get_local_bit(theta_local, v);
1101
- real1 val = get_G_m(G_m, b_offset + v, segment_size);
1102
- if (is_spin_glass) {
1103
- val *= 2;
1021
+ for (int u = 0; u < n; ++u) {
1022
+ const size_t u_offset = u * n;
1023
+ const bool o_u_bit = get_const_long_bit(theta, u);
1024
+ const bool n_u_bit = get_local_bit(theta_local, u);
1025
+ for (uint v = u + 1; v < n; ++v) {
1026
+ const bool o_diff = o_u_bit != get_const_long_bit(theta, v);
1027
+ const bool n_diff = n_u_bit != get_local_bit(theta_local, v);
1028
+ if (o_diff != n_diff) {
1029
+ const real1 val = get_G_m(G_m, u_offset + v, segment_size);
1030
+ best_energy += n_diff ? val : -val;
1031
+ }
1104
1032
  }
1105
- best_energy += (b_bit != v_bit) ? val : -val;
1106
1033
  }
1107
1034
 
1108
1035
  for (; i < gray_iterations; i += max_i) {
@@ -1115,18 +1042,12 @@ __kernel void gray_segmented(
1115
1042
  for (uint v = 0; v < k; v++) {
1116
1043
  const bool v_bit = get_local_bit(theta_local, v);
1117
1044
  real1 val = get_G_m(G_m, k_offset + v, segment_size);
1118
- if (is_spin_glass) {
1119
- val *= 2;
1120
- }
1121
- energy += (k_bit != v_bit) ? val : -val;
1045
+ energy += (k_bit == v_bit) ? -val : val;
1122
1046
  }
1123
1047
  for (uint v = k + 1; v < n; v++) {
1124
1048
  const bool v_bit = get_local_bit(theta_local, v);
1125
1049
  real1 val = get_G_m(G_m, k_offset + v, segment_size);
1126
- if (is_spin_glass) {
1127
- val *= 2;
1128
- }
1129
- energy += (k_bit != v_bit) ? val : -val;
1050
+ energy += (k_bit == v_bit) ? -val : val;
1130
1051
  }
1131
1052
 
1132
1053
  if (energy > ZERO_R1) {
@@ -1142,6 +1063,9 @@ __kernel void gray_segmented(
1142
1063
  for (int b = 0; b < blocks; ++b) {
1143
1064
  theta_out[offset + b] = theta_local[b];
1144
1065
  }
1066
+ if (!is_spin_glass) {
1067
+ best_energy /= 2;
1068
+ }
1145
1069
  energy_out[i] = best_energy;
1146
1070
  }
1147
1071
 
@@ -3,7 +3,7 @@ import numpy as np
3
3
  from numba import njit, prange
4
4
  import os
5
5
 
6
- from .maxcut_tfim_util import compute_cut, compute_energy, convert_bool_to_uint, get_cut, get_cut_base, heuristic_threshold, init_thresholds, make_G_m_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick
6
+ from .maxcut_tfim_util import compute_cut, compute_energy, compute_cut_diff_between, convert_bool_to_uint, get_cut, get_cut_base, heuristic_threshold, init_thresholds, make_G_m_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick
7
7
 
8
8
  IS_OPENCL_AVAILABLE = True
9
9
  try:
@@ -68,56 +68,59 @@ def sample_measurement(G_m, shots, thread_count, thresholds, repulsion_base, is_
68
68
  shots = shot_segment * thread_count
69
69
  n = len(G_m)
70
70
 
71
- solutions = np.empty((thread_count, n), dtype=np.bool_)
72
- energies = np.full(thread_count, np.finfo(dtype).min, dtype=dtype)
73
-
71
+ solutions = np.zeros((thread_count, n), dtype=np.bool_)
74
72
  best_solution = solutions[0]
75
- best_energy = -float("inf")
73
+ if is_spin_glass:
74
+ best_energy = compute_energy(best_solution, G_m, n)
75
+ energies = np.full(thread_count, best_energy, dtype=dtype)
76
+ else:
77
+ best_energy = 0.0
78
+ energies = np.zeros(thread_count, dtype=dtype)
76
79
 
77
80
  improved = True
78
81
  while improved:
79
82
  improved = False
80
- if is_spin_glass:
81
- for i in prange(thread_count):
82
- s_offset = i * shot_segment
83
- for j in range(shot_segment):
84
- s = s_offset + j
85
83
 
86
- # First dimension: Hamming weight
87
- m = sample_mag(thresholds)
84
+ for i in prange(thread_count):
85
+ s_offset = i * shot_segment
86
+ for j in range(shot_segment):
87
+ s = s_offset + j
88
88
 
89
- # Second dimension: permutation within Hamming weight
90
- sample = local_repulsion_choice(G_m, repulsion_base, n, m, s)
91
- energy = compute_energy(sample, G_m, n)
89
+ # First dimension: Hamming weight
90
+ m = sample_mag(thresholds)
92
91
 
93
- if energy > energies[i]:
94
- solutions[i], energies[i] = sample, energy
95
- else:
96
- for i in prange(thread_count):
97
- s_offset = i * shot_segment
98
- for j in range(shot_segment):
99
- s = s_offset + j
92
+ # Second dimension: permutation within Hamming weight
93
+ sample = local_repulsion_choice(G_m, repulsion_base, n, m, s)
94
+ energy = compute_cut_diff_between(solutions[i], sample, G_m, n)
100
95
 
101
- # First dimension: Hamming weight
102
- m = sample_mag(thresholds)
103
-
104
- # Second dimension: permutation within Hamming weight
105
- sample = local_repulsion_choice(G_m, repulsion_base, n, m, s)
106
- energy = compute_cut(sample, G_m, n)
107
-
108
- if energy > energies[i]:
109
- solutions[i], energies[i] = sample, energy
96
+ if energy > 0.0:
97
+ energies[i] += energy
98
+ solutions[i] = sample
110
99
 
111
100
  best_index = np.argmax(energies)
112
101
  energy = energies[best_index]
102
+
103
+ if energy == float("inf"):
104
+ best_indices = []
105
+ for idx, e in enumerate(energies):
106
+ if e == float("inf"):
107
+ best_indices.append(idx)
108
+ best_index = best_indices[0]
109
+ for idx in best_indices[1:]:
110
+ e = compute_cut_diff_between(solutions[best_index], solutions[idx], G_m, n)
111
+ if e > 0.0:
112
+ best_index = idx
113
+ improved = True
114
+ best_solution = solutions[best_index].copy()
115
+
116
+ if is_spin_glass:
117
+ energy *= 2.0
118
+
113
119
  if energy > best_energy:
114
120
  best_energy = energy
115
121
  best_solution = solutions[best_index].copy()
116
122
  improved = True
117
123
 
118
- if is_spin_glass:
119
- best_energy = compute_cut(best_solution, G_m, n)
120
-
121
124
  return best_solution, best_energy
122
125
 
123
126
 
@@ -136,8 +139,8 @@ def sample_for_opencl(G_m, G_m_buf, shots, thresholds, repulsion_base, is_spin_g
136
139
 
137
140
  solutions = np.empty((shots, n), dtype=np.bool_)
138
141
 
139
- best_solution = solutions[0]
140
- best_energy = -float("inf")
142
+ best_solution = np.zeros(n, dtype=np.bool_)
143
+ best_energy = compute_energy(best_solution, G_m, n) if is_spin_glass else 0.0
141
144
 
142
145
  opencl_args = setup_opencl(shots, shots, np.array([n, shots, is_spin_glass, segment_size, theta_segment_size], dtype=np.int32))
143
146
 
@@ -148,7 +151,7 @@ def sample_for_opencl(G_m, G_m_buf, shots, thresholds, repulsion_base, is_spin_g
148
151
  solution, energy = run_cut_opencl(best_energy, solutions, G_m_buf, is_segmented, *opencl_args)
149
152
  if energy > best_energy:
150
153
  best_energy = energy
151
- best_solution = solution.copy()
154
+ best_solution = solution
152
155
  improved = True
153
156
 
154
157
  if is_spin_glass:
@@ -234,12 +237,12 @@ def run_cut_opencl(best_energy, samples, G_m_buf, is_segmented, local_size, glob
234
237
 
235
238
  if energy <= best_energy:
236
239
  # No improvement: we can exit early
237
- return samples[0], max_energy_host[0]
240
+ return samples[0].copy(), max_energy_host[0]
238
241
 
239
242
  # We need the best index
240
243
  queue.finish()
241
244
 
242
- return samples[max_index_host[best_x]], energy
245
+ return samples[max_index_host[best_x]].copy(), energy
243
246
 
244
247
 
245
248
  @njit
@@ -146,8 +146,8 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
146
146
 
147
147
  solutions = np.empty((shots, n), dtype=np.bool_)
148
148
 
149
- best_solution = solutions[0]
150
- best_energy = -float("inf")
149
+ best_solution = np.zeros(n, dtype=np.bool_)
150
+ best_energy = compute_energy(best_solution, G_m, n) if is_spin_glass else 0.0
151
151
 
152
152
  opencl_args = setup_opencl(shots, shots, np.array([n, shots, is_spin_glass, segment_size, theta_segment_size], dtype=np.int32))
153
153
 
@@ -158,7 +158,7 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
158
158
  solution, energy = run_cut_opencl(best_energy, solutions, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, *opencl_args)
159
159
  if energy > best_energy:
160
160
  best_energy = energy
161
- best_solution = solution.copy()
161
+ best_solution = solution
162
162
  improved = True
163
163
 
164
164
  if is_spin_glass:
@@ -248,12 +248,12 @@ def run_cut_opencl(best_energy, samples, G_data_buf, G_rows_buf, G_cols_buf, is_
248
248
 
249
249
  if energy <= best_energy:
250
250
  # No improvement: we can exit early
251
- return samples[0], max_energy_host[0]
251
+ return samples[0].copy(), max_energy_host[0]
252
252
 
253
253
  # We need the best index
254
254
  queue.finish()
255
255
 
256
- return samples[max_index_host[best_x]], energy
256
+ return samples[max_index_host[best_x]].copy(), energy
257
257
 
258
258
 
259
259
  @njit