PyQrackIsing 1.10.7__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.
- {pyqrackising-1.10.7/PyQrackIsing.egg-info → pyqrackising-1.11.0}/PKG-INFO +1 -1
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/PyQrackIsing/maxcut_tfim.py +61 -52
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/PyQrackIsing/spin_glass_solver.py +3 -3
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/PyQrackIsing/tsp_symmetric.py +9 -8
- {pyqrackising-1.10.7 → pyqrackising-1.11.0/PyQrackIsing.egg-info}/PKG-INFO +1 -1
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/README.md +7 -7
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/pyproject.toml +1 -1
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/setup.py +1 -1
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/CMakeLists.txt +0 -0
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/LICENSE.md +0 -0
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/MANIFEST.in +0 -0
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/PyQrackIsing/__init__.py +0 -0
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/PyQrackIsing/generate_tfim_samples.py +0 -0
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/PyQrackIsing/tfim_magnetization.py +0 -0
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/PyQrackIsing/tfim_square_magnetization.py +0 -0
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/PyQrackIsing.egg-info/not-zip-safe +0 -0
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/PyQrackIsing.egg-info/top_level.txt +0 -0
- {pyqrackising-1.10.7 → pyqrackising-1.11.0}/setup.cfg +0 -0
|
@@ -5,36 +5,39 @@ 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
|
-
|
|
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)
|
|
9
12
|
if np.isclose(abs(h), 0.0):
|
|
10
|
-
bias
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
)
|
|
13
|
+
return bias
|
|
14
|
+
|
|
15
|
+
# critical angle
|
|
16
|
+
theta_c = np.arcsin(
|
|
17
|
+
max(
|
|
18
|
+
-1.0,
|
|
19
|
+
min(
|
|
20
|
+
1.0,
|
|
21
|
+
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h) / (z * J)),
|
|
22
|
+
),
|
|
23
23
|
)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
24
|
+
)
|
|
25
|
+
|
|
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
|
+
)
|
|
31
|
+
|
|
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
|
|
38
41
|
|
|
39
42
|
if J > 0.0:
|
|
40
43
|
return bias[::-1]
|
|
@@ -43,40 +46,46 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
|
|
|
43
46
|
|
|
44
47
|
|
|
45
48
|
@njit(parallel=True)
|
|
46
|
-
def maxcut_hamming_cdf(n_qubits, J_func, degrees,
|
|
49
|
+
def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality):
|
|
47
50
|
if n_qubits < 2:
|
|
48
51
|
return np.empty(0, dtype=np.float64)
|
|
49
52
|
|
|
50
|
-
n_steps = n_qubits <<
|
|
51
|
-
|
|
52
|
-
delta_t = 1.0 / (n_steps << (mult_log2 >> 1))
|
|
53
|
+
n_steps = n_qubits << (quality << 1)
|
|
54
|
+
delta_t = 1.0 / (n_steps << quality)
|
|
53
55
|
tot_t = (n_steps - 1) * delta_t
|
|
54
|
-
h_mult = (1 <<
|
|
56
|
+
h_mult = (1 << quality) / tot_t
|
|
55
57
|
n_bias = n_qubits - 1
|
|
56
|
-
hamming_prob = np.
|
|
57
|
-
|
|
58
|
+
hamming_prob = np.full(n_bias, 1 / n_bias)
|
|
59
|
+
theta = np.zeros(n_qubits)
|
|
58
60
|
for q in prange(n_qubits):
|
|
61
|
+
J = J_func[q]
|
|
59
62
|
z = degrees[q]
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
+
)
|
|
64
72
|
|
|
65
|
-
for
|
|
73
|
+
for qc in prange(n_qubits, n_steps * n_qubits):
|
|
74
|
+
step = qc // n_qubits
|
|
75
|
+
q = qc % n_qubits
|
|
66
76
|
t = step * delta_t
|
|
67
77
|
tm1 = (step - 1) * delta_t
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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]
|
|
76
86
|
|
|
77
87
|
tot_prob = sum(hamming_prob)
|
|
78
|
-
|
|
79
|
-
hamming_prob[i] /= tot_prob
|
|
88
|
+
hamming_prob /= tot_prob
|
|
80
89
|
|
|
81
90
|
tot_prob = 0.0
|
|
82
91
|
for i in range(n_bias):
|
|
@@ -162,7 +171,7 @@ def int_to_bitstring(integer, length):
|
|
|
162
171
|
|
|
163
172
|
def maxcut_tfim(
|
|
164
173
|
G,
|
|
165
|
-
quality=
|
|
174
|
+
quality=4,
|
|
166
175
|
shots=None,
|
|
167
176
|
):
|
|
168
177
|
# Number of qubits/nodes
|
|
@@ -177,7 +186,7 @@ def maxcut_tfim(
|
|
|
177
186
|
|
|
178
187
|
if shots is None:
|
|
179
188
|
# Number of measurement shots
|
|
180
|
-
shots = n_qubits << quality
|
|
189
|
+
shots = n_qubits << (quality << 1)
|
|
181
190
|
|
|
182
191
|
J_eff = np.array(
|
|
183
192
|
[
|
|
@@ -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=
|
|
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
|
|
|
@@ -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=
|
|
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
|
|
91
|
+
for k in range(1, max(2, 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=
|
|
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
|
-
|
|
59
|
-
|
|
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]
|
|
@@ -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=
|
|
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 `
|
|
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=
|
|
73
|
-
# solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=
|
|
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
|
|
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=
|
|
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.
|
|
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.
|
|
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
|
|
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
|