What is being done here is that the autograd engine expects the starting point for backward to be a scalar, and if it is not a scalar, the gradient parameter of backward is used to compute the gradient with respect to the inputs (in this case the inputs are x). When the elements of x are used in multiple parts of the graph, the total derivative is computed (the sum of the gradients for each of the uses). You can observe this as the magnitude of x.grad will increase the larger you make n.
If you expect these to be separate, you might want to try calling backward on individual elements of y and manually constructing your [n, p, q] gradient tensor (e.g., with torch.stack).