-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
180 lines (143 loc) · 4.08 KB
/
main.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
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
"""
Christmax tree for FedeRez
/\
< >
\/
/\
/ \
/++++\
/ () \
/ \
/~`~`~`~`\
/ () () \
/ \
/*&*&*&*&*&*&\
/ () () () \
/ \
/++++++++++++++++\
/ () () () () \
/ \
/~`~`~`~`~`~`~`~`~`~`\
/ () () () () () \
/*&*&*&*&*&*&*&*&*&*&*&\
/ \
/,.,.,.,.,.,.,.,.,.,.,.,.,.\
| |
|`````|
\_____/
"""
import numpy as np
import perceval as pcvl
from perceval.components import BS, PERM
import matplotlib.pyplot as plt
# Circuit definitions
M = 25 # Number of modes : 2*m+1
N = 10000 # Number of samples
# Tree aesthetics
HEIGHT_FIRST_CONE = 15
WIDTH_FIRST_CONE = 5
HEIGHT_SECOND_CONE = 22
WIDTH_SECOND_CONE = 10
HEIGHT_THIRD_CONE = 26
WIDTH_THIRD_CONE = 4
WIDTH_BOTTOM = 6
HEIGHT_BOTTOM = 5
NUM_LINES = (
HEIGHT_FIRST_CONE
+ HEIGHT_SECOND_CONE
+ HEIGHT_THIRD_CONE
+ HEIGHT_BOTTOM
- WIDTH_FIRST_CONE
- WIDTH_SECOND_CONE
+ 4
)
def sampling_level_symmetric(level: int):
"""Draw two points at m - level and m + level
with probability 50%, 50%
"""
circuit = pcvl.Circuit(2 * M + 1)
if level > 0:
circuit.add(M, BS())
for i in range(0, level):
circuit.add(M - i - 1, PERM([1, 0]))
if i > 0:
circuit.add(M + i, PERM([1, 0]))
p = pcvl.Processor("SLOS", circuit)
p.with_input(pcvl.BasicState([0] * M + [1] + [0] * M))
sampler = pcvl.algorithm.Sampler(p)
sample_count = sampler.sample_count(N)
return sample_count["results"]
def sampling_level_line(l: int, excl: int):
"""Draw two lines, each of them of length l,
exluced by a region of 2*excl-1 in the the center
l=2
excl=2
m=5
###**###**###
with probability 1/(2*l)
"""
# First draw a line from spatial mode 0 to spatial mode 2*l
L = 2 * l + (2 * excl - 1)
offset = (2 * M + 1 - L) // 2
circuit = pcvl.Circuit(2 * M + 1)
for i in range(2 * l):
circuit.add(i, BS(BS.r_to_theta(1 / (2 * l - i))))
# Use a nice permutation to get the good result
perm = (
[offset + i for i in range(l)]
+ [offset + l + (2 * excl - 1) + i for i in range(l)]
+ [i for i in range(offset)]
+ [offset + l + i for i in range(2 * excl - 1)]
)
circuit.add(0, PERM(perm))
p = pcvl.Processor("SLOS", circuit)
p.with_input(pcvl.BasicState([1] + [0] * 2 * M))
sampler = pcvl.algorithm.Sampler(p)
sample_count = sampler.sample_count(N)
return sample_count["results"]
def update_matrix(matrix, results):
for k, v in results.items():
for i, t in enumerate(k):
if t:
matrix[l][i] += v
# Compute the matrix of the tree
l = 0
matrix = np.zeros((NUM_LINES, 2 * M + 1))
for level in range(HEIGHT_FIRST_CONE):
update_matrix(matrix, sampling_level_symmetric(level))
l += 1
update_matrix(
matrix,
sampling_level_line(HEIGHT_FIRST_CONE - WIDTH_FIRST_CONE, WIDTH_FIRST_CONE),
)
l += 1
for level in range(WIDTH_FIRST_CONE, HEIGHT_SECOND_CONE):
update_matrix(matrix, sampling_level_symmetric(level))
l += 1
update_matrix(
matrix,
sampling_level_line(HEIGHT_SECOND_CONE - WIDTH_SECOND_CONE, WIDTH_SECOND_CONE),
)
l += 1
for level in range(WIDTH_SECOND_CONE, HEIGHT_THIRD_CONE):
update_matrix(matrix, sampling_level_symmetric(level))
l += 1
update_matrix(
matrix, sampling_level_line(HEIGHT_THIRD_CONE - WIDTH_THIRD_CONE, WIDTH_THIRD_CONE)
)
l += 1
for _ in range(HEIGHT_BOTTOM):
update_matrix(matrix, sampling_level_symmetric(WIDTH_THIRD_CONE))
l += 1
update_matrix(matrix, sampling_level_line(WIDTH_THIRD_CONE, 1))
# Plot the tree
# Use log trick to have less variations in the color map.
plt.figure()
plt.imshow(
10 * np.log10(1 + np.log10(1 + matrix)), cmap="Greens", interpolation="nearest"
)
plt.xlabel("Probability of each spatial mode")
plt.ylabel("Circuit")
plt.title("Joyeux Noël FedeRez !")
plt.colorbar()
plt.show()