PyQrackIsing 1.10.4__tar.gz → 1.10.5__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.4/PyQrackIsing.egg-info → pyqrackising-1.10.5}/PKG-INFO +1 -1
  2. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/PyQrackIsing/_pyqrack_ising.cpp +5 -10
  3. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/PyQrackIsing/generate_tfim_samples.py +3 -1
  4. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/PyQrackIsing/maxcut_tfim.py +109 -10
  5. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/PyQrackIsing/spin_glass_solver.py +11 -9
  6. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/PyQrackIsing/tsp_symmetric.py +14 -5
  7. {pyqrackising-1.10.4 → pyqrackising-1.10.5/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  8. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/pyproject.toml +1 -1
  9. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/setup.py +1 -1
  10. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/CMakeLists.txt +0 -0
  11. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/LICENSE.md +0 -0
  12. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/MANIFEST.in +0 -0
  13. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/PyQrackIsing/__init__.py +0 -0
  14. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/PyQrackIsing/tfim_magnetization.py +0 -0
  15. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  16. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  17. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  18. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  19. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/PyQrackIsing.egg-info/top_level.txt +0 -0
  20. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/README.md +0 -0
  21. {pyqrackising-1.10.4 → pyqrackising-1.10.5}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.10.4
3
+ Version: 1.10.5
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
- static inline std::vector<double> probability_by_hamming_weight(double J, double h, double z, double theta, double t, size_t n_qubits)
30
+ 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))));
@@ -290,15 +290,10 @@ double tfim_square_magnetization(double J, double h, double z, double theta, dou
290
290
 
291
291
  PYBIND11_MODULE(tfim_sampler, m) {
292
292
  m.doc() = "PyQrackIsing TFIM sample generator";
293
- m.def("_generate_tfim_samples", &generate_tfim_samples_cpp,
294
- py::arg("J"), py::arg("h"), py::arg("z"), py::arg("theta"),
295
- py::arg("t"), py::arg("n_qubits"), py::arg("shots"));
296
- m.def("_tfim_magnetization", &tfim_magnetization,
297
- py::arg("J"), py::arg("h"), py::arg("z"), py::arg("theta"),
298
- py::arg("t"), py::arg("n_qubits"));
299
- m.def("_tfim_square_magnetization", &tfim_square_magnetization,
300
- py::arg("J"), py::arg("h"), py::arg("z"), py::arg("theta"),
301
- py::arg("t"), py::arg("n_qubits"));
293
+ m.def("_generate_tfim_samples", &generate_tfim_samples_cpp, "Generate measurement samples from globally-uniform TFIM");
294
+ m.def("_tfim_magnetization", &tfim_magnetization, "Magnetization expectation value from globally-uniform TFIM");
295
+ m.def("_tfim_square_magnetization", &tfim_square_magnetization, "Square magnetization expectation value from globally-uniform TFIM");
302
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");
303
298
  }
304
299
 
@@ -1,5 +1,7 @@
1
1
  import tfim_sampler
2
2
 
3
3
 
4
- def generate_tfim_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100):
4
+ def generate_tfim_samples(
5
+ J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100
6
+ ):
5
7
  return [int(s) for s in tfim_sampler._generate_tfim_samples(J, h, z, theta, t, n_qubits, shots)]
@@ -1,7 +1,93 @@
1
1
  import networkx as nx
2
2
  import numpy as np
