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.

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