def testGenerateAliceStrategiesForOneDichotomicAndOneTernaryInput(self): outputsAliceSequence = [2, 3] chshPoly = BellPolytope(BellScenario(outputsAliceSequence, [2, 2])) strgsAlice = list(chshPoly._strategiesGenerator(outputsAliceSequence)) expectedStrategies = [{ 0: 0, 1: 0 }, { 0: 0, 1: 1 }, { 0: 0, 1: 2 }, { 0: 1, 1: 0 }, { 0: 1, 1: 1 }, { 0: 1, 1: 2 }] self.assertTrue(len(strgsAlice), len(expectedStrategies)) for stg in expectedStrategies: self.assertIn(stg, strgsAlice)
def testAliceConstantlyZeroAndBobConstantlyOneIsVertexOfCHSH(self): chshPoly = BellPolytope(BellScenario([2, 2], [2, 2])) verticesAsLists = [ behaviour.getProbabilityList() for behaviour in chshPoly.getListOfVertices() ] self.assertIn([0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0], verticesAsLists)
def getGeneratorForVertices(self): #local vertices yield from BellPolytope.getGeneratorForVertices(self) #distributions with nontrivial use of the communication channel communicationStrgs = [ format(i, '0' + str(self._numberOfInputsAlice()) + 'b') for i in range(1, 2**(self._numberOfInputsAlice() - 1)) ] strgsAlice = [[ (stgAlice[i], int(comm[i])) for i in range(0, len(stgAlice)) ] for stgAlice in self._strategiesGenerator(self.outputsAlice) for comm in communicationStrgs] strgsBob = [ stgBob for stgBob in self._strategiesGenerator( reduce(lambda acum, elem: acum + [elem, elem], self.outputsBob, [])) if stgBob[0::2] != stgBob[1::2] ] yield from ([ int(a == stgAlice[x][0]) & (b == stgBob[2 * y + stgAlice[x][1]]) for x in range(self._numberOfInputsAlice()) for y in range(self._numberOfInputsBob()) for a in range(self.outputsAlice[x]) for b in range(self.outputsBob[y]) ] for stgAlice in strgsAlice for stgBob in strgsBob)
def testExistsStrategyAchievingAlgMaxOfCHSH(self): vertices = BellPolytopeWithOneWayCommunication( BellPolytope(BellScenario([2, 2], [2, 2]))).getListOfVertices() chshFunctional = [ 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1 ] self.assertEqual( max([ np.dot(vertice.getProbabilityList(), chshFunctional) for vertice in vertices ]), 4)
def testNumberOfLocalVerticesOfChshIs16(self): chshPoly = BellPolytope(BellScenario([2, 2], [2, 2])) self.assertEquals(len(chshPoly.getListOfVertices()), 16)
def __init__(self, outputsAlice, outputsBob): BellPolytope.__init__(self, outputsAlice, outputsBob)
qubitObservable), aliceObservables)) bobUnBlochVectors = [[-1, -1, 0], [-1, 1, 0]] bobObservables = list( map(lambda bloch: createQubitObservable(bloch), bobUnBlochVectors)) bobEffects = list( map( lambda qubitObservable: projectorsForQubitObservable( qubitObservable), bobObservables)) psi = createMaxEntState(2) dist = computeDistributionFromStateAndEffects(psi, aliceEffects, bobEffects) vertices = BellPolytope(outputsAlice, outputsBob).getListOfVertices() print(In_ConvexHull(vertices, vertices[0])) # # with Model("lo1") as M: # M.setSolverParam('logInfeasAna',100) # # Create variable 'x' of length 4 # x = M.variable("x", len(vertices), Domain.greaterThan(0.0)) # # # Create constraints # constraints=[] # for prob in range(len(vertices[0])): # constraints.append(M.constraint('p('+str(prob)+')',Expr.dot(x,list(map(lambda ver : ver[prob],vertices))),Domain.equalsTo(dist[prob]))) # # constraints.append(M.constraint('norm',Expr.dot(x,np.ones((len(vertices), 1))),Domain.equalsTo(1))) #
def testNumberOfVerticesOfBellPolytope33Is81(self): self.assertEqual(BellPolytope(3, 3).numberOfVertices(), 3**6, '')
psi = 2 / np.sqrt(3) * (qt.tensor(qzero, qzero) - 1 / 2 * qt.tensor(plus, plus)) aliceEffects = [[qzero * qzero.dag(), qone * qone.dag()], [plus * plus.dag(), minus * minus.dag()]] bobEffects = [[qzero * qzero.dag(), qone * qone.dag()], [plus * plus.dag(), minus * minus.dag()]] bobPostMeasurmentStates = [[ 1 / ((qt.tensor(effect, qt.qeye(2)) * psi * psi.dag()).tr()) * (qt.tensor(effect, qt.qeye(2)) * psi).ptrace(1) for effect in measurement ] for measurement in aliceEffects] usdState1 = bobPostMeasurmentStates[0][0] usdState2 = bobPostMeasurmentStates[1][1] overlap = np.sqrt((usdState1 * usdState2).tr()) usd = [(qt.qeye(2) - usdState1) / (1 + overlap), (qt.qeye(2) - usdState2) / (1 + overlap)] #bobEffects.append([usd[0],usd[1],qt.qeye(2)-usd[0]-usd[1]]) dist = computeDistributionFromStateAndEffects(psi, aliceEffects, bobEffects) dist = [p.real for p in dist] scenario = BellScenario(outputsAlice, outputsBob) poly = BellPolytopeWithOneWayCommunication(BellPolytope(scenario)) print(poly.contains(dist))
import numpy as np from bellpolytope import BellPolytope if __name__ == '__main__': N=4 K=3 poly = BellPolytope(N,K) vertices=poly.getVertices() distributions=np.matrix(vertices) NumberOfCoefficients=(N*K)**2 with open('randomfunctionals.txt','w') as f: f.write('Random functionals \n') for j in range (0,10): functional=np.random.uniform(-63,1,size=NumberOfCoefficients) values=np.dot(distributions,np.transpose(functional)) c=np.amax(values) BellNormalisedFunctional=functional/c with open('randomfunctionals.txt','a') as f: np.savetxt(f, BellNormalisedFunctional, fmt='%.2f') f.write('\n')
ratios = [0, 0.464, 0.569, 0.636, 0.683, 0.718] outputsAlice = [2 for _ in range(K + 1)] outputsBob = [2 for _ in range(K + 1)] ratio = ratios[K] beta = 1 / (1 + ratio) alpha = beta * ratio c = [(-1)**k * beta**(k + 1 / 2) / np.sqrt(beta**(2 * k + 1) + alpha**(2 * k + 1)) for k in range(K + 1)] states = [ c[k] * qt.basis(2, 0) + np.sqrt(1 - c[k]**2) * qt.basis(2, 1) for k in range(K + 1) ] psi = (1 / np.sqrt(alpha**2 + beta**2)) * ( alpha * qt.tensor(qt.basis(2, 0), qt.basis(2, 0)) - beta * qt.tensor(qt.basis(2, 1), qt.basis(2, 1))) aliceEffects = [[state * state.dag(), qt.qeye(2) - state * state.dag()] for state in states] bobEffects = aliceEffects dist = computeDistributionFromStateAndEffects(psi, aliceEffects, bobEffects) print(dist) print(sum(dist)) polytope = BellPolytopeWithOneWayCommunication( BellPolytope(BellScenario(outputsAlice, outputsBob))) if not polytope.contains(dist): print('We found a qdist not reproducible with one bit of comm!')
def testSeqStratForOptimalRandIsSimulable(self): alice1outputs = [2, 2] alice2outputs = [2, 2, 3] bobOutputs = [2, 2] sequentialScenario = SequentialBellScenario( [alice1outputs, alice2outputs], bobOutputs) alice1blochVectors = [[0, 0, 1]] alice1Observables = list( map(lambda bloch: createQubitObservable(bloch), alice1blochVectors)) alice1Krauss = list( map( lambda qubitObservable: projectorsForQubitObservable( qubitObservable), alice1Observables)) epsilon = 7 * np.pi / 32 plus = 1 / np.sqrt(2) * (qt.basis(2, 0) + qt.basis(2, 1)) minus = 1 / np.sqrt(2) * (qt.basis(2, 0) - qt.basis(2, 1)) Kplus = np.cos(epsilon) * plus * plus.dag() + np.sin( epsilon) * minus * minus.dag() Kminus = -np.cos(epsilon) * minus * minus.dag() + np.sin( epsilon) * plus * plus.dag() alice1Krauss.append([Kplus, Kminus]) alice2blochVectors = [[0, 0, 1], [1, 0, 0]] alice2Observables = list( map(lambda bloch: createQubitObservable(bloch), alice2blochVectors)) alice2Effects = list( map( lambda qubitObservable: projectorsForQubitObservable( qubitObservable), alice2Observables)) trineAlice2 = [[0, 0, 1], [np.sin(2 * np.pi / 3), 0, np.cos(2 * np.pi / 3)], [np.sin(4 * np.pi / 3), 0, np.cos(4 * np.pi / 3)]] paulies = [qt.sigmax(), qt.sigmay(), qt.sigmaz()] alice2Effects.append( list( map( lambda bloch: effectForQubitPovm( 1 / 3, sum( [paulies[i] * bloch[i] for i in range(0, 3)])), trineAlice2))) mu = np.arctan(np.sin(2 * epsilon)) bobUnBlochVectors = [[np.sin(mu), 0, np.cos(mu)], [-np.sin(mu), 0, np.cos(mu)]] bobObservables = list( map(lambda bloch: createQubitObservable(bloch), bobUnBlochVectors)) bobEffects = list( map( lambda qubitObservable: projectorsForQubitObservable( qubitObservable), bobObservables)) psi = createMaxEntState(2) rho = psi * psi.dag() expectedCorrelations = {} for x1, x2, y in product(range(2), range(3), range(2)): for a1, a2, b in product(range(alice1outputs[x1]), range(alice2outputs[x2]), range(bobOutputs[y])): postMeasrmntState = qt.tensor( alice1Krauss[x1][a1], qt.qeye(2)) * rho * (qt.tensor( alice1Krauss[x1][a1], qt.qeye(2))).dag() expectedCorrelations[(x1, x2), y, (a1, a2), b] = ( qt.tensor(alice2Effects[x2][a2], bobEffects[y][b]) * postMeasrmntState).tr().real expectedBehaviour = Behaviour(sequentialScenario, expectedCorrelations) scenario = BellScenario([4, 4, 6, 4, 4, 6], [2, 2]) polytope = BellPolytopeWithOneWayCommunication(BellPolytope(scenario)) print(polytope.contains(expectedBehaviour.getProbabilityList()))
def testNumberOfVerticesInScenarioWith4BinaryInputsForBobAnd2ForAliceIs1408( self): vertices = BellPolytopeWithOneWayCommunication( BellPolytope(BellScenario([2, 2], [2, 2, 2, 2]))).getListOfVertices() self.assertEqual(len(vertices), 1408)
def testNumberOfVerticesForCHSHIs64(self): vertices = BellPolytopeWithOneWayCommunication( BellPolytope(BellScenario([2, 2], [2, 2]))).getListOfVertices() self.assertEqual(len(vertices), 64)
np.trace(rho * np.kron(prob.matsolX(a + N * x), prob.matsolY(b + N * y)))) return np.dot(np.array(functional), products) if __name__ == '__main__': parties = 2 N = 3 K = 4 outputsAlice = [4, 4, 4] outputsBob = [4, 4, 4] dim = 2 BellViolations = [] #Creo que así los vértices están bien scenario = BellScenario(outputsAlice, outputsBob) poly = BellPolytope(scenario) vertices = poly.getListOfVertices() distributions = np.matrix(vertices) NumberOfCoefficients = (N * K)**2 '''with open('results.txt','w') as f: f.write('Random functionals \n')''' rho = np.matrix([[0, 0, 0, 0], [0, 1 / 2, -1 / 2, 0], [0, -1 / 2, 1 / 2, 0], [0, 0, 0, 0]]) #Ineqs = np.loadtxt( 'Ineqs.txt') '''for i in range (0,2): functional=np.random.uniform(-N**2*(N**2-2),1,size=NumberOfCoefficients) values=np.dot(distributions,np.transpose(functional)) c=np.amax(values) if c==0: functional=np.random.uniform(-N**2*(N**2-2),1,size=NumberOfCoefficients)
from mosek.fusion import * from bellpolytope import BellPolytope import numpy as np from bellscenario import BellScenario from behaviour import Behaviour from itertools import product if __name__ == '__main__': n = 2 outputs = 2**n * [3] outputsAlice = outputs outputsBob = outputs scenario = BellScenario(outputsAlice, outputsBob) poly = BellPolytope(scenario) toBin = lambda x: format(x, '0' + str(n) + 'b') disjointness = lambda x, y: np.intersect1d(x, y).size == 0 probabilities = {(x, y, a, b): 1 / 2 * int( (a != 2 and b != 2 and (a + b) % 2 == disjointness(toBin(x), toBin(y)))) for (x, y, a, b) in scenario.getTuplesOfEvents()} dist = Behaviour(scenario, probabilities).getProbabilityList() with Model("lo1") as M: fullSpaceDimension = sum( [sum([a * b for b in outputsBob]) for a in outputsAlice]) bellFunctional = M.variable("bellFunctional", fullSpaceDimension)
def __init__(self, sequentialBellScenario): BellPolytope.__init__(self, sequentialBellScenario)
newCoeffForInputs) return ineffResistInequality i = 0 s = 0 parties = 2 InputsAlice = 4 InputsBob = 4 OutputsAlice = 2 OutputsBob = 2 NumberOfInequalities = 174 functionals = np.loadtxt('dataconverted.txt') poly = BellPolytope(InputsAlice, OutputsAlice) InefficiencyResistantInequalities = np.zeros( (NumberOfInequalities, InputsAlice * (OutputsAlice + 1) * InputsBob * (OutputsBob + 1))) text = open("IneffFunctionals4433LispCG.txt", 'w') lisp = LispHelper for j in range(0, NumberOfInequalities): NormalisedFunctional = np.zeros(InputsAlice * (OutputsAlice + 1) * InputsBob * (OutputsBob + 1)) vertices4422 = np.matrix(poly.getVertices()) values4422 = np.dot(vertices4422, np.transpose(functionals[j][:])) minimum = np.amin(values4422) if minimum < -1: #JUST THE INEQUALITIES WE WANT InefficiencyResistantInequalities[ j][:] = extendInequalityToDetecLoopholeSetting( functionals[j][:], InputsAlice, OutputsAlice, parties)
import numpy as np from bellpolytope import BellPolytope def Alg(functional[i][j],j,H[j-1]): return H.tolist() if __name__ == '__main__': N=3 K=2 poly = BellPolytope(N,K) ineffResistInequalities = poly.getInefficiencyResistantInequalities() vertices33 = BellPolytope(N,K+1).getVertices(); relevantIneqs = list(filter(lambda ineq : min( map(lambda vertex : np.dot(ineq[1:],vertex),vertices33))<-1, ineffResistInequalities)) numberOfCoefficients=N**2*(K+1)**2 Ineq=np.zeros((len(relevantIneqs),numberOfCoefficients)) for i in range (0,len(relevantIneqs)): for j in range (0,numberOfCoefficients): Ineq[i][j]=relevantIneqs[i][1+j] functional=np.zeros((len(relevantIneqs),numberOfCoefficients)) functional[i]=Ineq[i][:]
''' In this example we compute symmetric facets of the Bell local polytope in the {3,3,4,4} scenario. @authors: rravell,gsenno ''' import numpy as np import cdd from bellscenario import BellScenario from bellpolytope import BellPolytope if __name__ == '__main__': outputsAlice = [4,4,4] outputsBob = [4,4,4] scenario=BellScenario(outputsAlice,outputsBob) polytope=BellPolytope(scenario) symmetricVertices=polytope.getListOfVerticesOfSymmetricSubpolytope() VRepresentation=[] for vertex in symmetricVertices: VRepresentation.append([0]+vertex.getProbabilityList()) mat = cdd.Matrix(VRepresentation, number_type='fraction') mat.rep_type = cdd.RepType.GENERATOR poly=cdd.Polyhedron(mat) print(poly.get_inequalities())