PyQrackIsing 1.14.10__tar.gz → 1.14.11__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.
- {pyqrackising-1.14.10/PyQrackIsing.egg-info → pyqrackising-1.14.11}/PKG-INFO +1 -1
- pyqrackising-1.14.11/PyQrackIsing/tsp_symmetric.py +129 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11/PyQrackIsing.egg-info}/PKG-INFO +1 -1
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/pyproject.toml +1 -1
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/setup.py +1 -1
- pyqrackising-1.14.10/PyQrackIsing/tsp_symmetric.py +0 -79
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/CMakeLists.txt +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/LICENSE.md +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/MANIFEST.in +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/PyQrackIsing/__init__.py +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/PyQrackIsing/generate_tfim_samples.py +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/PyQrackIsing/maxcut_tfim.py +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/PyQrackIsing/spin_glass_solver.py +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/PyQrackIsing/tfim_magnetization.py +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/PyQrackIsing/tfim_square_magnetization.py +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/PyQrackIsing.egg-info/not-zip-safe +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/PyQrackIsing.egg-info/top_level.txt +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/README.md +0 -0
- {pyqrackising-1.14.10 → pyqrackising-1.14.11}/setup.cfg +0 -0
|
@@ -0,0 +1,129 @@
|
|
|
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]].get("weight", 1.0))
|
|
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
|
+
single = None
|
|
69
|
+
is_single_a = len(path_a) == 1
|
|
70
|
+
is_single_b = len(path_b) == 1
|
|
71
|
+
|
|
72
|
+
if is_single_a and is_single_b:
|
|
73
|
+
return (path_a + path_b, sol_weight + G[path_a[0]][path_b[0]].get("weight", 1.0))
|
|
74
|
+
|
|
75
|
+
singlet = None
|
|
76
|
+
bulk = None
|
|
77
|
+
if is_single_a:
|
|
78
|
+
singlet = path_a[0]
|
|
79
|
+
bulk = path_b
|
|
80
|
+
elif is_single_b:
|
|
81
|
+
singlet = path_b[0]
|
|
82
|
+
bulk = path_a
|
|
83
|
+
|
|
84
|
+
if not singlet is None:
|
|
85
|
+
best_weight = G[singlet][bulk[0]].get("weight", 1.0)
|
|
86
|
+
best_path = [singlet] + bulk
|
|
87
|
+
weight = G[singlet][bulk[-1]].get("weight", 1.0)
|
|
88
|
+
if weight < best_weight:
|
|
89
|
+
best_weight = weight
|
|
90
|
+
best_path = bulk + [singlet]
|
|
91
|
+
for i in range(len(bulk) - 1):
|
|
92
|
+
weight = (
|
|
93
|
+
G[singlet][bulk[i]].get("weight", 1.0) +
|
|
94
|
+
G[singlet][bulk[i + 1]].get("weight", 1.0) -
|
|
95
|
+
G[bulk[i]][bulk[i + 1]].get("weight", 1.0)
|
|
96
|
+
)
|
|
97
|
+
if weight < best_weight:
|
|
98
|
+
best_weight = weight
|
|
99
|
+
best_path = bulk.copy().insert(singlet, i + 1)
|
|
100
|
+
|
|
101
|
+
return (best_path, sol_weight + best_weight)
|
|
102
|
+
|
|
103
|
+
terminals_a = [path_a[0], path_a[-1]]
|
|
104
|
+
terminals_b = [path_b[0], path_b[-1]]
|
|
105
|
+
|
|
106
|
+
for _ in range(2):
|
|
107
|
+
for _ in range(2):
|
|
108
|
+
best_weight = G[terminals_a[1]][terminals_b[0]].get("weight", 1.0)
|
|
109
|
+
best_path = path_a + path_b
|
|
110
|
+
weight = G[terminals_b[1]][terminals_a[0]].get("weight", 1.0)
|
|
111
|
+
if weight < best_weight:
|
|
112
|
+
best_weight = weight
|
|
113
|
+
best_path = path_b + path_a
|
|
114
|
+
for i in range(len(path_b) - 1):
|
|
115
|
+
weight = (
|
|
116
|
+
G[terminals_a[0]][path_b[i]].get("weight", 1.0) +
|
|
117
|
+
G[terminals_a[1]][path_b[i + 1]].get("weight", 1.0) -
|
|
118
|
+
G[path_b[i]][path_b[i + 1]].get("weight", 1.0)
|
|
119
|
+
)
|
|
120
|
+
if weight < best_weight:
|
|
121
|
+
best_weight = weight
|
|
122
|
+
best_path = path_b.copy()
|
|
123
|
+
best_path[i + 1:i + 1] = path_a
|
|
124
|
+
path_a.reverse()
|
|
125
|
+
terminals_a.reverse()
|
|
126
|
+
path_a, path_b = path_b, path_a
|
|
127
|
+
terminals_a, terminals_b = terminals_b, terminals_a
|
|
128
|
+
|
|
129
|
+
return (best_path, sol_weight + best_weight)
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "PyQrackIsing"
|
|
15
|
-
version = "1.14.
|
|
15
|
+
version = "1.14.11"
|
|
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.
|
|
54
|
+
version='1.14.11',
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|