PyQrackIsing 1.3.0__tar.gz → 1.3.2__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.
@@ -0,0 +1,19 @@
1
+ cmake_minimum_required(VERSION 3.15...3.29)
2
+ project(PyQrackIsing LANGUAGES CXX)
3
+
4
+ set(PYBIND11_FINDPYTHON ON)
5
+ find_package(pybind11 CONFIG REQUIRED)
6
+
7
+ pybind11_add_module(tfim_sampler PyQrackIsing/_pyqrack_ising.cpp)
8
+
9
+ if (DEFINED ENV{BOOST_ROOT})
10
+ target_include_directories(tfim_sampler PUBLIC $ENV{BOOST_ROOT}/include/boost)
11
+ endif (DEFINED ENV{BOOST_ROOT})
12
+
13
+ if (MSVC)
14
+ target_compile_options(tfim_sampler PUBLIC /O2 /std:c++17)
15
+ else (MSVC)
16
+ target_compile_options(tfim_sampler PUBLIC -O3 -std=c++17)
17
+ endif (MSVC)
18
+
19
+ install(TARGETS tfim_sampler DESTINATION .)
@@ -0,0 +1,2 @@
1
+ include CMakeLists.txt
2
+ include PyQrackIsing/_pyqrack_ising.cpp
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.3.0
3
+ Version: 1.3.2
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,229 @@
1
+ #define _USE_MATH_DEFINES
2
+ #include <pybind11/pybind11.h>
3
+ #include <pybind11/stl.h>
4
+ #include <cmath>
5
+ #include <random>
6
+ #include <string>
7
+ #include <vector>
8
+ #include <bitset>
9
+ #include <algorithm>
10
+ #include <boost/multiprecision/cpp_int.hpp>
11
+
12
+ namespace py = pybind11;
13
+
14
+ typedef boost::multiprecision::cpp_int BigInteger;
15
+
16
+ static inline std::string int_to_bitstring(BigInteger integer, size_t length) {
17
+ std::string s(length, '0');
18
+ for (size_t i = 0; i < length; ++i) {
19
+ if (integer & (1ULL << (length - 1U - i))) {
20
+ s[i] = '1';
21
+ }
22
+ }
23
+ return s;
24
+ }
25
+
26
+ static inline double closeness_like_bits(BigInteger perm, size_t n_rows, size_t n_cols) {
27
+ std::string bits = int_to_bitstring(perm, n_rows * n_cols);
28
+ double like_count = 0.0;
29
+ double total_edges = 0.0;
30
+ // grid neighbors (right and down)
31
+ for (size_t i = 0; i < n_rows; ++i) {
32
+ for (size_t j = 0; j < n_cols; ++j) {
33
+ char s = bits[i * n_cols + j];
34
+ char s_right = bits[i * n_cols + ((j + 1) % n_cols)];
35
+ char s_down = bits[((i + 1) % n_rows) * n_cols + j];
36
+ like_count += (s == s_right) ? 1.0 : -1.0;
37
+ like_count += (s == s_down) ? 1.0 : -1.0;
38
+ total_edges += 2.0;
39
+ }
40
+ }
41
+ return like_count / total_edges;
42
+ }
43
+
44
+ static inline double expected_closeness_weight(size_t n_rows, size_t n_cols, size_t hamming_weight) {
45
+ const size_t L = n_rows * n_cols;
46
+ auto comb = [](size_t n, size_t k) {
47
+ if ((k < 0) || (k > n)) {
48
+ return 0ULL;
49
+ }
50
+ if ((k == 0) || (k == n)) {
51
+ return 1ULL;
52
+ }
53
+ unsigned long long res = 1ULL;
54
+ for (size_t i = 1; i <= k; ++i) {
55
+ res = res * (n - k + i) / i;
56
+ }
57
+ return res;
58
+ };
59
+ const double same_pairs = comb(hamming_weight, 2U) + comb(L - hamming_weight, 2U);
60
+ const double total_pairs = comb(L, 2U);
61
+ const double mu_k = same_pairs / total_pairs;
62
+
63
+ return 2.0 * mu_k - 1.0;
64
+ }
65
+
66
+ static inline std::vector<double> probability_by_hamming_weight(double J, double h, double z, double theta, double t, size_t n_qubits)
67
+ {
68
+ // critical angle
69
+ const double theta_c = std::asin(h / (z * J));
70
+ const double delta_theta = theta - theta_c;
71
+ std::vector<double> bias;
72
+ if (std::abs(h) < 1e-12) {
73
+ bias.assign(n_qubits + 1, 0.0);
74
+ bias[0] = 1.0;
75
+ } else if (std::abs(J) < 1e-12) {
76
+ bias.assign(n_qubits + 1, 1.0 / (n_qubits + 1));
77
+ } else {
78
+ const double sin_delta = std::sin(delta_theta);
79
+ const double omega = 1.5 * M_PI;
80
+ const double t2 = 1.0;
81
+ const double p = std::pow(2.0, std::abs(J / h) - 1.0) * (1.0 + sin_delta * std::cos(J * omega * t + theta) / (1.0 + std::sqrt(t / t2))) - 0.5;
82
+ if (p >= 1024) {
83
+ bias.assign(n_qubits + 1U, 0.0);
84
+ bias[0] = 1.0;
85
+ } else {
86
+ double tot_n = 0.0;
87
+ bias.resize(n_qubits + 1);
88
+ for (size_t q = 0U; q <= n_qubits; ++q) {
89
+ double n = 1.0 / ((n_qubits + 1) * std::pow(2.0, p * q));
90
+ bias[q] = n;
91
+ tot_n += n;
92
+ }
93
+ for (size_t q = 0U; q <= n_qubits; ++q) {
94
+ bias[q] /= tot_n;
95
+ }
96
+ }
97
+ }
98
+ if (J > 0) {
99
+ std::reverse(bias.begin(), bias.end());
100
+ }
101
+
102
+ return bias;
103
+ }
104
+
105
+ std::vector<std::string> generate_tfim_samples_cpp(double J, double h, double z, double theta, double t, size_t n_qubits, size_t shots) {
106
+ // lattice dimensions
107
+ size_t n_rows = 1U;
108
+ size_t n_cols = n_qubits;
109
+ for (size_t c = std::floor(std::sqrt(n_qubits)); c >= 1; --c) {
110
+ if ((n_qubits % c) == 0U) {
111
+ n_rows = n_qubits / c;
112
+ n_cols = c;
113
+ break;
114
+ }
115
+ }
116
+
117
+ const std::vector<double> bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits);
118
+
119
+ // thresholds
120
+ std::vector<double> thresholds(n_qubits + 1);
121
+ double tot_prob = 0.0;
122
+ for (size_t q = 0U; q <= n_qubits; ++q) {
123
+ tot_prob += bias[q];
124
+ thresholds[q] = tot_prob;
125
+ }
126
+ thresholds[n_qubits] = 1.0;
127
+
128
+ // random engine
129
+ std::mt19937_64 rng(std::random_device{}());
130
+ std::uniform_real_distribution<double> dist(0.0, 1.0);
131
+
132
+ std::vector<BigInteger> samples;
133
+ samples.reserve(shots);
134
+
135
+ std::vector<int> qubits(n_qubits);
136
+ for (size_t i = 0U; i < n_qubits; ++i) {
137
+ qubits[i] = i;
138
+ }
139
+
140
+ for (size_t s = 0U; s < shots; ++s) {
141
+ double mag_prob = dist(rng);
142
+ size_t m = 0U;
143
+ while (thresholds[m] < mag_prob) {
144
+ ++m;
145
+ }
146
+ double closeness_prob = dist(rng);
147
+ double tot_cprob = 0.0;
148
+ BigInteger state_int = 0;
149
+ // iterate combinations
150
+ std::vector<size_t> idx(m);
151
+ for (size_t i = 0U; i < m; ++i) {
152
+ idx[i] = i;
153
+ }
154
+ while (true) {
155
+ BigInteger candidate = 0U;
156
+ for (size_t pos : idx) {
157
+ candidate |= (BigInteger("1") << pos);
158
+ }
159
+ tot_cprob += (1.0 + closeness_like_bits(candidate, n_rows, n_cols)) /
160
+ (1.0 + expected_closeness_weight(n_rows, n_cols, m));
161
+ if (closeness_prob <= tot_cprob) {
162
+ state_int = candidate;
163
+ break;
164
+ }
165
+
166
+ // next combination
167
+ int64_t k = m - 1;
168
+ while ((k >= 0) && (idx[k] == (n_qubits - m + k))) {
169
+ --k;
170
+ }
171
+ if (k < 0) {
172
+ break;
173
+ }
174
+ ++idx[k];
175
+ for (int64_t j = k + 1; j < m; ++j) {
176
+ idx[j] = idx[j - 1U] + 1;
177
+ }
178
+ }
179
+ samples.push_back(state_int);
180
+ }
181
+
182
+ std::vector<std::string> output;
183
+ output.reserve(shots);
184
+ for (BigInteger& s : samples) {
185
+ output.push_back(boost::lexical_cast<std::string>(s));
186
+ }
187
+
188
+ return output;
189
+ }
190
+
191
+ double tfim_magnetization(double J, double h, double z, double theta, double t, size_t n_qubits) {
192
+ const std::vector<double> bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits);
193
+ double magnetization = 0.0;
194
+ const int64_t nqs = (int64_t)n_qubits;
195
+ const double nqd = (double)n_qubits;
196
+ for (int64_t q = 0U; q < bias.size(); ++q) {
197
+ const double mag = (nqs - 2 * q) / nqd;
198
+ magnetization += bias[q] * mag;
199
+ }
200
+
201
+ return magnetization;
202
+ }
203
+
204
+ double tfim_square_magnetization(double J, double h, double z, double theta, double t, size_t n_qubits) {
205
+ const std::vector<double> bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits);
206
+ double square_magnetization = 0.0;
207
+ const int64_t nqs = (int64_t)n_qubits;
208
+ const double nqd = (double)n_qubits;
209
+ for (int64_t q = 0U; q < bias.size(); ++q) {
210
+ const double mag = (nqs - 2 * q) / nqd;
211
+ square_magnetization += bias[q] * mag * mag;
212
+ }
213
+
214
+ return square_magnetization;
215
+ }
216
+
217
+ PYBIND11_MODULE(tfim_sampler, m) {
218
+ m.doc() = "PyQrackIsing TFIM sample generator";
219
+ m.def("_generate_tfim_samples", &generate_tfim_samples_cpp,
220
+ py::arg("J"), py::arg("h"), py::arg("z"), py::arg("theta"),
221
+ py::arg("t"), py::arg("n_qubits"), py::arg("shots"));
222
+ m.def("_tfim_magnetization", &tfim_magnetization,
223
+ py::arg("J"), py::arg("h"), py::arg("z"), py::arg("theta"),
224
+ py::arg("t"), py::arg("n_qubits"));
225
+ m.def("_tfim_square_magnetization", &tfim_square_magnetization,
226
+ py::arg("J"), py::arg("h"), py::arg("z"), py::arg("theta"),
227
+ py::arg("t"), py::arg("n_qubits"));
228
+ }
229
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.3.0
3
+ Version: 1.3.2
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
@@ -1,8 +1,11 @@
1
+ CMakeLists.txt
1
2
  LICENSE.md
