We consider the task of approximating the ground state energy of two-local quantum Hamiltonians on bounded-degree graphs. Most existing algorithms optimize the energy over the set of product states. Here we describe a family of shallow quantum circuits that can be used to improve the approximation ratio achieved by a given product state. The algorithm takes as input an n-qubit product state |v⟩ with mean energy e0=⟨v|H|v⟩ and variance Var=⟨v|(H−e0)2|v⟩, and outputs a state with an energy that is lower than e0 by an amount proportional to Var^2/n. In a typical case, we have Var=Ω(n) and the energy improvement is proportional to the number of edges in the graph. When applied to an initial random product state, we recover and generalize the performance guarantees of known algorithms for bounded-occurrence classical constraint satisfaction problems. We extend our results to k-local Hamiltonians and entangled initial states.