-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
155 lines (121 loc) · 4.54 KB
/
Copy pathmain.py
File metadata and controls
155 lines (121 loc) · 4.54 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
from grover_simulator import GroverSimulator
#g = GroverSimulator(3, 2, 1)
import sys
from PyQt6.QtWidgets import QApplication
from PyQt6.QtCore import QTimer, Qt, QElapsedTimer
from qt import MyWindow
class GameController:
def __init__(self, window):
self.window = window
self.keys = set()
self.speed = 5
self.timer = QElapsedTimer()
self.timer.start()
self.last_time = self.timer.elapsed() # ms
#grover simulator
self.qubit_count = 3
self.solutions = 2
self.iterations = 1
self.error_rate = 0.005
self.error_steps = 0 #this is done to avoid rounding errors
self.grover = GroverSimulator(self.qubit_count, self.solutions, self.iterations, 0)
self.window.set_simulator(self.grover)
#results
self.results = [0, 0]
self.window.set_results(self.results)
#controlling simulation
self.should_run = False
self.shots = 0
#button control
self.is_pressed = False
def key_press(self, key):
self.keys.add(key)
def key_release(self, key):
self.keys.discard(key)
def update(self):
current_time = self.timer.elapsed()
delta_ms = current_time - self.last_time
self.last_time = current_time
delta_time = delta_ms / 1000.0 # seconds
updated = False
if not self.is_pressed:
if Qt.Key.Key_W in self.keys:
updated = True
self.qubit_count += 1
if Qt.Key.Key_S in self.keys:
if (self.qubit_count > 2):
updated = True
self.qubit_count -= 1
if Qt.Key.Key_A in self.keys:
updated = True
self.solutions += 1
if Qt.Key.Key_D in self.keys:
if (self.solutions > 1):
updated = True
self.solutions -= 1
if Qt.Key.Key_Z in self.keys:
updated = True
self.iterations += 1
if Qt.Key.Key_X in self.keys:
if (self.iterations > 1):
updated = True
self.iterations -= 1
if Qt.Key.Key_C in self.keys:
updated = True
self.error_steps += 1
if Qt.Key.Key_V in self.keys:
if (self.error_steps >= 1):
updated = True
self.error_steps -= 1
self.is_pressed = updated
else:
keys = [Qt.Key.Key_W, Qt.Key.Key_S, Qt.Key.Key_A, Qt.Key.Key_D, Qt.Key.Key_Z, Qt.Key.Key_X,
Qt.Key.Key_C, Qt.Key.Key_V]
self.is_pressed = False
for x in keys:
if x in self.keys:
self.is_pressed = True
self.should_run = False
if Qt.Key.Key_T in self.keys:
self.should_run = True
if (updated):
self.grover = GroverSimulator(self.qubit_count, self.solutions, self.iterations, self.error_rate*self.error_steps)
self.window.set_simulator(self.grover)
self.results = [0, 0]
'''if Qt.Key.Key_S in self.keys:
self.window.y += self.speed
if Qt.Key.Key_A in self.keys:
self.window.x -= self.speed
if Qt.Key.Key_D in self.keys:
self.window.x += self.speed'''
if self.should_run:
val = self.grover.run()
for x in val.keys():
print(f"{str(x)[::-1]}, {self.grover.solution_list}")
print(val)
if str(x)[::-1] in self.grover.solution_list:
self.results[1] += 1
else:
self.results[0] += 1
print(self.results)
self.window.set_simulator(self.grover)
self.window.set_results(self.results)
self.window.update()
def main():
app = QApplication(sys.argv)
window = MyWindow()
window.setWindowTitle("GroverSimulator")
window.setGeometry(100, 100, 960, 540)
window.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
window.show()
controller = GameController(window)
# Inject input handlers
window.keyPressEvent = lambda e: controller.key_press(e.key())
window.keyReleaseEvent = lambda e: controller.key_release(e.key())
# Main loop
timer = QTimer()
timer.timeout.connect(controller.update)
timer.start(16) # ~60 FPS
sys.exit(app.exec())
if __name__ == "__main__":
main()