-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.cpp
More file actions
228 lines (192 loc) · 6.13 KB
/
Copy pathalgorithm.cpp
File metadata and controls
228 lines (192 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <random>
#include <vector>
using namespace std;
using Vec = vector<double>;
static constexpr double EPS = 1e-12;
Vec sorted_desc(Vec v) {
sort(v.begin(), v.end(), greater<double>());
return v;
}
double sum(const Vec& v) {
return accumulate(v.begin(), v.end(), 0.0);
}
Vec normalize_and_sort(Vec v) {
for (double &x : v) x = max(0.0, x);
double s = sum(v);
if (s < EPS) {
// fallback: uniform
double u = 1.0 / max<size_t>(1, v.size());
fill(v.begin(), v.end(), u);
} else {
for (double &x : v) x /= s;
}
return sorted_desc(v);
}
// Tensor product of Schmidt vectors: kron(a,c)
Vec tensor(const Vec& a, const Vec& c) {
Vec out;
out.reserve(a.size() * c.size());
for (double ai : a)
for (double cj : c)
out.push_back(ai * cj);
return sorted_desc(out);
}
// Check: x majorizes y (x >=_maj y), i.e. cumulative sums of sorted desc satisfy
// sum_{i=1..k} x_i >= sum_{i=1..k} y_i for all k<d, and totals equal.
bool majorizes(const Vec& x_in, const Vec& y_in) {
Vec x = sorted_desc(x_in);
Vec y = sorted_desc(y_in);
if (x.size() != y.size()) return false;
double cx = 0.0, cy = 0.0;
for (size_t k = 0; k + 1 < x.size(); ++k) {
cx += x[k];
cy += y[k];
if (cx + 1e-12 < cy) return false;
}
// totals should be ~equal
return abs(sum(x) - sum(y)) < 1e-9;
}
// Vidal optimal success probability for bipartite pure-state conversion.
double vidal_prob(const Vec& a_in, const Vec& b_in) {
Vec a = sorted_desc(a_in);
Vec b = sorted_desc(b_in);
if (a.size() != b.size()) return 0.0;
const size_t d = a.size();
// tail sums E_k = sum_{i=k..d-1} a_i (0-indexed)
Vec Ea(d), Eb(d);
double ta = 0.0, tb = 0.0;
for (size_t i = d; i-- > 0; ) {
ta += a[i];
tb += b[i];
Ea[i] = ta;
Eb[i] = tb;
}
double best = 1.0;
for (size_t k = 0; k < d; ++k) {
if (Eb[k] < EPS) continue; // should not happen for valid Schmidt vectors
best = min(best, Ea[k] / Eb[k]);
}
// clamp
if (best < 0.0) best = 0.0;
if (best > 1.0) best = 1.0;
return best;
}
// Sample a random catalyst on simplex using Dirichlet(alpha=1) via Gamma(1,1)=Exp(1).
Vec sample_dirichlet(size_t m, mt19937_64& rng) {
exponential_distribution<double> expd(1.0);
Vec v(m);
for (size_t i = 0; i < m; ++i) v[i] = expd(rng);
return normalize_and_sort(v);
}
struct Result {
double prob;
Vec catalyst; // empty means "no catalyst used"
bool deterministic;
};
// Random-search catalyst optimizer with early exit on deterministic conversion.
Result optimize_catalyst(
const Vec& from_raw,
const Vec& to_raw,
size_t m_max = 6,
size_t samples_per_m = 50000,
size_t local_steps = 2000,
uint64_t seed = 12345
) {
mt19937_64 rng(seed);
Vec from = normalize_and_sort(from_raw);
Vec to = normalize_and_sort(to_raw);
// Early exit: deterministic without catalyst?
// from -> to deterministic iff to majorizes from (from ≺ to)
if (majorizes(to, from)) {
return {1.0, {}, true};
}
double bestP = vidal_prob(from, to);
Vec bestC; // empty => no catalyst
normal_distribution<double> noise(0.0, 0.02);
uniform_int_distribution<int> pick_idx;
for (size_t m = 2; m <= m_max; ++m) {
// global random search
Vec bestC_m;
double bestP_m = bestP;
for (size_t s = 0; s < samples_per_m; ++s) {
Vec c = sample_dirichlet(m, rng);
Vec A = tensor(from, c);
Vec B = tensor(to, c);
// Deterministic with catalyst: A ≺ B <=> B majorizes A
if (majorizes(B, A)) {
return {1.0, c, true};
}
double p = vidal_prob(A, B);
if (p > bestP_m + 1e-12) {
bestP_m = p;
bestC_m = c;
}
}
// local polish: random perturbations around bestC_m
if (!bestC_m.empty()) {
Vec c = bestC_m;
pick_idx = uniform_int_distribution<int>(0, (int)m - 1);
for (size_t t = 0; t < local_steps; ++t) {
Vec cand = c;
// perturb two coordinates and renormalize
int i = pick_idx(rng);
int j = pick_idx(rng);
if (i == j) j = (j + 1) % (int)m;
cand[i] += noise(rng);
cand[j] -= noise(rng);
cand = normalize_and_sort(cand);
Vec A = tensor(from, cand);
Vec B = tensor(to, cand);
if (majorizes(B, A)) {
return {1.0, cand, true};
}
double p = vidal_prob(A, B);
if (p > bestP_m + 1e-12) {
bestP_m = p;
c = cand;
}
}
bestC_m = c;
}
// update global best
if (bestP_m > bestP + 1e-12) {
bestP = bestP_m;
bestC = bestC_m;
}
}
return {bestP, bestC, false};
}
void print_vec(const Vec& v) {
cout << "(";
for (size_t i = 0; i < v.size(); ++i) {
cout << v[i] << (i + 1 < v.size() ? ", " : "");
}
cout << ")";
}
int main() {
Vec psi = {0.4, 0.4, 0.2};
Vec phi = {0.5, 0.25, 0.25};
{
auto r = optimize_catalyst(psi, phi, /*m_max=*/6, /*samples=*/30000, /*local=*/2000, /*seed=*/1);
cout << "psi -> phi: P=" << r.prob
<< " deterministic=" << (r.deterministic ? "yes" : "no")
<< " catalyst=";
if (r.catalyst.empty()) cout << "(none)";
else print_vec(r.catalyst);
cout << "\n";
}
{
auto r = optimize_catalyst(phi, psi, /*m_max=*/6, /*samples=*/30000, /*local=*/2000, /*seed=*/2);
cout << "phi -> psi: P=" << r.prob
<< " deterministic=" << (r.deterministic ? "yes" : "no")
<< " catalyst=";
if (r.catalyst.empty()) cout << "(none)";
else print_vec(r.catalyst);
cout << "\n";
}
return 0;
}