PyQrackIsing 9.2.4__tar.gz → 9.3.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 (28) hide show
  1. {pyqrackising-9.2.4/pyqrackising.egg-info → pyqrackising-9.3.0}/PKG-INFO +1 -1
  2. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/README.md +21 -0
  3. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyproject.toml +1 -1
  4. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/__init__.py +3 -1
  5. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/generate_tfim_samples.py +7 -2
  6. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/maxcut_tfim_util.py +6 -3
  7. pyqrackising-9.3.0/pyqrackising/otoc.py +173 -0
  8. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/tsp.py +3 -4
  9. {pyqrackising-9.2.4 → pyqrackising-9.3.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  10. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising.egg-info/SOURCES.txt +1 -0
  11. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/setup.py +1 -1
  12. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/LICENSE.md +0 -0
  13. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/MANIFEST.in +0 -0
  14. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  15. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/kernels.cl +0 -0
  16. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/maxcut_tfim.py +0 -0
  17. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  18. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  19. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/spin_glass_solver.py +0 -0
  20. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/spin_glass_solver_sparse.py +0 -0
  21. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  22. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/tfim_magnetization.py +0 -0
  23. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  24. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising/tsp_maxcut.py +0 -0
  25. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-9.2.4 → pyqrackising-9.3.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.2.4
3
+ Version: 9.3.0
4
4
  Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -151,6 +151,27 @@ best_partition, best_cut_value = tsp_maxcut_sparse(G, k_neighbors=20, is_optimiz
151
151
 
152
152
  When `is_optimized=True`, the `spin_glass_solver(G)` is used as a final optimization pass. When `is_optimized=False`, this solver becomes entirely serial and can be parallelized over CPU processing elements by user code, easily.
153
153
 
154
+ ## Experimental OTOC sampling
155
+
156
+ ```py
157
+ from pyqrackising import generate_tfim_samples
158
+
159
+ samples = generate_otoc_samples(
160
+ J=-1.0,
161
+ h=2.0,
162
+ z=4,
163
+ theta=0.174532925199432957,
164
+ t=5,
165
+ n_qubits=56,
166
+ cycles=1,
167
+ pauli_string = 'X' + 'I' * 55
168
+ shots=100,
169
+ measurement_basis='Z' * 56
170
+ )
171
+ ```
172
+
173
+ **This function is experimental. It needs systematic validation.** However, we expose it in the public API while we do of the work of testing its validity at qubit counts that are tractable for exact simulation.
174
+
154
175
  ## Environment Variables
155
176
 
156
177
  We expose an environment variable, "`PYQRACKISING_MAX_GPU_PROC_ELEM`", for OpenCL-based solvers. The default value (when the variable is not set) is queried from the OpenCL device properties. You might see performance benefit from tuning this manually to several times your device's number of "compute units" (or tune it down to reduce private memory usage).
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
10
10
 
11
11
  [project]
12
12
  name = "pyqrackising"
13
- version = "9.2.4"
13
+ version = "9.3.0"
14
14
  requires-python = ">=3.8"
15
15
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
16
16
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -1,9 +1,11 @@
1
- from .generate_tfim_samples import generate_tfim_samples
1
+ from .generate_tfim_samples import generate_tfim_samples, get_tfim_hamming_distribution
2
2
  from .tfim_magnetization import tfim_magnetization
3
3
  from .tfim_square_magnetization import tfim_square_magnetization
4
+ from .otoc import generate_otoc_samples, get_otoc_hamming_distribution
4
5
  from .maxcut_tfim import maxcut_tfim
5
6
  from .maxcut_tfim_sparse import maxcut_tfim_sparse
6
7
  from .maxcut_tfim_streaming import maxcut_tfim_streaming
8
+ from .maxcut_tfim_util import probability_by_hamming_weight
7
9
  from .spin_glass_solver import spin_glass_solver
8
10
  from .spin_glass_solver_sparse import spin_glass_solver_sparse
9
11
  from .spin_glass_solver_streaming import spin_glass_solver_streaming
@@ -91,7 +91,6 @@ def sample_hamming_weight(thresholds, shots):
91
91
 
92
92
  @njit
93
93
  def fix_cdf(hamming_prob):
94
- hamming_prob /= hamming_prob.sum()
95
94
  tot_prob = 0.0
96
95
  n_bias = len(hamming_prob)
97
96
  cum_prob = np.empty(n_bias, dtype=np.float64)
@@ -103,13 +102,19 @@ def fix_cdf(hamming_prob):
103
102
  return cum_prob
104
103
 
105
104
 
105
+ @njit
106
+ def get_tfim_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56):
107
+ bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1)
108
+ return bias / bias.sum()
109
+
110
+
106
111
  def generate_tfim_samples(
107
112
  J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100
108
113
  ):
