Commit 9350adca authored by Peter Wittek's avatar Peter Wittek

Code reformatted

parent 19d7d7f1
......@@ -15,7 +15,6 @@ from ncpol2sdpa import flatten, SdpRelaxation, get_monomials
from ncpol2sdpa.nc_utils import get_support
def get_triang_ineqs(spins):
"""Generate the list of triangle inequalities involving the given spin
variables
......@@ -62,8 +61,7 @@ def get_det_guess(s_variables, cliques, Mom, one, two):
:returns: the deterministic configuration final_config as list of floats
"""
#taking a Choleski decomposition of each block in the moment matrix
Red = []
configs = []
......@@ -269,9 +267,9 @@ def get_strengthen_low(s_variables, substitutions, hamiltonian, cliques,
bound_new = cp.deepcopy(sdp.primal)
improv = abs(bound_new - bound_old) / abs(bound_init)
#if the relative improvement is good enough add the new inequalities
if (improv > 0.005):
ineqs = cp.deepcopy(new_ineqs)
......@@ -338,7 +336,6 @@ def get_up_and_low(s_variables, substitutions, ineqs, hamiltonian, one, two,
the deterministic configuration for each spin as list of floats
"""
time0 = time()
monomial_blocks = []
......@@ -376,7 +373,7 @@ def get_up_and_low(s_variables, substitutions, ineqs, hamiltonian, one, two,
return sdp.primal, bound, spins, config
def get_branching_node(spins,s_variables, cliques, eqs):
def get_branching_node(spins, s_variables, cliques, eqs):
"""Gets the spin on which to perform the next branching. Works with an "easy-first"
choice
......@@ -393,15 +390,15 @@ def get_branching_node(spins,s_variables, cliques, eqs):
"""
det = 1
sup = get_sup(s_variables,eqs)
for c,block in enumerate(spins):
for i,el in enumerate(block):
sup = get_sup(s_variables, eqs)
for c, block in enumerate(spins):
for i, el in enumerate(block):
if el not in sup:
if abs(el) < det:
det = cp.deepcopy(abs(el))
branch_node = cp.deepcopy(cliques[c][i])
......@@ -419,7 +416,7 @@ def get_as_coeffs(hamiltonian, s_variables):
:returns: the one-body coeffs as list of float, the two-body coeffs as list
of list of float
"""
one = []
two = []
pol = hamiltonian.as_poly()
......@@ -452,7 +449,7 @@ def get_sup(s_variables, eqs):
:returns: support of the branching equations as list of sympy.core.symbol.Symbol
"""
sup = []
for el in eqs:
......@@ -463,9 +460,13 @@ def get_sup(s_variables, eqs):
return sup
def get_groundBandB(s_variables,substitutions,hamiltonian,cliques,threshold,
solverparameters,verbose=0):
def get_groundBandB(s_variables,
substitutions,
hamiltonian,
cliques,
threshold,
solverparameters,
verbose=0):
"""Runs the whole branch&bound algorithm for the given hamiltonian
:param s_variables: list of spin variables
......@@ -522,7 +523,7 @@ def get_groundBandB(s_variables,substitutions,hamiltonian,cliques,threshold,
if ((z_low - z_up) / abs(z_up) < -0.00001):
node = get_branching_node(spins,s_variables, cliques, eqs)
node = get_branching_node(spins, s_variables, cliques, eqs)
tree = [[z_low, eqs, substitutions, node]]
while (len(tree) > 0):
......@@ -561,7 +562,7 @@ def get_groundBandB(s_variables,substitutions,hamiltonian,cliques,threshold,
if ((z_low - z_up) / abs(z_up) < -0.00001):
node_new = get_branching_node(spins,s_variables, cliques, eqs_new)
node_new = get_branching_node(spins, s_variables, cliques, eqs_new)
tree.append([z_low, eqs_new, subs_new, node_new])
# branch -1
......@@ -593,7 +594,7 @@ def get_groundBandB(s_variables,substitutions,hamiltonian,cliques,threshold,
print("The upper bound is " + str(z_up))
if ((z_low - z_up) / abs(z_up) < -0.00001):
node_new = get_branching_node(spins,s_variables, cliques, eqs_new)
node_new = get_branching_node(spins, s_variables, cliques, eqs_new)
tree.append([z_low, eqs_new, subs_new, node_new])
# Check all the previous nodes to see if we can eliminate some with
......
......@@ -23,7 +23,7 @@ def generate_commuting_variables(party, label):
:type label: str
:returns: list of sympy.core.symbol.Symbol
"""
variables = []
for i in range(len(party)):
variables.append(
......@@ -54,7 +54,6 @@ def get_square2D_spins(k, l, configuration):
return s_variables
def get_2Dsquare_localdisorder(k, l, sigma):
"""Generates random local magnetic field values for a 2D square lattice
distributed according to a Gaussian with zero mean and variance sigma
......@@ -87,7 +86,7 @@ def get_2Dsquare_localdisorder(k, l, sigma):
return local
def get_2Dsquare_ferromagneticdisorder_hamiltonian(k,l,local, s_variables):
def get_2Dsquare_ferromagneticdisorder_hamiltonian(k, l, local, s_variables):
"""Generates the hamiltonian of a ferromagnetic 2D Ising model and local
mangetic fields as a polynomial in the spin variables
......@@ -142,13 +141,15 @@ def get_lattice_spins(lattice, configuration):
and sympy.core.symbol.Symbol as values representing the spins
"""
s_variables = {}
for i,node in enumerate(lattice.nodes()):
s_variables[node] = generate_commuting_variables(configuration, chr(65 + i))
for i, node in enumerate(lattice.nodes()):
s_variables[node] = generate_commuting_variables(
configuration, chr(65 + i))
return s_variables
def get_lattice_localdisorder(lattice,sigma):
def get_lattice_localdisorder(lattice, sigma):
"""Generates the random magneitc field on each node of the lattice, according to
a gaussian distribution with zero mean and variance sigma
......@@ -160,13 +161,13 @@ def get_lattice_localdisorder(lattice,sigma):
:returns: the local field as dictionary with tuple as keys representing the nodes
and float as values
"""
h = {}
for node in lattice.nodes():
h[node] = np.random.normal(0,sigma)
h[node] = np.random.normal(0, sigma)
return h
......@@ -179,16 +180,17 @@ def get_lattice_ferromangetic(lattice):
:returns: the couplings as dictionary with tuple as keys representing the edges
and float as values
"""
J = {}
for edge in lattice.edges():
J[edge] = -1
return J
def get_lattice_hamiltonian(lattice,h,J,s_variables):
def get_lattice_hamiltonian(lattice, h, J, s_variables):
"""Generates the hamiltonian for the given lattice with the given local fields h
and couplings J as a polynomial in the spin variables
......@@ -203,15 +205,16 @@ def get_lattice_hamiltonian(lattice,h,J,s_variables):
:returns: the hamiltonian as sympy.core.add.Add
"""
hamiltonian = 0
for node in lattice.nodes():
hamiltonian += h[node]*s_variables[node][0][0]
hamiltonian += h[node] * s_variables[node][0][0]
for edge in lattice.edges():
hamiltonian += J[edge]*s_variables[edge[0]][0][0]*s_variables[edge[1]][0][0]
return hamiltonian
\ No newline at end of file
hamiltonian += J[edge] * s_variables[edge[0]][0][0] * s_variables[edge[
1]][0][0]
return hamiltonian
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment