Esempio n. 1
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. 2
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. 3
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. 4
0
import numpy as np
from gates import Gates
g = Gates()
print(g.RY(np.pi))