relationship between torque and potential energy for electromagnetism It is well known that the energy of a magnetic dipole in a magnetic field is taken as 
$$U = - \bf{m}.\bf{B}$$
 The dipole also experiences a torque 
$$\bf{\tau = m \times B}$$
In classical mechanics, the torque is given as 
$$\bf{\tau} = \bf{r \times F}$$ 
The force is derivable from a potential energy $V$, i.e. $\bf{F = -\nabla}$$V$; you can write the torque as $\bf{\tau = -r \times \nabla}$ $V$. 

The dynamics of an object carrying an electric current is governed by
  the Lorentz force, a velocity-dependent force that cannot be derived
  from a potential energy function
Source: Daniel R Stump, Am J Phys 66, No. 12 December 1998 pp 1042-1043

Keeping the above quote in mind, is it permissible to use this relationship in the electromagnetic case?
 A: A conservative force is one for which the curl is zero, i.e: force $F$ is conservative if and only if it satisfies the following condition,
$$\nabla \times F=0. $$
This is true for electrostatic forces. However, if at a point in space the magnetic field is changing with time, then the electric force will, in general, be non-conservative. The changing magnetic field induces a curl in the electric field.
The Lorentz force is given by,
$$\vec{F} = q(\vec{E} + \vec{v}×\vec{B})$$ 
$$\nabla \times F = q(\nabla \times E-B (\nabla. v) + (B.\nabla)v -(v.\nabla)B)$$
As you can see, in general, this will not be equal to zero. Therefore, we cannot write the Lorentz Force as the gradient of a scalar potential energy function, unless in the electrostatic case. But it can be derived from a velocity dependent potential function:
$$U= q\phi - q\vec{v}.\vec{A} $$  
where
$\phi$ a scalar function o position
$v$ is the velocity of charge on which the Lorentz force law is applied and
$\vec{A}$ is the vector potential
