Repository navigation
Expand file tree
/
Copy pathWindowClass.py
More file actions
504 lines (387 loc) · 16.4 KB
/
Copy pathWindowClass.py
File metadata and controls
504 lines (387 loc) · 16.4 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import logging
from QTextEditLogger import QTextEditLogger
from QiskitRun import QiskitRun
from Gate import Gate
import json
import time
from PyQt5.sip import delete
from trashWidget import trashWidget
from Util import Util
from Simulation import Simulation1 as sim
from GateGroupBox import GateGroupBox
from PyQt5 import QtCore
from QbitLine import QbitLine
from Circuit import Circuit
import pyqtgraph as pg
import sys, csv
import numpy as np
from QLabelClickable import QLabelClickable
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import gates as gt
import ast
plot1 = None
plot2 = None
class Window(QMainWindow):
imagePath = './images/'
qbitCounter = 2
currentCircuit = None
qbitsList = []
customGatesCounter = 0
customGates = []
def __init__(self):
super().__init__()
self.currentCircuit = Circuit()
self.initializeUI()
#deleteGates.deleted.connect(lambda:self.qbitCleanUp())
def initializeUI(self):
self.setWindowTitle("Quantum Simulator")
self.setWindowIcon(QIcon("./images/icon.jpg"))
self.setGeometry(200, 200, 1200, 700)
self.setupMenu()
self.setupUI()
self.show()
def setupMenu(self):
saveAct = QAction('Guardar', self)
saveAct.setShortcut('Ctrl+S')
saveAct.triggered.connect(self.file_save)
loadAct = QAction('Cargar', self)
loadAct.setShortcut('Ctrl+L')
loadAct.triggered.connect(self.file_load)
newAct = QAction('Nuevo', self)
newAct.setShortcut('Ctrl+N')
newAct.triggered.connect(self.new_circuit)
exitAct = QAction('Salir', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.triggered.connect(self.close)
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('Archivo')
fileMenu.addAction(newAct)
fileMenu.addAction(saveAct)
fileMenu.addAction(loadAct)
fileMenu.addSeparator()
fileMenu.addAction(exitAct)
runMenu = menuBar.addMenu('Ejecutar')
runIBMAct = QAction('IBM', self)
runIBMAct.triggered.connect(lambda:self.runIBMCircuit())
runMenu.addAction(runIBMAct)
def file_save(self):
name = QFileDialog.getSaveFileName(self, 'Save File', filter="JSON files (*.json)")
if self.currentCircuit.made == False:
self.currentCircuit.make(self.qbitsList)
saveDict = {
"circuit":{
},
"customGates":{}
}
# columnGates = [[]]
# for qbit in self.qbitsList:
# for j in range(qbit.grid.count()):
# if(type(qbit.grid.itemAtPosition(0, j).widget()) == type(QLabel())):
# columnGates.append('None')
# else:
# columnGates.append(qbit.grid.itemAtPosition(0, j).widget().gate)
# print(columnGates)
self.currentCircuit.save_circuit()
saveDict['circuit'] = self.currentCircuit.saved_circuit
saveDict["numqbits"] = self.qbitCounter
jsonString = json.dumps(saveDict)
file = open(str(name[0]), 'w')
file.write(jsonString)
file.close()
def file_load(self):
name = QFileDialog.getOpenFileName(self,'Cargar Archivo', filter="JSON files (*.json)")
file = open(str(name[0]), 'r')
with file:
text = file.read()
dictionary = ast.literal_eval(text)
qbits_loaded = dictionary["numqbits"]
self.new_circuit(True)
for times in range(qbits_loaded-2):
self.addQbit(self.vBox2)
for line in self.qbitsList:
line.currColumn = 0
for i in reversed(range(line.grid.count())):
line.grid.itemAt(i).widget().setParent(None)
print( "NUM ELEMS EN QBIT", line.grid.count())
for column in dictionary["circuit"]:
for item in range(len(column)):
self.qbitsList[item].add_gate(column[item])
def new_circuit(self, loaded = False):
self.close()
self.qbitCounter = 2
self.qbitsList.clear()
self.currentCircuit = Circuit(loaded)
self.__init__()
def runIBMCircuit(self):
if(self.currentCircuit.made == False):
self.currentCircuit.make(self.qbitsList)
run = QiskitRun()
run.create_circuit(self.currentCircuit)
run.setup()
run.run_circuit()
print("results: ", run.get_results())
run.results_formatting()
self.openResultDialog(run.results)
def setupUI(self):
verticalLytWdgt1 = QWidget()
simulateButton = QPushButton(QIcon("images/play.png"), "Simular")
addQbitButton = QPushButton(QIcon("images/add.png"), "Añadir cúbit")
customGateButton = QPushButton(QIcon("images/add.png"), "Crear puerta")
'''Conexión al método de simulación'''
simulateButton.clicked.connect(self.startSimulation)
vBox1 = QVBoxLayout()
buttonBarWidget = QWidget()
buttonBarLayout = QHBoxLayout()
vBox1.addWidget(buttonBarWidget)
buttonBarWidget.setLayout(buttonBarLayout)
buttonBarLayout.addWidget(simulateButton)
buttonBarLayout.addWidget(addQbitButton)
buttonBarLayout.addWidget(customGateButton)
verticalLytWdgt1.setLayout(vBox1)
self.setCentralWidget(verticalLytWdgt1)
buttonBarLayout.setStretch(0, 4)
buttonBarLayout.setStretch(1, 1)
buttonBarLayout.setStretch(2,1)
horizontalLytWdgt1 = QWidget()
vBox1.addWidget(horizontalLytWdgt1)
hbox1 = QHBoxLayout()
horizontalLytWdgt1.setLayout(hbox1)
gatesTabWidget = QTabWidget()
gatesWidget = QWidget()
customGatesWidget = QWidget()
gridGates = QGridLayout()
self.gridCustomGates = QGridLayout()
gatesWidget.setLayout(gridGates)
customGatesWidget.setLayout(self.gridCustomGates)
vboxGatesLyt = QVBoxLayout()
vboxGatesWdgt = QWidget()
hbox1.addWidget(vboxGatesWdgt)
vboxGatesLyt.addWidget(gatesTabWidget)
deleteGates = trashWidget()
vboxGatesLyt.addWidget(deleteGates)
#gatesTabWidget.setLayout(gridGates)
vboxGatesWdgt.setLayout(vboxGatesLyt)
vboxGatesLyt.setStretch(0,5)
vboxGatesLyt.setStretch(1,1)
self.fillGridGates(gridGates)
self.vBox2 = QVBoxLayout()
verticalLytWdgt2 = QWidget()
verticalLytWdgt2.setLayout(self.vBox2)
hbox1.addWidget(verticalLytWdgt2)
hbox1.setStretch(0,1)
hbox1.setStretch(1,5)
self.line1 = QbitLine(self.currentCircuit,0)
self.qbitsList.append(self.line1)
self.line2 = QbitLine(self.currentCircuit,1)
self.qbitsList.append(self.line2)
self.vBox2.addWidget(self.line1)
self.vBox2.addWidget(self.line2)
vBox1.setStretch(0,1)
vBox1.setStretch(1,10)
gatesTabWidget.addTab(gatesWidget, "Puertas")
gatesTabWidget.addTab(customGatesWidget, "Personalizadas")
addQbitButton.clicked.connect(lambda:self.addQbit(self.vBox2))
customGateButton.clicked.connect(lambda:self.createGate())
#TODO: Limpar qbit al borrar puerta de ese qbit
# def qbitCleanUp(self):
# qbitToReorder = self.deleteGates.gateToDelete.parent()
# qbitToReorder.cleanQGrid()
def addQbit(self, layout):
newQbitLine = QbitLine(self.currentCircuit, self.qbitCounter)
#newQbitLine.setOrderId(self.qbitCounter)
self.qbitCounter += 1
layout.addWidget(newQbitLine)
self.qbitsList.append(newQbitLine)
def createGate(self):
gateDialog = QDialog()
gateDialog.setModal(True)
gateDialog.setWindowTitle("Crear puerta cuántica")
gateDialog.setWindowIcon(QIcon("./images/icon.jpg"))
gateDialog.setGeometry(500, 500, 300, 300)
self.createGateUI(gateDialog)
gateDialog.show()
gateDialog.exec()
def createGateUI(self, dialog):
vBox = QVBoxLayout()
self.create_gate_dialog = dialog
gridInfo = QGridLayout()
gridInfo.addWidget(QLabel("Nombre"),1,0)
gridInfo.addWidget(QLabel("Símbolo"),2,0)
gridInfo.addWidget(QLabel("Matriz"),3,0)
self.gateNameLE = QLineEdit()
self.gateSymbolLE = QLineEdit()
self.gateMatrixTE = QTextEdit()
regex = QRegExp('[A-Z]{2,4}')
validator = QRegExpValidator(regex, self)
self.gateSymbolLE.setValidator(validator)
self.gateSymbolLE.setPlaceholderText("ID")
self.gateNameLE.setPlaceholderText("Identity")
self.gateMatrixTE.setPlaceholderText("[[1,0],[0,1]]")
gridInfo.addWidget(self.gateNameLE,1,1)
gridInfo.addWidget(self.gateSymbolLE,2,1)
gridInfo.addWidget(self.gateMatrixTE,3,1)
gridInfo.addWidget(acceptButton := QPushButton("Aceptar"),4,1)
acceptButton.clicked.connect(lambda:self.saveGate())
vBox.addLayout(gridInfo)
dialog.setLayout(vBox)
def saveGate(self):
new_Gate = Gate()
new_Gate.id = self.gateSymbolLE.text().lower()
new_Gate.name = self.gateNameLE.text()
new_Gate.symbol = self.gateSymbolLE.text()
new_Gate.matrix = np.array(self.gateMatrixTE.toPlainText())
img_ful_path = Util.generateGateImage(new_Gate)
new_QLabel = QLabelClickable(new_Gate)
new_QLabel.setPixmap(QPixmap(img_ful_path))
if (len(self.customGates)%2 == 0):
self.gridCustomGates.addWidget(new_QLabel,len(self.customGates),len(self.customGates)%2)
else:
self.gridCustomGates.addWidget(new_QLabel,len(self.customGates)-1,len(self.customGates)%2)
self.customGates.append(new_Gate)
self.create_gate_dialog.close()
new_QLabel.clicked.connect(lambda:self.openGate(self.gateSymbolLE.text().lower()))
def fillGridGates(self, grid):
gateX = self.generateGateLabel('X')
gateY = self.generateGateLabel('Y')
gateZ = self.generateGateLabel('Z')
gateH = self.generateGateLabel('H')
gateC = self.generateGateLabel('C')
gateID = self.generateGateLabel('ID')
#Añadir puertas al grid
grid.addWidget(gateX,1,1)
grid.addWidget(gateY,1,2)
grid.addWidget(gateZ,2,1)
grid.addWidget(gateH,2,2)
grid.addWidget(gateC,3,1)
grid.addWidget(gateID,3,2)
gateX.clicked.connect(lambda:self.openGate('x'))
gateY.clicked.connect(lambda:self.openGate('y'))
gateZ.clicked.connect(lambda:self.openGate('z'))
gateH.clicked.connect(lambda:self.openGate('h'))
gateC.clicked.connect(lambda:self.openGate('c'))
gateID.clicked.connect(lambda:self.openGate('id'))
def generateGateLabel(self, gate):
gateLabel = QLabelClickable(gate)
self.image_path = './images/'
fullpath = self.image_path + gate + '.jpg'
pixmap = QPixmap(fullpath)
gateLabel.setScaledContents(True)
gateLabel.setPixmap(pixmap)
gateLabel.setMaximumSize(50,50)
gateLabel.setAlignment(Qt.AlignCenter)
return gateLabel
def openGate(self, gate):
gateDialog = QDialog()
gateDialog.setModal(True)
gateDialog.setWindowTitle("Información de la puerta cuántica")
gateDialog.setWindowIcon(QIcon("./images/icon.jpg"))
gateDialog.setGeometry(500, 500, 300, 300)
self.loadGateInfoUI(gateDialog, gate) #Llamada a un método que disponga los elementos UI
gateDialog.show()
gateDialog.exec()
def loadGateInfoUI(self, dialog, gate):
data = Util.loadGateData(gate) #Llamada a un método que nos devuelva la información de la puerta en un diccionario
vBox = QVBoxLayout()
#vBox.setStretch(1,100)
gridInfo = QGridLayout()
gridInfo.setColumnStretch(0,2)
gridInfo.setColumnStretch(1,8)
gridInfo.addWidget(QLabel("Nombre"),1,0)
gridInfo.addWidget(QLabel("Icono"),2,0)
gridInfo.addWidget(QLabel("Matriz"),3,0)
gridInfo.addWidget(QLabel(data.get("name")),1,1)
gatePixmap = QPixmap(data.get("symbol"))
gateIcon = QLabel("Hola")
gateIcon.setScaledContents(True)
#gridInfo.addWidget(gateIcon.setPixmap(gatePixmap),2,1)
gridInfo.addWidget(self.generateGateLabel(data.get('id').upper()),2,1)
gridInfo.addWidget(QLabel(np.array2string(data.get("matrix"))),3,1)
vBox.addLayout(gridInfo)
vBox.setStretch(1,1)
dialog.setLayout(vBox)
#vBox1.addWidget(QLabel(data.get("name"))) TODO de los datos obtenidos obtenemos el nombre, la matrix,etc
def startSimulation(self):
#TODO Crear circuito y cálculo
if self.currentCircuit.made == False:
self.currentCircuit.make(self.qbitsList)
simul = sim()
start_time = time.time()
results = simul.simulate(self.currentCircuit, 5)
elapsed_time = time.time() - start_time
print("Tiempo de ejecucion de la simulacion: %0.10f segundos." %elapsed_time)
self.openResultDialog(results)
def getAllMatrices(self):
gates = gt.gates.values()
matrices = []
for gate in gates:
matrices.append(gate.get('matrix'))
return matrices
def loadGateData(self, gate):
gate = gate.lower()
data = gt.gates.get(gate)
return data
def openResultDialog(self, results):
resultDialog = QDialog()
resultDialog.setModal(True)
resultDialog.setWindowTitle("Resultados de la simulación")
resultDialog.setWindowIcon(QIcon("./images/icon.jpg"))
resultDialog.setGeometry(500, 500, 800, 500)
self.loadResultUI(resultDialog, results) #Llamada a un método que disponga los elementos UI
resultDialog.show()
resultDialog.exec()
def loadResultUI(self, resultDialog : QDialog, results):
qbits = results.get('numQbits')
vLayout = QVBoxLayout()
resultWindow = pg.plot()
resultWindow.setBackground('w')
resultWindow.setXRange(0,pow(2,qbits), 0.05)
resultWindow.setYRange(0, 100, 0.05)
plot1 = resultWindow.getPlotItem()
plot1.showGrid(True, True)
plot1.getAxis('left').setLabel('Porcentaje')
plot1.getAxis('bottom').setLabel('Estados')
#Eje de la derecha
# plot2 = pg.ViewBox()
# plot1.showAxis('right')
# plot1.scene().addItem(plot2)
# plot1.getAxis('right').linkToView(plot2)
# plot2.setXLink(plot1)
# plot1.getAxis('right').setLabel('Frecuencia')
xAxis = []
for i in range(pow(2,qbits)):
state = bin(i)[2:]
if len(state) < qbits:
state = state.zfill(2)
xAxis.append(state)
xdict = dict(enumerate(xAxis))
plot1.getAxis('bottom').setTicks([xdict.items()])
#plot2.getAxis('bottom').setTicks([xdict.items()])
probabilities = results.get('prob')
bargraph = pg.BarGraphItem(x=range(pow(2,qbits)), height = probabilities, width = 0.6, brush ='g')
#bargraph2 = pg.BarGraphItem(x=range(pow(2,qbits)), height = [0,0,1,1], width = 0.6, brush = 'r')
plot1.addItem(bargraph)
#plot2.addItem(bargraph2)
#updateViews()
#plot1.vb.sigResized.connect(updateViews)
vLayout.addWidget(QLabel("Vector de estados"))
for i in range(pow(2,qbits)):
stateStr = "" + str(xAxis[i]) + " " + str(probabilities[i]) + "%" + " " #+ str(results["resMatrix"])
probLabel = QLabel(stateStr)
vLayout.addWidget(probLabel)
vLayout.setContentsMargins(5,5,5,5)
vLayout.addWidget(resultWindow)
resultDialog.setLayout(vLayout)
def updateViews():
## view has resized; update auxiliary views to match
global plot1, plot2
plot2.setGeometry(plot1.vb.sceneBoundingRect())
## need to re-update linked axes since this was called
## incorrectly while views had different shapes.
## (probably this should be handled in ViewBox.resizeEvent)
plot2.linkedViewChanged(plot1.vb, plot2.XAxis)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())