Skip to content

Variational Optimization Notebook

Nonlinear parameters can make a small variational basis behave like a much larger poorly scaled one. They can also create shallow, multi-basin energy landscapes in which a plausible local optimizer returns the wrong stationary point. This notebook studies both effects in a controlled model.

The physical system is the stable quartic oscillator. The numerical experiment uses a frequency-scaled even-parity oscillator basis, diagonalizes the Hamiltonian at each scale, and then minimizes the lowest Ritz value over that scale. The calculation tests four claims:

  • every finite-basis result remains a variational upper bound;
  • global frequency optimization can accelerate basis convergence by many orders of magnitude;
  • the optimized frequency is a numerical coordinate, not a physical observable;
  • a local one-dimensional optimizer is insufficient once the finite-basis landscape develops several minima.

The complete one-Gaussian derivation belongs to Anharmonic Oscillator by Variational Methods. General parameter geometry belongs to Variational Parameters, and the upper-bound theorem belongs to the Variational Principle. This page owns the numerical optimization protocol, convergence audit, and reproducibility record.

Run the investigation. The program and retained results below support the stated experiment. Follow Running an Experiment for environment and output-directory guidance. The recorded evidence applies to its stated parameters and environment.

Use dimensionless oscillator units and consider

h(g)=12p2+12q2+gq4,[q,p]=i,g≥0.\begin{gathered} h(g) = \frac12p^2 + \frac12q^2 + gq^4, \\ [q,p]=i, \qquad g\geq0. \end{gathered}

The ground state is even. For g>0g>0, the quartic term narrows the central part of the wavefunction relative to the g=0g=0 oscillator ground state, while its tails are not exactly Gaussian. A useful approximation therefore needs both:

  • a scale parameter that can narrow the basis;
  • additional even oscillator states that can change the shape.

The target quantity is the ground-state energy E0(g)E_0(g). The notebook does not use perturbation theory to define the numerical reference, so it remains useful at moderate and strong coupling.

Let y>0y>0 be the ratio between the basis frequency and the frequency used to define the dimensionless Hamiltonian. For a single frequency-yy oscillator ground state, the Rayleigh quotient is

εmathrmG(y;g)=y4+14y+3g4y2.\varepsilon_{mathrm G}(y;g) = \frac{y}{4} + \frac{1}{4y} + \frac{3g}{4y^2}.

Its stationary point satisfies

ymathrmG3−ymathrmG−6g=0.y_{mathrm G}^3-y_{mathrm G}-6g=0.

This formula is an analytic validation target for the K=1K=1 numerical calculation. It also supplies a sensible frequency for the large-basis reference. The derivation, weak-coupling expansion, strong-coupling limit, and comparison with ordinary perturbation theory are kept at the canonical worked problem, rather than repeated here.

For each yy, define the KK-dimensional even subspace

VK(y)=span⁡{∣2j;y⟩:j=0,1,…,K−1}.\begin{aligned} \mathcal V_K(y) &= \operatorname{span} \left\{ |2j;y\rangle: \right. \\ &\hspace{3.8em} \left. j=0,1,\ldots,K-1 \right\}. \end{aligned}

The basis ladder operators obey

q=ay+ay†2y,q = \frac{a_y+a_y^\dagger}{\sqrt{2y}},

and the frequency-yy oscillator Hamiltonian is

12p2+12y2q2=y(ay†ay+12).\frac12p^2 + \frac12y^2q^2 = y \left( a_y^\dagger a_y+\frac12 \right).

It is numerically convenient to rewrite the target Hamiltonian as

h(g)=y(ay†ay+12)+1−y22q2+gq4.\begin{aligned} h(g) ={}& y \left( a_y^\dagger a_y+\frac12 \right) \\ &+ \frac{1-y^2}{2}q^2 + gq^4. \end{aligned}

Projecting onto VK(y)\mathcal V_K(y) gives a real symmetric matrix HK(y)H_K(y). Its lowest eigenpair solves

