forked from Jack-XHP/QuantumPuzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightUp.py
170 lines (157 loc) · 5.56 KB
/
lightUp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from dwave_qbsolv import QBSolv
import matplotlib.pyplot as plt
import numpy as np
import minorminer
import networkx as nx
from dwave.system.composites import FixedEmbeddingComposite
from dwave.system.composites import EmbeddingComposite
from dwave.system.samplers import DWaveSampler
#from numberLink import sumToN2
def sumToN2(neighbor, target, J, scale):
for ele1 in neighbor:
for ele2 in neighbor:
term = (ele1, ele2)
# if (ele1==1) and(ele2==2): print("hahahaha")
if ele1 == ele2:
# for binary variable a^2 = a, thus a^2 - 2*target*a = -(2*target -1)a
weight = -2 * target + 1
elif ele1 > ele2:
continue
else:
# 2ab term
weight = 2
if term in J:
J[term] += weight * scale
else:
J[term] = weight * scale
def connected(i, j):
"""
:param i: index of cell i on grid
:param j: index of cell j on grid
:return: 0 if i == j, 2 iff i and j are in same row/col with no black in between, -1 otherwise
"""
if i[0] != j[0] and i[1] != j[1]:
# not in same row/col
#return -3
return -2
if i[0] == j[0] and i[1] == j[1]:
# i == j
return 0
elif i[0] == j[0]:
# same row
a = min(i[1], j[1])
b = max(i[1], j[1])
part = grid[i[0], a:b]-9
if np.sum(part != 0) == 0:
#return 50
return 150
else:
#return -5
return -5
else:
# same col
a = min(i[0], j[0])
b = max(i[0], j[0])
part = grid[a:b, i[1]]-9
if np.sum(part != 0) == 0:
#return 50
return 150
else:
#return -5
return -5
if __name__ == "__main__":
hight, width = 7,7
grid = np.zeros((hight, width))
cc=grid
grid=grid+9
# all positions of black cells
blacks = [(0,6),(1,3),(2,0),(5,5),(5,6),(6,1)]
numbers = [(0,1,0),(2,4,3),(3,3,1),(4,1,4),(5,2,2),(6,5,1)]
# blacks =[(0,0),(2,1),(2,9),(3,4),(3,9),(4,9),(5,0),(5,1),(8,4),(9,2)]
# numbers =[(0,9,2),(1,4,1),(2,7,1),(3,8,1),(4,0,0),(4,5,3),(5,6,1),(6,1,2),(6,3,2),(6,5,1),(7,5,0),(7,9,2),(8,0,0),(9,3,1),(9,5,2)]
for b in blacks:
grid[b] = -1
for n in numbers:
grid[n[0:2]] = n[2]
# assign qbits to empty cells
var = np.ones((hight, width)) * -1
count = 0
for i in range(hight):
for j in range(width):
if grid[i, j] == 9:
var[i, j] = count
count += 1
J = {}
print(grid)
print(var)
for i in range(hight):
for j in range(width):
if grid[i, j] == -1: # black cell
continue
elif grid[i, j] != 9: # number cell check its empty cell neighbor
near = []
if j > 0:
neighbor = grid[i, j - 1]
if neighbor == 9: near.append(var[i, j - 1])
if j < width - 1:
neighbor = grid[i, j + 1]
if neighbor == 9: near.append(var[i, j + 1])
if i > 0:
neighbor = grid[i - 1][j]
if neighbor == 9: near.append(var[i - 1, j])
if i < hight - 1:
neighbor = grid[i + 1][j]
if neighbor == 9: near.append(var[i + 1, j])
if len(near) < grid[i, j]:
print("Unsolvable! at {}".format((i, j)))
sumToN2(near, grid[i, j], J, scale=120)#250
# print(near)
else: # empty cell, write independent set condition
for k in range(hight):
for m in range(width):
if grid[k, m] == 9:
term = [var[i, j], var[k, m]]
# term.sort()
term = tuple(term)
c = connected([i, j], [k, m])
# print((term, c))
if c != 0:
if term in J:
J[term] += c
else:
J[term] = c
# print(J)
use_qpu=True
if use_qpu:
solver_limit = 36
G = nx.complete_graph(solver_limit)
system = DWaveSampler(token='DEV-6189564036d19f88b3a555b4175a353d6d2c0218')
embedding = minorminer.find_embedding(J.keys(), system.edgelist)
print(embedding)
res = QBSolv().sample_qubo(J, solver=FixedEmbeddingComposite(system, embedding), solver_limit=solver_limit, num_reads=3000)
#Emb = EmbeddingComposite(DWaveSampler(token='DEV-*****')) #add API Token
#res = Emb.sample_qubo(D, num_reads=10000)
else:
res = QBSolv().sample_qubo(J, num_repeats=3000)
samples = list(res.samples())
energy = list(res.data_vectors['energy'])
# print(samples)
# print(energy)
for i in range(len(samples)):
result = samples[i]
output = grid.copy()
for k in range(count):
bit = result[k]
if bit == 1:
output[np.where(var == k)]+=5
if i<5:
print("energy: {}_____________________________".format(energy[i]))
if i<5:
print(output)
# for j in range(hight):
# for m in range(width):
# if output[j, m] == 0:
# elif output[j, m] == 5:
# cc[j, m] ='L'
# elif output[j,m]==-1: cc[j,m]='B'
# else: cc[j, m] = str(output[j,m])