What canonical momenta are the "right" ones? I'm doing some classical field theory exercises with the Lagrangian
$$\mathscr{L} = -\frac{1}{4}F_{\mu \nu}F^{\mu \nu}$$
where $F_{\mu \nu} = \partial_\mu A_\nu - \partial_\nu A_\mu$. To find the conjugate momenta $\pi^\mu_{\ \ \ \nu} = \partial \mathscr{L} / \partial(\partial_\mu A^\nu)$, I can use two methods.
First method: directly apply this to $\mathscr{L}$. We get a a factor of $2$ since there are two $F$'s, and another factor of $2$ since each $F$ contains two $\partial_\mu A_\nu$ terms, giving
$$\pi^\mu_{\ \ \ \nu} = -F^\mu_{\ \ \ \nu}.$$
Second method: get $\mathscr{L}$ in terms of $A$ by expanding and integrating by parts, yielding
$$\mathscr{L} = \frac{1}{2}(\partial_\mu A^\mu)^2 - \frac{1}{2}(\partial_\mu A^\nu)^2.$$
Differentiating this gets factors of $2$ and gives
$$\pi^\mu_{\ \ \ \nu} = \partial_\rho A^\rho \delta^\mu_\nu - \partial^\mu A_\nu.$$
These two answers are different! (They do give the same equations of motion, at least.) I guess that means doing the integration by parts changed the canonical momenta.
Is this something I should be worried about? In particular, I have another exercise that wants me to show that one of the canonical momenta vanishes -- this isn't true for the ones I get from the second method! Plus, my stress-energy tensor is changed too. When a problem asks for "the" canonical momenta, am I forbidden from integrating by parts?
 A: Two different Lagrangians give different canonical momentum.
