Esempio n. 1
0
def test_rot_gate_matrices():
    "Test rotation gates which are passed a parametere"
    g = Gates()

    rx = g.RX(np.pi / 4)
    assert np.isclose(
        rx, (np.cos(np.pi / 8) * np.eye(2)) -
        (1j * np.sin(np.pi / 8) * np.matrix([[0, 1], [1, 0]]))).all()

    ry = g.RY(np.pi / 4)
    assert np.isclose(
        ry, (np.cos(np.pi / 8) * np.eye(2)) -
        (1j * np.sin(np.pi / 8) * np.matrix([[0, -1j], [1j, 0]]))).all()

    rz = g.RZ(np.pi / 4)
    assert np.isclose(
        rz, (np.cos(np.pi / 8) * np.eye(2)) -
        (1j * np.sin(np.pi / 8) * np.matrix([[1, 0], [0, -1]]))).all()
Esempio n. 2
0
 def ZX(self, q, ix, iy):
     qm = q.clone()
     Gates.Z(qm, ix)
     Gates.X(qm, iy)
     lm = list(qm.v)
     ll = list(q.v)
     lz = map(lambda ix: (lm[ix] + ll[ix]) / 2, range(len(lm)))
     lo = map(lambda ix: (lm[ix] - ll[ix]) / 2, range(len(lm)))
     vz = Matrix(lz)
     vo = Matrix(lo)
     nz = my_norm(vz)**2
     no = my_norm(vo)**2
     qz = Qubits(q.length)
     qo = Qubits(q.length)
     if nz != 0:
         qz.v = vz / sqrt(nz)
     if no != 0:
         qo.v = vo / sqrt(no)
     return [(nz, qz), (no, qo)]
Esempio n. 3
0
def compile(program):
    p = Program()
    g = Gates()

    # Iterate over incoming gates
    for gate in program.instructions:
        gate_str = str(gate)
        g_info = gate_str.split(" ")

        # Parse angle
        if (g_info[0].find("(") > -1):
            g_name = g_info[0].split("(")[0]
            g_angle = g_info[0].split("(")[1].strip(")")
        else:
            g_name = g_info[0]
            g_angle = None

        # Parse qubits
        g_qubits = []
        for i in range(1, len(g_info)):
            g_qubits.append(int(g_info[i]))

        # Replace gate with respective decompositions
        if (g_name == "I"):
            g.I(p, g_qubits)
        elif (g_name == "H"):
            g.H(p, g_qubits)
        elif (g_name == "X"):
            g.X(p, g_qubits)
        elif (g_name == "Y"):
            g.Y(p, g_qubits)
        elif (g_name == "Z"):
            g.Z(p, g_qubits)
        elif (g_name == "RY"):
            # g_angle to be of the format = x*np.pi/y
            g.RY(p, g_qubits, g_angle)
        elif (g_name == "RX" or g_name == "RZ" or g_name == "CZ"):
            p += gate
        elif (g_name == "CNOT"):
            g.CNOT(p, g_qubits)
        else:
            raise Exception(
                "Gate not found in set: {I, H, X, Y, Z, RX, RY, RZ, CNOT, CZ}")

    return p
Esempio n. 4
0
def test_base_gate_matrices():
    "Test the gates which are hardcoded matrices"
    g = Gates()
    #I
    assert np.isclose(Gates.I, np.eye(2)).all()
    # X
    assert np.isclose(Gates.X, np.matrix([[0, 1], [1, 0]])).all()
    # Y
    assert np.isclose(Gates.Y, np.matrix([[0, -1j], [1j, 0]])).all()
    #Z
    assert np.isclose(Gates.Z, np.matrix([[1, 0], [0, -1]])).all()
    #H
    assert np.isclose(Gates.H,
                      1 / np.sqrt(2) * np.matrix([[1, 1], [1, -1]])).all()
    #T
    assert np.isclose(Gates.T, np.matrix([[1, 0],
                                          [0, np.exp(1j * np.pi / 4)]])).all()
    #S
    assert np.isclose(Gates.S, np.matrix([[1, 0], [0, 1j]])).all()
    #CNOT
    assert np.isclose(
        Gates.CNOT,
        np.matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1,
                                                              0]])).all()
    #CZ
    assert np.isclose(
        Gates.CZ,
        np.matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0,
                                                              -1]])).all()
    #SWAP
    assert np.isclose(
        Gates.SWAP,
        np.matrix([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0,
                                                              1]])).all()

    #X with RX
    assert np.isclose(Gates.X, g.RY(np.pi)).all()
