PyQrackIsing 1.14.10__tar.gz → 1.14.12__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 (22) hide show
  1. {pyqrackising-1.14.10/PyQrackIsing.egg-info → pyqrackising-1.14.12}/PKG-INFO +1 -1
  2. pyqrackising-1.14.12/PyQrackIsing/tsp_symmetric.py +146 -0
  3. {pyqrackising-1.14.10 → pyqrackising-1.14.12/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  4. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/pyproject.toml +1 -1
  5. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/setup.py +1 -1
  6. pyqrackising-1.14.10/PyQrackIsing/tsp_symmetric.py +0 -79
  7. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/CMakeLists.txt +0 -0
  8. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/LICENSE.md +0 -0
  9. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/MANIFEST.in +0 -0
  10. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/PyQrackIsing/__init__.py +0 -0
  11. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  12. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/PyQrackIsing/generate_tfim_samples.py +0 -0
  13. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/PyQrackIsing/maxcut_tfim.py +0 -0
  14. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/PyQrackIsing/spin_glass_solver.py +0 -0
  15. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/PyQrackIsing/tfim_magnetization.py +0 -0
  16. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  17. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  18. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  19. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  20. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/PyQrackIsing.egg-info/top_level.txt +0 -0
  21. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/README.md +0 -0
  22. {pyqrackising-1.14.10 → pyqrackising-1.14.12}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.14.10
3
+ Version: 1.14.12
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
@@ -0,0 +1,146 @@
1
+ from .spin_glass_solver import spin_glass_solver
2
+ import networkx as nx
3
+
4
+
5
+ # two_opt() written by Elara (OpenAI ChatGPT instance)
6
+ def two_opt(path, G):
7
+ improved = True
8
+ best_path = path
9
+ best_dist = path_length(best_path, G)
10
+
11
+ while improved:
12
+ improved = False
13
+ for i in range(1, len(path) - 2):
14
+ for j in range(i+1, len(path) - 1):
15
+ if j - i == 1: # adjacent edges, skip
16
+ continue
17
+ new_path = best_path[:]
18
+ new_path[i:j] = best_path[j-1:i-1:-1] # reverse segment
19
+ new_dist = path_length(new_path, G)
20
+ if new_dist < best_dist:
21
+ best_path, best_dist = new_path, new_dist
22
+ improved = True
23
+ path = best_path
24
+ return best_path, best_dist
25
+
26
+ def path_length(path, G):
27
+ return sum(G[path[i]][path[i+1]].get("weight", 1.0) for i in range(len(path)-1))
28
+
29
+ def tsp_symmetric(G, quality=0, shots=None, correction_quality=2, is_cyclic=True, start_node=None):
30
+ nodes = list(G.nodes())
31
+ n_nodes = len(nodes)
32
+
33
+ if n_nodes == 0:
34
+ return ([], 0)
35
+ if n_nodes == 1:
36
+ return ([nodes[0]], 0)
37
+ if n_nodes == 2:
38
+ return ([nodes[0], nodes[1]], G[nodes[0]][nodes[1]].get("weight", 1.0))
39
+
40
+ a = []
41
+ b = []
42
+ if not (start_node is None):
43
+ a = [start_node]
44
+ b = nodes
45
+ b.remove(start_node)
46
+ else:
47
+ while (len(a) == 0) or (len(b) == 0):
48
+ bits = ''
49
+ _, _, bits, _ = spin_glass_solver(G, quality=quality, shots=shots, correction_quality=correction_quality)
50
+ a = list(bits[0])
51
+ b = list(bits[1])
52
+
53
+ G_a = nx.Graph()
54
+ G_b = nx.Graph()
55
+ G_a.add_nodes_from(a)
56
+ G_b.add_nodes_from(b)
57
+ for u, v, data in G.edges(data=True):
58
+ if (u in a) and (v in a):
59
+ G_a.add_edge(u, v, weight=data.get("weight", 1.0))
60
+ continue
61
+
62
+ if (u in b) and (v in b):
63
+ G_b.add_edge(u, v, weight=data.get("weight", 1.0))
64
+
65
+ sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False)
66
+ sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False)
67
+
68
+ path_a = sol_a[0]
69
+ path_b = sol_b[0]
70
+
71
+ sol_weight = sol_a[1] + sol_b[1]
72
+
73
+ single = None
74
+ is_single_a = len(path_a) == 1
75
+ is_single_b = len(path_b) == 1
76
+
77
+ if is_single_a and is_single_b:
78
+ return (path_a + path_b, sol_weight + G[path_a[0]][path_b[0]].get("weight", 1.0))
79
+
80
+ singlet = None
81
+ bulk = None
82
+ if is_single_a:
83
+ singlet = path_a[0]
84
+ bulk = path_b
85
+ elif is_single_b:
86
+ singlet = path_b[0]
87
+ bulk = path_a
88
+
89
+ if not singlet is None:
90
+ best_weight = G[singlet][bulk[0]].get("weight", 1.0)
91
+ best_path = [singlet] + bulk
92
+ weight = G[singlet][bulk[-1]].get("weight", 1.0)
93
+ if weight < best_weight:
94
+ best_weight = weight
95
+ best_path = bulk + [singlet]
96
+ for i in range(len(bulk) - 1):
97
+ weight = (
98
+ G[singlet][bulk[i]].get("weight", 1.0) +
99
+ G[singlet][bulk[i + 1]].get("weight", 1.0) -
100
+ G[bulk[i]][bulk[i + 1]].get("weight", 1.0)
101
+ )
102
+ if weight < best_weight:
103
+ best_weight = weight
104
+ best_path = bulk.copy().insert(singlet, i + 1)
105
+
106
+ best_path, best_weight = two_opt(best_path, G)
107
+
108
+ if is_cyclic:
109
+ best_weight += G[best_path[-1]][best_path[0]].get("weight", 1.0)
110
+ best_path += [best_path[0]]
111
+
112
+ return best_path, best_weight
113
+
114
+ terminals_a = [path_a[0], path_a[-1]]
115
+ terminals_b = [path_b[0], path_b[-1]]
116
+
117
+ for _ in range(2):
118
+ for _ in range(2):
119
+ best_weight = G[terminals_a[1]][terminals_b[0]].get("weight", 1.0)
120
+ best_path = path_a + path_b
121
+ weight = G[terminals_b[1]][terminals_a[0]].get("weight", 1.0)
122
+ if weight < best_weight:
123
+ best_weight = weight
124
+ best_path = path_b + path_a
125
+ for i in range(len(path_b) - 1):
126
+ weight = (
127
+ G[terminals_a[0]][path_b[i]].get("weight", 1.0) +
128
+ G[terminals_a[1]][path_b[i + 1]].get("weight", 1.0) -
129
+ G[path_b[i]][path_b[i + 1]].get("weight", 1.0)
130
+ )
131
+ if weight < best_weight:
132
+ best_weight = weight
133
+ best_path = path_b.copy()
134
+ best_path[i + 1:i + 1] = path_a
135
+ path_a.reverse()
136
+ terminals_a.reverse()
137
+ path_a, path_b = path_b, path_a
138
+ terminals_a, terminals_b = terminals_b, terminals_a
139
+
140
+ best_path, best_weight = two_opt(best_path, G)
141
+
142
+ if is_cyclic:
143
+ best_weight += G[best_path[-1]][best_path[0]].get("weight", 1.0)
144
+ best_path += [best_path[0]]
145
+
146
+ return best_path, best_weight
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.14.10
3
+ Version: 1.14.12
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
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "PyQrackIsing"
15
- version = "1.14.10"
15
+ version = "1.14.12"
16
16
  requires-python = ">=3.8"