3
+ MANIFEST.in
2
4
  README.md
3
5
  pyproject.toml
4
6
  setup.py
5
7
  PyQrackIsing/__init__.py
8
+ PyQrackIsing/_pyqrack_ising.cpp
6
9
  PyQrackIsing/generate_tfim_samples.py
7
10
  PyQrackIsing/tfim_magnetization.py
8
11
  PyQrackIsing/tfim_square_magnetization.py
@@ -30,6 +30,7 @@ from PyQrackIsing import generate_tfim_samples
30
30
  samples = generate_tfim_samples(
31
31
  J=-1.0,
32
32
  h=2.0,
33
+ z=4,
33
34
  theta=0.174532925199432957,
34
35
  t=5,
35
36
  n_qubits=56,
@@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta"
8
8
 
9
9
  [project]
10
10
  name = "PyQrackIsing"
11
- version = "1.3.0"
11
+ version = "1.3.2"
12
12
  requires-python = ">=3.8"
13
13
  description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
14
14
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -40,7 +40,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
40
40
 
41
41
  setup(
42
42
  name='PyQrackIsing',
43
- version='1.3.0',
43
+ version='1.3.2',
44
44
  author='Dan Strano',
45
45
  author_email='stranoj@gmail.com',
46
46
  description='Near-ideal closed-form solutions for transverse field Ising model (TFIM)',
File without changes
File without changes