HK(y)cK(y)=EK(y)cK(y).H_K(y)c_K(y) = E_K(y)c_K(y).

The inner problem is a linear Rayleigh-Ritz diagonalization. The outer problem is the nonlinear optimization

EKopt=min⁡y>0EK(y).E_K^{\mathrm{opt}} = \min_{y>0}E_K(y).

This separation is important. At fixed yy, coefficients are optimized exactly within the represented subspace by diagonalization. The outer search changes the subspace itself.

For every admissible yy and finite KK, the Ritz value satisfies

E0(g)≤EK(y).E_0(g) \leq E_K(y).

Taking the minimum over yy cannot violate that inequality:

E0(g)≤EKopt.E_0(g) \leq E_K^{\mathrm{opt}}.

At fixed yy, the spaces are nested,

VK(y)⊂VK+1(y),\mathcal V_K(y) \subset \mathcal V_{K+1}(y),

so

EK+1(y)≤EK(y).E_{K+1}(y) \leq E_K(y).

The same monotonicity survives independent optimization at each dimension:

EK+1opt=min⁡yEK+1(y)≤EK+1(yK⋆)≤EK(yK⋆)=EKopt.\begin{aligned} E_{K+1}^{\mathrm{opt}} &= \min_y E_{K+1}(y) \\ &\leq E_{K+1}(y_K^\star) \\ &\leq E_K(y_K^\star) = E_K^{\mathrm{opt}}. \end{aligned}

Thus the optimized sequence is a decreasing sequence of upper bounds in exact arithmetic. A violation larger than roundoff indicates a missed lower basin, an incorrectly assembled matrix, or an unconverged eigensolver.

Matrix Construction Without Boundary Contamination

Section titled “Matrix Construction Without Boundary Contamination”

The quartic operator connects oscillator states with

Δn∈{0,±2,±4}.\Delta n \in \{0,\pm2,\pm4\}.

A common implementation mistake is to build a KK-state ladder matrix, form q4q^4 inside that truncated matrix, and then diagonalize. Multiplying after truncation removes intermediate paths that leave the retained space and return. The highest rows of q4q^4 are then wrong.

The calculation instead follows this order:

  1. Build the ladder operators in a full basis extending at least four oscillator levels above the largest retained nn.
  2. Form q2q^2 and q4q^4 in that padded basis.
  3. Project the completed operators onto n=0,2,…,2K−2n=0,2,\ldots,2K-2.
  4. Assemble HK(y)H_K(y) only after projection.

The core implementation is:

n_max = 2 * (K - 1)
M = n_max + padding + 1
a = np.zeros((M, M))
n = np.arange(1, M)
a[n - 1, n] = np.sqrt(n)
q = (a + a.T) / np.sqrt(2.0 * y)
q2_full = q @ q
q4_full = q2_full @ q2_full
even = np.arange(0, 2 * K, 2)
q2 = q2_full[np.ix_(even, even)]
q4 = q4_full[np.ix_(even, even)]

Padding by six levels is used in the published run. Repeating the calculation with ten levels gives the same displayed energies. Closed-form matrix elements provide an equally valid independent implementation.

The positivity constraint on yy is removed by setting

u=ln⁡y,−∞<u<∞.u = \ln y, \qquad -\infty<u<\infty.

This coordinate has three advantages:

  • every trial basis has y=eu>0y=e^u>0;
  • multiplicative changes in frequency become additive steps;
  • comparable relative changes at small and large yy receive comparable resolution.

For K=1K=1, the landscape is unimodal. For K>1K>1, this should not be assumed. The lowest eigenvalue of the finite projected matrix develops several local minima as yy varies. A scalar search over one broad bracket can settle in whichever basin happens to satisfy its local assumptions.

The reproducible search therefore uses:

  1. a uniform global scan in uu;
  2. detection of every discrete local minimum;
  3. local golden-section refinement inside each detected basin;
  4. comparison of all refined energies;
  5. a boundary check to ensure the scan brackets the global minimum.

