Beispiel #1
0
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)
Beispiel #2
0
 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]})
Beispiel #3
0
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)
Beispiel #4
0
 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)
Beispiel #5
0
    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)
def __main__():   
  # chsh quantum bound : 4
  # mermin quantum bound : 4
  # svetlichny quantum bound 4sqrt(2) ~ 5.65 

  P = Probability([2, 2], [2, 2], [2, 2])
  Chsh = chsh(P)
  Mermin = mermin(P)
  Svetlichny = svetlichny(P)
   
  dx = 0
  div = 1.0
  step = 0.4
  lim = 5.6
  x = []
  y = []
  while dx  <= lim :
  
    print (dx)
    ineq = []
    ineq.append(Svetlichny - dx)
    ineq.append(-1*Svetlichny + dx)
    sdpRelaxation = SdpRelaxation(P.get_all_operators(), verbose=0)
    sdpRelaxation.get_relaxation(2, substitutions = P.substitutions,inequalities = ineq)
    sdpRelaxation.set_objective(-Chsh)
    solve_sdp(sdpRelaxation, solver="cvxopt")
    x.append(dx)
    y.append(abs(sdpRelaxation.primal)/div)
    dx = dx + step 

  plt.plot(x,y,'k')   
  plt.show()   
Beispiel #7
0
 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)
Beispiel #8
0
 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)
Beispiel #9
0
 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)
Beispiel #10
0
 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)
Beispiel #11
0
 def test_maximum_violation(self):
     I = [[0, -1, 0], [-1, 1, 1], [0, 1, -1]]
     P = Probability([2, 2], [2, 2])
     relaxation = SdpRelaxation(P.get_all_operators())
     relaxation.get_relaxation(1,
                               objective=define_objective_with_I(I, P),
                               substitutions=P.substitutions,
                               extramonomials=P.get_extra_monomials('AB'))
     relaxation.solve()
     self.assertTrue(abs(relaxation.primal + (np.sqrt(2)-1)/2) < 10e-5)
Beispiel #12
0
 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)
Beispiel #13
0
 def setUp(self):
     X = generate_variables(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]},
     )
Beispiel #14
0
 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)
Beispiel #15
0
def __main__():
    # chsh quantum bound : 4
    # mermin quantum bound : 4
    # svetlichny quantum bound 4sqrt(2) ~ 5.65

    P = Probability([2, 2], [2, 2], [2, 2])
    Chsh = chsh(P)
    Mermin = mermin(P)
    Svetlichny = svetlichny(P)

    dx = 0
    div = 1.0
    step = 0.4
    lim = 5.6
    x = []
    y = []
    while dx <= lim:

        print(dx)
        ineq = []
        ineq.append(Svetlichny - dx)
        ineq.append(-1 * Svetlichny + dx)
        sdpRelaxation = SdpRelaxation(P.get_all_operators(), verbose=0)
        sdpRelaxation.get_relaxation(2,
                                     substitutions=P.substitutions,
                                     inequalities=ineq)
        sdpRelaxation.set_objective(-Chsh)
        solve_sdp(sdpRelaxation, solver="cvxopt")
        x.append(dx)
        y.append(abs(sdpRelaxation.primal) / div)
        dx = dx + step

    plt.plot(x, y, 'k')
    plt.show()
Beispiel #16
0
 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)
Beispiel #17
0
 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)
Beispiel #18
0
    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)
 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)
Beispiel #20
0
 def test_solving(self):
     x = generate_variables('x', 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)
 def test_chordal_extension(self):
     X = generate_variables('x', 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)
 def test_ground_state_energy(self):
     N = 3
     a = generate_operators('a', N)
     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)
Beispiel #23
0
 def test_maximum_violation(self):
     I = [[0, -1, 0], [-1, 1, 1], [0, 1, -1]]
     P = Probability([2, 2], [2, 2])
     relaxation = SdpRelaxation(P.get_all_operators())
     relaxation.get_relaxation(1,
                               objective=define_objective_with_I(I, P),
                               substitutions=P.substitutions,
                               extramonomials=P.get_extra_monomials('AB'))
     relaxation.solve()
     self.assertTrue(abs(relaxation.primal + (np.sqrt(2) - 1) / 2) < 10e-5)
 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('x', 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 + 13.5) < 10e-5)
Beispiel #25
0
Kim, S. & Kojima, M. (2012). Exploiting Sparsity in SDP Relaxation of
Polynomial Optimization Problems. In Handbook on Semidefinite, Conic and
Polynomial Optimization. Springer, 2012, 499--531.

Created on Sun Nov 30 19:18:04 2014

