-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathquantum_darts.py
194 lines (179 loc) · 7 KB
/
quantum_darts.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import pygame
import numpy as np
import pyquil.api as api
from pyquil.gates import *
from pyquil.quil import Program
## open a QVM/QPU connection
cxn = api.QVMConnection()
# cxn = api.QPUConnection('19Q-Acorn')
# define colors
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
red_blue = (255, 0, 255)
red_green = (255, 255, 0)
blue_green = (0, 255, 255)
black = (0, 0, 0)
color_code = 0
dartboard_A_color = (127.5, 127.5, 127.5)
# boolean controling when game stops
game_over = False
color_rect = True
# frames per second
fps = 60
# screen dimensions
size_x = 1000
size_y = 625
size = (size_x, size_y)
# dartboard parameters
radius_outer = 60
width = 10
radius_1st_inner = radius_outer - 2 * width # 60
radius_2nd_inner = radius_outer - 4 * width # 20
dartboard_offset = 150
# initialize program with random measurement of either 0 or 1
p = Program(H(0)).measure(0, [0])
results_A = cxn.run(p, [0])
results_B = []
# initialize pygame
pygame.init()
# setup screen
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Quantum darts")
# setup game clock
clock = pygame.time.Clock()
# define font
font = pygame.font.SysFont('Calibri', 25, True)
# keep track of score and score text, as well as attempts
str_text_score = ''
score = 0
attempts = 0
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
# comparison stage
if color_code % 3 == 2:
mouse_pos = pygame.mouse.get_pos()
print ("Clicked Mouse pos: ", mouse_pos)
if (mouse_pos[0] >= size_x//2 - radius_outer) and (mouse_pos[0] <= size_x//2 + radius_outer):
if (mouse_pos[1] >= size_y//2 - dartboard_offset - radius_outer) and (mouse_pos[1] <= size_y//2 - dartboard_offset + radius_outer):
if results_A == [[0]]:
str_text_score = "SCORE!!"
score += 1
elif results_A == [[1]]:
str_text_score = "........POOR............"
elif (mouse_pos[1] >= size_y//2 + dartboard_offset - radius_outer) and (mouse_pos[1] <= size_y//2 + dartboard_offset + radius_outer):
if results_A == [[0]]:
str_text_score = "........POOR............"
elif results_A == [[1]]:
str_text_score = "SCORE!!"
score += 1
else:
str_text_score = "Missed the target...MAJOR FAIL!!"
score -= 1
else:
str_text_score = "Missed the target...MAJOR FAIL!!"
score -= 1
attempts += 1
color_code += 1
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
pass
# set game color
if color_code % 3 == 0:
# make scren blue
screen.fill(blue)
# empty out results from dartboard_B
if len(results_B) > 0:
results_B.pop()
# run the common parts of the program
p = Program()
if score < 10:
a = np.sqrt(1/2.)
elif (score >= 10) and (score < 20):
a = np.sqrt(3/4.)
else:
a = np.sqrt(1.)
b = np.sqrt(1 - np.square(a))
Uf_ = np.array([[a, b], [b, -a]])
p.defgate("Uf", Uf_)
# set dartboard_A center, and calculate results_B in either case
if results_A == [[0]]:
dartboard_A_center = [size_x//2, size_y//2 - dartboard_offset]
# run the program for this case
p.inst(I(0))
p.inst(("Uf", 0))
p.measure(0, [0])
results_B = cxn.run(p, [0])
elif results_A == [[1]]:
dartboard_A_center = [size_x//2, size_y//2 + dartboard_offset]
# run the program for this case
p.inst(X(0))
p.inst(("Uf", 0))
p.measure(0, [0])
results_B = cxn.run(p, [0])
elif color_code % 3 == 1:
screen.fill(red)
if len(results_A) > 0:
results_A.pop()
# run the common parts of the program
p = Program()
if score < 10:
a = np.sqrt(1/2.)
elif (score >= 10) and (score < 20):
a = np.sqrt(3/4.)
else:
a = np.sqrt(1.)
b = np.sqrt(1 - np.square(a))
Uf_ = np.array([[a, b], [b, -a]])
p.defgate("Uf", Uf_)
# set dartboard_B centers
if results_B == [[0]]:
if score < 10:
dartboard_B_center = [size_x//2 + dartboard_offset, size_y//2]
elif (score >= 10) and (score < 20):
dartboard_B_center = [size_x//2 + dartboard_offset//2, size_y//2 - dartboard_offset//2]
else:
dartboard_B_center = [size_x//2, size_y//2 - dartboard_offset]
# run the program for this case
p.inst(I(0))
p.inst(("Uf", 0))
p.measure(0, [0])
results_A = cxn.run(p, [0])
elif results_B == [[1]]:
if score < 10:
dartboard_B_center = [size_x//2 - dartboard_offset, size_y//2]
elif (score >= 10) and (score < 20):
dartboard_B_center = [size_x//2 - dartboard_offset//2, size_y//2 + dartboard_offset//2]
else:
dartboard_B_center = [size_x//2, size_y//2 + dartboard_offset]
# run the program for this case
p.inst(X(0))
p.inst(("Uf", 0))
p.measure(0, [0])
results_A = cxn.run(p, [0])
elif color_code % 3 == 2:
screen.fill(blue)
text_score = font.render(str_text_score, True, black)
screen.blit(text_score, [100, size_y//2])
# draw dartboard
if color_code % 3 == 0:
pygame.draw.circle(screen, dartboard_A_color, dartboard_A_center, radius_outer, width)
pygame.draw.circle(screen, dartboard_A_color, dartboard_A_center, radius_1st_inner, width)
pygame.draw.circle(screen, dartboard_A_color, dartboard_A_center, radius_2nd_inner, width)
elif color_code % 3 == 1:
pygame.draw.circle(screen, dartboard_A_color, dartboard_B_center, radius_outer, width)
pygame.draw.circle(screen, dartboard_A_color, dartboard_B_center, radius_1st_inner, width)
pygame.draw.circle(screen, dartboard_A_color, dartboard_B_center, radius_2nd_inner, width)
elif color_code % 3 == 2:
pygame.draw.circle(screen, dartboard_A_color, dartboard_A_center, radius_outer, 10)
# display Score
text_score = font.render("Score: " + str(score), True, black)
screen.blit(text_score, [size_x - 150, 20])
text_attempts = font.render("Attempts: " + str(attempts), True, black)
screen.blit(text_attempts, [size_x - 150, 40])
pygame.display.flip()
clock.tick(fps)