For 0.1≤g≤100.1\leq g\leq10, the published scan uses

−2≤u≤4-2\leq u\leq4

with 24012401 points, followed by basin refinement to an interval width below 2×10−122\times10^{-12} in uu. Doubling the grid density does not change the reported global basin or displayed energy.

At g=1g=1, the large-basis reference is

Eref=0.803770651234274.E_{\mathrm{ref}} = 0.803770651234274.

The first panel below plots the excess EK(u)−ErefE_K(u)-E_{\mathrm{ref}} on a logarithmic scale. More basis states lower the entire envelope, but they also create additional shallow basins. The second panel compares a fixed frequency with a globally optimized frequency.

Two logarithmic plots showing a multibasin variational frequency landscape and faster Ritz convergence after global frequency optimization.

Frequency-scaled Rayleigh-Ritz results for h=p2/2+q2/2+q4h=p^2/2+q^2/2+q^4. Panel (a) shows the finite-basis energy landscape against u=ln⁡yu=\ln y for K=1,2,4,8K=1,2,4,8 even basis states. Panel (b) shows the energy excess above a K=64K=64 reference. Global scale optimization accelerates finite-KK convergence, while the increasingly shallow multi-basin landscape makes the numerical value of the optimal scale less stable.

For g=1g=1, the global minima found by the scan are:

KKDetected BasinsyK⋆y_K^\starEKopt−ErefE_K^{\mathrm{opt}}-E_{\mathrm{ref}}
11112.0000002.0000008.72935×10−38.72935\times10^{-3}
22222.9654392.9654394.04166×10−44.04166\times10^{-4}
44443.0513303.0513302.50828×10−62.50828\times10^{-6}
66664.0480194.0480198.78926×10−98.78926\times10^{-9}
88884.0487014.0487014.37155×10−114.37155\times10^{-11}

The observed basin count in this finite sweep is a diagnostic, not a general theorem. Its practical message is simpler: a one-shot local search is not a reproducible global optimizer.

The same protocol was run at weak, intermediate, and strong coupling. The reference uses K=64K=64 at the analytic one-Gaussian frequency yGy_{\mathrm G}. Increasing the reference dimension from K=32K=32 through 4848, 6464, and 8080 leaves all displayed reference digits unchanged.

ggErefE_{\mathrm{ref}}Optimized K=1K=1 ExcessOptimized K=4K=4 ExcessOptimized K=8K=8 ExcessFixed-y=1y=1, K=8K=8 Excess
0.10.10.5591463271840.5591463271841.16104×10−31.16104\times10^{-3}1.09425×10−71.09425\times10^{-7}7.45×10−137.45\times10^{-13}5.25918×10−95.25918\times10^{-9}
110.8037706512340.8037706512348.72935×10−38.72935\times10^{-3}2.50828×10−62.50828\times10^{-6}4.37155×10−114.37155\times10^{-11}6.69763×10−56.69763\times10^{-5}
10101.5049724077791.5049724077792.62776×10−22.62776\times10^{-2}9.78734×10−69.78734\times10^{-6}2.12461×10−102.12461\times10^{-10}5.99230×10−35.99230\times10^{-3}

Several conclusions are visible.

Optimization matters most before convergence

Section titled “Optimization matters most before convergence”

At g=10g=10, the unscaled K=8K=8 basis is still high by about 6×10−36\times10^{-3}, while scale optimization reduces the excess below 3×10−103\times10^{-10}. The fixed basis is not incorrect; it is poorly matched to a much narrower wavefunction.

Shape flexibility matters beyond one Gaussian

Section titled “Shape flexibility matters beyond one Gaussian”

The K=1K=1 result is already a useful upper bound and has the correct strong-coupling power law. Its remaining error reflects non-Gaussian shape information. Adding even excited basis functions supplies that information while retaining parity exactly.

Tiny energy error does not determine a unique scale

Section titled “Tiny energy error does not determine a unique scale”