@author: Peter Wittek
"""

from ncpol2sdpa import generate_variables, SdpRelaxation, solve_sdp

# Number of variables
n_vars = 3
# Level of relaxation
level = 2

# Get commutative variables
X = generate_variables(n_vars, commutative=True)

# Define the objective function
obj = X[1] - 2 * X[0] * X[1] + X[1] * X[2]

# Inequality constraints
inequalities = [1 - X[0]**2 - X[1]**2, 1 - X[1]**2 - X[2]**2]

# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation(X, hierarchy="npa_chordal")
sdpRelaxation.get_relaxation(level, objective=obj, inequalities=inequalities)
print(solve_sdp(sdpRelaxation))
Beispiel #26
0
"""

from ncpol2sdpa import generate_variables, SdpRelaxation, convert_to_picos

# 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 = []
equalities.append(X[0]**2 - X[0])

# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation(X)
sdpRelaxation.get_relaxation(level,
                             objective=obj,
                             inequalities=inequalities,
                             equalities=equalities)
P = convert_to_picos(sdpRelaxation)
P.solve()
Beispiel #27
0
Section 5.12 of the following paper:

Henrion, D.; Lasserre, J. & Löfberg, J. GloptiPoly 3: moments, optimization and
semidefinite programming. Optimization Methods & Software, 2009, 24, 761-779

Created on Thu May 15 12:12:40 2014

@author: wittek
"""
import numpy as np
from ncpol2sdpa import SdpRelaxation, generate_variables

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
n = len(W)
e = np.ones(n)
Q = (np.diag(np.dot(e.T, W)) - W) / 4

x = generate_variables(n, commutative=True)
equalities = [xi ** 2 - 1 for xi in x]

objective = -np.dot(x, np.dot(Q, np.transpose(x)))

level = 1

sdpRelaxation = SdpRelaxation(x)
sdpRelaxation.get_relaxation(level, objective=objective, equalities=equalities,
                             removeequalities=True)
sdpRelaxation.write_to_file("max_cut.dat-s")
"""
import time
from sympy.physics.quantum.dagger import Dagger
from ncpol2sdpa import generate_variables, SdpRelaxation,\
                       bosonic_constraints

# Level of relaxation
level = 1

# Number of variables
N = 3

# Parameters for the Hamiltonian
hbar, omega = 1, 1

# Define ladder operators
a = generate_variables(N, name='a')
substitutions = bosonic_constraints(a)

hamiltonian = sum(hbar * omega * (Dagger(a[i]) * a[i]) for i in range(N))

time0 = time.time()
# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation(a, verbose=1)
sdpRelaxation.get_relaxation(level, objective=hamiltonian,
                             substitutions=substitutions)
# Export relaxation to SDPA format
sdpRelaxation.write_to_file("harmonic_oscillator.dat-s")

print('%0.2f s' % ((time.time() - time0)))
Beispiel #29
0
    return exp_values

# Number of Hermitian variables
n_vars = 8
# Level of relaxation
level = 1

# Get Hermitian variables
E = generate_variables(n_vars, name='E', hermitian=True)

# Define measurements and outcomes
M, outcomes = [], []
for i in range(int(n_vars / 2)):
    M.append([E[2 * i], E[2 * i + 1]])
    outcomes.append([1, -1])

# Define which measurements Alice and Bob have
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=2)
sdpRelaxation.get_relaxation(level, objective=chsh,
                             substitutions=substitutions)
