You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an implementation that is close to being complete for upper bounding the value of a qubit Bell inequality. The logic is heavily derived from the nice implementation here in QETLAB.
importcvxpyascpfromscipy.sparseimporteyefromitertoolsimportcombinationsfromtoqito.permsimportpermutation_operator, swapfromtoqito.channelsimportpartial_transposedefMN_matrix(m: int, a: int, x: int) ->np.ndarray:
""" Computes the matrices M_a^x and N_b^y. Args: m: The number of measurement settings for Alice and Bob. a: The specific measurement setting for Alice. x: The specific measurement setting for Bob. Returns: The computed matrix. """# Create a permutation list as done in MATLABperm=list(range(1, m+2)) # MATLAB indices start from 1perm[0] =x+1perm[x] =1# Calculate the matrix as in the MATLAB codeMN=a*np.eye(2**(m+1)) + ((-1)**a) *permutation_operator([2] * (m+1), perm, 0, 1)
returnMNdefbell_inequality_max(joint_coe, a_coe, b_coe, a_val, b_val):
m, _=joint_coe.shapeoa=len(a_val)
ob=len(b_val)
# Ensure the input vectors are column vectorsa_val=a_val.reshape(-1, 1)
b_val=b_val.reshape(-1, 1)
a_coe=a_coe.reshape(-1, 1)
b_coe=b_coe.reshape(-1, 1)
# Check if vectors a_val and b_val have only two elementsifoa!=2orob!=2:
raiseValueError("This script is only capable of handling Bell inequalities with two outcomes.")
tot_dim=2** (2*m+2)
obj_mat=np.zeros((tot_dim, tot_dim), dtype=float)
# Nested loops to compute the objective matrixforainrange(2): # a = 0 to 1forbinrange(2): # b = 0 to 1forxinrange(1, m+1): # x = 1 to m (1-indexed in MATLAB, hence the range adjustment)foryinrange(1, m+1): # y = 1 to mb_coeff=joint_coe[x-1, y-1] *a_val[a, 0] *b_val[b, 0] # Adjust index for 0-based Python indexingify==1:
b_coeff+=a_coe[x-1, 0] *a_val[a, 0] # Adjust for 0-based indexingifx==1:
b_coeff+=b_coe[y-1, 0] *b_val[b, 0] # Adjust for 0-based indexing# Adding the result of the tensor product to the objective matrixobj_mat+=b_coeff*np.kron(MN_matrix(m, a, x), MN_matrix(m, b, y))
# Symmetrize the matrix to avoid numerical issuesobj_mat= (obj_mat+obj_mat.T) /2aux_mat=np.array([[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
W=cp.Variable((2**(2*m), 2**(2*m)), symmetric=True)
M=swap(W, [2, m+1], [2] * (2*m))
X=swap(cp.kron(M, aux_mat), [m+1, 2*m+1], [2] * (2*m+2))
Z=swap(X, [m+2, 2*m+1], [2] * (2*m+2))
objective=cp.Maximize(cp.trace(Z @ obj_mat))
# Define the constraintsconstraints= [cp.trace(W) ==1, W>>0]
# Adding PPT constraintsforszinrange(1, m):
# Generate all combinations of indices from 1 to 2*m-1 of size szforppt_partitionincombinations(range(1, 2*m-1), sz):
# Convert to 0-based indexing for Pythonppt_partition= [x-1forxinppt_partition]
# Partial transpose on the partition, ensuring it's positive semidefinitept_matrix=partial_transpose(W, ppt_partition, [4] + [2] * (2* (m-1)))
constraints.append(pt_matrix>>0)
# Solve the problemprob=cp.Problem(objective, constraints)
prob.solve(solver="MOSEK", verbose=True)
# Return the resultsrho=W.valuebmax=prob.valuereturnbmax
Indeed, following the example from this page, it is possible to replicate the answer for the I3322 Bell inequality:
I have an implementation that is close to being complete for upper bounding the value of a qubit Bell inequality. The logic is heavily derived from the nice implementation here in QETLAB.
Indeed, following the example from this page, it is possible to replicate the answer for the I3322 Bell inequality:
Great. However, it seems like this fails for another inequality (like something simple, i.e. the CHSH inequality):
Which is wrong (it should be$2 \sqrt{2} \approx 2.84$ ).
This is close, but diagnosing the issue without a MATLAB license is complicated as this would allow us to compare.
However, this could also be an issue with how I encode CHSH. If I flip the order of the entries in
a_val
andb_val
to:Then I get ~2.84 which is still incorrect (but closer).
The text was updated successfully, but these errors were encountered: