Tried it with
a = torch.zeros(2, 2)
a[0,0] = param
a[1,1] = 2 

then I get the following error
 RuntimeError: Trying to backward through the graph a second time (or directly access saved variables after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved variables after calling backward.
I tried with retain_graph = True, but then the solution doesnt converge to the expected value of 10.