print(solve_sdp(sdpRelaxation))
Beispiel #30
0
# -*- 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)
@author: Peter Wittek
"""

from ncpol2sdpa import generate_variables, SdpRelaxation, convert_to_picos

# 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 = []
equalities.append(X[0] ** 2 - X[0])

# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation(X)
sdpRelaxation.get_relaxation(level, objective=obj,
                             inequalities=inequalities,
                             equalities=equalities)
P = convert_to_picos(sdpRelaxation)
P.solve()
Beispiel #32
0
                       write_to_sdpa, bosonic_constraints

# Level of relaxation
level = 1

# Number of variables
N = 3

# Parameters for the Hamiltonian
hbar, omega = 1, 1

# Define ladder operators
a = generate_variables(N, name='a')
substitutions = bosonic_constraints(a)

hamiltonian = 0
for i in range(N):
    hamiltonian += hbar * omega * (Dagger(a[i]) * a[i])


time0 = time.time()
# Obtain SDP relaxation
print("Obtaining SDP relaxation...")
sdpRelaxation = SdpRelaxation(a, verbose=1)
sdpRelaxation.get_relaxation(level, objective=hamiltonian,
                             substitutions=substitutions)
# Export relaxation to SDPA format
write_to_sdpa(sdpRelaxation, 'harmonic_oscillator.dat-s')

print('%0.2f s' % ((time.time() - time0)))
Beispiel #33
0
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 generate_measurements, \
                       projective_measurement_constraints, flatten, \
                       SdpRelaxation, define_objective_with_I, solve_sdp

level = 1
A_configuration = [2, 2]
B_configuration = [2, 2]
I = [[ 0,   -1,    0 ],
     [-1,    1,    1 ],
     [ 0,    1,   -1 ]]
A = generate_measurements(A_configuration, 'A')
B = generate_measurements(B_configuration, 'B')
monomial_substitutions = projective_measurement_constraints(
    A, B)
objective = define_objective_with_I(I, A, B)

AB = [Ai*Bj for Ai in flatten(A) for Bj in flatten(B)]

sdpRelaxation = SdpRelaxation(flatten([A, B]))
sdpRelaxation.get_relaxation(level, objective=objective,
                             substitutions=monomial_substitutions,
                             extramonomials=AB)
print(solve_sdp(sdpRelaxation))
Beispiel #34
0
# Get Hermitian variables
X = generate_variables(n_vars, hermitian=True)

# Define the objective function
obj = 0
for i in range(n_vars):
    for j in range(n_vars):
        obj += X[i] * X[j]

# Equality constraints
equalities = []
for i in range(n_vars):
    equalities.append(X[i] * X[i] - 1.0)

# Simple monomial substitutions
substitutions = {}
for i in range(n_vars):
    for j in range(i + 1, n_vars):
        # [X_i, X_j] = 0
        substitutions[X[i] * X[j]] = X[j] * X[i]

# Obtain SDP relaxation
time0 = time.time()
sdpRelaxation = SdpRelaxation(X)
sdpRelaxation.get_relaxation(level,
                             objective=obj,
                             equalities=equalities,
                             substitutions=substitutions)
write_to_sdpa(sdpRelaxation, 'benchmark.dat-s')
print('%0.2f s' % ((time.time() - time0)))
Beispiel #35
0
# -*- coding: utf-8 -*-
"""
This script replicates the results of gloptipolydemo.m, which is packaged with
Gloptipoly3.

Created on Thu May 15 11:16:58 2014

@author: wittek
"""
from ncpol2sdpa import SdpRelaxation, generate_variables

# Get commutative variables
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

# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation(x)
sdpRelaxation.get_relaxation(3, objective=g0)
sdpRelaxation.write_to_file("gloptipoly_demo.dat-s")
Created on Fri May 10 09:45:11 2013

@author: Peter Wittek
"""

from ncpol2sdpa import generate_variables, SdpRelaxation

# Number of variables
n_vars = 2
# Level of relaxation
level = 2

# Get commutative variables
X = generate_variables(n_vars, commutative=True)

# Define the objective function
obj = X[0] * X[1] + X[1] * X[0]

# Inequality constraints
inequalities = [-X[1] ** 2 + X[1] + 0.5]

# Simple monomial substitutions
monomial_substitution = {}
monomial_substitution[X[0] ** 2] = X[0]

# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation(X)
sdpRelaxation.get_relaxation(level, objective=obj, inequalities=inequalities,
                             substitutions=monomial_substitution)
sdpRelaxation.write_to_file('example_commutative.dat-s')
# -*- coding: utf-8 -*-
"""
This script replicates the results of gloptipolydemo.m, which is packaged with
Gloptipoly3.

Created on Thu May 15 11:16:58 2014

@author: wittek
"""
from sympy.physics.quantum.operator import HermitianOperator
from ncpol2sdpa import SdpRelaxation, write_to_sdpa

# Get commutative variables
x1 = HermitianOperator("x1")
x1.is_commutative = True
x2 = HermitianOperator("x2")
x2.is_commutative = True

g0 = 4 * x1 ** 2 + x1 * x2 - 4 * x2 ** 2 - \
    2.1 * x1 ** 4 + 4 * x2 ** 4 + x1 ** 6 / 3

# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation([x1, x2])
sdpRelaxation.get_relaxation(3, objective=g0)
write_to_sdpa(sdpRelaxation, 'gloptipoly_demo.dat-s')
Beispiel #38
0
level = 1

# Get Hermitian variables
X = generate_variables(n_vars, hermitian=True)

# Define the objective function
obj = 0
for i in range(n_vars):
    for j in range(n_vars):
        obj += X[i] * X[j]

# Equality constraints
equalities = []
for i in range(n_vars):
    equalities.append(X[i] * X[i] - 1.0)

# Simple monomial substitutions
substitutions = {}
for i in range(n_vars):
    for j in range(i + 1, n_vars):
        # [X_i, X_j] = 0
        substitutions[X[i] * X[j]] = X[j] * X[i]

# Obtain SDP relaxation
time0 = time.time()
sdpRelaxation = SdpRelaxation(X)
sdpRelaxation.get_relaxation(level, objective=obj, equalities=equalities,
                             substitutions=substitutions)