3
- import tfim_sampler
4
- from numba import njit
3
+ from numba import njit, prange
4
+
5
+
6
+ @njit
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
+ bias = np.zeros(n_qubits + 1)
20
+
21
+ if np.isclose(abs(h), 0.0):
22
+ bias[0] = 1.0
23
+ elif np.isclose(abs(J), 0.0):
24
+ bias.fill(1.0 / (n_qubits + 1.0))
25
+ else:
26
+ sin_delta = np.sin(delta_theta)
27
+ omega = 1.5 * np.pi
28
+ t2 = 1.0
29
+ p = (
30
+ 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)))
32
+ - 0.5
33
+ )
34
+
35
+ if p >= 1024:
36
+ bias[0] = 1.0
37
+ else:
38
+ tot_n = 0.0
39
+ for q in range(n_qubits + 1):
40
+ n = 1.0 / ((n_qubits + 1) * pow(2.0, p * q))
41
+ bias[q] = n
42
+ tot_n += n
43
+ for q in range(n_qubits + 1):
44
+ bias[q] /= tot_n
45
+
46
+ if J > 0.0:
47
+ bias = bias[::-1]
48
+
49
+ return bias
50
+
51
+
52
+ @njit(parallel=True)
53
+ def maxcut_hamming_cdf(n_qubits, J_func, degrees, mult_log2):
54
+ if n_qubits == 0:
55
+ return np.empty(0, dtype=np.float64)
56
+
57
+ n_steps = n_qubits << mult_log2
58
+ shots = n_qubits << mult_log2
59
+ 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):
64
+ t = step * delta_t
65
+ tm1 = (step - 1) * delta_t
66
+ for q in prange(n_qubits):
67
+ z = degrees[q]
68
+ J_eff = J_func[q]
69
+ h_t = h_mult * t
70
+ 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
+ last_bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, tm1, n_qubits)
77
+ for i in range(len(hamming_prob)):
78
+ hamming_prob[i] += bias[i + 1] - last_bias[i + 1]
79
+
80
+ tot_prob = sum(hamming_prob)
81
+ for i in prange(len(hamming_prob)):
82
+ hamming_prob[i] /= tot_prob
83
+
84
+ tot_prob = 0.0
85
+ for i in range(len(hamming_prob)):
86
+ tot_prob += hamming_prob[i]
87
+ hamming_prob[i] = tot_prob
88
+ hamming_prob[-1] = 1.0
89
+
90
+ return hamming_prob
5
91
 
6
92
 
7
93
  # Written by Elara (OpenAI custom GPT)
@@ -46,7 +132,7 @@ def local_repulsion_choice(nodes, adjacency, degrees, weights, n, m):
46
132
  # Build integer mask
47
133
  mask = 0
48
134
  for pos in chosen:
49
- mask |= (1 << pos)
135
+ mask |= 1 << pos
50
136
 
51
137
  return mask
52
138
 
@@ -79,23 +165,36 @@ def int_to_bitstring(integer, length):
79
165
 
80
166
  def maxcut_tfim(
81
167
  G,
82
- quality = 12,
83
- shots = None,
168
+ quality=12,
169
+ shots=None,
84
170
  ):
85
171
  # Number of qubits/nodes
86
172
  nodes = list(G.nodes())
87
173
  n_qubits = len(nodes)
88
174
 
89
175
  if n_qubits == 0:
90
- return '', 0, ([], [])
176
+ return "", 0, ([], [])
91
177
 
92
178
  if shots is None:
93
179
  # Number of measurement shots
94
180
  shots = n_qubits << quality
95
181
 
96
- J_eff = np.array([-sum(edge_attributes.get('weight', 1.0) for _, edge_attributes in G.adj[n].items()) for n in nodes], dtype=np.float64)
97
- degrees = np.array([sum(abs(edge_attributes.get('weight', 1.0)) for _, edge_attributes in G.adj[n].items()) for n in nodes], dtype=np.float64)
98
- thresholds = tfim_sampler._maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
182
+ J_eff = np.array(
183
+ [
184
+ -sum(edge_attributes.get("weight", 1.0) for _, edge_attributes in G.adj[n].items())
185
+ for n in nodes
186
+ ],
187
+ dtype=np.float64,
188
+ )
189
+ degrees = np.array(
190
+ [
191
+ sum(abs(edge_attributes.get("weight", 1.0)) for _, edge_attributes in G.adj[n].items())
192
+ for n in nodes
193
+ ],
194
+ dtype=np.float64,
195
+ )
196
+ # thresholds = tfim_sampler._maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
197
+ thresholds = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
99
198
  G_dict = nx.to_dict_of_lists(G)
100
199
  J_max = max(J_eff)
101
200
  weights = 1.0 / (1.0 + (J_max - J_eff))
