PyQrackIsing 1.10.6__tar.gz → 1.11.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 (21) hide show
  1. {pyqrackising-1.10.6/PyQrackIsing.egg-info → pyqrackising-1.11.0}/PKG-INFO +1 -1
  2. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/PyQrackIsing/maxcut_tfim.py +55 -50
  3. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/PyQrackIsing/spin_glass_solver.py +6 -3
  4. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/PyQrackIsing/tsp_symmetric.py +24 -30
  5. {pyqrackising-1.10.6 → pyqrackising-1.11.0/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  6. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/README.md +7 -7
  7. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/pyproject.toml +1 -1
  8. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/setup.py +1 -1
  9. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/CMakeLists.txt +0 -0
  10. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/LICENSE.md +0 -0
  11. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/MANIFEST.in +0 -0
  12. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/PyQrackIsing/__init__.py +0 -0
  13. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  14. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/PyQrackIsing/generate_tfim_samples.py +0 -0
  15. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/PyQrackIsing/tfim_magnetization.py +0 -0
  16. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  17. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  18. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  19. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  20. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/PyQrackIsing.egg-info/top_level.txt +0 -0
  21. {pyqrackising-1.10.6 → pyqrackising-1.11.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.10.6
3
+ Version: 1.11.0
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
@@ -5,6 +5,13 @@ 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
+ if np.isclose(abs(J), 0.0):
9
+ return np.full(n_qubits - 1, 1.0 / (n_qubits - 1.0))
10
+
11
+ bias = np.zeros(n_qubits - 1)
12
+ if np.isclose(abs(h), 0.0):
13
+ return bias
14
+
8
15
  # critical angle
9
16
  theta_c = np.arcsin(
10
17
  max(
@@ -15,75 +22,70 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
15
22
  ),
16
23
  )
17
24
  )
18
- delta_theta = theta - theta_c
19
- bias = np.zeros(n_qubits + 1)
20
25
 
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
- )
26
+ p = (
27
+ pow(2.0, abs(J / h) - 1.0)
28
+ * (1.0 + np.sin(theta - theta_c) * np.cos(1.5 * np.pi * J * t + theta) / (1.0 + np.sqrt(t)))
29
+ - 0.5
30
+ )
34
31
 
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
32
+ if (p * n_qubits) >= 1024:
33
+ return bias
34
+
35
+ tot_n = 1.0 + 1.0 / pow(2.0, p * n_qubits)
36
+ for q in range(1, n_qubits):
37
+ n = 1.0 / pow(2.0, p * q)
38
+ bias[q] = n
39
+ tot_n += n
40
+ bias /= tot_n
45
41
 
46
42
  if J > 0.0:
47
- bias = bias[::-1]
43
+ return bias[::-1]
48
44
 
49
45
  return bias
50
46
 
51
47
 
52
48
  @njit(parallel=True)
53
- def maxcut_hamming_cdf(n_qubits, J_func, degrees, mult_log2):
49
+ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality):
54
50
  if n_qubits < 2:
55
51
  return np.empty(0, dtype=np.float64)
56
52
 
57
- n_steps = n_qubits << mult_log2
58
- shots = n_qubits << mult_log2
59
- delta_t = 1.0 / (n_steps << (mult_log2 >> 1))
53
+ n_steps = n_qubits << (quality << 1)
54
+ delta_t = 1.0 / (n_steps << quality)
60
55
  tot_t = (n_steps - 1) * delta_t
61
- h_mult = (1 << (mult_log2 >> 1)) / tot_t
56
+ h_mult = (1 << quality) / tot_t
62
57
  n_bias = n_qubits - 1
63
- hamming_prob = np.zeros(n_bias)
64
-
58
+ hamming_prob = np.full(n_bias, 1 / n_bias)
59
+ theta = np.zeros(n_qubits)
65
60
  for q in prange(n_qubits):
61
+ J = J_func[q]
66
62
  z = degrees[q]