109
114
  n_rows, n_cols = factor_width(n_qubits)
110
115
 
111
116
  # First dimension: Hamming weight
112
- thresholds = fix_cdf(probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1))
117
+ thresholds = fix_cdf(get_tfim_hamming_distribution(J, h, z, theta, t, n_qubits + 1))
113
118
  hamming_samples = sample_hamming_weight(thresholds, shots)
114
119
 
115
120
  samples = []
@@ -411,7 +411,7 @@ def init_thresholds(n_qubits):
411
411
 
412
412
 
413
413
  @njit
414
- def probability_by_hamming_weight(J, h, z, theta, t, n_bias):
414
+ def probability_by_hamming_weight(J, h, z, theta, t, n_bias, normalized=True):
415
415
  zJ = z * J
416
416
  theta_c = ((np.pi if J > 0 else -np.pi) / 2) if abs(zJ) < epsilon else np.arcsin(max(-1.0, min(1.0, h / zJ)))
417
417
 
@@ -435,6 +435,9 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_bias):
435
435
  if J > 0.0:
436
436
  return bias[::-1]
437
437
 
438
+ if normalized:
439
+ bias /= bias.sum()
440
+
438
441
  return bias
439
442
 
440
443
 
@@ -459,8 +462,8 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, tot_t, h_mult):
459
462
  t = step * delta_t
460
463
  tm1 = (step - 1) * delta_t
461
464
  h_t = h_mult * (tot_t - t)
462
- bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, t, n_bias)
463
- last_bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_bias)
465
+ bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, t, n_bias, False)
466
+ last_bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_bias, False)
464
467
  for i in range(n_bias):
465
468
  hamming_prob[i] += bias[i] - last_bias[i]
466
469
 