@@ -126,7 +225,7 @@ def maxcut_tfim(
126
225
  bit_list = list(bit_string)
127
226
  l, r = [], []
128
227
  for i in range(len(bit_list)):
129
- b = (bit_list[i] == '1')
228
+ b = bit_list[i] == "1"
130
229
  if b:
131
230
  r.append(nodes[i])
132
231
  else:
@@ -45,7 +45,7 @@ def bootstrap(theta, edge_keys, edge_values, k, indices_array):
45
45
  energies = np.empty(n)
46
46
  j = 0
47
47
  for i in prange(n):
48
- energies[i] = bootstrap_worker(theta, edge_keys, edge_values, indices_array[j:j+k])
48
+ energies[i] = bootstrap_worker(theta, edge_keys, edge_values, indices_array[j : j + k])
49
49
  j += k
50
50
 
51
51
  return energies
@@ -61,9 +61,9 @@ def spin_glass_solver(G, quality=1, best_guess=None):
61
61
  n_qubits = len(nodes)
62
62
 
63
63
  if n_qubits == 0:
64
- return '', 0, ([], [])
64
+ return "", 0, ([], []), 0
65
65
 
66
- bitstring = ''
66
+ bitstring = ""
67
67
  if isinstance(best_guess, str):
68
68
  bitstring = best_guess
69
69
  elif isinstance(best_guess, int):
@@ -72,7 +72,7 @@ def spin_glass_solver(G, quality=1, best_guess=None):
72
72
  bitstring = "".join(["1" if b else "0" for b in best_guess])
73
73
  else:
74
74
  bitstring, _, _ = maxcut_tfim(G, quality=max(0, quality - 1))
75
- best_theta = [ b == '1' for b in list(bitstring)]
75
+ best_theta = [b == "1" for b in list(bitstring)]
76
76
 
77
77
  edge_keys = []
78
78
  edge_values = []
@@ -91,13 +91,15 @@ def spin_glass_solver(G, quality=1, best_guess=None):
91
91
 
92
92
  theta = best_theta.copy()
93
93
 
94
- combos = list(item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist)
94
+ combos = list(
95
+ item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
96
+ )
95
97
  energies = bootstrap(theta, edge_keys, edge_values, k, combos)
96
98
 
97
99
  energy = min(energies)
98
100
  if energy < min_energy:
99
101
  index_match = np.random.choice(np.where(energies == energy)[0])
100
- indices = combos[(index_match * k):((index_match + 1) * k)]
102
+ indices = combos[(index_match * k) : ((index_match + 1) * k)]
101
103
  min_energy = energy
102
104
  for i in indices:
103
105
  best_theta[i] = not best_theta[i]
@@ -105,16 +107,16 @@ def spin_glass_solver(G, quality=1, best_guess=None):
105
107
  break
106
108
 
107
109
  sample = 0
108
- bitstring = ''
110
+ bitstring = ""
109
111
  l, r = [], []
110
112
  for i in range(len(best_theta)):
111
113
  b = best_theta[i]
112
114
  if b:
113
- bitstring += '1'
115
+ bitstring += "1"
114
116
  r.append(nodes[i])
115
117
  sample |= 1 << i
116
118
  else:
117
- bitstring += '0'
119
+ bitstring += "0"
118
120
  l.append(nodes[i])
119
121
 
120
122
  cut_value = evaluate_cut_edges(sample, edge_keys, edge_values)
@@ -1,6 +1,7 @@
1
1
  from .spin_glass_solver import spin_glass_solver
2
2
  import networkx as nx
3
3
 
4
+
4
5
  def get_best_stitch(adjacency, terminals_a, terminals_b, is_cyclic):
5
6
  best_weight = float("inf")
6
7
  best_edge = None
@@ -24,7 +25,7 @@ def tsp_symmetric(G, quality=2, is_cyclic=True, start_node=None):
24
25
  if G.number_of_nodes() == 1:
25
26
  return ([0], 0)
26
27
  if G.number_of_nodes() == 2:
27
- return ([0, 1], G[0][1]['weight'])
28
+ return ([0, 1], G[0][1]["weight"])
28
29
 
29
30
  nodes = list(G.nodes())
30
31
 
@@ -38,7 +39,7 @@ def tsp_symmetric(G, quality=2, is_cyclic=True, start_node=None):
38
39
  while (len(a) == 0) or (len(b) == 0):
39
40
  bitstring, _, _, _ = spin_glass_solver(G, quality=quality)
40
41
  for idx, bit in enumerate(bitstring):
41
- if bit == '1':
42
+ if bit == "1":
42
43
  b.append(nodes[idx])
43
44
  else:
44
45
  a.append(nodes[idx])
@@ -54,9 +55,17 @@ def tsp_symmetric(G, quality=2, is_cyclic=True, start_node=None):
54
55
  G_b.add_edge(b.index(u), b.index(v), weight=weight)
55
56
  continue
56
57
 
57
- sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False) if len(a) > 2 else (([0, 1], G_a[0][1]['weight']) if len(a) == 2 else ([0], 0))
58
- sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False) if len(b) > 2 else (([0, 1], G_b[0][1]['weight']) if len(b) == 2 else ([0], 0))
59
- sol_weight = sol_a[1] + sol_b[1]
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]
60
69
 
61
70
  path_a = [a[idx] for idx in sol_a[0]]
62
71
  path_b = [b[idx] for idx in sol_b[0]]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.10.4
3
+ Version: 1.10.5
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.4"
14
+ version = "1.10.5"
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.4',
54
+ version='1.10.5',
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