If two different Lagrangians differ by a surface term then they differ by a total divergence. And they thus yield the same actions hence have the same equations of motion.
When you do integration by parts you produce a surface term (the difference between the two). Imagine subtraction the two functions (Lagrangian's are functions of there independent variables not fields with fixed values) that you go from integration by parts. They differ by a surface term. Now image those as a voltage and then find the electric field that has no charge inside. That is a divergence free vector field whose flux on the surface is what you wanted.
Or just take the two Lagrangians and subtract them. In general you get something that is just a surface term so is the divergence of something.
As for which is right, canonical momentum is just canonical. It isn't for instance a source in a physical field equation, it just is what it is, which is less than maybe people want to say it is. You can't measure canonical momentum.
It's just the canonical momentum associated with a particular Lagrangian. And Lagrangians that differ by a total divergence give the same equations of motion but different canonical momentum.
A: *

*OP is pondering if the corresponding Hamiltonian formulation is affected if the Lagrangian density $$\tag{1} {\cal L}~\longrightarrow~\tilde{\cal L}~:=~ {\cal L}+\sum_{\mu=0}^3d_{\mu}F^{\mu}$$ is modified with a total divergence$^1$ term $d_{\mu}F^{\mu}$, so that the definition of canonical momentum
$$\tag{2} p_i~:=~ \frac{\delta L}{\delta v^i}-\frac{d}{dt}\frac{\delta L}{\delta \dot{v}^i}+\ldots, \qquad L~:=~\int\! d^3x~{\cal L}, $$
is modified as well? That's a good question.

*Some technical notes: 


*

*(i) The reason for the functional (rather than partial) derivatives in eq. (2) is because of the presence of spatial directions in field theory (as opposed to point mechanics), cf. e.g. this Phys.SE post. 

*(ii) The ellipsis $\ldots$ in eq. (2) denotes possible dependence of higher time derivatives in the Lagrangian $L[q,v,\dot{v},\ddot{v},\dddot{v},\ldots;t]$. (We assume implicitly that all the dependence $\dot{q},\ddot{q},\dddot{q},\ldots,$ has been replaced with $v,\dot{v},\ddot{v},\ldots,$ in the Lagrangian, respectively.) Although we are only here interested in the normal physical case where the Euler-Lagrange equations contain at most two time derivatives, there could still be higher time derivatives inside a total divergence term in the action. Higher time derivatives are not just a purely academic exercise. E.g. the Einstein-Hilbert (EH) action contains higher time derivatives, cf. e.g. this Phys.SE post. We briefly return to higher time derivatives in Section 7. 

*(iii) Changing the action with a total divergence term may affect the choice of consistent boundary conditions. E.g. the EH action is amended with a Gibbons–Hawking–York (GHY) boundary term for consistency reasons.


*OP is not asking about the Lagrangian formulation, and already knows that the Euler-Lagrange equations are not changed, cf. e.g this Phys.SE post. Let us from now on focus on the Legendre transformation and the Hamiltonian formulation.

*The transformation (1) consists of two types of transformations: 


*

*(i) a change by a total spatial derivative 
$$\tag{3} {\cal L}~\longrightarrow~\tilde{\cal L}~:=~ {\cal L} +\sum_{k=1}^3d_kF^k, $$ 
which does not change the momentum definition (2); and 

*(ii) a change by a total time derivative$^1$ 
$$ \tag{4}L~\longrightarrow~\tilde{L}~:=~L+\frac{\partial G}{\partial t}+ \int\! d^3x\left[\frac{\delta G}{\delta q^i} v^i 
+ \frac{\delta G}{\delta v^i} \dot{v}^i+\ldots \right]
~\approx~L+\frac{dG}{dt}. $$
We will for simplicity only consider the latter transformation (4) from now on.


*Let us for simplicity consider point mechanics. (The field theoretic generalization is straightforward.) Eq. (2) and (4) then become
$$\tag{5} p_i~:=~ \frac{\partial L}{\partial v^i}-\frac{d}{dt}\frac{\partial L}{\partial \dot{v}^i}+\ldots,  $$
$$ \tag{6}L~\longrightarrow~\tilde{L}~:=~L+\frac{\partial G}{\partial t}+ \frac{\partial G}{\partial q^i} v^i 
+ \frac{\partial G}{\partial v^i} \dot{v}^i+\ldots 
~\approx~L+\frac{dG}{dt}, $$
respectively. The canonical momentum (5) changes as
$$  \tag{7} P_i~=~p_i+\frac{\partial G}{\partial q^i}
+\frac{\partial^2 G}{\partial v^i\partial q^j} (v^j-\dot{q}^j) 
~\approx~p_i+\frac{\partial G}{\partial q^i} . $$
[The $\approx$ symbol means equality modulo equations of motion or $v^i\approx\dot{q}^i$.]

*First let us assume that the Legendre transformation $v\leftrightarrow p$ is regular. If $G$ does not depend on the velocity fields $v^i$ and higher time-derivatives in the transformation (6), this is Exercise 8.2 (Exercise 8.19) in Goldstein, Classical Mechanics, 3rd edition (2nd edition), respectively. One can use a type 2 canonical transformation 
$$ \tag{8}p_i\dot{q}^i-H
~=~ -\dot{P}_iQ^i-K + \frac{dF_2}{dt},$$
$$ \tag{9}  \qquad F_2~:=~P_i q^i-G, $$
where
$$\tag{10} Q^i~:=~q^i, \qquad P_i~:=~p_i+\frac{\partial G}{\partial q^i},   \qquad   K~:=~H-\frac{\partial G}{\partial t}.$$
An Hamiltonian action principle based on either the lhs. or rhs. of eq. (8) has 
the Hamilton's equations 
$$ \tag{11} \dot{q}^i~\approx~ \frac{\partial H}{\partial p_i}, \qquad -\dot{p}_i~\approx~ \frac{\partial H}{\partial q^i}, $$
and the Kamilton's equations 
$$ \tag{12} \dot{Q}^i~\approx~ \frac{\partial K}{\partial P_i}, \qquad -\dot{P}_i~\approx~ \frac{\partial K}{\partial Q^i}, $$
as stationary point, respectively. Hence eqs. (11) and (12)
are equivalent under the transformation (6).

*If $G$ depends on the velocity fields $v^i$, there appear higher time derivatives inside the total time-derivative term $\frac{dG}{dt}$, cf. eq. (6). Then additional complications arise (in writing down an equivalence proof). E.g. the relation for the next Ostrogradsky momentum
$$ \tag{13} P^{(2)}_i~:=~ \frac{\partial \tilde{L}}{\partial \dot{v}^i}+\ldots~=~\frac{\partial G}{\partial v^i}+\ldots, $$
can typically not be inverted to eliminate the acceleration $\dot{v}^j$. In other words, the Legendre transformation is singular. 

*In case of singular Legendre transformations, it is less clear, but widely believed, that the modified Hamiltonian formulation (resulting from the Dirac-Bergmann constrained analysis) is still equivalent.

*OP's case (E&M) has constraints (Gauss's law), but in that case, it is easy to check explicitly the equivalence.
--
$^1$ Note this subtlety.