sdpRelaxation.write_to_file('benchmark.dat-s')
print('%0.2f s' % ((time.time() - time0)))
Beispiel #39
0
Section 5.12 of the following paper:

Henrion, D.; Lasserre, J. & Löfberg, J. GloptiPoly 3: moments, optimization and
semidefinite programming. Optimization Methods & Software, 2009, 24, 761-779

Created on Thu May 15 12:12:40 2014

@author: wittek
"""
import numpy as np
from ncpol2sdpa import SdpRelaxation, write_to_sdpa, generate_variables

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
n = len(W)
e = np.ones(n)
Q = (np.diag(np.dot(e.T, W)) - W) / 4

x = generate_variables(n, commutative=True)
equalities = [xi ** 2 - 1 for xi in x]

objective = -np.dot(x, np.dot(Q, np.transpose(x)))

level = 1

sdpRelaxation = SdpRelaxation(x)
sdpRelaxation.get_relaxation(level, objective=objective, equalities=equalities,
                             removeequalities=True)
write_to_sdpa(sdpRelaxation, 'max_cut.dat-s')
Beispiel #40
0
@author: Peter Wittek
"""

from ncpol2sdpa import generate_variables, SdpRelaxation, write_to_sdpa

# Number of variables
n_vars = 2
# Level of relaxation
level = 2

# Get commutative variables
X = generate_variables(n_vars, commutative=True)

# Define the objective function
obj = X[0] * X[1] + X[1] * X[0]

# Inequality constraints
inequalities = [-X[1]**2 + X[1] + 0.5]

# Simple monomial substitutions
monomial_substitution = {}
monomial_substitution[X[0]**2] = X[0]

# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation(X)
sdpRelaxation.get_relaxation(level,
                             objective=obj,
                             inequalities=inequalities,
                             substitutions=monomial_substitution)
write_to_sdpa(sdpRelaxation, 'example_commutative.dat-s')
# Number of variables
N = 3

# Parameters for the Hamiltonian
hbar, omega = 1, 1

# Define ladder operators
a = generate_variables(N, name='a')

hamiltonian = 0
for i in range(N):
    hamiltonian += hbar * omega * (Dagger(a[i]) * a[i] + 0.5)

substitutions, equalities = bosonic_constraints(a)
inequalities = []

time0 = time.time()
# Obtain SDP relaxation
print("Obtaining SDP relaxation...")
sdpRelaxation = SdpRelaxation(a, verbose=1)
sdpRelaxation.get_relaxation(level, objective=hamiltonian,
                             equalities=equalities,
                             substitutions=substitutions,
                             removeequalities=True)
# Export relaxation to SDPA format
print("Writing to disk...")
write_to_sdpa(sdpRelaxation, 'harmonic_oscillator.dat-s')

print('%0.2f s' % ((time.time() - time0)))
Beispiel #42
0
Created on Fri May 10 09:45:11 2013

@author: Peter Wittek
"""

from ncpol2sdpa import generate_variables, SdpRelaxation, write_to_sdpa

# Number of variables
n_vars = 2
# Level of relaxation
level = 2

# Get commutative variables
X = generate_variables(n_vars, commutative=True)

# Define the objective function
obj = X[0] * X[1] + X[1] * X[0]

# Inequality constraints
inequalities = [-X[1] ** 2 + X[1] + 0.5]

# Simple monomial substitutions
monomial_substitution = {}
monomial_substitution[X[0] ** 2] = X[0]

# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation(X)
sdpRelaxation.get_relaxation(level, objective=obj, inequalities=inequalities,
                             substitutions=monomial_substitution)
write_to_sdpa(sdpRelaxation, 'example_commutative.dat-s')
Beispiel #43
0
# -*- coding: utf-8 -*-
"""
This example 18.1 from the following paper:

Kim, S. & Kojima, M. (2012). Exploiting Sparsity in SDP Relaxation of
Polynomial Optimization Problems. In Handbook on Semidefinite, Conic and
Polynomial Optimization. Springer, 2012, 499--531.

Created on Sun Nov 30 19:18:04 2014

@author: Peter Wittek
"""

from ncpol2sdpa import generate_variables, SdpRelaxation

level = 2
X = generate_variables(3, commutative=True)
inequalities = [1-X[0]**2-X[1]**2 >= 0,
                1-X[1]**2-X[2]**2 >= 0]
sdpRelaxation = SdpRelaxation(X, verbose=2)
sdpRelaxation.get_relaxation(level, objective=X[1] - 2*X[0]*X[1] + X[1]*X[2],
                             inequalities=inequalities, chordal_extension=True)
sdpRelaxation.solve()
sdpRelaxation.write_to_file("/home/wittek/sparse2.csv")
print(sdpRelaxation.primal, sdpRelaxation.dual, sdpRelaxation.status)