Skip to content

Hamiltonians and operators

A Hamiltonian or an observable is written as its operator expression, with the operator lists of Models. For the spin-1/2 Heisenberg chain,

\[H=J\sum_{i=1}^{n-1}\mathbf S_i\cdot\mathbf S_{i+1} =J\sum_{i=1}^{n-1}\left(S_i^xS_{i+1}^x+S_i^yS_{i+1}^y+S_i^zS_{i+1}^z\right),\]
from dmrgpy import spinchain
n = 30
sc = spinchain.Spin_Chain(["S=1/2" for i in range(n)])
h = 0
for i in range(n-1):
    h = h + sc.Sx[i]*sc.Sx[i+1] + sc.Sy[i]*sc.Sy[i+1] + sc.Sz[i]*sc.Sz[i+1]
sc.set_hamiltonian(h)

Any sum of products of local operators is allowed, with any range and complex coefficients. This covers single-ion anisotropy \(D(S^z_i)^2\), Dzyaloshinskii-Moriya terms \(\mathbf D\cdot(\mathbf S_i\times\mathbf S_{i+1})\), biquadratic exchange \((\mathbf S_i\cdot\mathbf S_{i+1})^2\), hopping \(t\sum_i(c_i^\dagger c_{i+1}+\mathrm{h.c.})\), the Hubbard term \(U\sum_in_{i\uparrow}n_{i\downarrow}\), and so on. The Cookbook gives the complete Hamiltonian of the common models. h.get_dagger() returns \(h^\dagger\), so h + h.get_dagger() adds the Hermitian conjugate. It is best to write \(H\) in units where its largest coefficient is of order 1: a few routes, such as real-time evolution, use absolute thresholds.

Shortcuts

On a spin chain, sc.SS(i, j) is the Heisenberg bond \(\mathbf S_i\cdot\mathbf S_j\), so the chain above is h = sum(sc.SS(i, i+1) for i in range(n-1)). On every chain, sc.generate_bilinear(f, A, B) returns the Hermitian part of \(\sum_{ij}f(i,j)\,A_iB_j\) for two operator lists, for example sc.generate_bilinear(f, sc.Sz, sc.Sz) for an Ising coupling with any range. A fermionic Hamiltonian is written the same way, as a sum of operators. The Hubbard-like chain with hopping \(t=1\) and nearest-neighbor repulsion \(V=2\) is:

from dmrgpy import fermionchain
fc = fermionchain.Fermionic_Chain(4)
h = 0
for i in range(3):
    h = h + fc.Cdag[i]*fc.C[i+1] + fc.Cdag[i+1]*fc.C[i]  # hopping
    h = h + 2.0*fc.N[i]*fc.N[i+1]                        # repulsion
fc.set_hamiltonian(h)

Operators

On an operator A, A.simplify() collects equal terms, and A.is_hermitian(), A.is_antihermitian() and A.is_zero() answer by reading the terms. A True answer is exact. A False answer can be wrong when the property depends on an identity of the local space, such as \(S^xS^x=1/4\) for spin 1/2. sc.is_hermitian(A) does not have this limitation, since it also checks numerically on the chain.

sc.toMPO(A) builds the matrix product operator of A once. Two such operators can be added, subtracted, multiplied by numbers and multiplied with each other (A + 2*B - A, A*B), and A.trace() is their trace, so (Sz[0]*Hmpo*Hmpo).trace() is a moment of the spectrum. To combine Hamiltonians, it is simpler to sum the operator expressions before calling set_hamiltonian. On a small chain, sc.get_full_matrix(A) returns the dense matrix of any operator, to check a model against another code.

States and operators

The chain also gives the basic operations on states. Here wf is a state, for example wf = sc.get_gs(), and A an operator:

Call Returns
sc.overlap(a, b) \(\langle a\vert b\rangle\)
sc.aMb(a, A, b) \(\langle a\vert A\vert b\rangle\)
sc.applyoperator(A, wf), or A*wf \(A\vert \psi\rangle\)
sc.applyinverse(A, wf) \(A^{-1}\vert \psi\rangle\), solved iteratively
sc.exponential(A, wf) \(e^{A}\vert \psi\rangle\)
sc.summps(a, b) \(\vert a\rangle+\vert b\rangle\)
sc.scale_mps(x, wf), or wf*x \(x\vert \psi\rangle\)
sc.trace(A), sc.inverse_trace(A) \(\mathrm{Tr}\,A\), \(\mathrm{Tr}\,A^{-1}\)
sc.operator_norm(A) an estimate of \(\lVert A\rVert\)
sc.is_zero_operator(A) whether \(A=0\)
wf.dot(wf2), wf.norm() \(\langle\psi\vert \psi_2\rangle\), \(\lVert\psi\rVert\)
wf.normalize() \(\vert \psi\rangle/\lVert\psi\rVert\)
wf.get_conjugate() the complex conjugate state
sc.hadamard_product(a, b, conj=(True, False)) the state with amplitudes \(a(s)^*b(s)\)
sc.elementwise_map(wf, f) the state with amplitudes \(f(\psi(s))\)

A state remembers the backend that made it (an MPS for DMRG, a vector for mode="ED"), and these operations run on that backend. Give mode="ED" to overlap and aMb when the states come from ED. On DMRG, exponential needs a Hermitian or an anti-Hermitian A, so sc.exponential(-1j*t*h, wf) is a real-time step. For a general A use mode="ED" on a small chain. On DMRG, applyinverse solves \(A x=\psi\) by two-site sweeps on itensor_version="python" when \(A\) is not Hermitian, such as the resolvent \(z-H\) off the real axis, as DMRG solves the ground state. A Hermitian \(A\), the other backends and a conserved sector take BiCGSTAB. sc.inverse_solver="bicgstab" or "sweep" forces one ("sweep" on "python" and "julia_live"). The sweeps warn if the residual stays above the tolerance, as it can for an \(A\) with eigenvalues of both signs. sc.inverse_nsweeps is the largest number of sweeps (default 10).

The elementwise products are taken in the computational basis \(s\) of the sites. On itensor_version=3, "python" and "julia_live" they use recursive sketched interpolation (arXiv:2602.17974), with maxm= the bond dimension of the result; method="direct" is the exact product compressed by SVD, more accurate and more expensive. elementwise_map is the extension of the same method to a nonlinear f, exact once the bond dimension holds the result. For fermions a product of two amplitudes depends on the sign convention of the basis states, while \(|\psi(s)|^2\) does not. The Participation entropy is built on them.