At larger KK, a broad range of frequencies spans nearly the same well-converged state. The optimized yK⋆y_K^\star can move between nearby basins while the energy changes below the target precision. Reporting many digits of yK⋆y_K^\star would confuse optimizer conditioning with physics.

Changing uu dilates every basis function. For a fully optimized eigenvector inside VK(eu)\mathcal V_K(e^u), the envelope theorem gives

dEKdu=⟨T⟩−12⟨q2⟩−2g⟨q4⟩.\frac{dE_K}{du} = \langle T\rangle - \frac12\langle q^2\rangle - 2g\langle q^4\rangle.

Define the virial residual

νK=2⟨T⟩−⟨q2⟩−4g⟨q4⟩.\nu_K = 2\langle T\rangle - \langle q^2\rangle - 4g\langle q^4\rangle.

Then

dEKdu=12νK.\frac{dE_K}{du} = \frac12\nu_K.

Frequency stationarity therefore enforces the quartic-oscillator virial relation within the optimized finite trial family. This is a stronger check than merely observing that the optimizer stopped: it compares the claimed stationary point with independently assembled expectation values.

For the reported minima, ∣νK∣|\nu_K| is below 7×10−87\times10^{-8} and is usually much smaller. The tolerance is set by the flatness of the landscape and double-precision cancellation, not by the eigensolver residual.

In the one-dimensional outer problem, the local Hessian is

κu=d2EKdu2∣u=uK⋆.\kappa_u = \left. \frac{d^2E_K}{du^2} \right|_{u=u_K^\star}.

At g=1g=1, a centered finite difference gives:

KKκu\kappa_u
111.375031.37503
223.80834×10−13.80834\times10^{-1}
444.39610×10−34.39610\times10^{-3}
665.43191×10−55.43191\times10^{-5}
883.37371×10−73.37371\times10^{-7}

Near a minimum,

EK(uK⋆+δu)−EK(uK⋆)≈12κu(δu)2.E_K(u_K^\star+\delta u) - E_K(u_K^\star) \approx \frac12\kappa_u(\delta u)^2.

The falling curvature explains why yK⋆y_K^\star becomes difficult to locate even as the energy becomes highly accurate. A small gradient is not enough to certify a well-determined parameter when the curvature is also tiny. The broader interpretation of stiff and soft variational directions is developed in Variational Parameters and Conditioning and Stability.

The program stops with an error if any required check fails.

At g=0g=0, K=1K=1, and y=1y=1,

E1=12E_1 = \frac12

to machine precision.

For every tested gg, the K=1K=1 optimizer reproduces the positive root of

y3−y−6g=0y^3-y-6g=0

and the analytic Gaussian energy.

The assembled matrix obeys

∥HK−HKT∥<10−13.\|H_K-H_K^{\mathsf T}\| < 10^{-13}.

An independently assembled full-basis matrix has a vanishing even-odd block to the same tolerance.

For the normalized lowest eigenvector,

ρK=∥HKcK−EKcK∥\rho_K = \|H_Kc_K-E_Kc_K\|

is below 2×10−152\times10^{-15} in the published sweep. This verifies the represented matrix eigenproblem, but not basis convergence by itself.

The optimized values decrease with KK and remain above the converged reference within a 5×10−125\times10^{-12} roundoff allowance. Once the excess reaches roughly 10−1310^{-13}, only fewer digits are reported because floating-point noise can obscure the sign of a still smaller difference.

Increasing the operator-construction padding from six to ten oscillator levels changes the test energy by less than 2×10−142\times10^{-14}.

The scan must bracket an interior minimum. The calculation records every detected basin and compares their refined energies, rather than trusting the first stationary point found.

Run the benchmark with:

python variational-optimization.py --output-dir variational-output

NumPy is the only required dependency for the energies, CSV files, and validation checks. If Matplotlib is installed, the program also writes a diagnostic PNG; otherwise it reports that plotting was skipped. The published vector figure is generated independently from the retained numerical data.

The source file carries the SPDX identifier MIT; the program and generated tabular data are released under the MIT License.

