-
Notifications
You must be signed in to change notification settings - Fork 3
/
chain_rule.py
154 lines (121 loc) · 5.04 KB
/
chain_rule.py
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
import numpy as np
from loop_hafnian_batch import loop_hafnian_batch
from loop_hafnian_batch_gamma import loop_hafnian_batch_gamma
from scipy.special import factorial
from strawberryfields.decompositions import williamson
from thewalrus.quantum import (
Amat,
Qmat,
photon_number_mean_vector,
mean_clicks,
reduced_gaussian
)
def decompose_cov(cov):
m = cov.shape[0] // 2
D, S = williamson(cov)
T = S @ S.T
DmI = D - np.eye(2*m)
DmI[abs(DmI) < 1e-11] = 0. # remove slightly negative values
sqrtW = S @ np.sqrt(DmI)
return T, sqrtW
def mu_to_alpha(mu, hbar=2):
M = len(mu) // 2
# mean displacement of each mode
alpha = (mu[:M] + 1j * mu[M:]) / np.sqrt(2 * hbar)
return alpha
def invert_permutation(p):
s = np.empty_like(p, dtype=int)
s[p] = np.arange(p.size, dtype=int)
return s
def photon_means_order(mu, cov):
means = photon_number_mean_vector(mu, cov)
order = [x for _, x in sorted(zip(means, range(len(means))))]
return np.asarray(order)
def click_means_order(cov):
M = cov.shape[0] // 2
mu = np.zeros(2*M)
means = np.zeros(M)
for i in range(M):
mu_i, cov_i = reduced_gaussian(mu, cov, [i])
means[i] = mean_clicks(cov_i)
order = [x for _, x in sorted(zip(means, range(len(means))))]
return np.asarray(order)
def get_samples(mu, cov, cutoff=10, n_samples=10):
M = cov.shape[0] // 2
order = photon_means_order(mu, cov)
order_inv = invert_permutation(order)
oo = np.concatenate((order, order+M))
mu = mu[oo]
cov = cov[np.ix_(oo, oo)]
T, sqrtW = decompose_cov(cov)
chol_T_I = np.linalg.cholesky(T+np.eye(2*M))
B = Amat(T)[:M,:M]
det_outcomes = np.arange(cutoff+1)
for i in range(n_samples):
det_pattern = np.zeros(M, dtype=int)
pure_mu = mu + sqrtW @ np.random.normal(size=2*M)
pure_alpha = mu_to_alpha(pure_mu)
heterodyne_mu = pure_mu + chol_T_I @ np.random.normal(size=2*M)
heterodyne_alpha = mu_to_alpha(heterodyne_mu)
gamma = pure_alpha.conj() + B @ (heterodyne_alpha - pure_alpha)
for mode in range(M):
m = mode + 1
gamma -= heterodyne_alpha[mode] * B[:, mode]
lhafs = loop_hafnian_batch(B[:m,:m], gamma[:m], det_pattern[:mode], cutoff)
probs = (lhafs * lhafs.conj()).real / factorial(det_outcomes)
norm_probs = probs.sum()
probs /= norm_probs
det_outcome_i = np.random.choice(det_outcomes, p=probs)
det_pattern[mode] = det_outcome_i
yield det_pattern[order_inv]
def get_heterodyne_fanout(alpha, fanout):
M = len(alpha)
alpha_fanout = np.zeros((M, fanout), dtype=np.complex128)
for j in range(M):
alpha_j = np.zeros(fanout, dtype=np.complex128)
alpha_j[0] = alpha[j] # put the coherent state in 0th mode
alpha_j[1:] = (np.random.normal(size=fanout-1) +
1j * np.random.normal(size=fanout-1))
alpha_fanout[j,:] = np.fft.fft(alpha_j, norm='ortho')
return alpha_fanout
def get_samples_click(mu, cov, cutoff=1, fanout=10, n_samples=10):
M = cov.shape[0] // 2
order = photon_means_order(mu, cov)
order_inv = invert_permutation(order)
oo = np.concatenate((order, order+M))
mu = mu[oo]
cov = cov[np.ix_(oo, oo)]
T, sqrtW = decompose_cov(cov)
chol_T_I = np.linalg.cholesky(T+np.eye(2*M))
B = Amat(T)[:M,:M] / fanout
det_outcomes = np.arange(cutoff+1)
for i in range(n_samples):
det_pattern = np.zeros(M, dtype=int)
click_pattern = np.zeros(M, dtype=np.int8)
fanout_clicks = np.zeros(M, dtype=int)
pure_mu = mu + sqrtW @ np.random.normal(size=2*M)
pure_alpha = mu_to_alpha(pure_mu)
het_mu = pure_mu + chol_T_I @ np.random.normal(size=2*M)
het_alpha = mu_to_alpha(het_mu)
het_alpha_fanout = get_heterodyne_fanout(het_alpha, fanout)
het_alpha_sum = het_alpha_fanout.sum(axis=1)
gamma = (pure_alpha.conj() / np.sqrt(fanout) +
B @ (het_alpha_sum - np.sqrt(fanout) * pure_alpha))
gamma_fanout = np.zeros((fanout, M), dtype=np.complex128)
for mode in range(M):
gamma_fanout[0,:] = gamma - het_alpha_fanout[mode, 0] * B[:, mode]
for k in range(1, fanout):
gamma_fanout[k,:] = gamma_fanout[k-1,:] - het_alpha_fanout[mode,k] * B[:,mode]
lhafs = loop_hafnian_batch_gamma(B[:mode+1,:mode+1], gamma_fanout[:,:mode+1],
det_pattern[:mode], cutoff)
probs = (lhafs * lhafs.conj()).real / factorial(det_outcomes)
for k in range(fanout):
gamma = gamma_fanout[k,:]
probs_k = probs[k,:] / probs[k,:].sum()
det_outcome = np.random.choice(det_outcomes, p=probs_k)
det_pattern[mode] += det_outcome
if det_outcome > 0:
click_pattern[mode] = 1
fanout_clicks[mode] = k
break
yield click_pattern[order_inv]