PyQrackIsing 1.10.5__tar.gz → 1.10.7__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 (21) hide show
  1. {pyqrackising-1.10.5/PyQrackIsing.egg-info → pyqrackising-1.10.7}/PKG-INFO +1 -1
  2. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/PyQrackIsing/_pyqrack_ising.cpp +1 -50
  3. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/PyQrackIsing/maxcut_tfim.py +36 -36
  4. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/PyQrackIsing/spin_glass_solver.py +3 -0
  5. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/PyQrackIsing/tsp_symmetric.py +19 -26
  6. {pyqrackising-1.10.5 → pyqrackising-1.10.7/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  7. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/pyproject.toml +1 -1
  8. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/setup.py +1 -1
  9. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/CMakeLists.txt +0 -0
  10. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/LICENSE.md +0 -0
  11. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/MANIFEST.in +0 -0
  12. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/PyQrackIsing/__init__.py +0 -0
  13. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/PyQrackIsing/generate_tfim_samples.py +0 -0
  14. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/PyQrackIsing/tfim_magnetization.py +0 -0
  15. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  16. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  17. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  18. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  19. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/PyQrackIsing.egg-info/top_level.txt +0 -0
  20. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/README.md +0 -0
  21. {pyqrackising-1.10.5 → pyqrackising-1.10.7}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.10.5
3
+ Version: 1.10.7
4
4
  Summary: Near-ideal closed-form solutions for transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -27,7 +27,7 @@ std::random_device rd = std::random_device{};
27
27
  std::mt19937 rng(rd());
28
28
 
29
29
 
30
- std::vector<double> probability_by_hamming_weight(double J, double h, double z, double theta, double t, size_t n_qubits)
30
+ static inline std::vector<double> probability_by_hamming_weight(double J, double h, double z, double theta, double t, size_t n_qubits)
31
31
  {
32
32
  // critical angle
33
33
  const double theta_c = std::asin(std::max(-1.0, std::min(1.0, (std::abs(z * J) >= (std::numeric_limits<double>::epsilon() / 2)) ? std::abs(h) / (z * J) : (J > 0 ? 1.0 : -1.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;
@@ -293,7 +246,5 @@ PYBIND11_MODULE(tfim_sampler, m) {
293
246
  m.def("_generate_tfim_samples", &generate_tfim_samples_cpp, "Generate measurement samples from globally-uniform TFIM");
294
247
  m.def("_tfim_magnetization", &tfim_magnetization, "Magnetization expectation value from globally-uniform TFIM");
295
248
  m.def("_tfim_square_magnetization", &tfim_square_magnetization, "Square magnetization expectation value from globally-uniform TFIM");
296
- m.def("_maxcut_hamming_cdf", &maxcut_hamming_cdf, "Adiabatic TFIM Hamming weight probability density function");
297
- m.def("_probability_by_hamming_weight", &probability_by_hamming_weight, "Trace probability from globally-uniform TFIM by Hamming weight");
298
249
  }
299
250
 
@@ -5,84 +5,81 @@ from numba import njit, prange
5
5
 
6
6
  @njit
7
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
8
  bias = np.zeros(n_qubits + 1)
20
-
21
9
  if np.isclose(abs(h), 0.0):
22
10
  bias[0] = 1.0
23
11
  elif np.isclose(abs(J), 0.0):
24
12
  bias.fill(1.0 / (n_qubits + 1.0))
25
13
  else:
26
- sin_delta = np.sin(delta_theta)
27
- omega = 1.5 * np.pi
28
- t2 = 1.0
14
+ # critical angle
15
+ theta_c = np.arcsin(
16
+ max(
17
+ -1.0,
18
+ min(
19
+ 1.0,
20
+ (1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h) / (z * J)),
21
+ ),
22
+ )
23
+ )
29
24
  p = (
30
25
  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)))
26
+ * (1.0 + np.sin(theta - theta_c) * np.cos(1.5 * np.pi * J * t + theta) / (1.0 + np.sqrt(t)))
32
27
  - 0.5
33
28
  )
34
-
35
- if p >= 1024:
29
+ if (p * n_qubits) >= 1024:
36
30
  bias[0] = 1.0
37
31
  else:
38
32
  tot_n = 0.0
39
33
  for q in range(n_qubits + 1):
40
- n = 1.0 / ((n_qubits + 1) * pow(2.0, p * q))
34
+ n = 1.0 / pow(2.0, p * q)
41
35
  bias[q] = n
42
36
  tot_n += n
43
- for q in range(n_qubits + 1):
44
- bias[q] /= tot_n
37
+ bias /= tot_n
45
38
 
46
39
  if J > 0.0:
47
- bias = bias[::-1]
40
+ return bias[::-1]
48
41
 
49
42
  return bias
50
43
 
51
44
 
52
45
  @njit(parallel=True)
53
46
  def maxcut_hamming_cdf(n_qubits, J_func, degrees, mult_log2):
54
- if n_qubits == 0:
47
+ if n_qubits < 2:
55
48
  return np.empty(0, dtype=np.float64)
56
49
 
57
50
  n_steps = n_qubits << mult_log2
58
51
  shots = n_qubits << mult_log2
59
52
  delta_t = 1.0 / (n_steps << (mult_log2 >> 1))
60
- h_mult = (1 << (mult_log2 >> 1)) / (n_steps * delta_t)
61
- hamming_prob = np.zeros(n_qubits - 1)
62
-
63
- for step in range(n_steps):
53
+ tot_t = (n_steps - 1) * delta_t
54
+ h_mult = (1 << (mult_log2 >> 1)) / tot_t
55
+ n_bias = n_qubits - 1
56
+ hamming_prob = np.zeros(n_bias)
57
+
58
+ for q in prange(n_qubits):
59
+ z = degrees[q]
60
+ J_eff = J_func[q]
61
+ bias = probability_by_hamming_weight(J_eff, h_mult, z, 0.0, 0, n_qubits)
62
+ for i in range(n_bias):
63
+ hamming_prob[i] += bias[i + 1]
64
+
65
+ for step in range(1, n_steps):
64
66
  t = step * delta_t
65
67
  tm1 = (step - 1) * delta_t
66
68
  for q in prange(n_qubits):
67
69
  z = degrees[q]
68
70
  J_eff = J_func[q]
69
- h_t = h_mult * t
71
+ h_t = h_mult * (tot_t - t)
70
72
  bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, t, n_qubits)
71
- if step == 0:
72
- for i in range(len(hamming_prob)):
73
- hamming_prob[i] += bias[i + 1]
74
- continue
75
-
76
73
  last_bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, tm1, n_qubits)