@@ -0,0 +1,173 @@
1
+ from .maxcut_tfim_util import probability_by_hamming_weight, sample_mag
2
+ from numba import njit
3
+ import numpy as np
4
+ import random
5
+
6
+
7
+ def get_otoc_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, cycles=1, pauli_string = 'X' + 'I' * 55):
8
+ pauli_string = list(pauli_string)
9
+ if len(pauli_string) != n_qubits:
10
+ raise ValueError("OTOCS pauli_string must be same length as n_qubits! (Use 'I' for qubits that aren't changed.)")
11
+
12
+ fwd = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1, False)
13
+ rev = probability_by_hamming_weight(-J, -h, z, -theta, t, n_qubits + 1, False)
14
+ diff_theta = rev - fwd
15
+
16
+ phi = theta - np.pi / 2
17
+ fwd = probability_by_hamming_weight(J, h, z, phi, t, n_qubits + 1, False)
18
+ rev = probability_by_hamming_weight(-J, -h, z, -phi, t, n_qubits + 1, False)
19
+ diff_phi = rev - fwd
20
+
21
+ diff_theta *= cycles
22
+ diff_phi *= cycles
23
+ diff_lam = 0.5 * (diff_theta + diff_phi)
24
+
25
+ n_bias = n_qubits + 1
26
+ diff_z = np.zeros(n_bias, dtype=np.float64)
27
+ diff_x = np.zeros(n_bias, dtype=np.float64)
28
+ diff_y = np.zeros(n_bias, dtype=np.float64)
29
+ for b in pauli_string:
30
+ match b:
31
+ case 'X':
32
+ diff_z += diff_theta
33
+ diff_x += diff_lam
34
+ diff_y += diff_phi
35
+ case 'Z':
36
+ diff_z += diff_phi
37
+ diff_x += diff_theta
38
+ diff_y += diff_lam
39
+ case 'Y':
40
+ diff_z += diff_lam
41
+ diff_x += diff_phi
42
+ diff_y += diff_theta
43
+
44
+ diff_z -= diff_z.min()
45
+ diff_x -= diff_x.min()
46
+ diff_y -= diff_y.min()
47
+
48
+ diff_z[0] += n_bias
49
+ diff_x[0] += n_bias
50
+ diff_y[0] += n_bias
51
+
52
+ diff_z /= diff_z.sum()
53
+ diff_x /= diff_x.sum()
54
+ diff_y /= diff_y.sum()
55
+
56
+ return { 'X': diff_x, 'Y': diff_y, 'Z': diff_z }
57
+
58
+
59
+ @njit
60
+ def fix_cdf(hamming_prob):
61
+ tot_prob = 0.0
62
+ n_bias = len(hamming_prob)
63
+ cum_prob = np.empty(n_bias, dtype=np.float64)
64
+ for i in range(n_bias):
65
+ tot_prob += hamming_prob[i]
66
+ cum_prob[i] = tot_prob
67
+ cum_prob[-1] = 2.0
68
+
69
+ return cum_prob
70
+
71
+
72
+ @njit
73
+ def take_all(b, basis, sample):
74
+ for i in range(len(basis)):
75
+ if basis[i] == b:
76
+ sample |= (1 << i)
77
+
78
+ return sample
79
+
80
+
81
+ def take_sample(b, basis, sample, m):
82
+ indices = []
83
+ for i in range(len(basis)):
84
+ if basis[i] == b:
85
+ indices.append(i)
86
+ indices = random.sample(indices, m)
87
+ for i in indices:
88
+ sample |= 1 << i
89
+
90
+ return sample
91
+
92
+
93
+ def generate_otoc_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, cycles=1, pauli_string = 'X' + 'I' * 55, shots=100, measurement_basis='Z' * 56):
94
+ pauli_string = list(pauli_string)
95
+ if len(pauli_string) != n_qubits:
96
+ raise ValueError("OTOC pauli_string must be same length as n_qubits! (Use 'I' for qubits that aren't changed.)")
97
+
98
+ measurement_basis = list(measurement_basis)
99
+ if len(measurement_basis) != n_qubits:
100
+ raise ValueError("OTOC measurement_basis must be same length as measurement_basis! (Use 'I' for excluded qubits.)")
101
+
102
+ basis_x, basis_y, basis_z = [], [], []
103
+ for b in pauli_string:
104
+ if b == 'Z':
105
+ basis_z.append('X')
106
+ basis_y.append('Z')
107
+ basis_x.append('I')
108
+ elif b == 'X':
109
+ basis_z.append('Z')
110
+ basis_y.append('I')
111
+ basis_x.append('X')
112
+ elif b == 'Y':
113
+ basis_z.append('I')
114
+ basis_y.append('I')
115
+ basis_x.append('X')
116
+ else:
117
+ basis_z.append('I')
118
+ basis_y.append('X')
119
+ basis_x.append('Z')
120
+
121
+ bases = { 'X': basis_x, 'Y': basis_y, 'Z': basis_z }
122
+ thresholds = { key: fix_cdf(value) for key, value in get_otoc_hamming_distribution(J, h, z, theta, t, n_qubits, cycles, pauli_string).items() }
123
+
124
+ samples_3_axis = {}
125
+ for key, value in thresholds.items():
126
+ basis = bases[key]
127
+
128
+ samples = []
129
+ for _ in range(shots):
130
+ sample_3_axis = { 'X': 0, 'Y': 0, 'Z': 0 }
131
+ for key, value in thresholds.items():
132
+ basis = bases[key]
133
+
134
+ # First dimension: Hamming weight
135
+ m = sample_mag(value)
136
+ if m >= n_qubits:
137
+ sample_3_axis[key] = (1 << n_qubits) - 1
138
+ continue
139
+
140
+ # Second dimension: permutation within Hamming weight
141
+ z_count = basis.count('Z')
142
+ if z_count > m:
143
+ sample_3_axis[key] = take_sample('Z', basis, sample_3_axis[key], m)
144
+ continue
145
+ m -= z_count
146
+ sample_3_axis[key] = take_all('Z', basis, sample_3_axis[key])
147
+ if m == 0:
148
+ continue
149
+
150
+ i_count = basis.count('I')
151
+ if i_count > m:
152
+ sample_3_axis[key] = take_sample('I', basis, sample_3_axis[key], m)
153
+ continue
154
+ m -= i_count
155
+ sample_3_axis[key] = take_all('I', basis, sample_3_axis[key])
156
+ if m == 0:
157
+ continue
158
+
159
+ sample_3_axis[key] = take_sample('X', basis, sample_3_axis[key], m)
160
+
161
+ sample = 0
162
+ j = 0
163
+ for i in range(n_qubits):
164
+ base = measurement_basis[i]
165
+ if base not in ['X', 'Y', 'Z']:
166
+ continue
167
+ if (sample_3_axis[base] >> i) & 1:
168
+ sample |= 1 << j
169
+ j += 1
170
+
171
+ samples.append(sample)
172
+
173
+ return samples
@@ -1,7 +1,6 @@
1
1
  from .maxcut_tfim_util import binary_search, opencl_context, to_scipy_sparse_upper_triangular
