Example #1
0
def test_qasm1():
    c = Circuit()
    c.h[0, 1].cx[0, 1].rz(1.23)[2].x[2].y[2].cz[2, 1].z[1].ry(4.56)[0]
    c.u(1.0, 2.0, 3.0)[0]
    c.cu(2.0, 3.0, 1.0, 0.5)[2, 0]
    c.reset[1]
    qasm = c.m[:].to_qasm()
    assert QASM == qasm
Example #2
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