PyQrackIsing 1.10.7__tar.gz → 1.12.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.7/PyQrackIsing.egg-info → pyqrackising-1.12.0}/PKG-INFO +1 -1
  2. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/PyQrackIsing/maxcut_tfim.py +59 -53
  3. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/PyQrackIsing/spin_glass_solver.py +3 -3
  4. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/PyQrackIsing/tsp_symmetric.py +9 -8
  5. {pyqrackising-1.10.7 → pyqrackising-1.12.0/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  6. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/README.md +7 -7
  7. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/pyproject.toml +1 -1
  8. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/setup.py +1 -1
  9. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/CMakeLists.txt +0 -0
  10. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/LICENSE.md +0 -0
  11. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/MANIFEST.in +0 -0
  12. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/PyQrackIsing/__init__.py +0 -0
  13. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  14. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/PyQrackIsing/generate_tfim_samples.py +0 -0
  15. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/PyQrackIsing/tfim_magnetization.py +0 -0
  16. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  17. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  18. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  19. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  20. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/PyQrackIsing.egg-info/top_level.txt +0 -0
  21. {pyqrackising-1.10.7 → pyqrackising-1.12.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.10.7
3
+ Version: 1.12.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,36 +5,34 @@ 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
- bias = np.zeros(n_qubits + 1)
9
- if np.isclose(abs(h), 0.0):
10
- bias[0] = 1.0
11
- elif np.isclose(abs(J), 0.0):
12
- bias.fill(1.0 / (n_qubits + 1.0))
13
- else:
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
- )
24
- p = (
25
- pow(2.0, abs(J / h) - 1.0)
26
- * (1.0 + np.sin(theta - theta_c) * np.cos(1.5 * np.pi * J * t + theta) / (1.0 + np.sqrt(t)))
27
- - 0.5
8
+ bias = np.zeros(n_qubits - 1)
9
+
10
+ # critical angle
11
+ theta_c = np.arcsin(
12
+ max(
13
+ -1.0,
14
+ min(
15
+ 1.0,
16
+ (1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h) / (z * J)),
17
+ ),
28
18
  )
29
- if (p * n_qubits) >= 1024:
30
- bias[0] = 1.0
31
- else:
32
- tot_n = 0.0
33
- for q in range(n_qubits + 1):
34
- n = 1.0 / pow(2.0, p * q)
35
- bias[q] = n
36
- tot_n += n
37
- bias /= tot_n
19
+ )
20
+
21
+ p = (
22
+ pow(2.0, abs(J / h) - 1.0)
23
+ * (1.0 + np.sin(theta - theta_c) * np.cos(1.5 * np.pi * J * t + theta) / (1.0 + np.sqrt(t)))
24
+ - 0.5
25
+ )
26
+
27
+ if (p * n_qubits) >= 1024:
28
+ return bias
29
+
30
+ tot_n = 1.0 + 1.0 / pow(2.0, p * n_qubits)
31
+ for q in range(1, n_qubits):
32
+ n = 1.0 / pow(2.0, p * q)
33
+ bias[q] = n
34
+ tot_n += n
35
+ bias /= tot_n
38
36
 
39
37
  if J > 0.0:
40
38
  return bias[::-1]
@@ -43,40 +41,48 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
43
41
 
44
42
 
45
43
  @njit(parallel=True)
46
- def maxcut_hamming_cdf(n_qubits, J_func, degrees, mult_log2):
44
+ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality):
47
45
  if n_qubits < 2:
48
46
  return np.empty(0, dtype=np.float64)
49
47
 
50
- n_steps = n_qubits << mult_log2
51
- shots = n_qubits << mult_log2
52
- delta_t = 1.0 / (n_steps << (mult_log2 >> 1))
53
- tot_t = (n_steps - 1) * delta_t
54
- h_mult = (1 << (mult_log2 >> 1)) / tot_t
48
+ n_steps = 1 << quality
49
+ delta_t = 1.0 / n_steps
50
+ tot_t = n_steps * delta_t
51
+ h_mult = 32.0 / tot_t
55
52
  n_bias = n_qubits - 1
56
- hamming_prob = np.zeros(n_bias)
57
-
53
+ hamming_prob = np.full(n_bias, 1 / n_bias)
54
+ theta = np.zeros(n_qubits)
58
55
  for q in prange(n_qubits):
