Esempio n. 1
0
def mcz_gray(c: Circuit, ctrl: Sequence[int], target: int) -> Circuit:
    """A macro of multi controlled Z gate."""
    n_ctrl = len(ctrl)
    if n_ctrl == 0:
        return c.z[target]
    angles = [math.pi / 2**(n_ctrl - 1), -math.pi / 2**(n_ctrl - 1)]
    for c0, c1, parity in gen_gray_controls(n_ctrl):
        if c0 >= 0:
            c.cx[ctrl[c0], ctrl[c1]]
        c.cr(angles[parity])[ctrl[c1], target]
    return c
Esempio n. 2
0
def mcr_gray(c: Circuit, theta: float, ctrl: Sequence[int],
             target: int) -> Circuit:
    """A macro of multi controlled R gate."""
    n_ctrl = len(ctrl)
    if n_ctrl == 0:
        return c.r(theta)[target]
    angles = [theta / 2**(n_ctrl - 1), -theta / 2**(n_ctrl - 1)]
    for c0, c1, parity in gen_gray_controls(n_ctrl):
        if c0 >= 0:
            c.cx[ctrl[c0], ctrl[c1]]
        c.cr(angles[parity])[ctrl[c1], target]
    return c
Esempio n. 3
0
def mcu_gray(c: Circuit, theta: float, phi: float, lam: float, gamma: float,
             ctrl: Sequence[int], target: int) -> Circuit:
    """A macro of multi controlled U gate."""
    n_ctrl = len(ctrl)
    if n_ctrl == 0:
        return c.u(theta, phi, lam, gamma)[target]
    if n_ctrl == 1:
        return c.cu(theta, phi, lam, gamma)[ctrl[0], target]
    mat = UGate.create(0, (theta, phi, lam, gamma)).matrix()
    for _ in range(n_ctrl - 1):
        mat = sqrt_2x2_matrix(mat)
    params = calc_u_params(mat)
    params_dagger = -params[0], -params[2], -params[1], -params[3]
    for c0, c1, parity in gen_gray_controls(n_ctrl):
        if c0 >= 0:
            c.cx[ctrl[c0], ctrl[c1]]
        if parity:
            c.cu(*params_dagger)[ctrl[c1], target]
        else:
            c.cu(*params)[ctrl[c1], target]
    return c