-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchsh_mod.py
223 lines (162 loc) · 7.03 KB
/
chsh_mod.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
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
#%%
import ncpol2sdpa
import numpy as np
from sympy import *
from ncpol2sdpa import SdpRelaxation, generate_measurements
import os
# %%
'''
This file calculates the tripartite winning probability of the modified CHSH game from Section III C of
Device independent security of quantum key distribution from arbitrary monogamy-of-entanglement games
Includes the constrained tripartite winning probabilities given Alice and Bob expected score
'''
# %%
os.mkdir('CHSH_mod__Data')
# %%
# # # # # # # # # # # # # # # # # # # # # # # #
# Get projection operators and substitution for CHSHmod_2
# # # # # # # # # # # # # # # # # # # # # # # #
def PVM_2():
P = ncpol2sdpa.generate_measurements([2]*3, 'P')
Q = ncpol2sdpa.generate_measurements([2]*2, 'Q')
return P, Q
def PVM_substitutions_2(P,Q):
substitutions = ncpol2sdpa.projective_measurement_constraints((P,Q))
return substitutions
# # # # # # # # # # # # # # # # # # # # # # # #
# Objective function for CHSHmod_2
# # # # # # # # # # # # # # # # # # # # # # # #
def objective_function_2(P,Q):
obj = (P[0][0] * Q[0][0] + (1-P[0][0]) * (1-Q[0][0]) + \
P[0][0] * Q[1][0] + (1-P[0][0]) * (1-Q[1][0]) + \
P[1][0] * Q[0][0] + (1-P[1][0]) * (1-Q[0][0]) + \
P[1][0] * (1-Q[1][0]) + (1-P[1][0]) * Q[1][0])/6
obj += (P[2][0] * Q[0][0] + (1 - P[2][0]) * (1 - Q[0][0]) +\
P[2][0] * Q[1][0] + (1 - P[2][0]) * (1 - Q[1][0]))/6
return -obj
# # # # # # # # # # # # # # # # # # # # # # # #
# We get the relaxation on the desired level for CHSHmod_2
# # # # # # # # # # # # # # # # # # # # # # # #
def relaxation_2(level, verbose=1):
'''The variables'''
P,Q = PVM_2()
variables = ncpol2sdpa.flatten([P,Q])
'''The substitions'''
substitutions = PVM_substitutions_2(P,Q)
'''The objective function'''
objective = objective_function_2(P,Q)
'''Getting the relaxation'''
sdpRelaxation = SdpRelaxation(variables=variables, verbose = verbose)
sdpRelaxation.get_relaxation(level, objective=objective, substitutions=substitutions)
sdpRelaxation.solve()
print('The optimum of level ', level, 'of the hierarchy is: ', -sdpRelaxation.primal)
return -sdpRelaxation.primal
relaxation_2(level=3,verbose=1)
# %%
# # # # # # # # # # # # # # # # # # # # # # # #
# Get projection operators and substitution for CHSHmod_3
# # # # # # # # # # # # # # # # # # # # # # # #
def PVM():
P = ncpol2sdpa.generate_measurements([2]*3, 'P')
Q = ncpol2sdpa.generate_measurements([2]*2, 'Q')
F = ncpol2sdpa.generate_measurements([2]*6, 'F')
return P, Q, F
def PVM_substitutions(P,Q,F):
substitutions = ncpol2sdpa.projective_measurement_constraints((P,Q,F))
return substitutions
# # # # # # # # # # # # # # # # # # # # # # # #
# Objective functions or CHSHmod_3
# # # # # # # # # # # # # # # # # # # # # # # #
'''Objective function for the three players'''
def objective_function(P,Q,F):
obj = (P[0][0] * Q[0][0] * F[0][0] + (1 - P[0][0]) * (1 - Q[0][0]) * (1 - F[0][0]) + \
P[0][0] * Q[1][0] * F[1][0] + (1 - P[0][0]) * (1 - Q[1][0]) * (1 - F[1][0]) + \
P[1][0] * Q[0][0] * F[2][0] + (1 - P[1][0]) * (1 - Q[0][0]) * (1 - F[2][0]) + \
P[1][0] * (1 - Q[1][0]) * F[3][0] + (1 - P[1][0]) * Q[1][0] * (1 - F[3][0]))/6
obj += (P[2][0] * Q[0][0] * F[4][0] + (1 - P[2][0]) * (1 - Q[0][0]) * (1 - F[4][0]) +\
P[2][0] * Q[1][0] * F[5][0] + (1 - P[2][0]) * (1 - Q[1][0]) * (1 - F[5][0]))/6
return -obj
'''Objective function for Alice and Bob'''
def objective_function_AB(P,Q,score):
constraint = -score
obj = (P[0][0] * Q[0][0] + (1-P[0][0]) * (1-Q[0][0]) + \
P[0][0] * Q[1][0] + (1-P[0][0]) * (1-Q[1][0]) + \
P[1][0] * Q[0][0] + (1-P[1][0]) * (1-Q[0][0]) + \
P[1][0] * (1-Q[1][0]) + (1-P[1][0]) * Q[1][0])/6
obj += (P[2][0] * Q[0][0] + (1 - P[2][0]) * (1 - Q[0][0]) +\
P[2][0] * Q[1][0] + (1 - P[2][0]) * (1 - Q[1][0]))/6
return constraint + obj
# # # # # # # # # # # # # # # # # # # # # # # #
# We get the relaxation on the desired level
# # # # # # # # # # # # # # # # # # # # # # # #
def relaxation(level, score, verbose=1, save=False):
if save:
dir = 'CHSH_mod__Data/CHSH_data_for_level_%s' %level
if score is not None:
dir += '_epsAB_equal_%s' %score
os.mkdir(dir)
'''The variables'''
P,Q,F = PVM()
variables = ncpol2sdpa.flatten([P,Q,F])
'''The substitutions'''
substitutions = PVM_substitutions(P,Q,F)
'''The constraints'''
inequalities = [objective_function_AB(P,Q, score)]
'''The objective function'''
objective = objective_function(P,Q,F)
'''Getting the relaxation'''
sdpRelaxation = SdpRelaxation(variables=variables, verbose = verbose)
sdpRelaxation.get_relaxation(level, objective=objective,
substitutions=substitutions,
momentinequalities=inequalities)
sdpRelaxation.solve(solver='mosek')
if save:
'''Getting the solved parameters'''
primal_mat = sdpRelaxation.x_mat[0]
primal_val = sdpRelaxation.primal
dual_mat = sdpRelaxation.y_mat[0]
dual_val = sdpRelaxation.dual
status = sdpRelaxation.status
time = sdpRelaxation.solution_time
monomial_sets = sdpRelaxation.monomial_sets[0]
'''Saving the solved parameters'''
np.savetxt(dir + '/primal_mat.csv', primal_mat)
np.savetxt(dir + '/dual_mat.csv', dual_mat)
np.save(dir + '/monomial_sets', monomial_sets)
sdpRelaxation.save_monomial_index(dir + '/monomial_index.txt')
primal_string = 'The primal objective value is %s. This is an upper bound on the CHSH quantum value.' %(-primal_val)
dual_string = '\n The dual objective value is %s.' %(-dual_val)
stat_string = '\n The status of level %s' %level
stat_string += ' is ' + status
stat_string += ' and it required %s' %time
stat_string += ' seconds.'
str_output = primal_string+dual_string+stat_string
output_file = open(dir + '/output_lvl%s.txt' %level, 'wt')
n = output_file.write(str_output)
output_file.close()
# print(str_output)
print('The optimum of level ', level,
'with AB winning probability at least ', score,
' is: ', -sdpRelaxation.primal)
print('The sdp: ', sdpRelaxation.status)
"The following are returned for making a figure"
return -sdpRelaxation.primal, score
# relaxation(level=2, score = 0.875, verbose=1)
# %%
# # # # # # # # # # # # # # # # # # # # # # # #
# Cons data for fully uniform CHSH_mod
# # # # # # # # # # # # # # # # # # # # # # # #
ABC_win = []
AB_win = []
o3 = 1/2+1/3
o2 = relaxation_2(level = 2, verbose = 0)
scores = np.linspace(o3, o2, 20)
for score in scores:
out = relaxation(level=2, score=score,verbose=0)
ABC_win.append(out[0])
AB_win.append(out[1])
import matplotlib.pyplot as plt
np.savetxt('CHSH_mod__Data/Pr_ABwin', AB_win)
np.savetxt('CHSH_mod__Data/Pr_ABCwin', ABC_win)
plt.scatter(AB_win, ABC_win)
plt.savefig('CHSH_mod__Data/Pr_ABC_constrained')