PyQrackIsing 9.11.0__tar.gz → 9.11.1__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-9.11.0/pyqrackising.egg-info → pyqrackising-9.11.1}/PKG-INFO +1 -1
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/README.md +1 -1
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyproject.toml +1 -1
- pyqrackising-9.11.1/pyqrackising/generate_tfim_samples.py +236 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/setup.py +1 -1
- pyqrackising-9.11.0/pyqrackising/generate_tfim_samples.py +0 -202
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/LICENSE.md +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/MANIFEST.in +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/otoc.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/tsp.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising.egg-info/requires.txt +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-9.11.0 → pyqrackising-9.11.1}/setup.cfg +0 -0
|
@@ -3,7 +3,7 @@ Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising
|
|
|
3
3
|
|
|
4
4
|
(It's "the **Ising** on top.")
|
|
5
5
|
|
|
6
|
-
[](https://doi.org/10.5281/zenodo.18706638) [](https://pepy.tech/projects/pyqrackising)
|
|
7
7
|
|
|
8
8
|
## Copyright and license
|
|
9
9
|
(c) Daniel Strano and the Qrack contributors 2025. All rights reserved.
|
|
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
|
|
|
7
7
|
|
|
8
8
|
[project]
|
|
9
9
|
name = "pyqrackising"
|
|
10
|
-
version = "9.11.
|
|
10
|
+
version = "9.11.1"
|
|
11
11
|
requires-python = ">=3.8"
|
|
12
12
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
13
13
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
from .maxcut_tfim_util import probability_by_hamming_weight, sample_mag, opencl_context
|
|
2
|
+
import itertools
|
|
3
|
+
import math
|
|
4
|
+
import random
|
|
5
|
+
import sys
|
|
6
|
+
import numpy as np
|
|
7
|
+
from numba import njit
|
|
8
|
+
|
|
9
|
+
from collections import Counter
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
epsilon = opencl_context.epsilon
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# Fixed-Hamming-weight Kawasaki swaps with acceptance probability, by Elara
|
|
16
|
+
def random_fixed_hamming_state(n_qubits, h):
|
|
17
|
+
bits = np.zeros(n_qubits, dtype=np.bool_)
|
|
18
|
+
bits[:h] = True
|
|
19
|
+
np.random.shuffle(bits)
|
|
20
|
+
state = 0
|
|
21
|
+
for b in bits:
|
|
22
|
+
state <<= 1
|
|
23
|
+
if b:
|
|
24
|
+
state |= 1
|
|
25
|
+
|
|
26
|
+
return state
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def build_neighbors(n_rows, n_cols):
|
|
30
|
+
L = n_rows * n_cols
|
|
31
|
+
neighbors = [[] for _ in range(L)]
|
|
32
|
+
|
|
33
|
+
for i in range(n_rows):
|
|
34
|
+
for j in range(n_cols):
|
|
35
|
+
idx = i * n_cols + j
|
|
36
|
+
|
|
37
|
+
right = i * n_cols + ((j + 1) % n_cols)
|
|
38
|
+
left = i * n_cols + ((j - 1) % n_cols)
|
|
39
|
+
down = ((i + 1) % n_rows) * n_cols + j
|
|
40
|
+
up = ((i - 1) % n_rows) * n_cols + j
|
|
41
|
+
|
|
42
|
+
neighbors[idx] = [right, left, down, up]
|
|
43
|
+
|
|
44
|
+
return neighbors
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def count_like_edges(state, neighbors):
|
|
48
|
+
like = 0
|
|
49
|
+
visited = set()
|
|
50
|
+
|
|
51
|
+
for i, nbrs in enumerate(neighbors):
|
|
52
|
+
si = (state >> i) & 1
|
|
53
|
+
for j in nbrs:
|
|
54
|
+
if (j, i) in visited:
|
|
55
|
+
continue
|
|
56
|
+
sj = (state >> j) & 1
|
|
57
|
+
if si == sj:
|
|
58
|
+
like += 1
|
|
59
|
+
visited.add((i, j))
|
|
60
|
+
|
|
61
|
+
return like
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def delta_like_edges(state, i, j, neighbors):
|
|
65
|
+
si = (state >> i) & 1
|
|
66
|
+
sj = (state >> j) & 1
|
|
67
|
+
|
|
68
|
+
delta = 0
|
|
69
|
+
|
|
70
|
+
for idx in [i, j]:
|
|
71
|
+
s_old = (state >> idx) & 1
|
|
72
|
+
|
|
73
|
+
for nbr in neighbors[idx]:
|
|
74
|
+
if nbr == i or nbr == j:
|
|
75
|
+
continue
|
|
76
|
+
|
|
77
|
+
s_nbr = (state >> nbr) & 1
|
|
78
|
+
|
|
79
|
+
old_like = s_old == s_nbr
|
|
80
|
+
new_spin = sj if idx == i else si
|
|
81
|
+
new_like = new_spin == s_nbr
|
|
82
|
+
|
|
83
|
+
delta += int(new_like) - int(old_like)
|
|
84
|
+
|
|
85
|
+
# handle edge between i and j if neighbors
|
|
86
|
+
if j in neighbors[i]:
|
|
87
|
+
old_like = si == sj
|
|
88
|
+
new_like = sj == si
|
|
89
|
+
delta += int(new_like) - int(old_like)
|
|
90
|
+
|
|
91
|
+
return delta
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def sample_fixed_hamming_weight(h_weight, count, n_rows, n_cols, burnin=10):
|
|
95
|
+
if count == 0:
|
|
96
|
+
return []
|
|
97
|
+
|
|
98
|
+
n_qubits = n_rows * n_cols
|
|
99
|
+
neighbors = build_neighbors(n_rows, n_cols)
|
|
100
|
+
|
|
101
|
+
like_edges = 0
|
|
102
|
+
while like_edges == 0:
|
|
103
|
+
state = random_fixed_hamming_state(n_qubits, h_weight)
|
|
104
|
+
like_edges = count_like_edges(state, neighbors)
|
|
105
|
+
|
|
106
|
+
samples = []
|
|
107
|
+
|
|
108
|
+
total_steps = burnin * count + count
|
|
109
|
+
|
|
110
|
+
for step in range(total_steps):
|
|
111
|
+
# choose random 1-bit and 0-bit
|
|
112
|
+
ones = [i for i in range(n_qubits) if (state >> i) & 1]
|
|
113
|
+
zeros = [i for i in range(n_qubits) if not (state >> i) & 1]
|
|
114
|
+
|
|
115
|
+
i = random.choice(ones)
|
|
116
|
+
j = random.choice(zeros)
|
|
117
|
+
|
|
118
|
+
delta = delta_like_edges(state, i, j, neighbors)
|
|
119
|
+
new_like = like_edges + delta
|
|
120
|
+
|
|
121
|
+
if new_like > 0:
|
|
122
|
+
accept_prob = new_like / like_edges
|
|
123
|
+
if np.random.random() < accept_prob:
|
|
124
|
+
state ^= 1 << i
|
|
125
|
+
state ^= 1 << j
|
|
126
|
+
like_edges = new_like
|
|
127
|
+
|
|
128
|
+
if step >= burnin * count:
|
|
129
|
+
samples.append(state)
|
|
130
|
+
|
|
131
|
+
return samples
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
@njit
|
|
135
|
+
def factor_width(width, is_transpose=False):
|
|
136
|
+
col_len = math.floor(math.sqrt(width))
|
|
137
|
+
while ((width // col_len) * col_len) != width:
|
|
138
|
+
col_len -= 1
|
|
139
|
+
row_len = width // col_len
|
|
140
|
+
|
|
141
|
+
return (col_len, row_len) if is_transpose else (row_len, col_len)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@njit
|
|
145
|
+
def comb(n, k):
|
|
146
|
+
if (k < 0) or (k > n):
|
|
147
|
+
return 0
|
|
148
|
+
if (k == 0) or (k == n):
|
|
149
|
+
return 1
|
|
150
|
+
res = 1
|
|
151
|
+
for i in range(1, k + 1):
|
|
152
|
+
res = res * (n - k + i) // i
|
|
153
|
+
return res
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@njit
|
|
157
|
+
def fix_cdf(hamming_prob):
|
|
158
|
+
tot_prob = 0.0
|
|
159
|
+
n_bias = len(hamming_prob)
|
|
160
|
+
cum_prob = np.empty(n_bias, dtype=np.float64)
|
|
161
|
+
for i in range(n_bias):
|
|
162
|
+
tot_prob += hamming_prob[i]
|
|
163
|
+
cum_prob[i] = tot_prob
|
|
164
|
+
cum_prob[-1] = 1.0
|
|
165
|
+
|
|
166
|
+
return cum_prob
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@njit
|
|
170
|
+
def get_tfim_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, omega=1.5 * np.pi):
|
|
171
|
+
if abs(t) <= epsilon:
|
|
172
|
+
p = (1.0 - np.cos(theta)) / 2.0
|
|
173
|
+
bias = np.empty(n_qubits + 1, dtype=np.float64)
|
|
174
|
+
for k in range(n_qubits + 1):
|
|
175
|
+
bias[k] = comb(n_qubits, k) * (p**k) * ((1.0 - p) ** (n_qubits - k))
|
|
176
|
+
|
|
177
|
+
return bias / bias.sum()
|
|
178
|
+
|
|
179
|
+
if abs(h) <= epsilon:
|
|
180
|
+
bias = np.empty(n_qubits + 1, dtype=np.float64)
|
|
181
|
+
if J > 0:
|
|
182
|
+
bias[-1] = 1.0
|
|
183
|
+
else:
|
|
184
|
+
bias[0] = 1.0
|
|
185
|
+
return bias
|
|
186
|
+
|
|
187
|
+
bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1, normalized=True, omega=omega)
|
|
188
|
+
|
|
189
|
+
return bias / bias.sum()
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def generate_tfim_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100):
|
|
193
|
+
samples = []
|
|
194
|
+
|
|
195
|
+
if abs(t) <= epsilon:
|
|
196
|
+
prob = (1.0 - np.cos(theta)) / 2.0
|
|
197
|
+
for s in range(shots):
|
|
198
|
+
sample = 0
|
|
199
|
+
for q in range(n_qubits):
|
|
200
|
+
sample <<= 1
|
|
201
|
+
if np.random.random() < prob:
|
|
202
|
+
sample |= 1
|
|
203
|
+
samples.append(sample)
|
|
204
|
+
|
|
205
|
+
return samples
|
|
206
|
+
|
|
207
|
+
n_rows, n_cols = factor_width(n_qubits)
|
|
208
|
+
|
|
209
|
+
# First dimension: Hamming weight
|
|
210
|
+
bias = get_tfim_hamming_distribution(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits)
|
|
211
|
+
counts = np.random.multinomial(shots, bias)
|
|
212
|
+
|
|
213
|
+
samples += [0] * counts[0]
|
|
214
|
+
samples += [(1 << n_qubits) - 1] * counts[-1]
|
|
215
|
+
if n_qubits > 1:
|
|
216
|
+
samples += [int(1 << np.random.randint(n_qubits)) for _ in range(counts[1])]
|
|
217
|
+
if n_qubits > 2:
|
|
218
|
+
mask = (1 << n_qubits) - 1
|
|
219
|
+
samples += [(mask ^ int(1 << np.random.randint(n_qubits))) for _ in range(counts[-2])]
|
|
220
|
+
|
|
221
|
+
# Second dimension: magnetic localization
|
|
222
|
+
for h_weight in range(2, len(bias) - 2):
|
|
223
|
+
samples += sample_fixed_hamming_weight(h_weight, counts[h_weight], n_rows, n_cols)
|
|
224
|
+
|
|
225
|
+
np.random.shuffle(samples)
|
|
226
|
+
|
|
227
|
+
return samples
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def generate_fermi_hubbard_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100, omega=1.5 * np.pi):
|
|
231
|
+
shots_x, shots_y, shots_z = np.random.multinomial(shots, [1 / 3, 1 / 3, 1 / 3])
|
|
232
|
+
return (
|
|
233
|
+
generate_tfim_samples(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, shots=shots_z)
|
|
234
|
+
+ generate_tfim_samples(J=-h, h=-J, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_x)
|
|
235
|
+
+ generate_tfim_samples(J=J, h=h, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_y)
|
|
236
|
+
)
|
|
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
|
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name="pyqrackising",
|
|
10
|
-
version="9.11.
|
|
10
|
+
version="9.11.1",
|
|
11
11
|
author="Dan Strano",
|
|
12
12
|
author_email="stranoj@gmail.com",
|
|
13
13
|
description="Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)",
|
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
from .maxcut_tfim_util import probability_by_hamming_weight, sample_mag, opencl_context
|
|
2
|
-
import itertools
|
|
3
|
-
import math
|
|
4
|
-
import sys
|
|
5
|
-
import numpy as np
|
|
6
|
-
from numba import njit
|
|
7
|
-
|
|
8
|
-
from collections import Counter
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
epsilon = opencl_context.epsilon
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
@njit
|
|
15
|
-
def factor_width(width, is_transpose=False):
|
|
16
|
-
col_len = math.floor(math.sqrt(width))
|
|
17
|
-
while ((width // col_len) * col_len) != width:
|
|
18
|
-
col_len -= 1
|
|
19
|
-
row_len = width // col_len
|
|
20
|
-
|
|
21
|
-
return (col_len, row_len) if is_transpose else (row_len, col_len)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
# By Gemini (Google Search AI)
|
|
25
|
-
def int_to_bitstring(integer, length):
|
|
26
|
-
return bin(integer)[2:].zfill(length)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
# Drafted by Elara (OpenAI custom GPT), improved by Dan Strano
|
|
30
|
-
def closeness_like_bits(perm, n_rows, n_cols):
|
|
31
|
-
"""
|
|
32
|
-
Compute closeness-of-like-bits metric C(state) for a given bitstring on an LxL toroidal grid.
|
|
33
|
-
|
|
34
|
-
Parameters:
|
|
35
|
-
perm: integer representing basis state, bit-length n_rows * n_cols
|
|
36
|
-
n_rows: row count of torus
|
|
37
|
-
n_cols: column count of torus
|
|
38
|
-
|
|
39
|
-
Returns:
|
|
40
|
-
normalized_closeness: float, in [-1, +1]
|
|
41
|
-
+1 means all neighbors are like-like, -1 means all neighbors are unlike
|
|
42
|
-
"""
|
|
43
|
-
# reshape the bitstring into LxL grid
|
|
44
|
-
bitstring = list(int_to_bitstring(perm, n_rows * n_cols))
|
|
45
|
-
grid = np.array(bitstring).reshape((n_rows, n_cols))
|
|
46
|
-
total_edges = 0
|
|
47
|
-
like_count = 0
|
|
48
|
-
|
|
49
|
-
# iterate over each site, count neighbors (right and down to avoid double-count)
|
|
50
|
-
for i in range(n_rows):
|
|
51
|
-
for j in range(n_cols):
|
|
52
|
-
s = grid[i, j]
|
|
53
|
-
|
|
54
|
-
# right neighbor (wrap around)
|
|
55
|
-
s_right = grid[i, (j + 1) % n_cols]
|
|
56
|
-
like_count += 1 if s == s_right else -1
|
|
57
|
-
total_edges += 1
|
|
58
|
-
|
|
59
|
-
# down neighbor (wrap around)
|
|
60
|
-
s_down = grid[(i + 1) % n_rows, j]
|
|
61
|
-
like_count += 1 if s == s_down else -1
|
|
62
|
-
total_edges += 1
|
|
63
|
-
|
|
64
|
-
# normalize
|
|
65
|
-
normalized_closeness = like_count / total_edges
|
|
66
|
-
return normalized_closeness
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
@njit
|
|
70
|
-
def comb(n, k):
|
|
71
|
-
if (k < 0) or (k > n):
|
|
72
|
-
return 0
|
|
73
|
-
if (k == 0) or (k == n):
|
|
74
|
-
return 1
|
|
75
|
-
res = 1
|
|
76
|
-
for i in range(1, k + 1):
|
|
77
|
-
res = res * (n - k + i) // i
|
|
78
|
-
return res
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
@njit
|
|
82
|
-
def expected_closeness_weight(n_rows, n_cols, hamming_weight):
|
|
83
|
-
L = n_rows * n_cols
|
|
84
|
-
same_pairs = comb(hamming_weight, 2) + comb(L - hamming_weight, 2)
|
|
85
|
-
total_pairs = comb(L, 2)
|
|
86
|
-
mu_k = same_pairs / total_pairs
|
|
87
|
-
return 2.0 * mu_k - 1.0
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
@njit
|
|
91
|
-
def sample_hamming_weight(thresholds, shots):
|
|
92
|
-
hamming_samples = np.zeros(shots, dtype=np.int32)
|
|
93
|
-
for s in range(shots):
|
|
94
|
-
hamming_samples[s] = sample_mag(thresholds)
|
|
95
|
-
|
|
96
|
-
return hamming_samples
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
@njit
|
|
100
|
-
def fix_cdf(hamming_prob):
|
|
101
|
-
tot_prob = 0.0
|
|
102
|
-
n_bias = len(hamming_prob)
|
|
103
|
-
cum_prob = np.empty(n_bias, dtype=np.float64)
|
|
104
|
-
for i in range(n_bias):
|
|
105
|
-
tot_prob += hamming_prob[i]
|
|
106
|
-
cum_prob[i] = tot_prob
|
|
107
|
-
cum_prob[-1] = 1.0
|
|
108
|
-
|
|
109
|
-
return cum_prob
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
@njit
|
|
113
|
-
def get_tfim_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, omega=1.5 * np.pi):
|
|
114
|
-
if abs(t) <= epsilon:
|
|
115
|
-
p = (1.0 - np.cos(theta)) / 2.0
|
|
116
|
-
bias = np.empty(n_qubits + 1, dtype=np.float64)
|
|
117
|
-
for k in range(n_qubits + 1):
|
|
118
|
-
bias[k] = comb(n_qubits, k) * (p**k) * ((1.0 - p) ** (n_qubits - k))
|
|
119
|
-
|
|
120
|
-
return bias / bias.sum()
|
|
121
|
-
|
|
122
|
-
if abs(h) <= epsilon:
|
|
123
|
-
bias = np.empty(n_qubits + 1, dtype=np.float64)
|
|
124
|
-
if J > 0:
|
|
125
|
-
bias[-1] = 1.0
|
|
126
|
-
else:
|
|
127
|
-
bias[0] = 1.0
|
|
128
|
-
return bias
|
|
129
|
-
|
|
130
|
-
bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1, normalized=True, omega=omega)
|
|
131
|
-
|
|
132
|
-
return bias / bias.sum()
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
def generate_tfim_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100, omega=1.5 * np.pi):
|
|
136
|
-
samples = []
|
|
137
|
-
|
|
138
|
-
if abs(t) <= epsilon:
|
|
139
|
-
prob = (1.0 - np.cos(theta)) / 2.0
|
|
140
|
-
for s in range(shots):
|
|
141
|
-
sample = 0
|
|
142
|
-
for q in range(n_qubits):
|
|
143
|
-
sample <<= 1
|
|
144
|
-
if np.random.random() < prob:
|
|
145
|
-
sample |= 1
|
|
146
|
-
samples.append(sample)
|
|
147
|
-
|
|
148
|
-
return samples
|
|
149
|
-
|
|
150
|
-
n_rows, n_cols = factor_width(n_qubits)
|
|
151
|
-
|
|
152
|
-
# First dimension: Hamming weight
|
|
153
|
-
bias = get_tfim_hamming_distribution(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, omega=omega)
|
|
154
|
-
thresholds = fix_cdf(bias)
|
|
155
|
-
hamming_samples = dict(Counter(sample_hamming_weight(thresholds, shots)))
|
|
156
|
-
|
|
157
|
-
for h_weight, count in hamming_samples.items():
|
|
158
|
-
if h_weight == 0:
|
|
159
|
-
samples += count * [0]
|
|
160
|
-
continue
|
|
161
|
-
|
|
162
|
-
if h_weight == n_qubits:
|
|
163
|
-
samples += count * [(1 << n_qubits) - 1]
|
|
164
|
-
continue
|
|
165
|
-
|
|
166
|
-
rands = [np.random.random() for _ in range(count)]
|
|
167
|
-
rands.sort()
|
|
168
|
-
state_int = 0
|
|
169
|
-
tot_prob = 0
|
|
170
|
-
s = 0
|
|
171
|
-
# How closely grouped are "like" bits to "like"?
|
|
172
|
-
expected_closeness = expected_closeness_weight(n_rows, n_cols, h_weight)
|
|
173
|
-
h_weight_combos = math.comb(n_qubits, h_weight)
|
|
174
|
-
for combo in itertools.combinations(range(n_qubits), h_weight):
|
|
175
|
-
state_int = sum(1 << pos for pos in combo)
|
|
176
|
-
# When we add all "closeness" possibilities for the particular Hamming weight, we should maintain the (n+1) mean probability dimensions.
|
|
177
|
-
normed_closeness = (1 + closeness_like_bits(state_int, n_rows, n_cols)) / (1 + expected_closeness)
|
|
178
|
-
# Use a normalized weighted average that favors the (n+1)-dimensional model at later times.
|
|
179
|
-
# The (n+1)-dimensional marginal probability is the product of a function of Hamming weight and "closeness," split among all basis states with that specific Hamming weight.
|
|
180
|
-
tot_prob += normed_closeness / h_weight_combos
|
|
181
|
-
while rands[s] <= tot_prob:
|
|
182
|
-
samples.append(state_int)
|
|
183
|
-
s += 1
|
|
184
|
-
if s == count:
|
|
185
|
-
break
|
|
186
|
-
if s == count:
|
|
187
|
-
break
|
|
188
|
-
if s < count:
|
|
189
|
-
samples += (count - s) * [state_int]
|
|
190
|
-
|
|
191
|
-
np.random.shuffle(samples)
|
|
192
|
-
|
|
193
|
-
return samples
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
def generate_fermi_hubbard_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100, omega=1.5 * np.pi):
|
|
197
|
-
shots_x, shots_y, shots_z = np.random.multinomial(shots, [1 / 3, 1 / 3, 1 / 3])
|
|
198
|
-
return (
|
|
199
|
-
generate_tfim_samples(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, shots=shots_z)
|
|
200
|
-
+ generate_tfim_samples(J=-h, h=-J, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_x)
|
|
201
|
-
+ generate_tfim_samples(J=J, h=h, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_y)
|
|
202
|
-
)
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|