Example #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
Example #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)]
Example #3
0
 def gate(self, q, index):
     Gates.X(q, index)