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
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