Fix gradient error in trace_gen_quad_form.