77
- for i in range(len(hamming_prob)):
74
+ for i in range(n_bias):
78
75
  hamming_prob[i] += bias[i + 1] - last_bias[i + 1]
79
76
 
80
77
  tot_prob = sum(hamming_prob)
81
- for i in prange(len(hamming_prob)):
78
+ for i in prange(n_bias):
82
79
  hamming_prob[i] /= tot_prob
83
80
 
84
81
  tot_prob = 0.0
85
- for i in range(len(hamming_prob)):
82
+ for i in range(n_bias):
86
83
  tot_prob += hamming_prob[i]
87
84
  hamming_prob[i] = tot_prob
88
85
  hamming_prob[-1] = 1.0
@@ -175,6 +172,9 @@ def maxcut_tfim(
175
172
  if n_qubits == 0:
176
173
  return "", 0, ([], [])
177
174
 
175
+ if n_qubits == 1:
176
+ return "0", 0, ([nodes[0]], [])
177
+
178
178
  if shots is None:
179
179
  # Number of measurement shots
180
180
  shots = n_qubits << quality
@@ -63,6 +63,9 @@ def spin_glass_solver(G, quality=1, best_guess=None):
63
63
  if n_qubits == 0:
64
64
  return "", 0, ([], []), 0
65
65
 
66
+ if n_qubits == 1:
67
+ return "0", 0, ([nodes[0]], [])
68
+
66
69
  bitstring = ""
67
70
  if isinstance(best_guess, str):
68
71
  bitstring = best_guess
@@ -22,12 +22,15 @@ def get_best_stitch(adjacency, terminals_a, terminals_b, is_cyclic):
22
22
 
23
23
 
24
24
  def tsp_symmetric(G, quality=2, is_cyclic=True, start_node=None):
25
- if G.number_of_nodes() == 1:
26
- return ([0], 0)
27
- if G.number_of_nodes() == 2:
28
- return ([0, 1], G[0][1]["weight"])
29
-
30
25
  nodes = list(G.nodes())
26
+ n_nodes = len(nodes)
27
+
28
+ if n_nodes == 0:
29
+ return ([], 0)
30
+ if n_nodes == 1:
31
+ return ([nodes[0]], 0)
32
+ if n_nodes == 2:
33
+ return ([nodes[0], nodes[1]], G[nodes[0]][nodes[1]]["weight"])
31
34
 
32
35
  a = []
33
36
  b = []
@@ -37,38 +40,28 @@ def tsp_symmetric(G, quality=2, is_cyclic=True, start_node=None):
37
40
  b.remove(start_node)
38
41
  else:
39
42
  while (len(a) == 0) or (len(b) == 0):
40
- bitstring, _, _, _ = spin_glass_solver(G, quality=quality)
41
- for idx, bit in enumerate(bitstring):
42
- if bit == "1":
43
- b.append(nodes[idx])
44
- else:
45
- a.append(nodes[idx])
43
+ _, _, bits, _ = spin_glass_solver(G, quality=quality)
44
+ a = list(bits[0])
45
+ b = list(bits[1])
46
46
 
47
47
  G_a = nx.Graph()
48
48
  G_b = nx.Graph()
49
49
  for u, v, data in G.edges(data=True):
50
50
  weight = data.get("weight", 1.0)
51
51
  if (u in a) and (v in a):
52
- G_a.add_edge(a.index(u), a.index(v), weight=weight)
52
+ G_a.add_edge(u, v, weight=weight)
53
53
  continue
54
54
  elif (u in b) and (v in b):
55
- G_b.add_edge(b.index(u), b.index(v), weight=weight)
55
+ G_b.add_edge(u, v, weight=weight)
56
56
  continue
57
57
 
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]
58
+ sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False) if len(a) > 1 else (a, 0)
59
+ sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False) if len(b) > 1 else (b, 0)
69
60
 
70
- path_a = [a[idx] for idx in sol_a[0]]
71
- path_b = [b[idx] for idx in sol_b[0]]
61
+ path_a = sol_a[0]
62
+ path_b = sol_b[0]
63
+
64
+ sol_weight = sol_a[1] + sol_b[1]
72
65
 
73
66
  terminals_a = (path_a[0], path_a[-1])
74
67
  terminals_b = (path_b[0], path_b[-1])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.10.5
3
+ Version: 1.10.7
4
4
  Summary: Near-ideal closed-form solutions for transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
11
11
 
12
12
  [project]
13
13
  name = "PyQrackIsing"
14
- version = "1.10.5"
14
+ version = "1.10.7"
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.5',
54
+ version='1.10.7',
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)',
File without changes
File without changes
File without changes
File without changes