本文整理汇总了Python中ncpol2sdpa.SdpRelaxation.solve方法的典型用法代码示例。如果您正苦于以下问题:Python SdpRelaxation.solve方法的具体用法?Python SdpRelaxation.solve怎么用?Python SdpRelaxation.solve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ncpol2sdpa.SdpRelaxation
的用法示例。
在下文中一共展示了SdpRelaxation.solve方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_maximum_violation
# 需要导入模块: from ncpol2sdpa import SdpRelaxation [as 别名]
# 或者: from ncpol2sdpa.SdpRelaxation import solve [as 别名]
def test_maximum_violation(self):
def expectation_values(measurement, outcomes):
exp_values = []
for k in range(len(measurement)):
exp_value = 0
for j in range(len(measurement[k])):
exp_value += outcomes[k][j] * measurement[k][j]
exp_values.append(exp_value)
return exp_values
E = generate_operators('E', 8, hermitian=True)
M, outcomes = [], []
for i in range(4):
M.append([E[2 * i], E[2 * i + 1]])
outcomes.append([1, -1])
A = [M[0], M[1]]
B = [M[2], M[3]]
substitutions = projective_measurement_constraints(A, B)
C = expectation_values(M, outcomes)
chsh = -(C[0] * C[2] + C[0] * C[3] + C[1] * C[2] - C[1] * C[3])
sdpRelaxation = SdpRelaxation(E, verbose=0)
sdpRelaxation.get_relaxation(1, objective=chsh,
substitutions=substitutions)
sdpRelaxation.solve()
self.assertTrue(abs(sdpRelaxation.primal + 2*np.sqrt(2)) < 10e-5)
示例2: test_solving
# 需要导入模块: from ncpol2sdpa import SdpRelaxation [as 别名]
# 或者: from ncpol2sdpa.SdpRelaxation import solve [as 别名]
def test_solving(self):
x = generate_variables(2, commutative=True)
g0 = 4 * x[0] ** 2 + x[0] * x[1] - 4 * x[1] ** 2 - 2.1 * x[0] ** 4 + 4 * x[1] ** 4 + x[0] ** 6 / 3
sdpRelaxation = SdpRelaxation(x)
sdpRelaxation.get_relaxation(3, objective=g0)
sdpRelaxation.solve()
self.assertTrue(abs(sdpRelaxation.primal + 1.0316282672706911) < 10e-5)
示例3: test_ground_state
# 需要导入模块: from ncpol2sdpa import SdpRelaxation [as 别名]
# 或者: from ncpol2sdpa.SdpRelaxation import solve [as 别名]
def test_ground_state(self):
length, n, h, U, t = 2, 0.8, 3.8, -6, 1
fu = generate_operators('fu', length)
fd = generate_operators('fd', length)
_b = flatten([fu, fd])
monomials = [[ci for ci in _b]]
monomials[-1].extend([Dagger(ci) for ci in _b])
monomials.append([cj*ci for ci in _b for cj in _b])
monomials.append([Dagger(cj)*ci for ci in _b for cj in _b])
monomials[-1].extend([cj*Dagger(ci)
for ci in _b for cj in _b])
monomials.append([Dagger(cj)*Dagger(ci)
for ci in _b for cj in _b])
hamiltonian = 0
for j in range(length):
hamiltonian += U * (Dagger(fu[j])*Dagger(fd[j]) * fd[j]*fu[j])
hamiltonian += -h/2*(Dagger(fu[j])*fu[j] - Dagger(fd[j])*fd[j])
for k in get_neighbors(j, len(fu), width=1):
hamiltonian += -t*Dagger(fu[j])*fu[k]-t*Dagger(fu[k])*fu[j]
hamiltonian += -t*Dagger(fd[j])*fd[k]-t*Dagger(fd[k])*fd[j]
momentequalities = [n-sum(Dagger(br)*br for br in _b)]
sdpRelaxation = SdpRelaxation(_b, verbose=0)
sdpRelaxation.get_relaxation(-1,
objective=hamiltonian,
momentequalities=momentequalities,
substitutions=fermionic_constraints(_b),
extramonomials=monomials)
sdpRelaxation.solve()
s = 0.5*(sum((Dagger(u)*u) for u in fu) -
sum((Dagger(d)*d) for d in fd))
magnetization = sdpRelaxation[s]
self.assertTrue(abs(magnetization-0.021325317328560453) < 10e-5)
示例4: test_solving_with_sdpa
# 需要导入模块: from ncpol2sdpa import SdpRelaxation [as 别名]
# 或者: from ncpol2sdpa.SdpRelaxation import solve [as 别名]
def test_solving_with_sdpa(self):
x = generate_variables('x', 2, commutative=True)
sdpRelaxation = SdpRelaxation(x)
sdpRelaxation.get_relaxation(2, objective=x[0]*x[1] + x[1]*x[0],
inequalities=[-x[1]**2 + x[1] + 0.5],
substitutions={x[0]**2: x[0]})
sdpRelaxation.solve(solver="sdpa")
self.assertTrue(abs(sdpRelaxation.primal + 0.7320505301965234) < 10e-5)
示例5: test_chordal_extension
# 需要导入模块: from ncpol2sdpa import SdpRelaxation [as 别名]
# 或者: from ncpol2sdpa.SdpRelaxation import solve [as 别名]
def test_chordal_extension(self):
X = generate_variables(3, commutative=True)
inequalities = [1 - X[0] ** 2 - X[1] ** 2, 1 - X[1] ** 2 - X[2] ** 2]
sdpRelaxation = SdpRelaxation(X)
sdpRelaxation.get_relaxation(
2, objective=X[1] - 2 * X[0] * X[1] + X[1] * X[2], inequalities=inequalities, chordal_extension=True
)
sdpRelaxation.solve()
self.assertTrue(abs(sdpRelaxation.primal + 2.2443690631722637) < 10e-5)
示例6: test_ground_state_energy
# 需要导入模块: from ncpol2sdpa import SdpRelaxation [as 别名]
# 或者: from ncpol2sdpa.SdpRelaxation import solve [as 别名]
def test_ground_state_energy(self):
N = 3
a = generate_variables(N, name="a")
substitutions = bosonic_constraints(a)
hamiltonian = sum(Dagger(a[i]) * a[i] for i in range(N))
sdpRelaxation = SdpRelaxation(a, verbose=0)
sdpRelaxation.get_relaxation(1, objective=hamiltonian, substitutions=substitutions)
sdpRelaxation.solve()
self.assertTrue(abs(sdpRelaxation.primal) < 10e-5)
示例7: test_max_cut
# 需要导入模块: from ncpol2sdpa import SdpRelaxation [as 别名]
# 或者: from ncpol2sdpa.SdpRelaxation import solve [as 别名]
def test_max_cut(self):
W = np.diag(np.ones(8), 1) + np.diag(np.ones(7), 2) + np.diag([1, 1], 7) + np.diag([1], 8)
W = W + W.T
Q = (np.diag(np.dot(np.ones(len(W)).T, W)) - W) / 4
x = generate_variables(len(W), commutative=True)
equalities = [xi ** 2 - 1 for xi in x]
objective = -np.dot(x, np.dot(Q, np.transpose(x)))
sdpRelaxation = SdpRelaxation(x)
sdpRelaxation.get_relaxation(1, objective=objective, equalities=equalities, removeequalities=True)
sdpRelaxation.solve()
self.assertTrue(abs(sdpRelaxation.primal + 4.5) < 10e-5)
示例8: ExampleNoncommutative
# 需要导入模块: from ncpol2sdpa import SdpRelaxation [as 别名]
# 或者: from ncpol2sdpa.SdpRelaxation import solve [as 别名]
class ExampleNoncommutative(unittest.TestCase):
def setUp(self):
X = generate_operators('x', 2, hermitian=True)
self.sdpRelaxation = SdpRelaxation(X)
self.sdpRelaxation.get_relaxation(2, objective=X[0]*X[1] + X[1]*X[0],
inequalities=[-X[1]**2 + X[1] + 0.5],
substitutions={X[0]**2: X[0]})
def tearDown(self):
clear_cache()
def test_solving_with_sdpa(self):
self.sdpRelaxation.solve(solver="sdpa")
self.assertTrue(abs(self.sdpRelaxation.primal + 0.75) < 10e-5)
def test_solving_with_mosek(self):
self.sdpRelaxation.solve(solver="mosek")
self.assertTrue(abs(self.sdpRelaxation.primal + 0.75) < 10e-5)
def test_solving_with_cvxopt(self):
self.sdpRelaxation.solve(solver="cvxopt")
self.assertTrue(abs(self.sdpRelaxation.primal + 0.75) < 10e-5)
def test_solving_with_cvxpy(self):
self.sdpRelaxation.solve(solver="cvxpy")
self.assertTrue(abs(self.sdpRelaxation.primal + 0.75) < 10e-5)
示例9: test_guessing_probability
# 需要导入模块: from ncpol2sdpa import SdpRelaxation [as 别名]
# 或者: from ncpol2sdpa.SdpRelaxation import solve [as 别名]
def test_guessing_probability(self):
p = [0.5, 0.5, 0.5, 0.5, 0.4267766952966368, 0.4267766952966368, 0.4267766952966368, 0.07322330470336313]
P = Probability([2, 2], [2, 2])
behaviour_constraint = [
P([0], [0], "A") - p[0],
P([0], [1], "A") - p[1],
P([0], [0], "B") - p[2],
P([0], [1], "B") - p[3],
P([0, 0], [0, 0]) - p[4],
P([0, 0], [0, 1]) - p[5],
P([0, 0], [1, 0]) - p[6],
P([0, 0], [1, 1]) - p[7],
]
behaviour_constraint.append("-0[0,0]+1.0")
sdpRelaxation = SdpRelaxation(P.get_all_operators(), normalized=False, verbose=0)
sdpRelaxation.get_relaxation(
1, objective=-P([0], [0], "A"), momentequalities=behaviour_constraint, substitutions=P.substitutions
)
sdpRelaxation.solve()
self.assertTrue(abs(sdpRelaxation.primal + 0.5) < 10e-5)
示例10: Probability
# 需要导入模块: from ncpol2sdpa import SdpRelaxation [as 别名]
# 或者: from ncpol2sdpa.SdpRelaxation import solve [as 别名]
# -*- coding: utf-8 -*-
"""
This example calculates the maximum quantum violation of the CHSH inequality in
the probability picture with a mixed-level relaxation of 1+AB.
Created on Mon Dec 1 14:19:08 2014
@author: Peter Wittek
"""
from ncpol2sdpa import Probability, SdpRelaxation, define_objective_with_I
level = 1
I = [[ 0, -1, 0 ],
[-1, 1, 1 ],
[ 0, 1, -1 ]]
P = Probability([2, 2], [2, 2])
objective = define_objective_with_I(I, P)
sdpRelaxation = SdpRelaxation(P.get_all_operators())
sdpRelaxation.get_relaxation(level, objective=objective,
substitutions=P.substitutions,
extramonomials=P.get_extra_monomials('AB'))
sdpRelaxation.solve()
print(sdpRelaxation.primal, sdpRelaxation.status)
示例11: generate_variables
# 需要导入模块: from ncpol2sdpa import SdpRelaxation [as 别名]
# 或者: from ncpol2sdpa.SdpRelaxation import solve [as 别名]
@author: Peter Wittek
"""
from ncpol2sdpa import generate_variables, SdpRelaxation
# Number of Hermitian variables
n_vars = 2
# Level of relaxation
level = 2
# Get Hermitian variables
X = generate_variables(n_vars, hermitian=True)
# Define the objective function
obj = X[0] * X[1] + X[1] * X[0]
# Inequality constraints
inequalities = [-X[1] ** 2 + X[1] + 0.5]
# Equality constraints
equalities = []
# Simple monomial substitutions
monomial_substitution = {}
monomial_substitution[X[0] ** 2] = X[0]
# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation(X, verbose=0)
sdpRelaxation.get_relaxation(level, objective=obj, inequalities=inequalities,
substitutions=monomial_substitution)
sdpRelaxation.solve(solver='mosek')
print(sdpRelaxation.primal, sdpRelaxation.dual)