67
- J_eff = J_func[q]
68
- bias = probability_by_hamming_weight(J_eff, h_mult, z, 0.0, 0, n_qubits)
69
- for i in range(n_bias):
70
- hamming_prob[i] += bias[i + 1]
63
+ theta[q] = np.arcsin(
64
+ max(
65
+ -1.0,
66
+ min(
67
+ 1.0,
68
+ (1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
69
+ ),
70
+ )
71
+ )
71
72
 
72
- for step in range(1, n_steps):
73
+ for qc in prange(n_qubits, n_steps * n_qubits):
74
+ step = qc // n_qubits
75
+ q = qc % n_qubits
73
76
  t = step * delta_t
74
77
  tm1 = (step - 1) * delta_t
75
- for q in prange(n_qubits):
76
- z = degrees[q]
77
- J_eff = J_func[q]
78
- h_t = h_mult * (tot_t - t)
79
- bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, t, n_qubits)
80
- last_bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, tm1, n_qubits)
81
- for i in range(n_bias):
82
- hamming_prob[i] += bias[i + 1] - last_bias[i + 1]
78
+ z = degrees[q]
79
+ J_eff = J_func[q]
80
+ theta_eff = theta[q]
81
+ h_t = h_mult * (tot_t - t)
82
+ bias = probability_by_hamming_weight(J_eff, h_mult, z, theta_eff, t, n_qubits)
83
+ last_bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_qubits)
84
+ for i in range(n_bias):
85
+ hamming_prob[i] += bias[i] - last_bias[i]
83
86
 
84
87
  tot_prob = sum(hamming_prob)
85
- for i in prange(n_bias):
86
- hamming_prob[i] /= tot_prob
88
+ hamming_prob /= tot_prob
87
89
 
88
90
  tot_prob = 0.0
89
91
  for i in range(n_bias):
@@ -169,7 +171,7 @@ def int_to_bitstring(integer, length):
169
171
 
170
172
  def maxcut_tfim(
171
173
  G,
172
- quality=12,
174
+ quality=4,
173
175
  shots=None,
174
176
  ):
175
177
  # Number of qubits/nodes
@@ -179,9 +181,12 @@ def maxcut_tfim(
179
181
  if n_qubits == 0:
180
182
  return "", 0, ([], [])
181
183
 
184
+ if n_qubits == 1:
185
+ return "0", 0, ([nodes[0]], [])
186
+
182
187
  if shots is None:
183
188
  # Number of measurement shots
184
- shots = n_qubits << quality
189
+ shots = n_qubits << (quality << 1)
185
190
 
186
191
  J_eff = np.array(
187
192
  [
@@ -56,13 +56,16 @@ def int_to_bitstring(integer, length):
56
56
  return (bin(integer)[2:].zfill(length))[::-1]
57
57
 
58
58
 
59
- def spin_glass_solver(G, quality=1, best_guess=None):
59
+ def spin_glass_solver(G, quality=4, best_guess=None):
60
60
  nodes = list(G.nodes())
61
61
  n_qubits = len(nodes)
62
62
 
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
@@ -71,7 +74,7 @@ def spin_glass_solver(G, quality=1, best_guess=None):
71
74
  elif isinstance(best_guess, list):
72
75
  bitstring = "".join(["1" if b else "0" for b in best_guess])
73
76
  else:
74
- bitstring, _, _ = maxcut_tfim(G, quality=max(0, quality - 1))
77
+ bitstring, _, _ = maxcut_tfim(G, quality=quality)
75
78
  best_theta = [b == "1" for b in list(bitstring)]
76
79
 
77
80
  edge_keys = []
@@ -85,7 +88,7 @@ def spin_glass_solver(G, quality=1, best_guess=None):
85
88
  improved = True
86
89
  while improved:
87
90
  improved = False
88
- for k in range(1, quality + 1):
91
+ for k in range(1, max(2, quality - 2)):
89
92
  if n_qubits < k:
90
93
  break
91
94
 
@@ -21,13 +21,16 @@ def get_best_stitch(adjacency, terminals_a, terminals_b, is_cyclic):
21
21
  return best_weight, best_edge
22
22
 
23
23
 
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
-
24
+ def tsp_symmetric(G, quality=1, is_cyclic=True, start_node=None):
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,29 @@ 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
+ G_a.add_nodes_from(a)
50
+ G_b.add_nodes_from(b)
49
51
  for u, v, data in G.edges(data=True):
50
- weight = data.get("weight", 1.0)
51
52
  if (u in a) and (v in a):
52
- G_a.add_edge(a.index(u), a.index(v), weight=weight)
53
- continue
54
- elif (u in b) and (v in b):
55
- G_b.add_edge(b.index(u), b.index(v), weight=weight)
53
+ G_a.add_edge(u, v, weight=data.get("weight", 1.0))
56
54
  continue
57
55
 
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]
56
+ if (u in b) and (v in b):
57
+ G_b.add_edge(u, v, weight=data.get("weight", 1.0))
69
58
 
70
- path_a = [a[idx] for idx in sol_a[0]]
71
- path_b = [b[idx] for idx in sol_b[0]]
59
+ sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False)
60
+ sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False)
61
+
62
+ path_a = sol_a[0]
63
+ path_b = sol_b[0]
64
+
65
+ sol_weight = sol_a[1] + sol_b[1]
72
66
 
