I have just solved that, it is the problem about the retain_graph. I am working on the testing.
It turns out that before each step, I have to refresh the matrix.
        A0_B = torch.FloatTensor([[s_ for _ in range(8)] for _ in range(8)])
        qstep = torch.zeros(8, dtype=torch.float32)
        # One-hot encoding locations