17
17
  description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
18
18
  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.14.10',
54
+ version='1.14.12',
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)',
@@ -1,79 +0,0 @@
1
- from .spin_glass_solver import spin_glass_solver
2
- import networkx as nx
3
-
4
-
5
- def get_best_stitch(adjacency, terminals_a, terminals_b, is_cyclic):
6
- best_weight = float("inf")
7
- best_edge = None
8
- for a in range(2):
9
- a_term = terminals_a[a]
10
- for b in range(2):
11
- b_term = terminals_b[b]
12
- weight = adjacency[a_term][b_term]["weight"]
13
- if is_cyclic:
14
- n_a_term = terminals_a[0 if a else 1]
15
- n_b_term = terminals_b[0 if b else 1]
16
- weight += adjacency[n_a_term][n_b_term]["weight"]
17
- if weight < best_weight:
18
- best_weight = weight
19
- best_edge = (a, b)
20
-
21
- return best_weight, best_edge
22
-
23
-
24
- def tsp_symmetric(G, quality=0, shots=None, correction_quality=2, is_cyclic=True, start_node=None):
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"])
34
-
35
- a = []
36
- b = []
37
- if not (start_node is None):
38
- a = [start_node]
39
- b = nodes
40
- b.remove(start_node)
41
- else:
42
- while (len(a) == 0) or (len(b) == 0):
43
- bits = ''
44
- _, _, bits, _ = spin_glass_solver(G, quality=quality, shots=shots, correction_quality=correction_quality)
45
- a = list(bits[0])
46
- b = list(bits[1])
47
-
48
- G_a = nx.Graph()
49
- G_b = nx.Graph()
50
- G_a.add_nodes_from(a)
51
- G_b.add_nodes_from(b)
52
- for u, v, data in G.edges(data=True):
53
- if (u in a) and (v in a):
54
- G_a.add_edge(u, v, weight=data.get("weight", 1.0))
55
- continue
56
-
57
- if (u in b) and (v in b):
58
- G_b.add_edge(u, v, weight=data.get("weight", 1.0))
59
-
60
- sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False)
61
- sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False)
62
-
63
- path_a = sol_a[0]
64
- path_b = sol_b[0]
65
-
66
- sol_weight = sol_a[1] + sol_b[1]
67
-
68
- terminals_a = (path_a[0], path_a[-1])
69
- terminals_b = (path_b[0], path_b[-1])
70
-
71
- # Find the best edge to stitch "a" to "b"
72
- best_weight, best_end = get_best_stitch(G, terminals_a, terminals_b, is_cyclic)
73
-
74
- if best_end[0] == 0:
75
- path_a.reverse()
76
- if best_end[1] == 1:
77
- path_b.reverse()
78
-
79
- return (path_a + path_b, sol_weight + best_weight)
File without changes
File without changes