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.
Physical Problem
Section titled “Physical Problem”Use dimensionless oscillator units and consider
The ground state is even. For , the quartic term narrows the central part of the wavefunction relative to the 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 . The notebook does not use perturbation theory to define the numerical reference, so it remains useful at moderate and strong coupling.
Analytic Anchor at One Basis State
Section titled “Analytic Anchor at One Basis State”Let be the ratio between the basis frequency and the frequency used to define the dimensionless Hamiltonian. For a single frequency- oscillator ground state, the Rayleigh quotient is
Its stationary point satisfies
This formula is an analytic validation target for the 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.
Frequency-Scaled Ritz Spaces
Section titled “Frequency-Scaled Ritz Spaces”For each , define the -dimensional even subspace
The basis ladder operators obey
and the frequency- oscillator Hamiltonian is
It is numerically convenient to rewrite the target Hamiltonian as
Projecting onto gives a real symmetric matrix . Its lowest eigenpair solves
The inner problem is a linear Rayleigh-Ritz diagonalization. The outer problem is the nonlinear optimization
This separation is important. At fixed , coefficients are optimized exactly within the represented subspace by diagonalization. The outer search changes the subspace itself.
Why the Result Is Still an Upper Bound
Section titled “Why the Result Is Still an Upper Bound”For every admissible and finite , the Ritz value satisfies
Taking the minimum over cannot violate that inequality:
At fixed , the spaces are nested,
so
The same monotonicity survives independent optimization at each dimension:
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
A common implementation mistake is to build a -state ladder matrix, form inside that truncated matrix, and then diagonalize. Multiplying after truncation removes intermediate paths that leave the retained space and return. The highest rows of are then wrong.
The calculation instead follows this order:
- Build the ladder operators in a full basis extending at least four oscillator levels above the largest retained .
- Form and in that padded basis.
- Project the completed operators onto .
- Assemble only after projection.
The core implementation is:
n_max = 2 * (K - 1)M = n_max + padding + 1a = 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 @ qq4_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.
Optimize the Log-Frequency
Section titled “Optimize the Log-Frequency”The positivity constraint on is removed by setting
This coordinate has three advantages:
- every trial basis has ;
- multiplicative changes in frequency become additive steps;
- comparable relative changes at small and large receive comparable resolution.
For , the landscape is unimodal. For , this should not be assumed. The lowest eigenvalue of the finite projected matrix develops several local minima as varies. A scalar search over one broad bracket can settle in whichever basin happens to satisfy its local assumptions.
The reproducible search therefore uses:
- a uniform global scan in ;
- detection of every discrete local minimum;
- local golden-section refinement inside each detected basin;
- comparison of all refined energies;
- a boundary check to ensure the scan brackets the global minimum.
For , the published scan uses
with points, followed by basin refinement to an interval width below in . Doubling the grid density does not change the reported global basin or displayed energy.
Energy Landscape and Convergence
Section titled “Energy Landscape and Convergence”At , the large-basis reference is
The first panel below plots the excess 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.
Frequency-scaled Rayleigh-Ritz results for . Panel (a) shows the finite-basis energy landscape against for even basis states. Panel (b) shows the energy excess above a reference. Global scale optimization accelerates finite- convergence, while the increasingly shallow multi-basin landscape makes the numerical value of the optimal scale less stable.
For , the global minima found by the scan are:
| Detected Basins | |||
|---|---|---|---|
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.
Coupling Sweep
Section titled “Coupling Sweep”The same protocol was run at weak, intermediate, and strong coupling. The reference uses at the analytic one-Gaussian frequency . Increasing the reference dimension from through , , and leaves all displayed reference digits unchanged.
| Optimized Excess | Optimized Excess | Optimized Excess | Fixed-, Excess | ||
|---|---|---|---|---|---|
Several conclusions are visible.
Optimization matters most before convergence
Section titled “Optimization matters most before convergence”At , the unscaled basis is still high by about , while scale optimization reduces the excess below . 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 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 , a broad range of frequencies spans nearly the same well-converged state. The optimized can move between nearby basins while the energy changes below the target precision. Reporting many digits of would confuse optimizer conditioning with physics.
Stationarity and the Virial Check
Section titled “Stationarity and the Virial Check”Changing dilates every basis function. For a fully optimized eigenvector inside , the envelope theorem gives
Define the virial residual
Then
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, is below and is usually much smaller. The tolerance is set by the flatness of the landscape and double-precision cancellation, not by the eigensolver residual.
Curvature and Parameter Conditioning
Section titled “Curvature and Parameter Conditioning”In the one-dimensional outer problem, the local Hessian is
At , a centered finite difference gives:
Near a minimum,
The falling curvature explains why 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.
Validation Ledger
Section titled “Validation Ledger”The program stops with an error if any required check fails.
Harmonic limit
Section titled “Harmonic limit”At , , and ,
to machine precision.
One-Gaussian agreement
Section titled “One-Gaussian agreement”For every tested , the optimizer reproduces the positive root of
and the analytic Gaussian energy.
Hermiticity and parity
Section titled “Hermiticity and parity”The assembled matrix obeys
An independently assembled full-basis matrix has a vanishing even-odd block to the same tolerance.
Eigensolver residual
Section titled “Eigensolver residual”For the normalized lowest eigenvector,
is below in the published sweep. This verifies the represented matrix eigenproblem, but not basis convergence by itself.
Variational ordering
Section titled “Variational ordering”The optimized values decrease with and remain above the converged reference within a roundoff allowance. Once the excess reaches roughly , only fewer digits are reported because floating-point noise can obscure the sign of a still smaller difference.
Padding independence
Section titled “Padding independence”Increasing the operator-construction padding from six to ten oscillator levels changes the test energy by less than .
Scan refinement
Section titled “Scan refinement”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.
Downloadable Program and Data
Section titled “Downloadable Program and Data”Run the benchmark with:
python variational-optimization.py --output-dir variational-outputNumPy 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.
Reproducibility Metadata
Section titled “Reproducibility Metadata”| Item | Published Run |
|---|---|
| Operating system | Windows 11, x86-64 |
| Python | 3.12.13 |
| NumPy | 2.3.5 |
| BLAS/LAPACK | OpenBLAS 0.3.30, 64-bit integer interface |
| Scalar type | IEEE 754 binary64 |
| Couplings | |
| Optimized dimensions | even states |
| Reference dimension | even states |
| Scan coordinate | |
| Scan interval | for the reported couplings |
| Scan points | |
| Basin tolerance | in |
| Operator padding | six levels above the largest retained |
| Random seed | none; the calculation is deterministic |
| License | MIT |
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.
Known Failure Modes
Section titled “Known Failure Modes”Assuming one broad bracket is unimodal
Section titled “Assuming one broad bracket is unimodal”A golden-section or Brent-style search is reliable only when its bracket satisfies the method’s local shape assumptions. At , 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 , the basis family, the objective, and numerical tolerance. Only predictions stable under enlargement of the trial space should receive physical interpretation.
Optimizing below the numerical floor
Section titled “Optimizing below the numerical floor”When 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.
Building powers after projection
Section titled “Building powers after projection”Computing from an already truncated corrupts boundary matrix elements. Pad first or use analytic matrix elements.
Comparing to an unconverged reference
Section titled “Comparing to an unconverged reference”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 was solved accurately. It says nothing about the distance between and the infinite-dimensional energy.
Losing the symmetry sector
Section titled “Losing the symmetry sector”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.
Common Mistakes
Section titled “Common Mistakes”- Optimizing directly with unconstrained steps that enter .
- Reporting only the optimizer’s success flag, without a landscape scan or virial residual.
- Comparing energies at different without checking that each outer minimum is global.
- Keeping many digits of 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 level as a failure of the variational theorem.
Exercises
Section titled “Exercises”- Show that the projected matrix reproduces the analytic Gaussian Rayleigh quotient.
Solution
For the frequency- ground state,
The frequency- oscillator contribution is , and the correction to the quadratic potential is
Therefore
- Prove that independently optimizing at each preserves monotone variational convergence.
Solution
Let minimize . Since at every fixed ,
The optimized -dimensional energy cannot exceed its value at , so
Each term is also an upper bound on by the variational principle.
- Derive the relation between the scale derivative and the virial residual.
Solution
Under the dilation , kinetic expectation values scale as , quadratic moments as , and quartic moments as . Differentiating at fixed optimized coefficient vector gives
Twice this expression is
which is the virial residual . Thus .
- Why can forming after truncating give the wrong projected operator even when only low states are retained?
Solution
A matrix element of contains sums over intermediate oscillator states. Starting from a retained state near the upper boundary, an intermediate application of can leave the truncated space and a later application can return. Truncating first deletes that path. Building in a padded space and projecting afterward retains every intermediate state needed for the desired matrix elements.
- At , the curvature falls from about at to at . If the local frequency coordinate has an uncertainty , estimate the associated energy uncertainty at each dimension.
Solution
Use
At ,
At ,
The same uncertainty in the optimizer coordinate matters far less once the basis is converged and the landscape is flat.
- 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.
Cross-Links
Section titled “Cross-Links”- Computational Notebooks
- Anharmonic Oscillator by Variational Methods
- Variational Principle
- Rayleigh-Ritz Method
- Variational Parameters
- Gaussian Variational Methods
- Common Variational Pitfalls
- Perturbation Theory Benchmarks
- Matrix Diagonalization
- Conditioning and Stability
References
Section titled “References”- 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.