ItemPublished Run
Operating systemWindows 11, x86-64
Python3.12.13
NumPy2.3.5
BLAS/LAPACKOpenBLAS 0.3.30, 64-bit integer interface
Scalar typeIEEE 754 binary64
Couplingsg=0.1,1,10g=0.1,1,10
Optimized dimensionsK=1,…,10K=1,\ldots,10 even states
Reference dimensionKref=64K_{\mathrm{ref}}=64 even states
Scan coordinateu=ln⁡yu=\ln y
Scan interval[−2,4][-2,4] for the reported couplings
Scan points24012401
Basin tolerance2×10−122\times10^{-12} in uu
Operator paddingsix levels above the largest retained nn
Random seednone; the calculation is deterministic
LicenseMIT

The script prints the Python and NumPy versions. A rerun intended for archival comparison should also retain np.show_config() output because different linear-algebra backends can change the final few floating-point digits.

A golden-section or Brent-style search is reliable only when its bracket satisfies the method’s local shape assumptions. At K>1K>1, different brackets can converge to different minima. Use a global scan or a genuinely global optimizer, then refine each candidate.

Treating the basis frequency as an observable

Section titled “Treating the basis frequency as an observable”

The finite-basis scale is a representation choice. Its optimum depends on KK, the basis family, the objective, and numerical tolerance. Only predictions stable under enlargement of the trial space should receive physical interpretation.

When EK(u)E_K(u) changes by less than roundoff across a broad interval, additional optimizer iterations do not add information. Curvature estimates may change sign from cancellation. Stop reporting parameter digits before this regime.

Computing qK4q_K^4 from an already truncated qKq_K corrupts boundary matrix elements. Pad first or use analytic matrix elements.

An apparent violation of the upper bound can mean that the alleged reference is still above the optimized trial result. Converge the reference in dimension and scale before using its difference as an error estimate.

Confusing residual error with truncation error

Section titled “Confusing residual error with truncation error”

A tiny eigenpair residual says that HKc=EcH_Kc=Ec was solved accurately. It says nothing about the distance between EKE_K and the infinite-dimensional energy.

Including both parities is mathematically valid, but an indexing error can mix them and hide structural mistakes. Explicit parity blocks reduce cost and provide a sharp diagnostic.

  • Optimizing yy directly with unconstrained steps that enter y≤0y\leq0.
  • Reporting only the optimizer’s success flag, without a landscape scan or virial residual.
  • Comparing energies at different KK without checking that each outer minimum is global.
  • Keeping many digits of yK⋆y_K^\star after the energy landscape has become flat.
  • Claiming that scale optimization removes basis error; it only reduces it within the chosen family.
  • Using a fixed reference frequency at strong coupling without testing basis convergence.
  • Interpreting a below-reference value at the 10−1510^{-15} level as a failure of the variational theorem.
  1. Show that the K=1K=1 projected matrix reproduces the analytic Gaussian Rayleigh quotient.
Solution

For the frequency-yy ground state,

⟨q2⟩=12y,⟨q4⟩=34y2.\langle q^2\rangle = \frac{1}{2y}, \qquad \langle q^4\rangle = \frac{3}{4y^2}.

The frequency-yy oscillator contribution is y/2y/2, and the correction to the quadratic potential is

1−y22⟨q2⟩=1−y24y.\frac{1-y^2}{2} \langle q^2\rangle = \frac{1-y^2}{4y}.

Therefore

E1(y)=y2+1−y24y+3g4y2=y4+14y+3g4y2.\begin{aligned} E_1(y) &= \frac{y}{2} + \frac{1-y^2}{4y} + \frac{3g}{4y^2} \\ &= \frac{y}{4} + \frac{1}{4y} + \frac{3g}{4y^2}. \end{aligned}
  1. Prove that independently optimizing yy at each KK preserves monotone variational convergence.
Solution

Let yK⋆y_K^\star minimize EK(y)E_K(y). Since VK(y)⊂VK+1(y)\mathcal V_K(y)\subset\mathcal V_{K+1}(y) at every fixed yy,

