The fact that $p = \large \frac{\partial L}{\partial \dot{q}} = 0$ introduces a problem in the equivalence between Lagrangian and Hamiltonian representations.

The idea is that the Hamiltonian representation **plus** the constraint $p = 0$ is equivalent to the Lagrangian representation

The Lagrangian $L$ is a function of $q$ and $\dot q$, that is $L(q, \dot q)$

If we work with the Lagrangian, we will apply the Euler-Lagrange equations which are :

$$\frac{\partial L}{\partial q} = \frac{d}{dt} \left(\frac{\partial L}{\partial \dot{q}}\right)$$

Because $\large \frac{\partial L}{\partial \dot{q}} = 0$, the equation is simply $\large \frac{\partial L}{\partial q} = 0$, that is $ \frac{1}{q} - 2\lambda = 0$, so $q = \frac{1}{2 \lambda}$

Now try to work with the Hamiltonian.

The Hamiltonian $H$ is a function of $q$ and $p$, that is $H(q, p)$

The link between the two is the Legendre transformation :

$$H=\dot{q}\frac{\partial L}{\partial \dot{q}}-L$$

Because your Lagrangian does not depends of $\dot q$, then $p = \frac{\partial L}{\partial \dot{q}} = 0$, and so :

$$H(q, p) = - L(q, \dot q) = - \ln(q) + (2q-10)\lambda$$

From this hamiltonian, you get the equations of movement :

$$\dot q = \frac{\partial H}{\partial p} ~,~\dot p = - \frac{\partial H}{\partial q}$$
So we have :

$$\dot q = 0~,~\dot p = \frac{1}{q} - 2\lambda \tag{1}$$

From this, we cannot recover the equation obtained from Euler-Lagrange equations, we have to add the constraint $p = 0$.

If $p = 0$, it means that $\dot p = 0$, and so :

$$q = \frac{1}{2 \lambda}\tag{2}$$

This is coherent with the fact that $\dot q = 0$

In general the Legendre transformation$^1$ from the Lagrangian to the Hamiltonian formulation may be singular, which leads to primary constraints. This is e.g. the case for gauge theories like Yang-Mills (YM) theory with or without matter, which OP mentions.

However, in case of a singular Legendre transformation, by performing a so-called Dirac-Bergmann analysis (which may lead to secondary constraints), it is still possible in principle to define a corresponding Hamiltonian formulation. Typically, the canonical Hamiltonian $H_0=p\dot{q}-L$ gets amended with terms of the form 'constraint times Lagrange multiplier'. For details, see e.g. Refs. 1 & 2.

References:

P.A.M. Dirac, *Lectures on QM,* 1964.

M. Henneaux & C. Teitelboim, *Quantization of Gauge Systems,* 1994.

--

$^1$ Concerning fermions, see e.g. this Phys.SE post.

## Best Answer

Adding to Lubos Motl's correct answer, it should be stressed that one may not always invert the relation $p_i=f_i(q,\dot{q},t)$ to isolate $\dot{q}^j$, not even in principle, because of constraints. Such cases are known as

singularLegendre transformations, and they are the starting point of the topic of constrained dynamics.Example.Consider e.g. the Lagrangian$$\tag{1} L~=~ y\dot{x} - \frac{y^2}{2m} $$

with two dynamical variables $x$ and $y$. (This Lagrangian (1) is in fact the so-called Hamiltonian Lagrangian for a non-relativistic 1D free particle if we identify the variable $y$ with the particle's momentum $p_x$, cf. the Faddeev-Jackiw method, but let's imagine we don't know that.)

Now let's go through the Legendre transformation via the Dirac-Bergmann method. The momenta are

$$\tag{2} p_x~:=~\frac{\partial L}{\partial \dot{x}}~=~y,$$

and

$$\tag{3} p_y~:=~\frac{\partial L}{\partial \dot{y}}~=~0.$$

Obviously, we cannot invert eqs. (2) and (3) to find $\dot{x}$ in terms of $x,y,p_x,p_y$ and $t$. Eqs. (2) and (3) are primary constraints,

$$\tag{4} p_x-y~\approx~ 0\quad\text{and} \quad p_y~\approx~ 0.$$

The Hamiltonian is

$$\tag{5} H~=~p_x \dot{x} +p_y \dot{y} - L ~\approx~ \frac{p^2_x}{2m}, $$

where the $\approx$ symbol means

equality modulo constraints (4).Indeed, it is just a non-relativistic 1D free particle, where we have used the constraints (4) to eliminate any physics in the $y$-direction. (It is easy to check that there are no secondary constraints if we use $\frac{p^2_x}{2m}$ as the Hamiltonian.)