|
@ -4,9 +4,10 @@ import numpy as np |
|
|
from . import kSAT |
|
|
from . import kSAT |
|
|
from tqdm import tqdm |
|
|
from tqdm import tqdm |
|
|
import math |
|
|
import math |
|
|
|
|
|
import random |
|
|
|
|
|
|
|
|
__VERTEX_WEIGHT__ = -1 |
|
|
|
|
|
__EDGE_WEIGHT__ = 2 |
|
|
|
|
|
|
|
|
__VERTEX_WEIGHT__ = -2#-1 |
|
|
|
|
|
__EDGE_WEIGHT__ = 2#2 |
|
|
|
|
|
|
|
|
def WMISdictQUBO(kSATInstance): |
|
|
def WMISdictQUBO(kSATInstance): |
|
|
quboInstance = {} |
|
|
quboInstance = {} |
|
@ -32,6 +33,37 @@ def WMISdictQUBO(kSATInstance): |
|
|
|
|
|
|
|
|
return quboInstance |
|
|
return quboInstance |
|
|
|
|
|
|
|
|
|
|
|
def WMISdictQUBO_2(kSATInstance): |
|
|
|
|
|
quboInstance = {} |
|
|
|
|
|
|
|
|
|
|
|
for clauseIndex in range(kSATInstance.getNumberOfClauses()): |
|
|
|
|
|
clause = kSATInstance.getClause(clauseIndex) |
|
|
|
|
|
|
|
|
|
|
|
# build triangles |
|
|
|
|
|
for varIndexInClause in range(len(clause)): |
|
|
|
|
|
lit = clause[varIndexInClause] |
|
|
|
|
|
var = abs(lit) |
|
|
|
|
|
|
|
|
|
|
|
aux = "z{}_{}".format(clauseIndex, var) |
|
|
|
|
|
var_node = "x{}".format(var) |
|
|
|
|
|
|
|
|
|
|
|
if lit < 0: |
|
|
|
|
|
quboInstance[(aux, aux)] = __VERTEX_WEIGHT__ |
|
|
|
|
|
quboInstance[(var_node, aux)] = __EDGE_WEIGHT__ |
|
|
|
|
|
else: |
|
|
|
|
|
quboInstance[(var_node, aux)] = __VERTEX_WEIGHT__ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(varIndexInClause + 1, len(clause)): |
|
|
|
|
|
var2 = abs(clause[i]) |
|
|
|
|
|
|
|
|
|
|
|
aux2 = "z{}_{}".format(clauseIndex, var2) |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(aux, aux2)] = __EDGE_WEIGHT__ |
|
|
|
|
|
|
|
|
|
|
|
return quboInstance |
|
|
|
|
|
|
|
|
# only 3sat |
|
|
# only 3sat |
|
|
def primitiveQUBO(sat): |
|
|
def primitiveQUBO(sat): |
|
|
quboInstance = {} |
|
|
quboInstance = {} |
|
@ -47,10 +79,10 @@ def primitiveQUBO(sat): |
|
|
lit1 = "c{}_l{}".format(clauseIndex, clause[0]) |
|
|
lit1 = "c{}_l{}".format(clauseIndex, clause[0]) |
|
|
lit2 = "c{}_l{}".format(clauseIndex, clause[1]) |
|
|
lit2 = "c{}_l{}".format(clauseIndex, clause[1]) |
|
|
lit3 = "c{}_l{}".format(clauseIndex, clause[2]) |
|
|
lit3 = "c{}_l{}".format(clauseIndex, clause[2]) |
|
|
aux1 = "a{}_{}".format(clauseIndex, 1) |
|
|
|
|
|
aux2 = "a{}_{}".format(clauseIndex, 2) |
|
|
|
|
|
aux3 = "a{}_{}".format(clauseIndex, 3) |
|
|
|
|
|
aux4 = "a{}_{}".format(clauseIndex, 4) |
|
|
|
|
|
|
|
|
aux1 = "z{}_{}".format(clauseIndex, 1) |
|
|
|
|
|
aux2 = "z{}_{}".format(clauseIndex, 2) |
|
|
|
|
|
aux3 = "z{}_{}".format(clauseIndex, 3) |
|
|
|
|
|
aux4 = "z{}_{}".format(clauseIndex, 4) |
|
|
|
|
|
|
|
|
quboInstance[(lit1, lit1)] = 1; |
|
|
quboInstance[(lit1, lit1)] = 1; |
|
|
quboInstance[(lit2, lit2)] = 1; |
|
|
quboInstance[(lit2, lit2)] = 1; |
|
@ -91,7 +123,15 @@ def primitiveQUBO(sat): |
|
|
longestChain = len(nodes) |
|
|
longestChain = len(nodes) |
|
|
|
|
|
|
|
|
if lit > 0 and -1 * lit in chains: |
|
|
if lit > 0 and -1 * lit in chains: |
|
|
quboInstance[(chains[lit][0], chains[-1*lit][0])] = 2 |
|
|
|
|
|
|
|
|
len_smaller_chain = min(len(chains[lit]), len(chains[-lit])) |
|
|
|
|
|
|
|
|
|
|
|
indices = random.sample(list(range(len_smaller_chain)), |
|
|
|
|
|
round(len_smaller_chain / 2)) |
|
|
|
|
|
|
|
|
|
|
|
for index in indices: |
|
|
|
|
|
quboInstance[(chains[lit][index], chains[-1*lit][index])] = 10 |
|
|
|
|
|
|
|
|
|
|
|
#quboInstance[(chains[lit][0], chains[-1*lit][0])] = 2 |
|
|
|
|
|
|
|
|
print("longest chain = {}".format(longestChain)) |
|
|
print("longest chain = {}".format(longestChain)) |
|
|
|
|
|
|
|
@ -101,6 +141,574 @@ def primitiveQUBO(sat): |
|
|
nodes.pop(0) |
|
|
nodes.pop(0) |
|
|
|
|
|
|
|
|
return quboInstance |
|
|
return quboInstance |
|
|
|
|
|
|
|
|
|
|
|
# only 3sat |
|
|
|
|
|
def primitiveQUBO_2(sat): |
|
|
|
|
|
quboInstance = {} |
|
|
|
|
|
|
|
|
|
|
|
chains = {} |
|
|
|
|
|
|
|
|
|
|
|
for clauseIndex in range(sat.getNumberOfClauses()): |
|
|
|
|
|
clause = sat.getClause(clauseIndex) |
|
|
|
|
|
|
|
|
|
|
|
lit1 = clause[0] |
|
|
|
|
|
lit2 = clause[1] |
|
|
|
|
|
lit3 = clause[2] |
|
|
|
|
|
|
|
|
|
|
|
var1 = abs(lit1) |
|
|
|
|
|
var2 = abs(lit2) |
|
|
|
|
|
var3 = abs(lit3) |
|
|
|
|
|
|
|
|
|
|
|
sign1 = 1 if lit1 > 0 else -1 |
|
|
|
|
|
sign2 = 1 if lit2 > 0 else -1 |
|
|
|
|
|
sign3 = 1 if lit3 > 0 else -1 |
|
|
|
|
|
|
|
|
|
|
|
node_var1 = "x{}".format(var1) |
|
|
|
|
|
node_var2 = "x{}".format(var2) |
|
|
|
|
|
node_var3 = "x{}".format(var3) |
|
|
|
|
|
|
|
|
|
|
|
node_aux1 = "a{}_{}".format(clauseIndex, 1) |
|
|
|
|
|
node_aux2 = "a{}_{}".format(clauseIndex, 2) |
|
|
|
|
|
node_aux3 = "a{}_{}".format(clauseIndex, 3) |
|
|
|
|
|
node_aux4 = "a{}_{}".format(clauseIndex, 4) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_var1, node_var1)] = 1 * sign1 |
|
|
|
|
|
quboInstance[(node_var2, node_var2)] = 1 * sign2 |
|
|
|
|
|
quboInstance[(node_var3, node_var3)] = 1 * sign3 |
|
|
|
|
|
quboInstance[(node_aux1, node_aux1)] = -2 |
|
|
|
|
|
quboInstance[(node_aux2, node_aux2)] = 1 |
|
|
|
|
|
quboInstance[(node_aux3, node_aux3)] = -2 |
|
|
|
|
|
quboInstance[(node_aux4, node_aux4)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_var1, node_var2)] = 1 * sign1 * sign2 |
|
|
|
|
|
quboInstance[(node_var1, node_aux1)] = -2 * sign1 |
|
|
|
|
|
quboInstance[(node_var2, node_aux1)] = -2 * sign2 |
|
|
|
|
|
quboInstance[(node_aux1, node_aux2)] = -2 |
|
|
|
|
|
quboInstance[(node_aux2, node_var3)] = 1 * sign3 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_var3, node_aux3)] = -2 * sign3 |
|
|
|
|
|
quboInstance[(node_aux2, node_aux3)] = -2 |
|
|
|
|
|
quboInstance[(node_var3, node_aux4)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
return quboInstance |
|
|
|
|
|
|
|
|
|
|
|
# only 3sat |
|
|
|
|
|
def primitiveQUBO_3(sat): |
|
|
|
|
|
quboInstance = {} |
|
|
|
|
|
|
|
|
|
|
|
chains = {} |
|
|
|
|
|
|
|
|
|
|
|
lits = {} |
|
|
|
|
|
vars = {} |
|
|
|
|
|
|
|
|
|
|
|
n_clauses = sat.getNumberOfClauses() |
|
|
|
|
|
|
|
|
|
|
|
for clauseIndex in range(sat.getNumberOfClauses()): |
|
|
|
|
|
clause = sat.getClause(clauseIndex) |
|
|
|
|
|
|
|
|
|
|
|
lit1 = clause[0] |
|
|
|
|
|
lit2 = clause[1] |
|
|
|
|
|
lit3 = clause[2] |
|
|
|
|
|
|
|
|
|
|
|
lits[lit1] = True |
|
|
|
|
|
lits[lit2] = True |
|
|
|
|
|
lits[lit3] = True |
|
|
|
|
|
|
|
|
|
|
|
var1 = abs(lit1) |
|
|
|
|
|
var2 = abs(lit2) |
|
|
|
|
|
var3 = abs(lit3) |
|
|
|
|
|
|
|
|
|
|
|
vars[var1] = True |
|
|
|
|
|
vars[var2] = True |
|
|
|
|
|
vars[var3] = True |
|
|
|
|
|
|
|
|
|
|
|
node_lit1 = "x{}".format(lit1) |
|
|
|
|
|
node_lit2 = "x{}".format(lit2) |
|
|
|
|
|
node_lit3 = "x{}".format(lit3) |
|
|
|
|
|
|
|
|
|
|
|
node_aux1 = "z{}_{}".format(clauseIndex, 1) |
|
|
|
|
|
node_aux2 = "z{}_{}".format(clauseIndex, 2) |
|
|
|
|
|
node_aux3 = "z{}_{}".format(clauseIndex, 3) |
|
|
|
|
|
node_aux4 = "z{}_{}".format(clauseIndex, 4) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_lit1, node_lit1)] = 1 |
|
|
|
|
|
quboInstance[(node_lit2, node_lit2)] = 1 |
|
|
|
|
|
quboInstance[(node_lit3, node_lit3)] = 1 |
|
|
|
|
|
quboInstance[(node_aux1, node_aux1)] = -2 |
|
|
|
|
|
quboInstance[(node_aux2, node_aux2)] = 1 |
|
|
|
|
|
quboInstance[(node_aux3, node_aux3)] = -2 |
|
|
|
|
|
quboInstance[(node_aux4, node_aux4)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_lit1, node_lit2)] = 1 |
|
|
|
|
|
quboInstance[(node_lit1, node_aux1)] = -2 |
|
|
|
|
|
quboInstance[(node_lit2, node_aux1)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux1, node_aux2)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux2, node_lit3)] = 1 |
|
|
|
|
|
quboInstance[(node_lit3, node_aux3)] = -2 |
|
|
|
|
|
quboInstance[(node_aux2, node_aux3)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux3, node_aux4)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
for var in vars.keys(): |
|
|
|
|
|
if var in lits and -var in lits: |
|
|
|
|
|
node_var = "x{}".format(var) |
|
|
|
|
|
node_nvar = "x{}".format(-var) |
|
|
|
|
|
|
|
|
|
|
|
print((node_var, node_nvar)) |
|
|
|
|
|
quboInstance[(node_var, node_nvar)] = 2 |
|
|
|
|
|
|
|
|
|
|
|
return quboInstance |
|
|
|
|
|
|
|
|
|
|
|
def primitiveQUBO_4(sat): |
|
|
|
|
|
quboInstance = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clauses_per_lit = {} |
|
|
|
|
|
|
|
|
|
|
|
lits = {} |
|
|
|
|
|
vars = {} |
|
|
|
|
|
|
|
|
|
|
|
n_clauses = sat.getNumberOfClauses() |
|
|
|
|
|
|
|
|
|
|
|
master_z = "zm" |
|
|
|
|
|
|
|
|
|
|
|
#quboInstance[(master_z, master_z)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
for clauseIndex in range(sat.getNumberOfClauses()): |
|
|
|
|
|
clause = sat.getClause(clauseIndex) |
|
|
|
|
|
|
|
|
|
|
|
lit1 = clause[0] |
|
|
|
|
|
lit2 = clause[1] |
|
|
|
|
|
lit3 = clause[2] |
|
|
|
|
|
|
|
|
|
|
|
lits[lit1] = True |
|
|
|
|
|
lits[lit2] = True |
|
|
|
|
|
lits[lit3] = True |
|
|
|
|
|
|
|
|
|
|
|
sign1 = 1 if lit1 > 0 else -1 |
|
|
|
|
|
sign2 = 1 if lit2 > 0 else -1 |
|
|
|
|
|
sign3 = 1 if lit3 > 0 else -1 |
|
|
|
|
|
|
|
|
|
|
|
for lit in clause: |
|
|
|
|
|
if lit in clauses_per_lit: |
|
|
|
|
|
clauses_per_lit[lit] += 1 |
|
|
|
|
|
else: |
|
|
|
|
|
clauses_per_lit[lit] = 1 |
|
|
|
|
|
|
|
|
|
|
|
var1 = abs(lit1) |
|
|
|
|
|
var2 = abs(lit2) |
|
|
|
|
|
var3 = abs(lit3) |
|
|
|
|
|
|
|
|
|
|
|
vars[var1] = True |
|
|
|
|
|
vars[var2] = True |
|
|
|
|
|
vars[var3] = True |
|
|
|
|
|
|
|
|
|
|
|
node_lit1 = "x{}".format(lit1) |
|
|
|
|
|
node_lit2 = "x{}".format(lit2) |
|
|
|
|
|
node_lit3 = "x{}".format(lit3) |
|
|
|
|
|
|
|
|
|
|
|
node_aux = "z{}".format(clauseIndex) |
|
|
|
|
|
|
|
|
|
|
|
#quboInstance[(node_aux, node_aux)] = -2 |
|
|
|
|
|
#quboInstance[(node_aux, master_z)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
#quboInstance[(node_lit1, node_lit1)] = +1 #* sign3 |
|
|
|
|
|
#quboInstance[(node_lit2, node_lit2)] = +1 #* sign3 |
|
|
|
|
|
#quboInstance[(node_lit3, node_lit3)] = +1 #* sign3 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_lit1, node_aux)] = -2 #* sign1 |
|
|
|
|
|
quboInstance[(node_lit2, node_aux)] = -2 #* sign2 |
|
|
|
|
|
quboInstance[(node_lit3, node_aux)] = -2 #* sign3 |
|
|
|
|
|
|
|
|
|
|
|
for lit in lits.keys(): |
|
|
|
|
|
node_lit = "x{}".format(lit) |
|
|
|
|
|
|
|
|
|
|
|
#quboInstance[(node_lit, node_lit)] = 2 * clauses_per_lit[lit] |
|
|
|
|
|
|
|
|
|
|
|
for var in vars.keys(): |
|
|
|
|
|
if var in lits and -var in lits: |
|
|
|
|
|
node_var = "x{}".format(var) |
|
|
|
|
|
node_nvar = "x{}".format(-var) |
|
|
|
|
|
|
|
|
|
|
|
max_clauses = max(clauses_per_lit[var], clauses_per_lit[-var]) |
|
|
|
|
|
num_clauses = clauses_per_lit[var] + clauses_per_lit[-var] |
|
|
|
|
|
|
|
|
|
|
|
print((node_var, node_nvar)) |
|
|
|
|
|
quboInstance[(node_var, node_nvar)] = 2 * num_clauses |
|
|
|
|
|
#quboInstance[(node_var, node_nvar)] = 2# * num_clauses |
|
|
|
|
|
|
|
|
|
|
|
return quboInstance |
|
|
|
|
|
|
|
|
|
|
|
def primitiveQUBO_5(sat): |
|
|
|
|
|
quboInstance = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clauses_per_lit = {} |
|
|
|
|
|
|
|
|
|
|
|
lits = {} |
|
|
|
|
|
vars = {} |
|
|
|
|
|
|
|
|
|
|
|
n_clauses = sat.getNumberOfClauses() |
|
|
|
|
|
|
|
|
|
|
|
master_z = "zm" |
|
|
|
|
|
|
|
|
|
|
|
#quboInstance[(master_z, master_z)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
for clauseIndex in range(sat.getNumberOfClauses()): |
|
|
|
|
|
clause = sat.getClause(clauseIndex) |
|
|
|
|
|
|
|
|
|
|
|
lit1 = clause[0] |
|
|
|
|
|
lit2 = clause[1] |
|
|
|
|
|
lit3 = clause[2] |
|
|
|
|
|
|
|
|
|
|
|
lits[lit1] = True |
|
|
|
|
|
lits[lit2] = True |
|
|
|
|
|
lits[lit3] = True |
|
|
|
|
|
|
|
|
|
|
|
for lit in clause: |
|
|
|
|
|
if lit in clauses_per_lit: |
|
|
|
|
|
clauses_per_lit[lit] += 1 |
|
|
|
|
|
else: |
|
|
|
|
|
clauses_per_lit[lit] = 1 |
|
|
|
|
|
|
|
|
|
|
|
var1 = abs(lit1) |
|
|
|
|
|
var2 = abs(lit2) |
|
|
|
|
|
var3 = abs(lit3) |
|
|
|
|
|
|
|
|
|
|
|
vars[var1] = True |
|
|
|
|
|
vars[var2] = True |
|
|
|
|
|
vars[var3] = True |
|
|
|
|
|
|
|
|
|
|
|
node_lit1 = "x{}".format(lit1) |
|
|
|
|
|
node_lit2 = "x{}".format(lit2) |
|
|
|
|
|
node_lit3 = "x{}".format(lit3) |
|
|
|
|
|
|
|
|
|
|
|
node_aux1 = "z{}_1".format(clauseIndex) |
|
|
|
|
|
node_aux2 = "z{}_2".format(clauseIndex) |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_lit1, node_aux1)] = -2 |
|
|
|
|
|
quboInstance[(node_lit2, node_aux1)] = -2 |
|
|
|
|
|
quboInstance[(node_lit1, node_lit2)] = +2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux1, node_lit3)] = +2 |
|
|
|
|
|
quboInstance[(node_lit3, node_aux2)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for var in vars.keys(): |
|
|
|
|
|
if var in lits and -var in lits: |
|
|
|
|
|
node_var = "x{}".format(var) |
|
|
|
|
|
node_nvar = "x{}".format(-var) |
|
|
|
|
|
|
|
|
|
|
|
max_clauses = max(clauses_per_lit[var], clauses_per_lit[-var]) |
|
|
|
|
|
num_clauses = clauses_per_lit[var] + clauses_per_lit[-var] |
|
|
|
|
|
|
|
|
|
|
|
#print((node_var, node_nvar)) |
|
|
|
|
|
quboInstance[(node_var, node_nvar)] = 2 * max_clauses |
|
|
|
|
|
|
|
|
|
|
|
return quboInstance |
|
|
|
|
|
|
|
|
|
|
|
def primitiveQUBO_6(sat): |
|
|
|
|
|
quboInstance = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clauses_per_lit = {} |
|
|
|
|
|
|
|
|
|
|
|
lits = {} |
|
|
|
|
|
vars = {} |
|
|
|
|
|
|
|
|
|
|
|
n_clauses = sat.getNumberOfClauses() |
|
|
|
|
|
|
|
|
|
|
|
master_z = "zm" |
|
|
|
|
|
|
|
|
|
|
|
#quboInstance[(master_z, master_z)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
for clauseIndex in range(sat.getNumberOfClauses()): |
|
|
|
|
|
clause = sat.getClause(clauseIndex) |
|
|
|
|
|
|
|
|
|
|
|
lit1 = clause[0] |
|
|
|
|
|
lit2 = clause[1] |
|
|
|
|
|
lit3 = clause[2] |
|
|
|
|
|
|
|
|
|
|
|
lits[lit1] = True |
|
|
|
|
|
lits[lit2] = True |
|
|
|
|
|
lits[lit3] = True |
|
|
|
|
|
|
|
|
|
|
|
for lit in clause: |
|
|
|
|
|
if lit in clauses_per_lit: |
|
|
|
|
|
clauses_per_lit[lit] += 1 |
|
|
|
|
|
else: |
|
|
|
|
|
clauses_per_lit[lit] = 1 |
|
|
|
|
|
|
|
|
|
|
|
var1 = abs(lit1) |
|
|
|
|
|
var2 = abs(lit2) |
|
|
|
|
|
var3 = abs(lit3) |
|
|
|
|
|
|
|
|
|
|
|
vars[var1] = True |
|
|
|
|
|
vars[var2] = True |
|
|
|
|
|
vars[var3] = True |
|
|
|
|
|
|
|
|
|
|
|
node_lit1 = "x{}".format(lit1) |
|
|
|
|
|
node_lit2 = "x{}".format(lit2) |
|
|
|
|
|
node_lit3 = "x{}".format(lit3) |
|
|
|
|
|
|
|
|
|
|
|
node_aux1 = "z{}_1".format(clauseIndex) |
|
|
|
|
|
node_aux2 = "z{}_2".format(clauseIndex) |
|
|
|
|
|
node_aux3 = "z{}_3".format(clauseIndex) |
|
|
|
|
|
node_aux4 = "z{}_4".format(clauseIndex) |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_lit1, node_aux1)] = -2 |
|
|
|
|
|
quboInstance[(node_lit2, node_aux2)] = -2 |
|
|
|
|
|
quboInstance[(node_aux1, node_aux2)] = +2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux1, node_aux3)] = -2 |
|
|
|
|
|
quboInstance[(node_aux2, node_aux3)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux1, node_aux1)] = +2 |
|
|
|
|
|
quboInstance[(node_aux2, node_aux2)] = +2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux3, node_lit3)] = +2 |
|
|
|
|
|
quboInstance[(node_lit3, node_aux4)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for var in vars.keys(): |
|
|
|
|
|
if var in lits and -var in lits: |
|
|
|
|
|
node_var = "x{}".format(var) |
|
|
|
|
|
node_nvar = "x{}".format(-var) |
|
|
|
|
|
|
|
|
|
|
|
max_clauses = max(clauses_per_lit[var], clauses_per_lit[-var]) |
|
|
|
|
|
num_clauses = clauses_per_lit[var] + clauses_per_lit[-var] |
|
|
|
|
|
|
|
|
|
|
|
#print((node_var, node_nvar)) |
|
|
|
|
|
quboInstance[(node_var, node_nvar)] = 2 * max_clauses |
|
|
|
|
|
|
|
|
|
|
|
return quboInstance |
|
|
|
|
|
|
|
|
|
|
|
def primitiveQUBO_7(sat): |
|
|
|
|
|
quboInstance = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clauses_per_lit = {} |
|
|
|
|
|
|
|
|
|
|
|
lits = {} |
|
|
|
|
|
vars = {} |
|
|
|
|
|
|
|
|
|
|
|
n_clauses = sat.getNumberOfClauses() |
|
|
|
|
|
|
|
|
|
|
|
master_z = "zm" |
|
|
|
|
|
|
|
|
|
|
|
#quboInstance[(master_z, master_z)] = -2 |
|
|
|
|
|
direct_cupplers = {} |
|
|
|
|
|
|
|
|
|
|
|
for clauseIndex in range(sat.getNumberOfClauses()): |
|
|
|
|
|
clause = sat.getClause(clauseIndex) |
|
|
|
|
|
|
|
|
|
|
|
lit1 = clause[0] |
|
|
|
|
|
lit2 = clause[1] |
|
|
|
|
|
lit3 = clause[2] |
|
|
|
|
|
|
|
|
|
|
|
lits[lit1] = True |
|
|
|
|
|
lits[lit2] = True |
|
|
|
|
|
lits[lit3] = True |
|
|
|
|
|
|
|
|
|
|
|
for lit in clause: |
|
|
|
|
|
if lit in clauses_per_lit: |
|
|
|
|
|
clauses_per_lit[lit] += 1 |
|
|
|
|
|
else: |
|
|
|
|
|
clauses_per_lit[lit] = 1 |
|
|
|
|
|
|
|
|
|
|
|
var1 = abs(lit1) |
|
|
|
|
|
var2 = abs(lit2) |
|
|
|
|
|
var3 = abs(lit3) |
|
|
|
|
|
|
|
|
|
|
|
vars[var1] = True |
|
|
|
|
|
vars[var2] = True |
|
|
|
|
|
vars[var3] = True |
|
|
|
|
|
|
|
|
|
|
|
node_lit1 = "x{}".format(lit1) |
|
|
|
|
|
node_lit2 = "x{}".format(lit2) |
|
|
|
|
|
node_lit3 = "x{}".format(lit3) |
|
|
|
|
|
|
|
|
|
|
|
node_aux1 = "z{}_1".format(clauseIndex) |
|
|
|
|
|
node_aux2 = "z{}_2".format(clauseIndex) |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_lit1, node_aux1)] = -2 |
|
|
|
|
|
quboInstance[(node_lit2, node_aux1)] = -2 |
|
|
|
|
|
quboInstance[(node_lit1, node_lit2)] = +2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux1, node_lit3)] = +2 |
|
|
|
|
|
quboInstance[(node_lit3, node_aux2)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (node_lit1, node_lit2) in direct_cupplers: |
|
|
|
|
|
direct_cupplers[(node_lit1, node_lit2)] += 1 |
|
|
|
|
|
else: |
|
|
|
|
|
direct_cupplers[(node_lit1, node_lit2)] = 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for var in vars.keys(): |
|
|
|
|
|
if var in lits and -var in lits: |
|
|
|
|
|
node_var = "x{}".format(var) |
|
|
|
|
|
node_nvar = "x{}".format(-var) |
|
|
|
|
|
|
|
|
|
|
|
max_clauses = max(clauses_per_lit[var], clauses_per_lit[-var]) |
|
|
|
|
|
num_clauses = clauses_per_lit[var] + clauses_per_lit[-var] |
|
|
|
|
|
|
|
|
|
|
|
print((node_var, node_nvar)) |
|
|
|
|
|
quboInstance[(node_var, node_nvar)] = 2 * max_clauses |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for coupler, count in direct_cupplers.items(): |
|
|
|
|
|
quboInstance[coupler] = count * 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return quboInstance |
|
|
|
|
|
|
|
|
|
|
|
def primitiveQUBO_8(sat): |
|
|
|
|
|
quboInstance = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clauses_per_lit = {} |
|
|
|
|
|
|
|
|
|
|
|
lits = {} |
|
|
|
|
|
vars = {} |
|
|
|
|
|
|
|
|
|
|
|
n_clauses = sat.getNumberOfClauses() |
|
|
|
|
|
|
|
|
|
|
|
direct_cupplers = {} |
|
|
|
|
|
|
|
|
|
|
|
for clauseIndex in range(sat.getNumberOfClauses()): |
|
|
|
|
|
clause = sorted(sat.getClause(clauseIndex)) |
|
|
|
|
|
|
|
|
|
|
|
if clause[2] < 0: |
|
|
|
|
|
__add_3not_or_clause(quboInstance, clause, clauseIndex) |
|
|
|
|
|
elif clause[1] < 0: |
|
|
|
|
|
__add_2not_or_clause(quboInstance, clause, clauseIndex) |
|
|
|
|
|
elif clause[0] < 0: |
|
|
|
|
|
__add_1not_or_clause(quboInstance, clause, clauseIndex, direct_cupplers) |
|
|
|
|
|
else: |
|
|
|
|
|
__add_3_or_clause(quboInstance, clause, clauseIndex, direct_cupplers) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for coupler, count in direct_cupplers.items(): |
|
|
|
|
|
quboInstance[coupler] = count * 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return quboInstance |
|
|
|
|
|
|
|
|
|
|
|
def __add_3not_or_clause(quboInstance, clause, clause_index): |
|
|
|
|
|
var1 = abs(clause[0]) |
|
|
|
|
|
var2 = abs(clause[1]) |
|
|
|
|
|
var3 = abs(clause[2]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
node_var1 = "x{}".format(var1) |
|
|
|
|
|
node_var2 = "x{}".format(var2) |
|
|
|
|
|
node_var3 = "x{}".format(var3) |
|
|
|
|
|
|
|
|
|
|
|
node_aux1 = "z{}_1".format(clause_index) |
|
|
|
|
|
node_aux2 = "z{}_2".format(clause_index) |
|
|
|
|
|
node_aux3 = "z{}_3".format(clause_index) |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_var1, node_aux1)] = +2 |
|
|
|
|
|
quboInstance[(node_var2, node_aux2)] = +2 |
|
|
|
|
|
quboInstance[(node_aux1, node_aux2)] = +2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux1, node_aux1)] = -2 |
|
|
|
|
|
quboInstance[(node_aux2, node_aux2)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_var1, node_aux3)] = -2 |
|
|
|
|
|
quboInstance[(node_var2, node_aux3)] = -2 |
|
|
|
|
|
quboInstance[(node_var3, node_aux3)] = +2 |
|
|
|
|
|
quboInstance[(node_aux3, node_aux3)] = +2 |
|
|
|
|
|
|
|
|
|
|
|
def __add_2not_or_clause(quboInstance, clause, clause_index): |
|
|
|
|
|
var1 = abs(clause[0]) |
|
|
|
|
|
var2 = abs(clause[1]) |
|
|
|
|
|
var3 = abs(clause[2]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
node_var1 = "x{}".format(var1) |
|
|
|
|
|
node_var2 = "x{}".format(var2) |
|
|
|
|
|
node_var3 = "x{}".format(var3) |
|
|
|
|
|
|
|
|
|
|
|
node_aux1 = "z{}_1".format(clause_index) |
|
|
|
|
|
node_aux2 = "z{}_2".format(clause_index) |
|
|
|
|
|
node_aux3 = "z{}_3".format(clause_index) |
|
|
|
|
|
node_aux4 = "z{}_4".format(clause_index) |
|
|
|
|
|
node_aux5 = "z{}_5".format(clause_index) |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_var1, node_aux1)] = +2 |
|
|
|
|
|
quboInstance[(node_var2, node_aux2)] = +2 |
|
|
|
|
|
quboInstance[(node_aux1, node_aux2)] = +2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux1, node_aux3)] = -2 |
|
|
|
|
|
quboInstance[(node_aux2, node_aux3)] = -2 |
|
|
|
|
|
quboInstance[(node_aux3, node_aux3)] = +2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux3, node_aux4)] = -2 |
|
|
|
|
|
quboInstance[(node_var3, node_aux5)] = -2 |
|
|
|
|
|
quboInstance[(node_aux4, node_aux5)] = +2 |
|
|
|
|
|
|
|
|
|
|
|
def __add_1not_or_clause(quboInstance, clause, clause_index, direct_cupplers): |
|
|
|
|
|
var1 = abs(clause[1]) |
|
|
|
|
|
var2 = abs(clause[2]) |
|
|
|
|
|
var3 = abs(clause[0]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
node_var1 = "x{}".format(var1) |
|
|
|
|
|
node_var2 = "x{}".format(var2) |
|
|
|
|
|
node_var3 = "x{}".format(var3) |
|
|
|
|
|
|
|
|
|
|
|
node_aux1 = "z{}_1".format(clause_index) |
|
|
|
|
|
node_aux2 = "z{}_2".format(clause_index) |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_var1, node_var2)] = +2 |
|
|
|
|
|
quboInstance[(node_var1, node_aux1)] = -2 |
|
|
|
|
|
quboInstance[(node_var2, node_aux1)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux1, node_aux2)] = +2 |
|
|
|
|
|
quboInstance[(node_var3, node_aux2)] = +2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux2, node_aux2)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
if (node_var1, node_var2) in direct_cupplers: |
|
|
|
|
|
direct_cupplers[(node_var1, node_var2)] += 1 |
|
|
|
|
|
else: |
|
|
|
|
|
direct_cupplers[(node_var1, node_var2)] = 1 |
|
|
|
|
|
|
|
|
|
|
|
def __add_3_or_clause(quboInstance, clause, clause_index, direct_cupplers): |
|
|
|
|
|
var1 = abs(clause[0]) |
|
|
|
|
|
var2 = abs(clause[1]) |
|
|
|
|
|
var3 = abs(clause[2]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
node_var1 = "x{}".format(var1) |
|
|
|
|
|
node_var2 = "x{}".format(var2) |
|
|
|
|
|
node_var3 = "x{}".format(var3) |
|
|
|
|
|
|
|
|
|
|
|
node_aux1 = "z{}_1".format(clause_index) |
|
|
|
|
|
node_aux2 = "z{}_2".format(clause_index) |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_var1, node_var2)] = +2 |
|
|
|
|
|
quboInstance[(node_var1, node_aux1)] = -2 |
|
|
|
|
|
quboInstance[(node_var2, node_aux1)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
quboInstance[(node_aux1, node_var3)] = +2 |
|
|
|
|
|
quboInstance[(node_var3, node_aux2)] = -2 |
|
|
|
|
|
|
|
|
|
|
|
if (node_var1, node_var2) in direct_cupplers: |
|
|
|
|
|
direct_cupplers[(node_var1, node_var2)] += 1 |
|
|
|
|
|
else: |
|
|
|
|
|
direct_cupplers[(node_var1, node_var2)] = 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QuboWriter: |
|
|
class QuboWriter: |
|
|
def __init__(self, qubo): |
|
|
def __init__(self, qubo): |
|
|