2
- from .maxcut_tfim import maxcut_tfim
2
+ from .spin_glass_solver import spin_glass_solver
3
3
  from concurrent.futures import ProcessPoolExecutor
4
- import time
5
4
  import itertools
6
5
  import networkx as nx
7
6
  from numba import njit, prange
@@ -639,7 +638,7 @@ def tsp_symmetric_header(G_m, nodes, quality, shots, anneal_t, anneal_h, repulsi
639
638
  for _ in range(multi_start):
640
639
  bits = ([], [])
641
640
  while (len(bits[0]) == 0) or (len(bits[1]) == 0):
642
- _, cut_value, bits = maxcut_tfim(G_m, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
641
+ _, cut_value, bits, _ = spin_glass_solver(G_m, is_spin_glass=False, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
643
642
  if cut_value > best_cut:
644
643
  best_cut = cut_value
645
644
  a, b = bits
@@ -851,7 +850,7 @@ def tsp_asymmetric_header(G_m, nodes, quality, shots, anneal_t, anneal_h, repuls
851
850
  for _ in range(multi_start):
852
851
  bits = ([], [])
853
852
  while (len(bits[0]) == 0) or (len(bits[1]) == 0):
854
- _, cut_value, bits = maxcut_tfim((G_m + G_m.T) / 2, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
853
+ _, cut_value, bits, _ = spin_glass_solver((G_m + G_m.T) / 2, is_spin_glass=False, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
855
854
  if cut_value > best_cut:
856
855
  best_cut = cut_value
857
856
  a, b = bits
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.2.4
3
+ Version: 9.3.0
4
4
  Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -11,6 +11,7 @@ pyqrackising/maxcut_tfim.py
11
11
  pyqrackising/maxcut_tfim_sparse.py
12
12
  pyqrackising/maxcut_tfim_streaming.py
13
13
  pyqrackising/maxcut_tfim_util.py
14
+ pyqrackising/otoc.py
14
15
  pyqrackising/spin_glass_solver.py
15
16
  pyqrackising/spin_glass_solver_sparse.py
16
17
  pyqrackising/spin_glass_solver_streaming.py
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
7
7
 
8
8
  setup(
9
9
  name='pyqrackising',
10
- version='9.2.4',
10
+ version='9.3.0',
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)',
File without changes
File without changes
File without changes