def try_merge_cz(cz_op: ops.GateOperation, start_i: int) -> int:
        """Returns the number of operations removed at or before start_i."""
        for i in reversed(range(start_i)):
            op = all_ops[i]
            if not set(cz_op.qubits) & set(op.qubits):
                # Don't share qubits
                # Keep looking
                continue
            elif not (
                isinstance(op, ops.GateOperation)
                and isinstance(op.gate, ops.CZPowGate)
                and op.gate.exponent == 1
            ):
                # Not a CZ gate
                return 0
            elif cz_op == op:
                # Cancel two CZ gates
                all_ops.pop(start_i)
                all_ops.pop(i)
                return 2
            else:
                # Two CZ gates that share one qubit
                # Pass through and keep looking
                continue  # pragma: no cover
                # The above line is covered by test_remove_staggered_czs but the
                # coverage checker disagrees.
        return 0
Returns the number of operations removed at or before start_i.
cirq-core/cirq/contrib/paulistring/clifford_optimize.py