def _gen_gray_code(n: int) -> Iterator[tuple[int, int]]:
    """Generate the Gray Code from 0 to 2^n-1.

    Each iteration yields a two-tuple, `(gray_code, bit_flip)`. `gray_code` is the decimal
    representation of the gray code and `bit_flip` is the position of bits flipped for next
    gray code.
    """
    gray_code = 0
    for i in range(1, 2**n):
        next_gray = i ^ (i >> 1)
        bit_flip = int(np.log2(gray_code ^ next_gray))
        yield gray_code, bit_flip
        gray_code = next_gray
    yield gray_code, int(np.log2(gray_code))
Generate the Gray Code from 0 to 2^n-1.

    Each iteration yields a two-tuple, `(gray_code, bit_flip)`. `gray_code` is the decimal
    representation of the gray code and `bit_flip` is the position of bits flipped for next
    gray code.
    
cirq-core/cirq/ops/diagonal_gate.py