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