def __init__(self, data): self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() cx, cy = self.screen_rect.center # display objects self.Message = Message((cx, cy)) # buttons self.YesButton = YesButton() self.NoButton = NoButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton() self.tutorial_on = False # self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False self.real_qc = None
def __init__(self, data): self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.checked_db = None cx, cy = self.screen_rect.center self.DoorBobs = [ DoorBob((cx / 2, cy), self.data), DoorBob((cx, cy), self.data), DoorBob((3 * cx / 2, cy), self.data) ] self.ConfirmButton = ConfirmButton() self.CircuitButton = CircuitButton() self.BackButton = BackButton() self.tutorial_on = False # self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False
def __init__(self): self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.data = {'BallProbDist': [1/3, 1/3, 1/3], 'BobChosenDoor': -1, 'AliceOpenedDoor': -1, 'SwitchProbDist': [1, 0], 'Measurement': 0.0, 'WinRate': [0, 1]} self.stages = [AliceArrangesBalls, BobChoosesDoor, AliceOpensDoor, BobSwitchesDoor, RunOnRealQC, ShowResult,] self.stage_index = 0 self.quit = False self.back = False self.Loader = Loader() self.CircuitDisplay = CircuitDisplay(self.data) self.ConfirmButton = ConfirmButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton()
def __init__(self, data): self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() cx, cy = self.screen_rect.center # display objects self.Celebration = Celebration( (cx, Celebration.height/2), self.data) self.Message = Message((cx, cy + Message.height/4), self.data) # buttons self.ConfirmButton = ConfirmButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton() self.tutorial_on = False # self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False
def __init__(self): self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.data = { 'J': array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]), 'UA': array([[1, 0], [0, 1]]) } self.stages = [ StartStage, AliceStage, BobStage, # AliceDecides, # ShowResult ] self.stage_index = 0 self.quit = False self.back = False cx, cy = self.screen_rect.center # self.CircuitDisplay = CircuitDisplay(self.data) self.BackButton = BackButton() self.CircuitButton = CircuitButton()
def __init__(self, data): self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.checked_da = None cx, cy = self.screen_rect.center self.DoorBars = [ DoorBar((cx / 2, cy), self.data['BallProbDist'][0]), DoorBar((cx, cy), self.data['BallProbDist'][1]), DoorBar((3 * cx / 2, cy), self.data['BallProbDist'][2]) ] self.DoorAlices = [ DoorAlice((cx / 2, cy), self.data), DoorAlice((cx, cy), self.data), DoorAlice((3 * cx / 2, cy), self.data) ] self.DoorAlices[self.data['BobChosenDoor']].chosen = True # the door with smaller prob cannot be opened! prob = None index = None for i in range(3): if not self.DoorAlices[i].chosen: if prob == None and index == None: prob = self.data['BallProbDist'][i] index = i else: this_prob = self.data['BallProbDist'][i] if prob > this_prob: self.DoorAlices[index].lock = True elif prob == this_prob: pass else: self.DoorAlices[i].lock = True self.ConfirmButton = ConfirmButton() self.CircuitButton = CircuitButton() self.BackButton = BackButton() self.tutorial_on = False # self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False
def __init__(self, data): """ Get a reference to the screen (created in main); define necessary attributes; and create our thing. """ self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.dragged_k = None self.checked_cb = None cx, cy = self.screen_rect.center self.alice = Alice((cx - 100, 1.2 * cy)) self.bob = Bob((cx + 100, 1.2 * cy)) self.Knobs = [Knob((cx / 2, 0.5 * cy), 0)] self.caption = Caption((cx, 0.5 * cy)) # self.Doors = [Door((cx / 2, cy)), # Door((cx, cy)), # Door((3 * cx / 2, cy))] # # self.CheckBoxes = [CheckBox((cx / 2, cy + Door.height / 2 + 20)), # CheckBox((cx, cy + Door.height / 2 + 20)), # CheckBox((3 * cx / 2, cy + Door.height / 2 + 20))] # # self.Ball = Ball([(cx / 2, cy - Door.height / 4), # (cx, cy - Door.height / 4), # (3 * cx / 2, cy - Door.height / 4)], # self.data['BallProbDist']) self.ConfirmButton = ConfirmButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton() self.tutorial_on = False # self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False
def __init__(self, data): self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.dragged_db = None self.checked_cb = None cx, cy = self.screen_rect.center self.DoorBars = [ DoorBar((cx / 2, cy), self.data['BallProbDist'][0]), DoorBar((cx, cy), self.data['BallProbDist'][1]), DoorBar((3 * cx / 2, cy), self.data['BallProbDist'][2]) ] self.Doors = [ Door((cx / 2, cy)), Door((cx, cy)), Door((3 * cx / 2, cy)) ] self.CheckBoxes = [ CheckBox((cx / 2, cy + Door.height / 2 + 20)), CheckBox((cx, cy + Door.height / 2 + 20)), CheckBox((3 * cx / 2, cy + Door.height / 2 + 20)) ] self.Ball = Ball([(cx / 2, cy - Door.height / 4), (cx, cy - Door.height / 4), (3 * cx / 2, cy - Door.height / 4)], self.data['BallProbDist']) self.ConfirmButton = ConfirmButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton() self.tutorial_on = False #self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False
def __init__(self, data): self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.dragged_k = None self.checked_cb = None cx, cy = self.screen_rect.center self.Officer = Officer((5 * cx / 3, cy - 100)) self.Phi = Phi((100, cy + 60)) self.Theta = Theta((100, cy - 60)) self.caption = Caption((cx, 0.5 * cy)) self.Knobs = [ Knob((50, cy - 60), np.pi / 2), # theta Knob((50, cy + 60), 0) ] # phi self.GBButtons = [ GBButton((cx + 75, cy + 150), self.Knobs[0].angle, 'red', 'GOOD!'), GBButton((cx - 100, cy + 150), self.Knobs[0].angle, 'blue', 'BAD!') ] self.ConfirmButton = ConfirmButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton() self.next_stage = False self.quit = False self.back = False self.show_circuit = False
class AliceOpensDoor: def __init__(self, data): self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.checked_da = None cx, cy = self.screen_rect.center self.DoorBars = [ DoorBar((cx / 2, cy), self.data['BallProbDist'][0]), DoorBar((cx, cy), self.data['BallProbDist'][1]), DoorBar((3 * cx / 2, cy), self.data['BallProbDist'][2]) ] self.DoorAlices = [ DoorAlice((cx / 2, cy), self.data), DoorAlice((cx, cy), self.data), DoorAlice((3 * cx / 2, cy), self.data) ] self.DoorAlices[self.data['BobChosenDoor']].chosen = True # the door with smaller prob cannot be opened! prob = None index = None for i in range(3): if not self.DoorAlices[i].chosen: if prob == None and index == None: prob = self.data['BallProbDist'][i] index = i else: this_prob = self.data['BallProbDist'][i] if prob > this_prob: self.DoorAlices[index].lock = True elif prob == this_prob: pass else: self.DoorAlices[i].lock = True self.ConfirmButton = ConfirmButton() self.CircuitButton = CircuitButton() self.BackButton = BackButton() self.tutorial_on = False # self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False def event_loop(self): for event in pygame.event.get(): if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]: self.quit = True elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: for da in self.DoorAlices: da.check_click(event.pos) if da.click: # if True, it means we unchecked it if da.checked: self.checked_da = None # if not, it means we checked it else: self.checked_da = da break self.ConfirmButton.check_click(event.pos) self.BackButton.check_click(event.pos) self.CircuitButton.check_click(event.pos) def render(self): self.screen.fill(pygame.Color(BACKGROUND_COLOR)) for db in self.DoorBars: db.draw(self.screen) for da in self.DoorAlices: da.draw(self.screen) self.ConfirmButton.draw(self.screen) self.BackButton.draw(self.screen) self.CircuitButton.draw(self.screen) pygame.display.update() def main_loop(self): while not (self.quit or self.next_stage or self.back or self.show_circuit): self.event_loop() # CheckAlice for i in range(0, 3): da = self.DoorAlices[i] if da != self.checked_da: da.force_unchecked() else: self.data["AliceOpenedDoor"] = i open_index = i add_index = (i + 1) % 3 prob_dist = list(self.data["BallProbDist"]) prob_dist[add_index] += prob_dist[open_index] prob_dist[open_index] = 0 for j in range(0, 3): self.DoorBars[j].update_with_prob(prob_dist[j]) da.update_click() if self.data["AliceOpenedDoor"] == -1: for j in range(0, 3): self.DoorBars[j].update_with_prob( self.data["BallProbDist"][j]) if self.ConfirmButton.click: self.ConfirmButton.update_click() # continue only when bob's choice has been set if self.data["AliceOpenedDoor"] != -1: self.next_stage = True self.data["BallProbDist"][add_index] += self.data[ "BallProbDist"][open_index] self.data["BallProbDist"][open_index] = 0 elif self.BackButton.click: self.BackButton.update_click() self.back = True elif self.CircuitButton.click: self.CircuitButton.update_click() self.show_circuit = True self.render() self.clock.tick(self.fps)
class BobChoosesDoor: def __init__(self, data): self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.checked_db = None cx, cy = self.screen_rect.center self.DoorBobs = [ DoorBob((cx / 2, cy), self.data), DoorBob((cx, cy), self.data), DoorBob((3 * cx / 2, cy), self.data) ] self.ConfirmButton = ConfirmButton() self.CircuitButton = CircuitButton() self.BackButton = BackButton() self.tutorial_on = False # self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False def event_loop(self): for event in pygame.event.get(): if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]: self.quit = True elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: for db in self.DoorBobs: db.check_click(event.pos) if db.click: # if True, it means we unchecked it if db.checked: self.checked_db = None # if not, it means we checked it else: self.checked_db = db break self.ConfirmButton.check_click(event.pos) self.BackButton.check_click(event.pos) self.CircuitButton.check_click(event.pos) def render(self): self.screen.fill(pygame.Color(BACKGROUND_COLOR)) for db in self.DoorBobs: db.draw(self.screen) self.ConfirmButton.draw(self.screen) self.BackButton.draw(self.screen) self.CircuitButton.draw(self.screen) pygame.display.update() def main_loop(self): while not (self.quit or self.next_stage or self.back or self.show_circuit): self.event_loop() # CheckBob for i in range(0, 3): db = self.DoorBobs[i] if db != self.checked_db: db.force_unchecked() else: self.data["BobChosenDoor"] = i db.update_click() if self.ConfirmButton.click: self.ConfirmButton.update_click() # continue only when bob's choice has been set if self.data["BobChosenDoor"] != -1: self.next_stage = True elif self.BackButton.click: self.BackButton.update_click() self.back = True elif self.CircuitButton.click: self.CircuitButton.update_click() self.show_circuit = True self.render() self.clock.tick(self.fps)
class ShowResult(): def __init__(self, data): self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() cx, cy = self.screen_rect.center # display objects self.Celebration = Celebration( (cx, Celebration.height/2), self.data) self.Message = Message((cx, cy + Message.height/4), self.data) # buttons self.ConfirmButton = ConfirmButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton() self.tutorial_on = False # self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False def event_loop(self): for event in pygame.event.get(): if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]: self.quit = True elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: self.ConfirmButton.check_click(event.pos) self.BackButton.check_click(event.pos) self.CircuitButton.check_click(event.pos) elif event.type in (pygame.KEYUP, pygame.KEYDOWN): self.keys = pygame.key.get_pressed() def render(self): self.screen.fill(pygame.Color(BACKGROUND_COLOR)) self.Celebration.draw(self.screen) self.Message.draw(self.screen) self.ConfirmButton.draw(self.screen) self.BackButton.draw(self.screen) self.CircuitButton.draw(self.screen) pygame.display.update() pygame.display.update() def main_loop(self): while not (self.quit or self.next_stage or self.back or self.show_circuit): self.event_loop() if self.ConfirmButton.click: self.ConfirmButton.update_click() self.next_stage = True elif self.BackButton.click: self.BackButton.update_click() self.back = True elif self.CircuitButton.click: self.CircuitButton.update_click() self.show_circuit = True self.render() self.clock.tick(self.fps)
class BobSwitchesDoor(): #expect receiving an int(0,1,2) represents which Door Alice opens def __init__(self, data): self.data = data self.Alice = self.data['AliceOpenedDoor'] self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.dragged_db = None self.checked_cb = None cx, cy = self.screen_rect.center self.DoorPos = [(cx / 2, cy), (cx, cy), (1.5 * cx, cy)] self.BobPos = [(cx / 2 - Door.width * 4 // 3, cy), (cx - Door.width * 4 // 3, cy), (3 * cx / 2 - Door.width * 4 // 3, cy)] # swap if bob did not choose door 00, to ensure |0> is bob's initial choice if self.data['BobChosenDoor'] != 0: choice = self.data['BobChosenDoor'] self.DoorPos[choice], self.DoorPos[0] = self.DoorPos[ 0], self.DoorPos[choice] self.BobPos[choice], self.BobPos[0] = self.BobPos[0], self.BobPos[ choice] # swap Alice as well if Alice choosed 0 if self.Alice == 0: self.Alice = choice #remove Alice's choice from Bob's image self.BobPos.pop(self.Alice) #remove and take out the pos of Alice's Choice self.OpenedDoorPos = self.DoorPos.pop(self.Alice) self.DoorBars = [ DoorBarBob(self.DoorPos[0], self.data['SwitchProbDist'][0]), DoorBarBob(self.DoorPos[1], self.data['SwitchProbDist'][1]) ] self.Doors = [Door(self.DoorPos[0]), Door(self.DoorPos[1])] self.BobImage = BobImage(self.BobPos, self.data['SwitchProbDist']) self.OpenedDoor = OpenedDoor(self.OpenedDoorPos) self.ConfirmButton = ConfirmButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton() self.tutorial_on = False #self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False def event_loop(self): for event in pygame.event.get(): if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]: self.quit = True elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: for db in self.DoorBars: db.check_click(event.pos) if db.click == True: self.dragged_db = db break self.ConfirmButton.check_click(event.pos) self.BackButton.check_click(event.pos) self.CircuitButton.check_click(event.pos) elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: for db in self.DoorBars: if db.click: db.click = False self.dragged_db = None break def render(self): self.screen.fill(pygame.Color(BACKGROUND_COLOR)) for db in self.DoorBars: db.draw(self.screen) for d in self.Doors: d.draw(self.screen) #Capital OpenedDoor is the object pointing to class OpenedDoor(pos) self.OpenedDoor.draw(self.screen) self.BobImage.draw(self.screen) self.ConfirmButton.draw(self.screen) self.BackButton.draw(self.screen) self.CircuitButton.draw(self.screen) pygame.display.update() def main_loop(self): while not (self.quit or self.next_stage or self.back or self.show_circuit): self.event_loop() #DoorBar for db in self.DoorBars: db.update_drag() prob_sum = sum(self.DoorBars[i].prob for i in range(2)) #if prob_sum !=1: if self.dragged_db == None: for db in self.DoorBars: db.update_with_prob(db.prob / prob_sum) else: for db in self.DoorBars: if db != self.dragged_db: db.update_with_prob(1 - self.dragged_db.prob) self.data['SwitchProbDist'] = [db.prob for db in self.DoorBars] self.BobImage.update_distribution( [db.prob for db in self.DoorBars]) if self.ConfirmButton.click: self.ConfirmButton.update_click() self.next_stage = True elif self.BackButton.click: self.BackButton.update_click() self.back = True elif self.CircuitButton.click: self.CircuitButton.update_click() self.show_circuit = True self.render() self.clock.tick(self.fps)
class MontyHall(): def __init__(self): self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.data = {'BallProbDist': [1/3, 1/3, 1/3], 'BobChosenDoor': -1, 'AliceOpenedDoor': -1, 'SwitchProbDist': [1, 0], 'Measurement': 0.0, 'WinRate': [0, 1]} self.stages = [AliceArrangesBalls, BobChoosesDoor, AliceOpensDoor, BobSwitchesDoor, RunOnRealQC, ShowResult,] self.stage_index = 0 self.quit = False self.back = False self.Loader = Loader() self.CircuitDisplay = CircuitDisplay(self.data) self.ConfirmButton = ConfirmButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton() def update_circuit(self): if self.stage_index == 0: self.CircuitDisplay.update_circuit_0() elif self.stage_index == 1: self.CircuitDisplay.update_circuit_1() elif self.stage_index == 2: self.CircuitDisplay.update_circuit_2() elif self.stage_index == 3: self.CircuitDisplay.update_circuit_3() elif self.stage_index == 4: self.CircuitDisplay.update_circuit_4() def calculate_result(self, mode): qc = self.CircuitDisplay.qc if mode == 'real': provider = IBMQ.load_account() backend = provider.backends.ibmq_16_melbourne qobj = assemble(transpile(qc, backend = backend), backend = backend, shots = 1000) job = backend.run(qobj) elif mode == 'simulator': job = qiskit.execute(qc, qiskit.Aer.get_backend('qasm_simulator'), shots = 1000) self.Loader.take_job(job, mode) while not self.Loader.done: self.load_event_loop() if self.quit == True: job.cancel() return self.screen.fill(pygame.Color(BACKGROUND_COLOR)) self.Loader.draw(self.screen) pygame.display.update() if job.status() == JOB_FINAL_STATES[0]: result_dict = job.result().get_counts() if '1' not in result_dict: aw, bw = 1, 0 elif '0' not in result_dict: aw, bw = 0, 1 else: aw, bw = result_dict['0']/1000, result_dict['1']/1000 self.data['WinRate'] = [aw, bw] self.data['Measurement'] = random.choice([0, 1], p = self.data['WinRate']) else: raise ValueError('job did not run correctly') while True: self.confirm_event_loop() if self.ConfirmButton.click: self.ConfirmButton.update_click() self.stage_index += 1 break self.screen.fill(pygame.Color(BACKGROUND_COLOR)) self.Loader.draw(self.screen) self.ConfirmButton.draw(self.screen) pygame.display.update() else: raise ValueError('mode must either be real or simulator') def event_loop(self): for event in pygame.event.get(): if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]: self.quit = True elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: self.BackButton.check_click(event.pos) self.CircuitButton.check_click(event.pos) elif event.type in (pygame.KEYUP, pygame.KEYDOWN): self.keys = pygame.key.get_pressed() def load_event_loop(self): for event in pygame.event.get(): if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]: self.quit = True elif event.type in (pygame.KEYUP, pygame.KEYDOWN): self.keys = pygame.key.get_pressed() def confirm_event_loop(self): for event in pygame.event.get(): if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]: self.quit = True elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: self.ConfirmButton.check_click(event.pos) elif event.type in (pygame.KEYUP, pygame.KEYDOWN): self.keys = pygame.key.get_pressed() def render(self): self.screen.fill(pygame.Color(BACKGROUND_COLOR)) self.CircuitDisplay.draw(self.screen) self.BackButton.draw(self.screen) self.CircuitButton.draw(self.screen) pygame.display.update() def main_loop(self): current_stage = None while not (self.quit or self.back): if self.stages[self.stage_index] != type(current_stage): current_stage = self.stages[self.stage_index](self.data) self.update_circuit() current_stage.main_loop() if current_stage.next_stage == True: if self.stage_index == len(self.stages) - 1: self.back = True else: self.update_circuit() self.stage_index += 1 elif current_stage.back == True: self.back = True elif current_stage.quit == True: self.quit = True elif current_stage.show_circuit == True: self.update_circuit() while (current_stage.show_circuit and not (self.quit or self.back)): self.event_loop() if self.BackButton.click: self.BackButton.update_click() self.back = True elif self.CircuitButton.click: self.CircuitButton.update_click() current_stage.show_circuit = False self.render() self.clock.tick(self.fps) elif type(current_stage) == RunOnRealQC: if current_stage.real_qc == True: self.calculate_result('real') elif current_stage.real_qc == False: self.calculate_result('simulator') else: raise ValueError('real_qc is not bool') else: raise ValueError('Stage quits without proceeding')
class PrisonerDilemma: def __init__(self): self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.data = { 'J': array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]), 'UA': array([[1, 0], [0, 1]]) } self.stages = [ StartStage, AliceStage, BobStage, # AliceDecides, # ShowResult ] self.stage_index = 0 self.quit = False self.back = False cx, cy = self.screen_rect.center # self.CircuitDisplay = CircuitDisplay(self.data) self.BackButton = BackButton() self.CircuitButton = CircuitButton() def event_loop(self): """ This is the event loop for the whole program. Regardless of the complexity of a program, there should never be a need to have more than one event loop. """ for event in pygame.event.get(): if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]: self.quit = True elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: self.BackButton.check_click(event.pos) self.CircuitButton.check_click(event.pos) elif event.type in (pygame.KEYUP, pygame.KEYDOWN): self.keys = pygame.key.get_pressed() def render(self): self.screen.fill(pygame.Color(BACKGROUND_COLOR)) # self.CircuitDisplay.draw(self.screen) self.BackButton.draw(self.screen) self.CircuitButton.draw(self.screen) pygame.display.update() def main_loop(self): current_stage = None while not (self.quit or self.back): for k, v in self.data.items(): print(k, v) print() if self.stages[self.stage_index] != type(current_stage): current_stage = self.stages[self.stage_index](self.data) current_stage.main_loop() if current_stage.next_stage == True: # self.update_circuit() self.stage_index += 1 if self.stage_index == len(self.stages): self.back = True elif current_stage.back == True: self.back = True elif current_stage.quit == True: self.quit = True elif current_stage.show_circuit == True: # self.update_circuit() while (current_stage.show_circuit and not (self.quit or self.back)): self.event_loop() if self.BackButton.click: self.BackButton.update_click() self.back = True elif self.CircuitButton.click: self.CircuitButton.update_click() current_stage.show_circuit = False self.render() self.clock.tick(self.fps) else: raise ValueError('Stage quits without proceeding')
class AliceArrangesBalls(): def __init__(self, data): self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.dragged_db = None self.checked_cb = None cx, cy = self.screen_rect.center self.DoorBars = [ DoorBar((cx / 2, cy), self.data['BallProbDist'][0]), DoorBar((cx, cy), self.data['BallProbDist'][1]), DoorBar((3 * cx / 2, cy), self.data['BallProbDist'][2]) ] self.Doors = [ Door((cx / 2, cy)), Door((cx, cy)), Door((3 * cx / 2, cy)) ] self.CheckBoxes = [ CheckBox((cx / 2, cy + Door.height / 2 + 20)), CheckBox((cx, cy + Door.height / 2 + 20)), CheckBox((3 * cx / 2, cy + Door.height / 2 + 20)) ] self.Ball = Ball([(cx / 2, cy - Door.height / 4), (cx, cy - Door.height / 4), (3 * cx / 2, cy - Door.height / 4)], self.data['BallProbDist']) self.ConfirmButton = ConfirmButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton() self.tutorial_on = False #self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False def event_loop(self): for event in pygame.event.get(): if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]: self.quit = True elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: for db in self.DoorBars: db.check_click(event.pos) if db.click == True: self.dragged_db = db break for cb in self.CheckBoxes: cb.check_click(event.pos) if cb.click == True: # if True, it means we unchecked it if cb.checked == True: self.checked_cb = None # if not, it means we checked it else: self.checked_cb = cb break self.ConfirmButton.check_click(event.pos) self.BackButton.check_click(event.pos) self.CircuitButton.check_click(event.pos) ''' print(self.DoorBars.index(self.dragged_db) if self.dragged_db != None else None, self.CheckBoxes.index(self.checked_cb) if self.checked_cb != None else None) ''' elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: for db in self.DoorBars: if db.click: db.click = False self.dragged_db = None break elif event.type in (pygame.KEYUP, pygame.KEYDOWN): self.keys = pygame.key.get_pressed() def render(self): self.screen.fill(pygame.Color(BACKGROUND_COLOR)) for db in self.DoorBars: db.draw(self.screen) for d in self.Doors: d.draw(self.screen) for cb in self.CheckBoxes: cb.draw(self.screen) self.Ball.draw(self.screen) self.ConfirmButton.draw(self.screen) self.BackButton.draw(self.screen) self.CircuitButton.draw(self.screen) pygame.display.update() def main_loop(self): """ This is the game loop for the entire program. Like the event_loop, there should not be more than one game_loop. """ while not (self.quit or self.next_stage or self.back or self.show_circuit): self.event_loop() # CheckBox for cb in self.CheckBoxes: if cb != self.checked_cb: cb.force_unchecked() cb.update_click() # DoorBar for i in range(3): db = self.DoorBars[i] cb = self.CheckBoxes[i] if not cb.checked: db.update_drag() prob_sum = sum(self.DoorBars[i].prob for i in range(3)) if prob_sum != 1: if self.checked_cb == None or self.dragged_db == None: for db in self.DoorBars: db.update_with_prob(db.prob / prob_sum) else: checked_db = self.DoorBars[self.CheckBoxes.index( self.checked_cb)] checked_prob = checked_db.prob if self.dragged_db.prob > 1 - checked_prob: self.dragged_db.update_with_prob(1 - checked_prob) for db in self.DoorBars: if db != checked_db and db != self.dragged_db: db.update_with_prob(1 - checked_prob - self.dragged_db.prob) self.data['BallProbDist'] = [db.prob for db in self.DoorBars] #Ball self.Ball.update_distribution([db.prob for db in self.DoorBars]) if self.ConfirmButton.click: self.ConfirmButton.update_click() self.next_stage = True elif self.BackButton.click: self.BackButton.update_click() self.back = True elif self.CircuitButton.click: self.CircuitButton.update_click() self.show_circuit = True self.render() self.clock.tick(self.fps)
def __init__(self, data): self.data = data self.Alice = self.data['AliceOpenedDoor'] self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.dragged_db = None self.checked_cb = None cx, cy = self.screen_rect.center self.DoorPos = [(cx / 2, cy), (cx, cy), (1.5 * cx, cy)] self.BobPos = [(cx / 2 - Door.width * 4 // 3, cy), (cx - Door.width * 4 // 3, cy), (3 * cx / 2 - Door.width * 4 // 3, cy)] # swap if bob did not choose door 00, to ensure |0> is bob's initial choice if self.data['BobChosenDoor'] != 0: choice = self.data['BobChosenDoor'] self.DoorPos[choice], self.DoorPos[0] = self.DoorPos[ 0], self.DoorPos[choice] self.BobPos[choice], self.BobPos[0] = self.BobPos[0], self.BobPos[ choice] # swap Alice as well if Alice choosed 0 if self.Alice == 0: self.Alice = choice #remove Alice's choice from Bob's image self.BobPos.pop(self.Alice) #remove and take out the pos of Alice's Choice self.OpenedDoorPos = self.DoorPos.pop(self.Alice) self.DoorBars = [ DoorBarBob(self.DoorPos[0], self.data['SwitchProbDist'][0]), DoorBarBob(self.DoorPos[1], self.data['SwitchProbDist'][1]) ] self.Doors = [Door(self.DoorPos[0]), Door(self.DoorPos[1])] self.BobImage = BobImage(self.BobPos, self.data['SwitchProbDist']) self.OpenedDoor = OpenedDoor(self.OpenedDoorPos) self.ConfirmButton = ConfirmButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton() self.tutorial_on = False #self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False
class StartStage: def __init__(self, data): """ Get a reference to the screen (created in main); define necessary attributes; and create our thing. """ self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.dragged_k = None self.checked_cb = None cx, cy = self.screen_rect.center self.alice = Alice((cx - 100, 1.2 * cy)) self.bob = Bob((cx + 100, 1.2 * cy)) self.Knobs = [Knob((cx / 2, 0.5 * cy), 0)] self.caption = Caption((cx, 0.5 * cy)) # self.Doors = [Door((cx / 2, cy)), # Door((cx, cy)), # Door((3 * cx / 2, cy))] # # self.CheckBoxes = [CheckBox((cx / 2, cy + Door.height / 2 + 20)), # CheckBox((cx, cy + Door.height / 2 + 20)), # CheckBox((3 * cx / 2, cy + Door.height / 2 + 20))] # # self.Ball = Ball([(cx / 2, cy - Door.height / 4), # (cx, cy - Door.height / 4), # (3 * cx / 2, cy - Door.height / 4)], # self.data['BallProbDist']) self.ConfirmButton = ConfirmButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton() self.tutorial_on = False # self.TutorialBlocks = [TutorialBlock()] self.next_stage = False self.quit = False self.back = False self.show_circuit = False def event_loop(self): """ This is the event loop for the whole program. Regardless of the complexity of a program, there should never be a need to have more than one event loop. """ for event in pygame.event.get(): if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]: self.quit = True elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: for k in self.Knobs: k.check_click(event.pos) if k.click == True: self.dragged_k = k break # for cb in self.CheckBoxes: # cb.check_click(event.pos) # if cb.click == True: # # if True, it means we unchecked it # if cb.checked == True: # self.checked_cb = None # # if not, it means we checked it # else: # self.checked_cb = cb # break self.ConfirmButton.check_click(event.pos) self.BackButton.check_click(event.pos) self.CircuitButton.check_click(event.pos) ''' print(self.Knobs.index(self.dragged_k) if self.dragged_k != None else None, self.CheckBoxes.index(self.checked_cb) if self.checked_cb != None else None) ''' elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: for k in self.Knobs: if k.click: k.click = False self.dragged_k = None break elif event.type in (pygame.KEYUP, pygame.KEYDOWN): self.keys = pygame.key.get_pressed() def render(self): """ All drawing should be found here. This is the only place that pygame.display.update() should be found. """ self.screen.fill(pygame.Color(BACKGROUND_COLOR)) for k in self.Knobs: k.draw(self.screen) # for d in self.Doors: # d.draw(self.screen) # # for cb in self.CheckBoxes: # cb.draw(self.screen) # self.Ball.draw(self.screen) self.bob.draw(self.screen) self.alice.draw(self.screen) self.ConfirmButton.draw(self.screen) self.BackButton.draw(self.screen) self.CircuitButton.draw(self.screen) self.caption.draw(self.screen) pygame.display.update() def main_loop(self): """ This is the game loop for the entire program. Like the event_loop, there should not be more than one game_loop. """ while not (self.quit or self.next_stage or self.back or self.show_circuit): self.event_loop() # CheckBox # for cb in self.CheckBoxes: # if cb != self.checked_cb: # cb.force_unchecked() # cb.update_click() # Knob for i in range(1): k = self.Knobs[i] # cb = self.CheckBoxes[i] # if not cb.checked: k.update_drag() # prob_sum = sum(self.Knobs[i].prob for i in range(3)) # if prob_sum != 1: # if self.checked_cb == None or self.dragged_k == None: # for k in self.Knobs: # k.update_with_prob(k.prob / prob_sum) # else: # checked_k = self.Knobs[self.CheckBoxes.index(self.checked_cb)] # checked_prob = checked_k.prob # if self.dragged_k.prob > 1 - checked_prob: # self.dragged_k.update_with_prob(1 - checked_prob) # for k in self.Knobs: # if k != checked_k and k != self.dragged_k: # k.update_with_prob(1 - checked_prob - self.dragged_k.prob) # # self.data['BallProbDist'] = [k.prob for k in self.Knobs] # Ball # self.Ball.update_distribution([k.prob for k in self.Knobs]) if self.ConfirmButton.click: self.ConfirmButton.update_click() self.next_stage = True elif self.BackButton.click: self.BackButton.update_click() self.back = True elif self.CircuitButton.click: self.CircuitButton.update_click() self.show_circuit = True self.render() self.clock.tick(self.fps)
class BobStage: def __init__(self, data): self.data = data self.screen = pygame.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pygame.time.Clock() self.fps = FPS self.keys = pygame.key.get_pressed() self.dragged_k = None self.checked_cb = None cx, cy = self.screen_rect.center self.Officer = Officer((5 * cx / 3, cy - 100)) self.Phi = Phi((100, cy + 60)) self.Theta = Theta((100, cy - 60)) self.caption = Caption((cx, 0.5 * cy)) self.Knobs = [ Knob((50, cy - 60), np.pi / 2), # theta Knob((50, cy + 60), 0) ] # phi self.GBButtons = [ GBButton((cx + 75, cy + 150), self.Knobs[0].angle, 'red', 'GOOD!'), GBButton((cx - 100, cy + 150), self.Knobs[0].angle, 'blue', 'BAD!') ] self.ConfirmButton = ConfirmButton() self.BackButton = BackButton() self.CircuitButton = CircuitButton() self.next_stage = False self.quit = False self.back = False self.show_circuit = False def event_loop(self): for event in pygame.event.get(): if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]: self.quit = True elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: for k in self.Knobs: k.check_click(event.pos) if k.click == True: self.dragged_k = k break self.ConfirmButton.check_click(event.pos) self.BackButton.check_click(event.pos) self.CircuitButton.check_click(event.pos) elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: for k in self.Knobs: if k.click: k.click = False self.dragged_k = None break elif event.type in (pygame.KEYUP, pygame.KEYDOWN): self.keys = pygame.key.get_pressed() def render(self): self.screen.fill(pygame.Color(BACKGROUND_COLOR)) for k in self.Knobs: k.draw(self.screen) for i in self.GBButtons: i.draw(self.screen) self.Officer.draw(self.screen) self.Theta.draw(self.screen) self.Phi.draw(self.screen) self.ConfirmButton.draw(self.screen) self.BackButton.draw(self.screen) self.CircuitButton.draw(self.screen) self.caption.draw(self.screen) pygame.display.update() def main_loop(self): while not (self.quit or self.next_stage or self.back or self.show_circuit): self.event_loop() for k in range(2): ## print(self.Knobs[k].angle) self.Knobs[k].update_drag() for i in range(2): self.GBButtons[i].update_ratio(self.Knobs[0].angle) self.phi = (self.Knobs[1].angle) * np.pi / 180 self.theta = (self.Knobs[0].angle) * np.pi / 180 self.data['UA'] = np.array([[ np.exp(self.phi * 1j) * np.cos(self.theta / 2), -(np.sin(self.theta / 2)) ], [ np.sin(self.theta / 2), np.exp(-self.phi * 1j) * np.cos(self.theta / 2) ]]) if self.ConfirmButton.click: self.ConfirmButton.update_click() self.next_stage = True elif self.BackButton.click: self.BackButton.update_click() self.back = True elif self.CircuitButton.click: self.CircuitButton.update_click() self.show_circuit = True self.render() self.clock.tick(self.fps)