You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

848 lines
25 KiB

6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
  1. #!/usr/bin/env python3
  2. import numpy as np
  3. from . import kSAT
  4. from tqdm import tqdm
  5. import math
  6. import random
  7. #__VERTEX_WEIGHT__ = -2
  8. #__EDGE_WEIGHT__ = 2
  9. __VERTEX_WEIGHT__ = 1
  10. __EDGE_WEIGHT__ = -2
  11. def WMISdictQUBO(kSATInstance):
  12. quboInstance = {}
  13. for clauseIndex in range(kSATInstance.getNumberOfClauses()):
  14. clause = kSATInstance.getClause(clauseIndex)
  15. # build triangles
  16. for varIndexInClause in range(len(clause)):
  17. label = kSATInstance.getLableOfBinding(clauseIndex,
  18. clause[varIndexInClause])
  19. quboInstance[(label, label)] = __VERTEX_WEIGHT__
  20. for i in range(varIndexInClause + 1, len(clause)):
  21. targetLabel = kSATInstance.getLableOfBinding(clauseIndex,
  22. clause[i])
  23. quboInstance[(label, targetLabel)] = __EDGE_WEIGHT__
  24. # connect conflicts
  25. for conflict in kSATInstance.getConflicts():
  26. quboInstance[(conflict[0], conflict[1])] = __EDGE_WEIGHT__
  27. return quboInstance
  28. def WMISdictQUBO_2(kSATInstance):
  29. quboInstance = {}
  30. for clauseIndex in range(kSATInstance.getNumberOfClauses()):
  31. clause = kSATInstance.getClause(clauseIndex)
  32. # build triangles
  33. for varIndexInClause in range(len(clause)):
  34. lit = clause[varIndexInClause]
  35. var = abs(lit)
  36. aux = "z{}_{}".format(clauseIndex, var)
  37. var_node = "x{}".format(var)
  38. if lit < 0:
  39. quboInstance[(aux, aux)] = __VERTEX_WEIGHT__
  40. quboInstance[(var_node, aux)] = __EDGE_WEIGHT__
  41. else:
  42. quboInstance[(var_node, aux)] = __VERTEX_WEIGHT__
  43. for i in range(varIndexInClause + 1, len(clause)):
  44. var2 = abs(clause[i])
  45. aux2 = "z{}_{}".format(clauseIndex, var2)
  46. quboInstance[(aux, aux2)] = __EDGE_WEIGHT__
  47. return quboInstance
  48. def WMISdictQUBO_3(kSATInstance):
  49. quboInstance = {}
  50. for clauseIndex in range(kSATInstance.getNumberOfClauses()):
  51. clause = kSATInstance.getClause(clauseIndex)
  52. # build triangles
  53. for varIndexInClause in range(len(clause)):
  54. lit = clause[varIndexInClause]
  55. var = abs(lit)
  56. aux = "z{}_{}".format(clauseIndex, var)
  57. var_node = "x{}".format(var)
  58. if lit < 0:
  59. quboInstance[(aux, aux)] = __VERTEX_WEIGHT__
  60. quboInstance[(var_node, aux)] = __EDGE_WEIGHT__
  61. else:
  62. quboInstance[(var_node, aux)] = __VERTEX_WEIGHT__
  63. for i in range(varIndexInClause + 1, len(clause)):
  64. var2 = abs(clause[i])
  65. aux2 = "z{}_{}".format(clauseIndex, var2)
  66. quboInstance[(aux, aux2)] = __EDGE_WEIGHT__ * 2
  67. return quboInstance
  68. def WMISdictQUBO_4(kSATInstance):
  69. quboInstance = {}
  70. for clauseIndex in range(kSATInstance.getNumberOfClauses()):
  71. clause = kSATInstance.getClause(clauseIndex)
  72. # build triangles
  73. for varIndexInClause in range(len(clause)):
  74. lit = clause[varIndexInClause]
  75. var = abs(lit)
  76. aux = "z{}_{}".format(clauseIndex, var)
  77. var_node = "x{}".format(var)
  78. if lit < 0:
  79. quboInstance[(aux, aux)] = __VERTEX_WEIGHT__
  80. quboInstance[(var_node, aux)] = __EDGE_WEIGHT__ * 2
  81. else:
  82. quboInstance[(var_node, aux)] = __VERTEX_WEIGHT__
  83. for i in range(varIndexInClause + 1, len(clause)):
  84. var2 = abs(clause[i])
  85. aux2 = "z{}_{}".format(clauseIndex, var2)
  86. quboInstance[(aux, aux2)] = __EDGE_WEIGHT__
  87. return quboInstance
  88. # only 3sat
  89. def primitiveQUBO(sat):
  90. quboInstance = {}
  91. chains = {}
  92. for clauseIndex in range(sat.getNumberOfClauses()):
  93. clause = sat.getClause(clauseIndex);
  94. #build clause primitives
  95. lit1 = "c{}_l{}".format(clauseIndex, clause[0])
  96. lit2 = "c{}_l{}".format(clauseIndex, clause[1])
  97. lit3 = "c{}_l{}".format(clauseIndex, clause[2])
  98. aux1 = "z{}_{}".format(clauseIndex, 1)
  99. aux2 = "z{}_{}".format(clauseIndex, 2)
  100. aux3 = "z{}_{}".format(clauseIndex, 3)
  101. aux4 = "z{}_{}".format(clauseIndex, 4)
  102. quboInstance[(lit1, lit1)] = 1;
  103. quboInstance[(lit2, lit2)] = 1;
  104. quboInstance[(lit3, lit3)] = 1;
  105. quboInstance[(aux1, aux1)] = -2;
  106. quboInstance[(aux2, aux2)] = 1;
  107. quboInstance[(aux3, aux3)] = -2;
  108. quboInstance[(aux4, aux4)] = -2;
  109. quboInstance[(lit1, lit2)] = 1;
  110. quboInstance[(lit1, aux1)] = -2;
  111. quboInstance[(lit2, aux1)] = -2;
  112. quboInstance[(aux1, aux2)] = -2;
  113. quboInstance[(aux2, aux3)] = -2;
  114. quboInstance[(aux2, lit3)] = 1;
  115. quboInstance[(lit3, aux3)] = -2;
  116. quboInstance[(aux3, aux4)] = -2;
  117. if clause[0] in chains:
  118. chains[clause[0]].append(lit1)
  119. else:
  120. chains[clause[0]] = [lit1]
  121. if clause[1] in chains:
  122. chains[clause[1]].append(lit2)
  123. else:
  124. chains[clause[1]] = [lit2]
  125. if clause[2] in chains:
  126. chains[clause[2]].append(lit3)
  127. else:
  128. chains[clause[2]] = [lit3]
  129. #build chains
  130. longestChain = 0;
  131. for lit, nodes in chains.items():
  132. if len(nodes) > longestChain:
  133. longestChain = len(nodes)
  134. if lit > 0 and -1 * lit in chains:
  135. len_smaller_chain = min(len(chains[lit]), len(chains[-lit]))
  136. indices = random.sample(list(range(len_smaller_chain)),
  137. round(len_smaller_chain / 2))
  138. for index in indices:
  139. quboInstance[(chains[lit][index], chains[-1*lit][index])] = 10
  140. #quboInstance[(chains[lit][0], chains[-1*lit][0])] = 2
  141. print("longest chain = {}".format(longestChain))
  142. for nodes in chains.values():
  143. while len(nodes) > 1:
  144. quboInstance[(nodes[0], nodes[1])] = -2;
  145. nodes.pop(0)
  146. return quboInstance
  147. # only 3sat
  148. def primitiveQUBO_2(sat):
  149. quboInstance = {}
  150. chains = {}
  151. for clauseIndex in range(sat.getNumberOfClauses()):
  152. clause = sat.getClause(clauseIndex)
  153. lit1 = clause[0]
  154. lit2 = clause[1]
  155. lit3 = clause[2]
  156. var1 = abs(lit1)
  157. var2 = abs(lit2)
  158. var3 = abs(lit3)
  159. sign1 = 1 if lit1 > 0 else -1
  160. sign2 = 1 if lit2 > 0 else -1
  161. sign3 = 1 if lit3 > 0 else -1
  162. node_var1 = "x{}".format(var1)
  163. node_var2 = "x{}".format(var2)
  164. node_var3 = "x{}".format(var3)
  165. node_aux1 = "a{}_{}".format(clauseIndex, 1)
  166. node_aux2 = "a{}_{}".format(clauseIndex, 2)
  167. node_aux3 = "a{}_{}".format(clauseIndex, 3)
  168. node_aux4 = "a{}_{}".format(clauseIndex, 4)
  169. quboInstance[(node_var1, node_var1)] = 1 * sign1
  170. quboInstance[(node_var2, node_var2)] = 1 * sign2
  171. quboInstance[(node_var3, node_var3)] = 1 * sign3
  172. quboInstance[(node_aux1, node_aux1)] = -2
  173. quboInstance[(node_aux2, node_aux2)] = 1
  174. quboInstance[(node_aux3, node_aux3)] = -2
  175. quboInstance[(node_aux4, node_aux4)] = -2
  176. quboInstance[(node_var1, node_var2)] = 1 * sign1 * sign2
  177. quboInstance[(node_var1, node_aux1)] = -2 * sign1
  178. quboInstance[(node_var2, node_aux1)] = -2 * sign2
  179. quboInstance[(node_aux1, node_aux2)] = -2
  180. quboInstance[(node_aux2, node_var3)] = 1 * sign3
  181. quboInstance[(node_var3, node_aux3)] = -2 * sign3
  182. quboInstance[(node_aux2, node_aux3)] = -2
  183. quboInstance[(node_var3, node_aux4)] = -2
  184. return quboInstance
  185. # only 3sat
  186. def primitiveQUBO_3(sat):
  187. quboInstance = {}
  188. chains = {}
  189. lits = {}
  190. vars = {}
  191. n_clauses = sat.getNumberOfClauses()
  192. for clauseIndex in range(sat.getNumberOfClauses()):
  193. clause = sat.getClause(clauseIndex)
  194. lit1 = clause[0]
  195. lit2 = clause[1]
  196. lit3 = clause[2]
  197. lits[lit1] = True
  198. lits[lit2] = True
  199. lits[lit3] = True
  200. var1 = abs(lit1)
  201. var2 = abs(lit2)
  202. var3 = abs(lit3)
  203. vars[var1] = True
  204. vars[var2] = True
  205. vars[var3] = True
  206. node_lit1 = "x{}".format(lit1)
  207. node_lit2 = "x{}".format(lit2)
  208. node_lit3 = "x{}".format(lit3)
  209. node_aux1 = "z{}_{}".format(clauseIndex, 1)
  210. node_aux2 = "z{}_{}".format(clauseIndex, 2)
  211. node_aux3 = "z{}_{}".format(clauseIndex, 3)
  212. node_aux4 = "z{}_{}".format(clauseIndex, 4)
  213. quboInstance[(node_lit1, node_lit1)] = 1
  214. quboInstance[(node_lit2, node_lit2)] = 1
  215. quboInstance[(node_lit3, node_lit3)] = 1
  216. quboInstance[(node_aux1, node_aux1)] = -2
  217. quboInstance[(node_aux2, node_aux2)] = 1
  218. quboInstance[(node_aux3, node_aux3)] = -2
  219. quboInstance[(node_aux4, node_aux4)] = -2
  220. quboInstance[(node_lit1, node_lit2)] = 1
  221. quboInstance[(node_lit1, node_aux1)] = -2
  222. quboInstance[(node_lit2, node_aux1)] = -2
  223. quboInstance[(node_aux1, node_aux2)] = -2
  224. quboInstance[(node_aux2, node_lit3)] = 1
  225. quboInstance[(node_lit3, node_aux3)] = -2
  226. quboInstance[(node_aux2, node_aux3)] = -2
  227. quboInstance[(node_aux3, node_aux4)] = -2
  228. for var in vars.keys():
  229. if var in lits and -var in lits:
  230. node_var = "x{}".format(var)
  231. node_nvar = "x{}".format(-var)
  232. print((node_var, node_nvar))
  233. quboInstance[(node_var, node_nvar)] = 2
  234. return quboInstance
  235. def primitiveQUBO_4(sat):
  236. quboInstance = {}
  237. clauses_per_lit = {}
  238. lits = {}
  239. vars = {}
  240. n_clauses = sat.getNumberOfClauses()
  241. master_z = "zm"
  242. #quboInstance[(master_z, master_z)] = -2
  243. for clauseIndex in range(sat.getNumberOfClauses()):
  244. clause = sat.getClause(clauseIndex)
  245. lit1 = clause[0]
  246. lit2 = clause[1]
  247. lit3 = clause[2]
  248. lits[lit1] = True
  249. lits[lit2] = True
  250. lits[lit3] = True
  251. sign1 = 1 if lit1 > 0 else -1
  252. sign2 = 1 if lit2 > 0 else -1
  253. sign3 = 1 if lit3 > 0 else -1
  254. for lit in clause:
  255. if lit in clauses_per_lit:
  256. clauses_per_lit[lit] += 1
  257. else:
  258. clauses_per_lit[lit] = 1
  259. var1 = abs(lit1)
  260. var2 = abs(lit2)
  261. var3 = abs(lit3)
  262. vars[var1] = True
  263. vars[var2] = True
  264. vars[var3] = True
  265. node_lit1 = "x{}".format(lit1)
  266. node_lit2 = "x{}".format(lit2)
  267. node_lit3 = "x{}".format(lit3)
  268. node_aux = "z{}".format(clauseIndex)
  269. #quboInstance[(node_aux, node_aux)] = -2
  270. #quboInstance[(node_aux, master_z)] = -2
  271. #quboInstance[(node_lit1, node_lit1)] = +1 #* sign3
  272. #quboInstance[(node_lit2, node_lit2)] = +1 #* sign3
  273. #quboInstance[(node_lit3, node_lit3)] = +1 #* sign3
  274. quboInstance[(node_lit1, node_aux)] = -2 #* sign1
  275. quboInstance[(node_lit2, node_aux)] = -2 #* sign2
  276. quboInstance[(node_lit3, node_aux)] = -2 #* sign3
  277. for lit in lits.keys():
  278. node_lit = "x{}".format(lit)
  279. #quboInstance[(node_lit, node_lit)] = 2 * clauses_per_lit[lit]
  280. for var in vars.keys():
  281. if var in lits and -var in lits:
  282. node_var = "x{}".format(var)
  283. node_nvar = "x{}".format(-var)
  284. max_clauses = max(clauses_per_lit[var], clauses_per_lit[-var])
  285. num_clauses = clauses_per_lit[var] + clauses_per_lit[-var]
  286. print((node_var, node_nvar))
  287. quboInstance[(node_var, node_nvar)] = 2 * num_clauses
  288. #quboInstance[(node_var, node_nvar)] = 2# * num_clauses
  289. return quboInstance
  290. def primitiveQUBO_5(sat):
  291. quboInstance = {}
  292. clauses_per_lit = {}
  293. lits = {}
  294. vars = {}
  295. n_clauses = sat.getNumberOfClauses()
  296. master_z = "zm"
  297. #quboInstance[(master_z, master_z)] = -2
  298. for clauseIndex in range(sat.getNumberOfClauses()):
  299. clause = sat.getClause(clauseIndex)
  300. lit1 = clause[0]
  301. lit2 = clause[1]
  302. lit3 = clause[2]
  303. lits[lit1] = True
  304. lits[lit2] = True
  305. lits[lit3] = True
  306. for lit in clause:
  307. if lit in clauses_per_lit:
  308. clauses_per_lit[lit] += 1
  309. else:
  310. clauses_per_lit[lit] = 1
  311. var1 = abs(lit1)
  312. var2 = abs(lit2)
  313. var3 = abs(lit3)
  314. vars[var1] = True
  315. vars[var2] = True
  316. vars[var3] = True
  317. node_lit1 = "x{}".format(lit1)
  318. node_lit2 = "x{}".format(lit2)
  319. node_lit3 = "x{}".format(lit3)
  320. node_aux1 = "z{}_1".format(clauseIndex)
  321. node_aux2 = "z{}_2".format(clauseIndex)
  322. quboInstance[(node_lit1, node_aux1)] = -2
  323. quboInstance[(node_lit2, node_aux1)] = -2
  324. quboInstance[(node_lit1, node_lit2)] = +2
  325. quboInstance[(node_aux1, node_lit3)] = +2
  326. quboInstance[(node_lit3, node_aux2)] = -2
  327. for var in vars.keys():
  328. if var in lits and -var in lits:
  329. node_var = "x{}".format(var)
  330. node_nvar = "x{}".format(-var)
  331. max_clauses = max(clauses_per_lit[var], clauses_per_lit[-var])
  332. num_clauses = clauses_per_lit[var] + clauses_per_lit[-var]
  333. #print((node_var, node_nvar))
  334. quboInstance[(node_var, node_nvar)] = 2 * max_clauses
  335. return quboInstance
  336. def primitiveQUBO_6(sat):
  337. quboInstance = {}
  338. clauses_per_lit = {}
  339. lits = {}
  340. vars = {}
  341. n_clauses = sat.getNumberOfClauses()
  342. master_z = "zm"
  343. #quboInstance[(master_z, master_z)] = -2
  344. for clauseIndex in range(sat.getNumberOfClauses()):
  345. clause = sat.getClause(clauseIndex)
  346. lit1 = clause[0]
  347. lit2 = clause[1]
  348. lit3 = clause[2]
  349. lits[lit1] = True
  350. lits[lit2] = True
  351. lits[lit3] = True
  352. for lit in clause:
  353. if lit in clauses_per_lit:
  354. clauses_per_lit[lit] += 1
  355. else:
  356. clauses_per_lit[lit] = 1
  357. var1 = abs(lit1)
  358. var2 = abs(lit2)
  359. var3 = abs(lit3)
  360. vars[var1] = True
  361. vars[var2] = True
  362. vars[var3] = True
  363. node_lit1 = "x{}".format(lit1)
  364. node_lit2 = "x{}".format(lit2)
  365. node_lit3 = "x{}".format(lit3)
  366. node_aux1 = "z{}_1".format(clauseIndex)
  367. node_aux2 = "z{}_2".format(clauseIndex)
  368. node_aux3 = "z{}_3".format(clauseIndex)
  369. node_aux4 = "z{}_4".format(clauseIndex)
  370. quboInstance[(node_lit1, node_aux1)] = -2
  371. quboInstance[(node_lit2, node_aux2)] = -2
  372. quboInstance[(node_aux1, node_aux2)] = +2
  373. quboInstance[(node_aux1, node_aux3)] = -2
  374. quboInstance[(node_aux2, node_aux3)] = -2
  375. quboInstance[(node_aux1, node_aux1)] = +2
  376. quboInstance[(node_aux2, node_aux2)] = +2
  377. quboInstance[(node_aux3, node_lit3)] = +2
  378. quboInstance[(node_lit3, node_aux4)] = -2
  379. for var in vars.keys():
  380. if var in lits and -var in lits:
  381. node_var = "x{}".format(var)
  382. node_nvar = "x{}".format(-var)
  383. max_clauses = max(clauses_per_lit[var], clauses_per_lit[-var])
  384. num_clauses = clauses_per_lit[var] + clauses_per_lit[-var]
  385. #print((node_var, node_nvar))
  386. quboInstance[(node_var, node_nvar)] = 2 * max_clauses
  387. return quboInstance
  388. def primitiveQUBO_7(sat):
  389. quboInstance = {}
  390. clauses_per_lit = {}
  391. lits = {}
  392. vars = {}
  393. n_clauses = sat.getNumberOfClauses()
  394. master_z = "zm"
  395. #quboInstance[(master_z, master_z)] = -2
  396. direct_cupplers = {}
  397. for clauseIndex in range(sat.getNumberOfClauses()):
  398. clause = sat.getClause(clauseIndex)
  399. lit1 = clause[0]
  400. lit2 = clause[1]
  401. lit3 = clause[2]
  402. lits[lit1] = True
  403. lits[lit2] = True
  404. lits[lit3] = True
  405. for lit in clause:
  406. if lit in clauses_per_lit:
  407. clauses_per_lit[lit] += 1
  408. else:
  409. clauses_per_lit[lit] = 1
  410. var1 = abs(lit1)
  411. var2 = abs(lit2)
  412. var3 = abs(lit3)
  413. vars[var1] = True
  414. vars[var2] = True
  415. vars[var3] = True
  416. node_lit1 = "x{}".format(lit1)
  417. node_lit2 = "x{}".format(lit2)
  418. node_lit3 = "x{}".format(lit3)
  419. node_aux1 = "z{}_1".format(clauseIndex)
  420. node_aux2 = "z{}_2".format(clauseIndex)
  421. quboInstance[(node_lit1, node_aux1)] = -2
  422. quboInstance[(node_lit2, node_aux1)] = -2
  423. quboInstance[(node_lit1, node_lit2)] = +2
  424. quboInstance[(node_aux1, node_lit3)] = +2
  425. quboInstance[(node_lit3, node_aux2)] = -2
  426. if (node_lit1, node_lit2) in direct_cupplers:
  427. direct_cupplers[(node_lit1, node_lit2)] += 1
  428. else:
  429. direct_cupplers[(node_lit1, node_lit2)] = 1
  430. for var in vars.keys():
  431. if var in lits and -var in lits:
  432. node_var = "x{}".format(var)
  433. node_nvar = "x{}".format(-var)
  434. max_clauses = max(clauses_per_lit[var], clauses_per_lit[-var])
  435. num_clauses = clauses_per_lit[var] + clauses_per_lit[-var]
  436. print((node_var, node_nvar))
  437. quboInstance[(node_var, node_nvar)] = 2 * max_clauses
  438. for coupler, count in direct_cupplers.items():
  439. quboInstance[coupler] = count * 2
  440. return quboInstance
  441. def primitiveQUBO_8(sat):
  442. quboInstance = {}
  443. clauses_per_lit = {}
  444. lits = {}
  445. vars = {}
  446. n_clauses = sat.getNumberOfClauses()
  447. direct_cupplers = {}
  448. for clauseIndex in range(sat.getNumberOfClauses()):
  449. clause = sorted(sat.getClause(clauseIndex))
  450. if clause[2] < 0:
  451. __add_3not_or_clause(quboInstance, clause, clauseIndex)
  452. elif clause[1] < 0:
  453. __add_2not_or_clause(quboInstance, clause, clauseIndex)
  454. elif clause[0] < 0:
  455. __add_1not_or_clause(quboInstance, clause, clauseIndex, direct_cupplers)
  456. else:
  457. __add_3_or_clause(quboInstance, clause, clauseIndex, direct_cupplers)
  458. for coupler, count in direct_cupplers.items():
  459. quboInstance[coupler] = count * 2
  460. return quboInstance
  461. def __add_3not_or_clause(quboInstance, clause, clause_index):
  462. var1 = abs(clause[0])
  463. var2 = abs(clause[1])
  464. var3 = abs(clause[2])
  465. node_var1 = "x{}".format(var1)
  466. node_var2 = "x{}".format(var2)
  467. node_var3 = "x{}".format(var3)
  468. node_aux1 = "z{}_1".format(clause_index)
  469. node_aux2 = "z{}_2".format(clause_index)
  470. node_aux3 = "z{}_3".format(clause_index)
  471. quboInstance[(node_var1, node_aux1)] = +2
  472. quboInstance[(node_var2, node_aux2)] = +2
  473. quboInstance[(node_aux1, node_aux2)] = +2
  474. quboInstance[(node_aux1, node_aux1)] = -2
  475. quboInstance[(node_aux2, node_aux2)] = -2
  476. quboInstance[(node_var1, node_aux3)] = -2
  477. quboInstance[(node_var2, node_aux3)] = -2
  478. quboInstance[(node_var3, node_aux3)] = +2
  479. quboInstance[(node_aux3, node_aux3)] = +2
  480. def __add_2not_or_clause(quboInstance, clause, clause_index):
  481. var1 = abs(clause[0])
  482. var2 = abs(clause[1])
  483. var3 = abs(clause[2])
  484. node_var1 = "x{}".format(var1)
  485. node_var2 = "x{}".format(var2)
  486. node_var3 = "x{}".format(var3)
  487. node_aux1 = "z{}_1".format(clause_index)
  488. node_aux2 = "z{}_2".format(clause_index)
  489. node_aux3 = "z{}_3".format(clause_index)
  490. node_aux4 = "z{}_4".format(clause_index)
  491. node_aux5 = "z{}_5".format(clause_index)
  492. quboInstance[(node_var1, node_aux1)] = +2
  493. quboInstance[(node_var2, node_aux2)] = +2
  494. quboInstance[(node_aux1, node_aux2)] = +2
  495. quboInstance[(node_aux1, node_aux3)] = -2
  496. quboInstance[(node_aux2, node_aux3)] = -2
  497. quboInstance[(node_aux3, node_aux3)] = +2
  498. quboInstance[(node_aux3, node_aux4)] = -2
  499. quboInstance[(node_var3, node_aux5)] = -2
  500. quboInstance[(node_aux4, node_aux5)] = +2
  501. def __add_1not_or_clause(quboInstance, clause, clause_index, direct_cupplers):
  502. var1 = abs(clause[1])
  503. var2 = abs(clause[2])
  504. var3 = abs(clause[0])
  505. node_var1 = "x{}".format(var1)
  506. node_var2 = "x{}".format(var2)
  507. node_var3 = "x{}".format(var3)
  508. node_aux1 = "z{}_1".format(clause_index)
  509. node_aux2 = "z{}_2".format(clause_index)
  510. quboInstance[(node_var1, node_var2)] = +2
  511. quboInstance[(node_var1, node_aux1)] = -2
  512. quboInstance[(node_var2, node_aux1)] = -2
  513. quboInstance[(node_aux1, node_aux2)] = +2
  514. quboInstance[(node_var3, node_aux2)] = +2
  515. quboInstance[(node_aux2, node_aux2)] = -2
  516. if (node_var1, node_var2) in direct_cupplers:
  517. direct_cupplers[(node_var1, node_var2)] += 1
  518. else:
  519. direct_cupplers[(node_var1, node_var2)] = 1
  520. def __add_3_or_clause(quboInstance, clause, clause_index, direct_cupplers):
  521. var1 = abs(clause[0])
  522. var2 = abs(clause[1])
  523. var3 = abs(clause[2])
  524. node_var1 = "x{}".format(var1)
  525. node_var2 = "x{}".format(var2)
  526. node_var3 = "x{}".format(var3)
  527. node_aux1 = "z{}_1".format(clause_index)
  528. node_aux2 = "z{}_2".format(clause_index)
  529. quboInstance[(node_var1, node_var2)] = +2
  530. quboInstance[(node_var1, node_aux1)] = -2
  531. quboInstance[(node_var2, node_aux1)] = -2
  532. quboInstance[(node_aux1, node_var3)] = +2
  533. quboInstance[(node_var3, node_aux2)] = -2
  534. if (node_var1, node_var2) in direct_cupplers:
  535. direct_cupplers[(node_var1, node_var2)] += 1
  536. else:
  537. direct_cupplers[(node_var1, node_var2)] = 1
  538. class QuboWriter:
  539. def __init__(self, qubo):
  540. self.__labelIndexDict = {}
  541. self.__qubo = qubo
  542. self.__initLabelIndexDict(self.__qubo)
  543. print(self.__labelIndexDict)
  544. def __initLabelIndexDict(self, qubo):
  545. indexCount = 0
  546. for coupler in qubo:
  547. label1 = coupler[0]
  548. label2 = coupler[1]
  549. if label1 not in self.__labelIndexDict:
  550. self.__labelIndexDict[label1] = indexCount
  551. indexCount += 1
  552. if label2 not in self.__labelIndexDict:
  553. self.__labelIndexDict[label2] = indexCount
  554. indexCount += 1
  555. def write(self, filePath):
  556. quboFile = open(filePath, "w")
  557. self.__writeQuboFileHeader(quboFile)
  558. self.__writeQuboFileNodes(quboFile)
  559. self.__writeQuboFileCouplers(quboFile)
  560. quboFile.close()
  561. def __writeQuboFileHeader(self, quboFile):
  562. numberOfNodes = len(self.__labelIndexDict)
  563. numberOfCouplers = len(self.__qubo) - numberOfNodes
  564. quboFile.write("c\n")
  565. quboFile.write("c this is a generated qubo file\n")
  566. quboFile.write("c\n")
  567. quboFile.write("p qubo 0 %d %d %d\n" %(numberOfNodes,
  568. numberOfNodes,
  569. numberOfCouplers))
  570. def __writeQuboFileNodes(self, quboFile):
  571. for label in self.__labelIndexDict:
  572. self.__writeCoupler(quboFile, (label, label))
  573. def __writeCoupler(self, quboFile, coupler):
  574. indices = self.__getNodeIndicesFromCoupler(coupler)
  575. quboFile.write("%d %d %d\n" % (indices[0],
  576. indices[1],
  577. self.__qubo[coupler]))
  578. def __getNodeIndicesFromCoupler(self, coupler):
  579. index1 = self.__labelIndexDict[coupler[0]]
  580. index2 = self.__labelIndexDict[coupler[1]]
  581. if index1 <= index2:
  582. return [index1, index2]
  583. else:
  584. return [index2, index1]
  585. def __writeQuboFileCouplers(self, quboFile):
  586. for coupler in self.__qubo:
  587. if coupler[0] != coupler[1]:
  588. self.__writeCoupler(quboFile, coupler)