Esempio n. 5
0
def test_movement(moving_right, moving_left, jumping, moved_right, moved_left,
                  jumped):
    # initialize everything
    controller = GeneralController()
    player_cords = (32, 336)
    player = HydroGirl(player_cords)

    gates = Gates((285, 128), [(190, 168), (390, 168)])
    board = Board('data/level1.txt')

    # inital locaton
    init_x = player.rect.x
    init_y = player.rect.y

    # set player movement
    player.moving_right = moving_right
    player.moving_left = moving_left
    player.jumping = jumping

    Game.move_player(Game(), board, [gates], [player])

    assert (player.rect.x > init_x) == moved_right
    assert (player.rect.x < init_x) == moved_left
    assert (player.rect.y < init_y) == jumped
Esempio n. 6
0
 def gate(self, q, index):
     Gates.Z(q, index)
Esempio n. 7
0
import numpy as np
from gates import Gates
g = Gates()
print(g.RY(np.pi))
Esempio n. 8
0
        # pnames ([Label]): list of pin names.
        pnames = drawing.pnames

        if args.output is not None:
            # We dump everything that any stage after this requires.
            with open(args.output[0], 'wt', encoding='utf-8') as f:
                json.dump({
                    "nets": [Net(netname, net) for netname, net in nets.items()],
                    "qs": qs,
                    "pnames": pnames,
                    "drawing_bounding_box": drawing_bounding_box,  # note: this tuple becomes a list.
                }, f, cls=PolychipJsonEncoder)

    if args.input is not None:
        with open(args.input[0], 'rt', encoding='utf-8') as f:
            d = json.load(f, object_hook=polychip_decode_json)
            nets = {netname: net for (netname, net) in d["nets"]}
            qs = d["qs"]
            pnames = d["pnames"]
            box = d["drawing_bounding_box"]
            drawing_bounding_box = (box[0], box[1], box[2], box[3])

    gates = Gates(nets, qs, pnames)

    print("{:d} total transistors".format(len(gates.qs)))

    gates.find_all_the_things()

    if args.sch:
        write_sch_file("polychip.sch", drawing_bounding_box, gates)
Esempio n. 9
0
def run_game(game, controller, level="level1"):
    # load level data
    if level == "level1":
        board = Board('data/level1.txt')
        gate_location = (285, 128)
        plate_locations = [(190, 168), (390, 168)]
        gate = Gates(gate_location, plate_locations)
        gates = [gate]

        fire_door_location = (64, 48)
        fire_door = FireDoor(fire_door_location)
        water_door_location = (128, 48)
        water_door = WaterDoor(water_door_location)
        doors = [fire_door, water_door]

        magma_boy_location = (16, 336)
        magma_boy = MagmaBoy(magma_boy_location)
        hydro_girl_location = (35, 336)
        hydro_girl = HydroGirl(hydro_girl_location)

    if level == "level2":
        board = Board('data/level2.txt')
        gates = []

        fire_door_location = (390, 48)
        fire_door = FireDoor(fire_door_location)
        water_door_location = (330, 48)
        water_door = WaterDoor(water_door_location)
        doors = [fire_door, water_door]

        magma_boy_location = (16, 336)
        magma_boy = MagmaBoy(magma_boy_location)
        hydro_girl_location = (35, 336)
        hydro_girl = HydroGirl(hydro_girl_location)

    if level == "level3":
        board = Board('data/level3.txt')
        gates = []

        fire_door_location = (5 * 16, 4 * 16)
        fire_door = FireDoor(fire_door_location)
        water_door_location = (28 * 16, 4 * 16)
        water_door = WaterDoor(water_door_location)
        doors = [fire_door, water_door]

        magma_boy_location = (28 * 16, 4 * 16)
        magma_boy = MagmaBoy(magma_boy_location)
        hydro_girl_location = (5 * 16, 4 * 16)
        hydro_girl = HydroGirl(hydro_girl_location)

    # initialize needed classes

    arrows_controller = ArrowsController()
    wasd_controller = WASDController()

    clock = pygame.time.Clock()

    # main game loop
    while True:
        # pygame management
        clock.tick(60)
        events = pygame.event.get()

        # draw features of level
        game.draw_level_background(board)
        game.draw_board(board)
        if gates:
            game.draw_gates(gates)
        game.draw_doors(doors)

        # draw player
        game.draw_player([magma_boy, hydro_girl])

        # move player
        arrows_controller.control_player(events, magma_boy)
        wasd_controller.control_player(events, hydro_girl)

        game.move_player(board, gates, [magma_boy, hydro_girl])

        # check for player at special location
        game.check_for_death(board, [magma_boy, hydro_girl])

        game.check_for_gate_press(gates, [magma_boy, hydro_girl])

        game.check_for_door_open(fire_door, magma_boy)
        game.check_for_door_open(water_door, hydro_girl)

        # refresh window
        game.refresh_window()

        # special events
        if hydro_girl.is_dead() or magma_boy.is_dead():
            show_death_screen(game, controller, level)

        if game.level_is_done(doors):
            show_win_screen(game, controller)

        if controller.press_key(events, K_ESCAPE):
            show_level_screen(game, controller)

        # close window is player clicks on [x]
        for event in events:
            if event.type == QUIT:
                pygame.quit()
                sys.exit()