73
67
  terminals_a = (path_a[0], path_a[-1])
74
68
  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.6
3
+ Version: 1.11.0
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
@@ -46,10 +46,10 @@ from PyQrackIsing import maxcut_tfim
46
46
  import networkx as nx
47
47
 
48
48
  G = nx.petersen_graph()
49
- best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=12)
49
+ best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=8)
50
50
  ```
51
51
 
52
- The (integer) `quality` setting is optional, with a default value of `12`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
52
+ The (integer) `quality` setting is optional, with a default value of `4`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
53
53
 
54
54
  Based on a combination of the TFIM-inspired MAXCUT solver and another technique for finding ground-state energy in quantum chemistry that we call the _"binary Clifford eigensolver,"_ we also provide an (approximate) spin glass ground-state solver:
55
55
  ```py
@@ -69,10 +69,10 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
69
69
 
70
70
 
71
71
  G = generate_spin_glass_graph(n_nodes=64, seed=42)
72
- solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=2, best_guess=None)
73
- # solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=12)[1])
72
+ solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=8, best_guess=None)
73
+ # solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=8)[0])
74
74
  ```
75
- The `quality` setting default is `2`, and overhead grows roughly like the qubit count to the power of `quality` (so `O(n ** 2)` for default). `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). By default, `spin_glass_solver()` uses `maxcut_tfim(G, quality=0)` as `best_guess`, which typically works well, but it could be seeded with higher `maxcut_tfim()` `quality` or Goemans-Williamson, for example. This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
75
+ The (integer) `quality` setting is optional, with a default value of `4`. `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). By default, `spin_glass_solver()` uses `maxcut_tfim(G)` with passed-through `quality` as `best_guess`, which typically works well, but it could be seeded with higher `maxcut_tfim()` `quality` or Goemans-Williamson, for example. This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
76
76
 
77
77
  From the `spin_glass_solver()`, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
78
78
  ```py
@@ -93,13 +93,13 @@ def generate_tsp_graph(n_nodes=64, seed=None):
93
93
 
94
94
  n_nodes = 128
95
95
  G = generate_tsp_graph(n_nodes=n_nodes, seed=42)
96
- circuit, path_length = tsp_symmetric(G, quality=2, is_cyclic=True, start_node=None)
96
+ circuit, path_length = tsp_symmetric(G, quality=8, is_cyclic=True, start_node=None)
97
97
 
98
98
  print(f"Node count: {n_nodes}")
99
99
  print(f"Path: {circuit}")
100
100
  print(f"Path length: {path_length}")
101
101
  ```
102
- We only provide a solver for the symmetric version of the TSP (i.e., the distance from "A" to "B" is considered the same as from "B" to "A"). `is_cyclic` controls whether the path must ultimately complete a circuit, if `True`, or a one-way trip, if `False`. When the desired route is acyclic, a fixed starting point can optionally be specified via passing the node label to `start_node` (or, without this parameter, the solver will also attempt to pick an optimal starting point).
102
+ The (integer) `quality` setting is optional, with a default value of `1`. We only provide a solver for the symmetric version of the TSP (i.e., the distance from "A" to "B" is considered the same as from "B" to "A"). `is_cyclic` controls whether the path must ultimately complete a circuit, if `True`, or a one-way trip, if `False`. When the desired route is acyclic, a fixed starting point can optionally be specified via passing the node label to `start_node` (or, without this parameter, the solver will also attempt to pick an optimal starting point).
103
103
 
104
104
  ## About
105
105
  Transverse field Ising model (TFIM) is the basis of most claimed algorithmic "quantum advantage," circa 2025, with the notable exception of Shor's integer factoring algorithm.
@@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
11
11
 
12
12
  [project]
13
13
  name = "PyQrackIsing"
14
- version = "1.10.6"
14
+ version = "1.11.0"
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.6',
54
+ version='1.11.0',
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