EK+1(yK⋆)≤EK(yK⋆).E_{K+1}(y_K^\star) \leq E_K(y_K^\star).

The optimized (K+1)(K+1)-dimensional energy cannot exceed its value at yK⋆y_K^\star, so

EK+1opt≤EK+1(yK⋆)≤EKopt.E_{K+1}^{\mathrm{opt}} \leq E_{K+1}(y_K^\star) \leq E_K^{\mathrm{opt}}.

Each term is also an upper bound on E0E_0 by the variational principle.

  1. Derive the relation between the scale derivative and the virial residual.
Solution

Under the dilation u↦u+δuu\mapsto u+\delta u, kinetic expectation values scale as eδue^{\delta u}, quadratic moments as e−δue^{-\delta u}, and quartic moments as e−2δue^{-2\delta u}. Differentiating at fixed optimized coefficient vector gives

dEKdu=⟨T⟩−12⟨q2⟩−2g⟨q4⟩.\frac{dE_K}{du} = \langle T\rangle - \frac12\langle q^2\rangle - 2g\langle q^4\rangle.

Twice this expression is

2⟨T⟩−⟨q2⟩−4g⟨q4⟩,2\langle T\rangle - \langle q^2\rangle - 4g\langle q^4\rangle,

which is the virial residual νK\nu_K. Thus dEK/du=νK/2dE_K/du=\nu_K/2.

  1. Why can forming q4q^4 after truncating qq give the wrong projected operator even when only low states are retained?
Solution

A matrix element of q4q^4 contains sums over intermediate oscillator states. Starting from a retained state near the upper boundary, an intermediate application of qq can leave the truncated space and a later application can return. Truncating qq first deletes that path. Building q4q^4 in a padded space and projecting afterward retains every intermediate state needed for the desired matrix elements.

  1. At g=1g=1, the curvature falls from about 1.381.38 at K=1K=1 to 3.4×10−73.4\times10^{-7} at K=8K=8. If the local frequency coordinate has an uncertainty ∣δu∣=10−3|\delta u|=10^{-3}, estimate the associated energy uncertainty at each dimension.
Solution

Use

ΔE≈12κu(δu)2.\Delta E \approx \frac12\kappa_u(\delta u)^2.

At K=1K=1,

ΔE≈12(1.38)10−6≈6.9×10−7.\Delta E \approx \frac12(1.38)10^{-6} \approx 6.9\times10^{-7}.

At K=8K=8,

ΔE≈12(3.4×10−7)10−6≈1.7×10−13.\begin{aligned} \Delta E &\approx \frac12 \left(3.4\times10^{-7}\right) 10^{-6} \\ &\approx 1.7\times10^{-13}. \end{aligned}

The same uncertainty in the optimizer coordinate matters far less once the basis is converged and the landscape is flat.

  1. Design an independent check that does not use oscillator-basis diagonalization.
Solution

One option is a coordinate-space finite-difference or spectral calculation on a symmetric interval. Increase the box size and grid resolution independently, impose even parity at the origin, and converge the lowest eigenvalue below the variational excess being tested. Agreement with the Ritz sequence then compares different representations and different truncation mechanisms. A shooting method with logarithmic-derivative matching provides another independent route.

  • C. M. Bender and T. T. Wu, “Anharmonic oscillator,” Physical Review 184, 1231–1260, 1969.
  • T. Kato, Perturbation Theory for Linear Operators, 2nd ed., Springer, 1976.
  • G. H. Golub and C. F. Van Loan, Matrix Computations, 4th ed., Johns Hopkins University Press, 2013.
  • J. M. Thijssen, Computational Physics, 2nd ed., Cambridge University Press, 2007.
  • R. Shankar, Principles of Quantum Mechanics, 2nd ed., Springer, 1994.
  • C. M. Bender and S. A. Orszag, Advanced Mathematical Methods for Scientists and Engineers, Springer, 1999.