def serialize(serializer: str, num_gates: int, nesting_depth: int) -> int:
    """Runs a round-trip of the serializer."""
    circuit = cirq.Circuit()
    for _ in range(num_gates):
        which = np.random.choice(['expz', 'expw', 'exp11'])
        if which == 'expw':
            q1 = cirq.GridQubit(0, np.random.randint(NUM_QUBITS))
            circuit.append(
                cirq.PhasedXPowGate(
                    phase_exponent=np.random.random(), exponent=np.random.random()
                ).on(q1)
            )
        elif which == 'expz':
            q1 = cirq.GridQubit(0, np.random.randint(NUM_QUBITS))
            circuit.append(cirq.Z(q1) ** np.random.random())
        elif which == 'exp11':
            q1 = cirq.GridQubit(0, np.random.randint(NUM_QUBITS - 1))
            q2 = cirq.GridQubit(0, q1.col + 1)
            circuit.append(cirq.CZ(q1, q2) ** np.random.random())
    cs = [circuit]
    for _ in range(1, nesting_depth):
        fc = cs[-1].freeze()
        cs.append(cirq.Circuit(fc.to_op(), fc.to_op()))
    test_circuit = cs[-1]

    if serializer == _JSON:
        json_data = cirq.to_json(test_circuit)
        assert json_data is not None
        data_size = len(json_data)
        cirq.read_json(json_text=json_data)
    elif serializer == _JSON_GZIP:
        gzip_data = cirq.to_json_gzip(test_circuit)
        assert gzip_data is not None
        data_size = len(gzip_data)
        cirq.read_json_gzip(gzip_raw=gzip_data)
    return data_size
Runs a round-trip of the serializer.
dev_tools/profiling/benchmark_serializers.py