def findQDistMaximizingCHSH2ForCHSH1ValueOf(alpha): prob = pic.Problem() A = {} for x1, x2 in product(range(2), range(2)): for a1, a2 in product(range(2), range(2)): A[x1, x2, a1, a2] = prob.add_variable('A_{0}{1}{2}{3}'.format(x1, x2, a1, a2), (2, 2), 'hermitian') prob.add_constraint(A[x1, x2, a1, a2] >> 0) for x1, x2 in product(range(2), range(2)): prob.add_constraint( sum(A[x1, x2, a1, a2] for a1 in range(2) for a2 in range(2)) == np.eye(2)) #sequential constraints for x1, a1 in product(range(2), range(2)): prob.add_constraint( sum([A[x1, 0, a1, a2] for a2 in (0, 1)]) == sum([A[x1, 1, a1, a2] for a2 in (0, 1)])) z0 = np.array([[1, 0], [0, 0]]) z1 = np.array([[0, 0], [0, 1]]) x0 = np.array([[1, 1], [1, 1]]) / 2 x1 = np.array([[1, -1], [-1, 1]]) / 2 B = {} B[0, 0] = pic.new_param('B_00', z0) B[0, 1] = pic.new_param('B_01', z1) B[1, 0] = pic.new_param('B_10', x0) B[1, 1] = pic.new_param('B_11', x1) rho = pic.new_param('rho', np.outer([1, 0, 0, 1], [1, 0, 0, 1]) / 2) A1 = [ 1 / 2 * sum(A[x1, x2, a1, a2] * (-1)**a1 for a1 in range(2) for a2 in range(2) for x2 in range(2)) for x1 in range(2) ] A2 = [ 1 / 2 * sum(A[x1, x2, a1, a2] * (-1)**a2 for a1 in range(2) for a2 in range(2) for x1 in range(2)) for x2 in range(2) ] B1 = [sum(B[y, b] * (-1)**b for b in range(2)) for y in range(2)] prob.add_constraint(pic.trace(CHSH(A1, B1) * rho) == alpha) prob.set_objective('max', pic.trace(CHSH(A2, B1) * rho)) prob.solve() # return [ pic.trace(pic.kron(A[x1, x2, a1, a2], B[y, b]) * rho).get_value().real for x1, x2, y, a1, a2, b in product(range(2), range(2), range(2), range(2), range(2), range(2)) ]
def get_CBT_norm(J, n, m, rev=False): import cvxopt as cvx import picos as pic # Get completely bounded trace norm of Choi-matrix J representing quantum channel from number_of_qubits-dimensional space to m-dimensional space J = cvx.matrix(J) prob = pic.Problem(verbose=0) X = prob.add_variable("X", (n * m, n * m), vtype='complex') I = pic.new_param('I', np.eye(m)) rho0 = prob.add_variable("rho0", (n, n), vtype='hermitian') rho1 = prob.add_variable("rho1", (n, n), vtype='hermitian') prob.add_constraint(rho0 >> 0) prob.add_constraint(rho1 >> 0) prob.add_constraint(pic.trace(rho0) == 1) prob.add_constraint(pic.trace(rho1) == 1) if (rev == True): # TODO FBM: tests which conention is good. # TODO FBM: add reference to paper # This is convention REVERSED with respect to the paper, # and seems that this is a proper one???? C0 = pic.kron(rho0, I) C1 = pic.kron(rho1, I) else: C0 = pic.kron(I, rho0) C1 = pic.kron(I, rho1) F = pic.trace((J.H) * X) + pic.trace(J * (X.H)) prob.add_constraint(((C0 & X) // (X.H & C1)) >> 0) prob.set_objective('max', F) prob.solve(verbose=0) if prob.status.count("optimal") > 0: # print('solution optimal') 1 elif (prob.status.count("optimal") == 0): print('uknown_if_solution_optimal') else: print('solution not found') cbt_norm = prob.obj_value() / 2 if (abs(np.imag(cbt_norm)) >= 0.00001): raise ValueError else: cbt_norm = np.real(cbt_norm) return cbt_norm
def solve_sdp(w, k): nsquare = w.shape[0] n = int(np.sqrt(nsquare)) ww = np.c_[w, np.zeros((nsquare, 1))] ww = np.r_[ww, np.zeros((1, nsquare + 1))] maps, mapeq, maps_b, mapeq_b = utils.generate_sdp_constraint_map(ww, n, k) sdp = picos.Problem() y = sdp.add_variable('y', (nsquare + 1, nsquare + 1), 'symmetric') sdp.add_constraint(y >> 0) sdp.add_constraint(y[nsquare, nsquare] == 1.0) sdp.add_constraint(y[nsquare, :] == y[:, nsquare].T) for i, m in enumerate(maps): sdp.add_constraint(to_spmatrix(m) | y <= maps_b.astype(np.double)[i, 0]) sdp.add_constraint(to_spmatrix(mapeq) | y == matrix(mapeq_b)) sdp.add_constraint(picos.trace(y) == k + 1) sdp.set_objective('max', matrix(ww) | y) sdp.set_option('tol', 0.1) sdp.set_option('verbose', 0) # print(sdp) sdp.solve() y = y.value t = np.array(y[-1, :-1]) x1 = t.reshape((n, n)) x = linear_projection.linear_projection(x1.ravel(order='F')) idx, = np.where(x.ravel(order='F')==1.0) score = x1.ravel(order='F')[idx] sidx = np.argsort(score) idx = np.delete(idx, sidx[(n - k):]) x.ravel(order='F')[idx] = 0.0 return x
def sdp(P, k): """Solve the SDP relaxation. Parameters ---------- P : np.array m*N matrix of N m-dimensional points stacked columnwise k : int Number of clusters Returns ------- The SDP minimizer S_D """ N = P.shape[1] # Compute pairwise distances D = scipy.spatial.distance.pdist(P.T) D = scipy.spatial.distance.squareform(D) # Create the semi-definite program sdp = pic.Problem() Dparam = pic.new_param("D", D) X = sdp.add_variable("X", (N, N), vtype="symmetric") sdp.add_constraint(pic.trace(X) == k) sdp.add_constraint(X * "|1|({},1)".format(N) == 1) sdp.add_constraint(X > 0) sdp.add_constraint(X >> 0) sdp.minimize(Dparam | X) # Extract the SDP solution X_D = np.array(X.value) return X_D
def f(rho1, sig2, gam1, gam2, d2, cov=True): # rho -> sigma under GP (set cov=False)/ GPC map? Output \approx 1 --> yes, output<1 --> no. # dim(rho1) = dim(gam1) # dim(sig2) = dim(gam2) = d2 id_d2 = pic.new_param('id_d2', np.eye(d2)) ## def problem & variables ## p = pic.Problem() X1 = p.add_variable('X1', (3, 3), 'hermitian') X2 = p.add_variable('X2', (d2, d2), 'hermitian') X3 = p.add_variable('X3', (d2, d2), 'hermitian') p.add_constraint((sig2 | X2) + (gam2 | X3) == 1) if cov == True: p.add_constraint( pic.kron(id_d2, X1) - dephase2(pic.kron(X2, rho1)) - pic.kron(X3, gam1) >> 0.) else: p.add_constraint( pic.kron(id_d2, X1) - pic.kron(X2, rho1) - pic.kron(X3, gam1) >> 0.) p.add_constraint(X1 >> 0) p.add_constraint(X2 >> 0) p.add_constraint(X3 >> 0) ## objective fn & solve ## p.set_objective('min', pic.trace(X1)) p.solve(verbose=0, solver='cvxopt') return p.obj_value().real
def f(rho1,sig2,gam1,gam2,cov): # rho -> sigma under GP (set cov=False)/ GPC map? Output \approx 1 --> yes, output<1 --> no. # dim(rho1) = dim(gam1) # dim(sig2) = dim(gam2) = d2 ## def problem & variables ## #TODO ask Rhea where general solution to this problem is p = pic.Problem() X1 = p.add_variable('X1', (dim,dim), 'hermitian') X2 = p.add_variable('X2', (2,2), 'hermitian') X3 = p.add_variable('X3', (2,2), 'hermitian') p.add_constraint((sig2|X2)+(gam2|X3)==1) if cov==True: #time-covariance constraint included p.add_constraint(pic.kron(I2, X1) - dephase2(pic.kron(X2, rho1)) - pic.kron(X3, gam1) >> 0. ) else: #time-covariance constraint not included p.add_constraint(pic.kron(I2, X1) - pic.kron(X2, rho1) - pic.kron(X3, gam1) >> 0. ) p.add_constraint(X1 >> 0) p.add_constraint(X2 >> 0) p.add_constraint(X3 >> 0) ## objective fn & solve ## p.set_objective('min', pic.trace(X1)) p.solve(verbose = 0,solver = 'cvxopt') return p.obj_value().real
def f(rho1,sig2,beta,cov=True,GP=True): ## rho -> sigma under CPTP/ GP/ GPC/ Cov map? ## output \approx 1 --> yes, output < 1 --> no. ## dim(rho1) = dim(H1) = d1 ## dim(sig2) = dim(H2) = d2 ## default: qutrit-qubit CGP, equally-spaced energy levels, inv.temp=1 ## def problem & variables ## p = pic.Problem() X1 = p.add_variable('X1', (d1,d1), 'hermitian') X2 = p.add_variable('X2', (d2,d2), 'hermitian') X3 = p.add_variable('X3', (d2,d2), 'hermitian') ### constraints ### if GP == False: p.add_constraint((sig2|X2)==1) if cov == True: p.add_constraint(pic.kron(id_d2, X1) - dephase_H(pic.kron(X2, rho1)) >> 0. ) else: p.add_constraint(pic.kron(id_d2, X1) - pic.kron(X2, rho1) >> 0. ) else: p.add_constraint((sig2|X2)+(gam2|X3)==1) p.add_constraint(X3 >> 0) if cov == True: p.add_constraint(pic.kron(id_d2, X1) - dephase_H(pic.kron(X2, rho1)) - dephase_H(pic.kron(X3, gam1)) >> 0. ) else: p.add_constraint(pic.kron(id_d2, X1) - pic.kron(X2, rho1) - pic.kron(X3, gam1) >> 0. ) p.add_constraint(X1 >> 0) p.add_constraint(X2 >> 0) ### objective fn & solve ### p.set_objective('min', pic.trace(X1)) p.solve(verbose = 0,solver = 'cvxopt') return p.obj_value().real
def check_exstendibility(rho, sigma_AB, dim_A, dim_B, k,extend_system=1): ''' Check if σ_AB is an extension, by checking constraints :param rho: input state :param sigma_AB: solution to the proposed extension σ_AB. σ_AB should be 𝓗_A ⊗ 𝓗_B^(⊗k) :param dim_A: dimensions of system ρ_A :param dim_B: dimsenions of system ρ_B :param extend_system: Which system that is extended. Specify either 0 for system A or 1 for system B. ''' print("----------------------------------------------------") print("Checking that the solution fulfills the constraints:") print("----------------------------------------------------") #Checking the partial trace, with a tolerence of 1e-7 if all((np.real(picos.trace(sigma_AB).value)-1)<1e-7): print("tr(σ_AB) = 1 : TRUE") else: print("tr(σ_AB) = 1 : FALSE") #Checking that each extension is equal to ρ sigma_i_constraints=[np.allclose(get_sigma_AB_i(sigma_AB, dim_A, dim_B, i, k, extend_system=extend_system).value,rho.value) for i in range(1,k+1)] if all(sigma_i_constraints): print("(σ_AB)_i = ρ : TRUE") else: for i, sigma_i in enumerate(sigma_i_constraints): #Loop over the extensions which does not equal ρ if not sigma_i: print("(σ_AB)_%d = ρ : FALSE"%(i)) if all((np.linalg.eigvals(np.asarray(sigma_AB.value))+1e-7)>0): #Check if the matrix is positive with a tolerence of 1e-7 print("σ_AB > 0 : TRUE") else: print("σ_AB > 0 : FALSE") print("eigenvals are :") print(np.linalg.eigvals(np.asarray(sigma_AB.value)))
def check_exstendibility(rho, sigma_AB, dim_A, dim_B, k): ''' Check if sigma_AB is an extension, by checking constraints :param ρ: input state :param sigma_AB: solution to the proposed extension sigma_AB. sigma_AB should be 𝓗_A ⊗ 𝓗_B^(⊗k) :param dim_A: dimensions of system ρ_A :param dim_B: dimsenions of system ρ_B ''' print("----------------------------------------------------") print("Checking that the solution fulfills the constraints:") print("----------------------------------------------------") #Checking the partial trace, with a tolerence of 1e-7 if all((np.real(picos.trace(sigma_AB).value) - 1) < 1e-7): print("tr(σ_AB) = 1 : TRUE") else: print("tr(σ_AB) = 1 : FALSE") #Checking that each extension is equal to ρ sigma_i_constraints = np.allclose( bose_trace(sigma_AB, dim_A, dim_B, k).value, rho.value) if sigma_i_constraints: print("tr_B^N-1(sigma_AB) = ρ : TRUE") else: print("tr_B^N-1(sigma_AB) = ρ : FALSE") if all( (np.linalg.eigvals(np.asarray(sigma_AB.value)) + 1e-7) > 0): #Check if the matrix is positive with a tolerence of 1e-7 print("sigma_AB > 0 : TRUE") else: print("sigma_AB > 0 : FALSE") print("eigenvals are :") print(np.linalg.eigvals(np.asarray(sigma_AB.value)))
def extendibility(rho, dim_A, dim_B, k=2, verbose=0): ''' Checks if the state ρ is k-extendible. -------------------------------------- Given an input state ρ ∈ 𝓗_A ⊗ 𝓗_B. Try to find an extension σ_AB_1..B_k ∈ 𝓗_A ⊗ 𝓗_B^(⊗k), such that (σ_AB)_i=ρ Not that the extensions are only the B-system. :param ρ: The state we want to check :param dim_A: Dimensions of system A :param dim_B: Dimensions of system B :param k: The extendibility order ''' #Define variables, and create problem rho = picos.new_param('ρ', rho) problem = picos.Problem() sigma_AB = problem.add_variable( 'σ_AB', (dim_A * binom(dim_B + k - 1, k), dim_A * binom(dim_B + k - 1, k)), 'hermitian') #Set objective to a feasibility problem. The second argument is ignored by picos, so set some random scalar function. problem.set_objective('find', picos.trace(sigma_AB)) #Add constrains problem.add_constraint(sigma_AB >> 0) problem.add_constraint(picos.trace(sigma_AB) == 1) problem.add_constraint(bose_trace(sigma_AB, dim_A, dim_B, k) == rho) print("\nChecking for %d extendibility..." % (k)) #Solve the SDP either silently or verbose if verbose: try: print(problem) problem.solve(verbose=verbose, solver='mosek') print(problem.status) check_exstendibility(rho, sigma_AB, dim_A, dim_B, k) #Run a solution check if the user wants except UnicodeEncodeError: print( "!!!Can't print the output due to your terminal not supporting unicode encoding!!!\nThis can be solved by setting verbose=0, or running the function using ipython instead." ) else: problem.solve(verbose=verbose, solver='mosek') print(problem.status) return sigma_AB
def Hmin(rho, ref): ### compute Hmin(A|B)_rho, covariant map, ref=reference state. ### # def problem & variables # p = pic.Problem() X1 = p.add_variable('X1', (d1, d1), 'hermitian') p.add_constraint(pic.kron(id_d2, X1) - dephase_H(pic.kron(ref, rho)) >> 0.) p.add_constraint(X1 >> 0) # objective fn & solve # p.set_objective('min', pic.trace(X1)) p.solve(verbose=0, solver='cvxopt') return -np.log2(p.obj_value().real)
def f(rho1, sig2, beta=1, H1=[0, 1, 2], H2=[0, 1], cov=True, GP=True): ## rho -> sigma under CPTP/ GP/ GPC/ Cov map? ## output \approx 1 --> yes, output < 1 --> no. ## dim(rho1) = dim(H1) = d1 ## dim(sig2) = dim(H2) = d2 ## default: qutrit-qubit CPG, equally-spaced energy levels, inv.temp=1 d1 = len(H1) d2 = len(H2) id_d2 = pic.new_param('id_d2', np.eye(d2)) ## def problem & variables ## p = pic.Problem() X1 = p.add_variable('X1', (d1, d1), 'hermitian') X2 = p.add_variable('X2', (d2, d2), 'hermitian') X3 = p.add_variable('X3', (d2, d2), 'hermitian') ## Gibbs-state: def g(x): return np.exp(-beta * x) exp_array1 = np.array(list(map(g, H1))) exp_array2 = np.array(list(map(g, H2))) gam1 = pic.diag(np.true_divide(exp_array1, np.sum(exp_array1))) gam2 = pic.diag(np.true_divide(exp_array2, np.sum(exp_array2))) #g2_0 = gam2[0].value ### constraints ### if GP == False: p.add_constraint((sig2 | X2) == 1) if cov == True: p.add_constraint( pic.kron(id_d2, X1) - dephase(pic.kron(X2, rho1), H1, H2) >> 0.) else: p.add_constraint(pic.kron(id_d2, X1) - pic.kron(X2, rho1) >> 0.) else: p.add_constraint((sig2 | X2) + (gam2 | X3) == 1) p.add_constraint(X3 >> 0) if cov == True: p.add_constraint( pic.kron(id_d2, X1) - dephase(pic.kron(X2, rho1), H1, H2) - pic.kron(X3, gam1) >> 0.) else: p.add_constraint( pic.kron(id_d2, X1) - pic.kron(X2, rho1) - pic.kron(X3, gam1) >> 0.) p.add_constraint(X1 >> 0) p.add_constraint(X2 >> 0) ### objective fn & solve ### p.set_objective('min', pic.trace(X1)) p.solve(verbose=0, solver='cvxopt') return p.obj_value().real
def extendibility(rho, dim_A, dim_B, k=2, verbose=0, extend_system=1): ''' Checks if the state ρ is k-extendible. -------------------------------------- Given an input state ρ ∈ 𝓗_A ⊗ 𝓗_B. Try to find an extension σ_AB_1..B_k ∈ 𝓗_A ⊗ 𝓗_B^(⊗k), such that (σ_AB)_i=ρ :param rho: The state we want to check :param dim_A: Dimensions of system A :param dim_B: Dimensions of system B :param k: The extendibility order :param extend_system: Which system to create the copies from. Specify either 0 for system A or 1 for system B. ''' #Define variables, and create problem rho = picos.new_param('ρ',rho) problem = picos.Problem() if extend_system==1: sigma_AB = problem.add_variable('σ_AB', (dim_A*dim_B**k, dim_A*dim_B**k),'hermitian') else: sigma_AB = problem.add_variable('σ_AB', (dim_A**k*dim_B, dim_A**k*dim_B),'hermitian') #Set objective to a feasibility problem. The second argument is ignored by picos, so set some random scalar function. problem.set_objective('find', picos.trace(sigma_AB)) #Add constrains problem.add_constraint(sigma_AB>>0) problem.add_constraint(picos.trace(sigma_AB)==1) problem.add_list_of_constraints([get_sigma_AB_i(sigma_AB, dim_A, dim_B, i, k, extend_system=extend_system)==rho for i in range(1, k+1)],'i','1...'+str(k)) print("\nChecking for %d extendibility..."%(k)) #Solve the SDP either silently or verbose if verbose: try: print(problem) problem.solve(verbose=verbose, solver='mosek') check_exstendibility(rho, sigma_AB, dim_A, dim_B, k, extend_system=extend_system) #Run a solution check if the user wants except UnicodeEncodeError: print("!!!Can't print the output due to your terminal not supporting unicode encoding!!!\nThis can be solved by setting verbose=0, or running the function using ipython instead.") else: problem.solve(verbose=verbose, solver='mosek')
def calc_update(graph, alive_points, coloring, verbose=False): # Updates alive variables incidence = graph.incidence[alive_points] (remaining_n, l) = np.shape(incidence) sdp = pic.Problem() Xp = sdp.add_variable("X", (remaining_n, remaining_n), vtype='symmetric') a = 6 big_set_flags = (np.sum(incidence == 1, axis=0) >= a * graph.degree) alive_coloring = coloring[alive_points] if verbose: print('incidence mat. of alive points =') print(incidence) vs = [incidence[:, i] for i in range(l)] xs = [vs[i] * alive_coloring for i in range(l)] vvs = [ pic.new_param('vv' + str(i), np.outer(vs[i], vs[i])) for i in range(l) ] iden = pic.new_param('I', np.identity(remaining_n)) xxs = [ pic.new_param('xx' + str(i), np.outer(xs[i], xs[i])) for i in range(l) ] if verbose: print('vs =') print(vs) print('xs =') print(xs) print('vvs =') print([np.outer(vs[i], vs[i]) for i in range(l)]) print('xxs =') print([np.outer(xs[i], xs[i]) for i in range(l)]) sdp.add_list_of_constraints( [vvs[i] | Xp == 0 for i in range(l) if big_set_flags[i]]) sdp.add_list_of_constraints([(vvs[i] - 2 * iden) | Xp < 0 for i in range(l) if not big_set_flags[i]]) sdp.add_list_of_constraints([(xxs[i] - 2 * iden) | Xp < 0 for i in range(l) if not big_set_flags[i]]) sdp.add_list_of_constraints([Xp[i, i] < 1 for i in range(remaining_n)]) sdp.add_constraint(Xp >> 0) sdp.set_objective('max', pic.trace(Xp)) sdp.solve(verbose=0) U = np.linalg.cholesky(Xp.value) if verbose: print(sdp) print('U =') print(U) return (np.linalg.cholesky(Xp.value))
def MT_hopkins(x, r, Z): # implementation of SDP relaxation of MTE problem /!\ as found in Hopkins[2018] k, d = Z.shape Z_c = Z - x sdp = pic.Problem() z_c = pic.new_param('z_c', cvx.matrix(Z_c)) X = sdp.add_variable('X', (1 + k + d, 1 + k + d), vtype="symmetric") sdp.add_constraint(X >> 0) sdp.add_constraint(X[0, 0] == 1) sdp.add_list_of_constraints([X[i, i] <= 1 for i in range(1, k + 1)]) sdp.add_constraint(pic.trace(X[k + 1:, k + 1:]) <= 1) sdp.add_list_of_constraints([ (z_c[i, :].T | X[k + 1:, i + 1]) >= r * X[0, i + 1] for i in range(k) ]) sdp.set_objective('max', pic.sum([X[0, i] for i in range(1, k + 1)])) sdp.solve(verbose=0, solver='cvxopt') return X.value, sdp.obj_value()
def calc_update(constraints_vectors, beta): (n, l) = np.shape(constraints_vectors) sdp = pic.Problem() Xp = sdp.add_variable("X", (n, n), vtype='symmetric') wws = [ pic.new_param( 'ww' + str(i), constraints_vectors[:, i][:, np.newaxis] * constraints_vectors[:, i]) for i in range(l) ] diagX = pic.tools.diag(pic.tools.diag_vect(Xp) / beta) # for i in range(dim): # sdp.add_constraint(wws[i] | Xp == 0) sdp.add_list_of_constraints([wws[i] | Xp == 0 for i in range(l)]) sdp.add_constraint(Xp << diagX) sdp.add_list_of_constraints([Xp[i, i] < 1 for i in range(n)], 'i') sdp.add_constraint(Xp >> 0) sdp.set_objective('max', pic.trace(Xp)) sdp.solve(verbose=0) # print(Xp.value) return (np.linalg.cholesky(Xp.value))
def calc_update(graph): incidence = graph.incidence print(incidence) (n, l) = np.shape(incidence) sdp = pic.Problem() Xp = sdp.add_variable("X", (n, n), vtype = 'symmetric') a = 6 degree = graph.degree() big_set_flags = (np.sum(incidence == 1, axis = 0) >= a*degree) coloring = np.asarray(np.random.rand(n))*0.4-0.2 vs = [incidence[:, i] for i in range(l)] xs = [vs[i]*coloring for i in range(l)] vvs = [pic.new_param('vv'+str(i), np.outer(vs[i], vs[i])) for i in range(l)] iden = pic.new_param('I', np.identity(n)) xxs = [pic.new_param('xx'+str(i), np.outer(xs[i], xs[i])) for i in range(l)] # print('vs =') # print(vs) # print('xs =') # print(xs) # print('vvs =') # print([np.outer(vs[i], vs[i]) -2*np.identity(n) for i in range(l)]) # print('xxs =') # print([np.outer(xs[i], xs[i]) -2*np.identity(n) for i in range(l)]) sdp.add_list_of_constraints([vvs[i] | Xp == 0 for i in range(l) if big_set_flags[i]]) sdp.add_list_of_constraints([(vvs[i] - 2*iden) | Xp < 0 for i in range(l) if not big_set_flags[i]]) sdp.add_list_of_constraints([(xxs[i] - 2*iden) | Xp < 0 for i in range(l) if not big_set_flags[i]]) sdp.add_list_of_constraints([Xp[i, i] < 1 for i in range(n)]) sdp.add_constraint(Xp >> 0) sdp.set_objective('max', pic.trace(Xp)) sdp.solve() print(Xp.value) U = np.linalg.cholesky(Xp.value) print(U) return U, np.asarray(Xp.value)
A1=[1/2*sum(A[x1,x2,a1,a2]*(-1)**a1 for a1 in range(2) for a2 in range(2) for x2 in range(2)) for x1 in range(n)] A2=[1/n*sum(A[x1,x2,a1,a2]*(-1)**a2 for a1 in range(2) for a2 in range(2) for x1 in range(n)) for x2 in range(2)] B1=[sum(B[y,b]*(-1)**b for b in range(2)) for y in range(n)] prob.add_constraint(pic.trace(CHAINED(n,A1,B1)*rho)==alpha) prob.set_objective('max', pic.trace(CHSH(A2,B1)*rho)) prob.solve() # dist=[pic.trace(pic.kron(A[x1,x2,a1,a2],B[y,b])*rho).get_value().real for x1,x2,y,a1,a2,b in product(range(n),range(2),range(n),range(2),range(2),range(2))] vertices=BellPolytopeWithOneWayCommunication(outputsAlice,outputsBob).getGeneratorForVertices() with Model("lo1") as M: # Create variables bellFunctional = M.variable("func",144)
def team_opt(adj_mat, current_weights, covariance_matrices, how='geom', ne=1, edge_decisions=None): """ Runs the team optimization problem :param adj_mat: the Adjacency matrix :param current_weights: current node weights :param covariance_matrices: list of each node's large covariance matrix :param how: string that denotes fusion method :param ne: limit for number of edges to change :param edge_decisions: dictionary, if provided, the set of edges to set as 1 or 0 in their corresponding entries in PI :return: new adjacency matrix and new weights """ n = adj_mat.shape[0] beta = 1 / n tol = 0.1 s = covariance_matrices[0].shape[0] p_size = n * s edge_mod_limit = ne * 2 # Init Problem problem = pic.Problem() # Add Variables A = problem.add_variable('A', adj_mat.shape, 'symmetric') mu = problem.add_variable('mu', 1) Pbar = problem.add_variable('Pbar', (p_size, p_size)) PI = problem.add_variable('PI', adj_mat.shape, 'symmetric') delta_list = [] for i in range(n): delta_list.append(problem.add_variable('delta[{0}]'.format(i), (s, s))) delta_bar = problem.add_variable('delta_bar', (p_size, p_size)) delta_array = problem.add_variable('delta_array', (p_size, s)) # Add Params (ie constant affine expressions to help with creating constraints) cov_array = np.zeros((n * s, s)) for i in range(n): start = i * s end = i * s + s cov_array[start:end, 0:s] = covariance_matrices[i] I = pic.new_param('I', np.eye(s)) Ibar = pic.new_param('Ibar', np.eye(p_size)) cov_array_param = pic.new_param('covs', cov_array) # Set Objective if how == 'geom': problem.set_objective('min', pic.trace(Pbar)) else: problem.set_objective('min', pic.trace(delta_bar)) # Constraints # Setting Additional Constraint such that delta_bar elements equal elements in delta_list (with some tolerance) for i in range(n): start = i * s end = i * s + s problem.add_constraint( abs(delta_bar[start:end, start:end] - delta_list[i]) <= tol) if i < (n - 1): # Fill everything to left with 0s problem.add_constraint(delta_bar[start:end, end:] == np.zeros((s, (n * s) - end))) # Fill everything below with 0s problem.add_constraint(delta_bar[end:, start:end] == np.zeros(((n * s) - end, s))) # Setting Additional Constraint such that delta_array elements equal elements in delta_list (with some tolerance) for i in range(n): start = i * s end = i * s + s problem.add_constraint( abs(delta_array[start:end, :] - delta_list[i]) <= tol) if how == 'geom': # Schur constraint problem.add_constraint(((Pbar & Ibar) // (Ibar & delta_bar)).hermitianized >> 0) # Kron constraint problem.add_constraint(pic.kron(A, I) * cov_array_param == delta_array) problem.add_constraint(mu >= 0.001) problem.add_constraint(mu < 1) problem.add_constraint((A * np.ones((n, 1))) == np.ones((n, 1))) problem.add_constraint((beta * np.dot(np.ones(n).T, np.ones(n))) + (1 - mu) * np.eye(n) >= A) for i in range(n): problem.add_constraint(A[i, i] > 0) for j in range(n): if i == j: problem.add_constraint(PI[i, j] == 1.0) else: problem.add_constraint(PI[i, j] <= 1.0) problem.add_constraint(PI[i, j] >= 0.0) problem.add_constraint(A[i, j] > 0) problem.add_constraint(A[i, j] <= PI[i, j]) if edge_decisions is not None: # Ensures the set edge_decisions are maintained in PI for e, d in edge_decisions.items(): if d is not None: problem.add_constraint(PI[e[0], e[1]] == d) problem.add_constraint(PI[e[1], e[0]] == d) # Ensures the previous edges are maintained in PI for i in range(n): for j in range(n): if adj_mat[i, j] == 1: problem.add_constraint(PI[i, j] == 1.0) problem.add_constraint(abs(PI - adj_mat)**2 <= edge_mod_limit) try: problem.solve(verbose=0, solver='mosek') # problem_status = problem.status # print(problem_status) new_config = np.zeros(adj_mat.shape) new_weights = {} for i in range(n): new_weights[i] = {} for i in range(n): nw = A[i, i].value if nw == 0: nw = 0.1 new_weights[i][i] = nw new_config[i, i] = 1 for j in range(i + 1, n): if round(PI[i, j].value) == 1: new_config[i, j] = round(PI[i, j].value) new_config[j, i] = round(PI[j, i].value) nw = A[i, j].value if nw == 0: nw = 0.1 new_weights[i][j] = nw new_weights[j][i] = nw new_weights = normalize_weights(new_weights) return problem, problem.obj_value(), new_config, new_weights except Exception as e: print('solve error') print(e) return problem, 'infeasible', adj_mat, current_weights
rho = pic.new_param('rho', np.outer([1, 0, 0, 1], [1, 0, 0, 1]) / 2) A1 = [ 1 / 2 * sum(A[x1, x2, a1, a2] * (-1)**a1 for a1 in range(2) for a2 in range(2) for x2 in range(2)) for x1 in range(n) ] A2 = [ 1 / n * sum(A[x1, x2, a1, a2] * (-1)**a2 for a1 in range(2) for a2 in range(2) for x1 in range(n)) for x2 in range(2) ] B1 = [sum(B[y, b] * (-1)**b for b in range(2)) for y in range(n)] prob.add_constraint(pic.trace(CHAINED(n, A1, B1) * rho) == alpha) prob.set_objective('max', pic.trace(CHSH(A2, B1) * rho)) prob.solve() # dist = [ pic.trace(pic.kron(A[x1, x2, a1, a2], B[y, b]) * rho).get_value().real for x1, x2, y, a1, a2, b in product(range(n), range(2), range(n), range(2), range(2), range(2)) ] vertices = BellPolytopeWithOneWayCommunication( outputsAlice, outputsBob).getListOfVertices() print(In_ConvexHull(vertices, vertices[0]))
(2 * p - 1) * np.sin(np.pi * t) * X) ### params ### E1 = [0, 1] # list of energy eigenvalues of input Hamiltonian E2 = [0, 1] # list of energy eigenvalues of output Hamiltonian d1 = len(E1) d2 = len(E2) threshold = 0.99999999 # set threshold for f(rho,sig), <=1 x0 = 0.5 #0.25 z0 = 0.0 #0.5 rho = state(x0, z0) # def initial state rho theta_ref = 1 / 3 # reference state angle to Z-axis/pi p_ref = 1. reference = state_t(theta_ref, p_ref) # def reference state xref = pic.trace(reference * X).value zref = pic.trace(reference * Z).value def dephase(M, H1, H2): ## dephase matrix M w.r.t. H_tot = id_d2 X H1 + HR X id_d1, ## HR = -H2 ## H1, H2 lists of ordered energy eigenvalues of arb dim: low to high. d1 = len(H1) d2 = len(H2) id_d1 = pic.new_param('id_d1', np.eye(d1)) id_d2 = pic.new_param('id_d2', np.eye(d2)) dim = d1 * d2 HR = np.negative(H2) H_tot = np.diag( np.matrix((pic.kron(id_d2, pic.diag(H1)) +
def _check_qutrit_proj_sdp(K, solver=None): """Verifies whether a qutrit POVM is projective-simulable and checks for the visibility that would make it simulable. It returns a visibility value, which is one if the POVM is simulable. :param K: The POVM. :type K: list of :class:`numpy.array`. :param solver: The solver to be called, either `None`, "sdpa", "mosek", or "cvxopt". The default is `None`, which triggers autodetect. :type solver: str. :returns: float """ # Exact same logic as before, except that the POVM might have a different # of effects, and we have a lot more ways of simulating a POVM. n_outcomes = len(K) problem = picos.Problem(verbose=0) n_three_sets = n_choose_r(n_outcomes, 3) P = [[problem.add_variable("P^%s_%s" % (i, j), (3, 3), vtype='hermitian') for j in range(3)] for i in range(n_three_sets)] p = problem.add_variable("p", n_three_sets, lower=[0.0] * n_three_sets) n_two_sets = n_choose_r(n_outcomes, 2) R = [[problem.add_variable("R^%s_%s" % (i, j), (3, 3), vtype='hermitian') for j in range(2)] for i in range(n_two_sets)] r = problem.add_variable("r", n_two_sets, lower=[0.0] * n_two_sets) problem.add_list_of_constraints([sum(P[i][j] for j in range(3)) == p[i]*np.eye(3) for i in range(n_three_sets)], 'i', '0...%s' % (n_three_sets-1)) problem.add_list_of_constraints([P[i][j] >> 0 for i in range(n_three_sets) for j in range(3)], 'ij', '0...%s, 0...%s' % (n_three_sets-1, 2)) problem.add_list_of_constraints([picos.trace(P[i][j]) == p[i] for i in range(n_three_sets) for j in range(3)], 'ij', '0...%s, 0...%s' % (n_three_sets-1, 2)) problem.add_list_of_constraints([R[i][j] >> 0 for i in range(n_two_sets) for j in range(2)], 'i_j', '0...%s, 0...%s' % (n_two_sets-1, 1)) problem.add_list_of_constraints([sum(R[i][j] for j in range(2)) == r[i]*np.eye(3) for i in range(n_two_sets)], 'i', '0...%s' % (n_two_sets-1)) problem.add_constraint(sum(p[i] for i in range(n_three_sets)) + sum(r[i] for i in range(n_two_sets)) == 1) t = problem.add_variable('t', 1, lower=0.0) problem.set_objective('max', t) problem.add_constraint(t <= 1.0) problem.add_constraint(t >= 0.0) noise = [np.trace(Ki).real*np.eye(3)/3 for Ki in K] for k in range(n_outcomes): sP = sum(P[i][c.index(k)] for i, c in enumerate(itertools.combinations(range(n_outcomes), 3)) if c.count(k) > 0) sR = sum(R[i][c.index(k)] for i, c in enumerate(itertools.combinations(range(n_outcomes), 2)) if c.count(k) > 0) problem.add_constraint(sP + sR == t*K[k] + (1 - t)*noise[k]) problem.solve(solver=solver) if problem.status.count("optimal") > 0: obj = problem.obj_value() else: obj = None return obj
def solve_SDP(f, grp_irreducible_rep): num_quats = f.shape[0] num_elems_grp = f.shape[2] num_irreducible_reps = grp_irreducible_rep.shape[0] d, is_all_real = irreducible_rep_analyze(grp_irreducible_rep) # convert grp_irreducible_rep into picos parameters dim and rho. ## dim dim = [pic.new_param('d[{k}]'.format(k = k), x) for k, x in enumerate(d)] ## rho rho = [[pic.new_param('rho[{k}, {g}]'.format(k = k, g = g), x) for g, x in enumerate(y)] for k, y in enumerate(grp_irreducible_rep)] # convert grp_irreducible_rep and f inot picos parameters C C = num_irreducible_reps * [None] for k in range(num_irreducible_reps): Ck = np.zeros((num_quats * d[k], num_quats * d[k]), dtype = np.float64 if is_all_real else np.complex128) for (i, j, g), _ in np.ndenumerate(f): Ck[i * d[k] : (i + 1) * d[k], j * d[k] : (j + 1) * d[k]] += f[j, i, g] * grp_irreducible_rep[k, g].conj().transpose() Ck *= (d[k] / num_elems_grp) C[k] = pic.new_param('C[{k}]'.format(k = k), Ck) # declare PICOS problem problem = pic.Problem() # construct variables X. # note that X[0] is fixed by constraints. # so we only construct X[1] .. X[K-1]. X = num_irreducible_reps * [None] for k in range(1, num_irreducible_reps): X[k] = problem.add_variable('X[{k}]'.format(k = k), \ (num_quats * d[k], num_quats * d[k]), \ vtype = 'symmetric' if is_all_real else 'hermitian') # add constraints # add semi-positive defineness constraint problem.add_list_of_constraints([X[k] >> 0 for k in range(1, num_irreducible_reps)]) # add identity matrix constraint problem.add_list_of_constraints([pic.diag_vect(X[k]) == 1 for k in range(1, num_irreducible_reps)]) # add NUG relaxed constraints for (i, j, g), _ in np.ndenumerate(f): if is_all_real: problem.add_constraint(pic.sum([dim[k] * \ pic.trace(rho[k][g] * X[k][i * dim[k] : (i + 1) * dim[k], \ j * dim[k] : (j + 1) * dim[k]]) \ for k in range(1, num_irreducible_reps)]) >= -1) else: problem.add_constraint(pic.sum([dim[k] * \ pic.trace(rho[k][g] * X[k][i * dim[k] : (i + 1) * dim[k], \ j * dim[k] : (j + 1) * dim[k]]) \ for k in range(1, num_irreducible_reps)]).real >= -1) problem.add_constraint(pic.sum([dim[k] * \ pic.trace(rho[k][g] * X[k][i * dim[k] : (i + 1) * dim[k], \ j * dim[k] : (j + 1) * dim[k]]) \ for k in range(1, num_irreducible_reps)]).imag >= 0) problem.add_constraint(pic.sum([dim[k] * \ pic.trace(rho[k][g] * X[k][i * dim[k] : (i + 1) * dim[k], \ j * dim[k] : (j + 1) * dim[k]]) \ for k in range(1, num_irreducible_reps)]).imag <= 0) # set objective if is_all_real: obj = pic.sum([pic.trace(C[k] * X[k]) for k in range(1, num_irreducible_reps)]) problem.set_objective('min', obj) else: obj = pic.sum([pic.trace(C[k] * X[k]) for k in range(1, num_irreducible_reps)]).real problem.set_objective('min', obj) problem.add_constraint(pic.sum([pic.trace(C[k] * X[k]) for k in range(1, num_irreducible_reps)]).imag >= 0) problem.add_constraint(pic.sum([pic.trace(C[k] * X[k]) for k in range(1, num_irreducible_reps)]).imag <= 0) # solve problem.set_option('verbose', 0) problem.solve(solver = 'cvxopt') # get X X[0] = np.ones((num_quats, num_quats), dtype = np.float64 if is_all_real else np.complex128) for k in range(1, num_irreducible_reps): X[k] = np.array(X[k].value, dtype = np.float64 if is_all_real else np.complex128) return X