56
+ J = J_func[q]
59
57
  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]
58
+ theta[q] = np.arcsin(
59
+ max(
60
+ -1.0,
61
+ min(
62
+ 1.0,
63
+ (1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
64
+ ),
65
+ )
66
+ )
64
67
 
65
- for step in range(1, n_steps):
68
+ for qc in prange(n_qubits, n_steps * n_qubits):
69
+ step = qc // n_qubits
70
+ q = qc % n_qubits
71
+ J_eff = J_func[q]
72
+ if np.isclose(abs(J_eff), 0.0):
73
+ continue
74
+ z = degrees[q]
75
+ theta_eff = theta[q]
66
76
  t = step * delta_t
67
77
  tm1 = (step - 1) * delta_t
68
- for q in prange(n_qubits):
69
- z = degrees[q]
70
- J_eff = J_func[q]
71
- h_t = h_mult * (tot_t - t)
72
- bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, t, n_qubits)
73
- last_bias = probability_by_hamming_weight(J_eff, h_t, z, 0.0, tm1, n_qubits)
74
- for i in range(n_bias):
75
- hamming_prob[i] += bias[i + 1] - last_bias[i + 1]
78
+ h_t = h_mult * (tot_t - t)
79
+ bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, t, n_qubits)
80
+ last_bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_qubits)
81
+ for i in range(n_bias):
82
+ hamming_prob[i] += bias[i] - last_bias[i]
76
83
 
77
84
  tot_prob = sum(hamming_prob)
78
- for i in prange(n_bias):
79
- hamming_prob[i] /= tot_prob
85
+ hamming_prob /= tot_prob
80
86
 
81
87
  tot_prob = 0.0
82
88
  for i in range(n_bias):
@@ -162,7 +168,7 @@ def int_to_bitstring(integer, length):
162
168
 
163
169
  def maxcut_tfim(
164
170
  G,
165
- quality=12,
171
+ quality=8,
166
172
  shots=None,
167
173
  ):
168
174
  # Number of qubits/nodes
@@ -56,7 +56,7 @@ 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=5, best_guess=None):
60
60
  nodes = list(G.nodes())
61
61
  n_qubits = len(nodes)
62
62
 
@@ -74,7 +74,7 @@ def spin_glass_solver(G, quality=1, best_guess=None):
74
74
  elif isinstance(best_guess, list):
75
75
  bitstring = "".join(["1" if b else "0" for b in best_guess])
76
76
  else:
77
- bitstring, _, _ = maxcut_tfim(G, quality=max(0, quality - 1))
77
+ bitstring, _, _ = maxcut_tfim(G, quality=quality)
78
78
  best_theta = [b == "1" for b in list(bitstring)]
79
79
 
80
80
  edge_keys = []
@@ -88,7 +88,7 @@ def spin_glass_solver(G, quality=1, best_guess=None):
88
88
  improved = True
89
89
  while improved:
90
90
  improved = False
91
- for k in range(1, quality + 1):
91
+ for k in range(1, max(3, quality - 2)):
92
92
  if n_qubits < k:
93
93
  break
94
94
 
@@ -21,7 +21,7 @@ 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):
24
+ def tsp_symmetric(G, quality=1, is_cyclic=True, start_node=None):
25
25
  nodes = list(G.nodes())
26
26
  n_nodes = len(nodes)
27
27
 
@@ -46,17 +46,18 @@ def tsp_symmetric(G, quality=2, is_cyclic=True, start_node=None):
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(u, v, weight=weight)
53
- continue
54
- elif (u in b) and (v in b):
55
- G_b.add_edge(u, v, weight=weight)
53
+ G_a.add_edge(u, v, weight=data.get("weight", 1.0))
56
54
  continue
57
55
 
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)
56
+ if (u in b) and (v in b):
57
+ G_b.add_edge(u, v, weight=data.get("weight", 1.0))
58
+
59
+ sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False)
60
+ sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False)
60
61
 
61
62
  path_a = sol_a[0]
62
63
  path_b = sol_b[0]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.10.7
3
+ Version: 1.12.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 `8`, 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=5, 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 `5`. `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=1, 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.7"
14
+ version = "1.12.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.7',
54
+ version='1.12.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