Coverage for tbkit/localization.py: 100%
60 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-21 11:05 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-21 11:05 +0000
1r'''
2Localization lengths of disordered quasi-1D strips by the transfer matrix
3(MacKinnon and Kramer 1981, 1983).
5The amplitudes :math:`A_i` on the slices of a strip obey
7.. math::
9 \sum_{l=1}^{l_0}\left(V_l^\dagger A_{i-l} + V_l A_{i+l}\right) + h_i A_i = E\,A_i\, ,
11with :math:`h_i = h_0 + \mathrm{diag}(\epsilon_i)`, so that
12:math:`A_{i+l_0} = -V_{l_0}^{-1}[\dots]` is propagated slice by slice. The
13Lyapunov exponents of the product of transfer matrices are the averaged
14logarithms of the diagonal of R in a QR factorization repeated along the
15strip; at a real energy they come in pairs :math:`\pm\gamma`, and the
16smallest positive one sets the localization length
17:math:`\lambda_M = 1/\gamma_{\min}` of a strip of cross-section *M*.
18'''
19from __future__ import annotations
21from typing import NamedTuple
23import numpy as np
24import scipy.linalg as LA
25from numpy.typing import ArrayLike, NDArray
27import tbkit.error_handling as error_handling
30class Lyapunov(NamedTuple):
31 r'''
32 Result of *lyapunov_exponents*, one row per energy:
34 * **exponents** -- Real ndarray, shape (n_e, l0*m). The positive
35 Lyapunov exponents per slice, in decreasing order.
36 * **error** -- Real ndarray, shape (n_e,). Standard error of the
37 smallest one, from the scatter of its means over *n_chunks*
38 consecutive pieces of the strip.
39 * **localization_length** -- Real ndarray, shape (n_e,).
40 :math:`\lambda = 1/\gamma_{\min}`, in slices.
41 * **length_error** -- Real ndarray, shape (n_e,). Standard error of
42 *localization_length*.
43 '''
44 exponents: NDArray[np.float64]
45 error: NDArray[np.float64]
46 localization_length: NDArray[np.float64]
47 length_error: NDArray[np.float64]
50def lyapunov_exponents(
51 h0: ArrayLike, v, energies: ArrayLike, disorder: float = 0., n_slices: int = 10000,
52 n_warmup: int = 200, n_chunks: int = 20, qr_every: int = 1, seed=None,
53) -> Lyapunov:
54 r'''
55 Get the Lyapunov exponents and the localization length of a long strip
56 with uniform onsite disorder (the transfer-matrix method of MacKinnon
57 and Kramer), without building its Hamiltonian: every slice is drawn,
58 used and discarded.
60 Parameters
61 ----------
62 h0 : Hermitian matrix, shape (m, m)
63 Hamiltonian of one slice of the clean strip (e.g. from
64 *transport.lead_from_kspace*, or the ``ham`` of a *System* of the
65 cross-section).
66 v : Matrix of shape (m, m), or array of shape (l0, m, m)
67 Coupling :math:`V_1 = H_{i,i+1}` to the next slice, or the couplings
68 :math:`[V_1, \dots, V_{l_0}]` to the next :math:`l_0` slices. The
69 last one must be invertible: group several cells into one slice if
70 it is not (e.g. a zigzag ribbon).
71 energies : Real number or array
72 Several energies are propagated along the same strip (the same
73 disorder), which correlates their errors.
74 disorder : Positive real or zero, default 0
75 Width *W*: onsite energies uniform in :math:`[-W/2, W/2]`,
76 independent on every site of every slice.
77 n_slices : Positive integer, default 10000
78 Slices averaged over (rounded down to a multiple of
79 ``n_chunks * qr_every``). The relative error of
80 :math:`\gamma_{\min}` decreases as :math:`1/\sqrt{n\gamma_{\min}}`.
81 n_warmup : Positive integer or zero, default 200
82 Slices propagated first, to align the random initial basis.
83 n_chunks : Integer, at least 2, default 20
84 Pieces of the strip for the error estimate.
85 qr_every : Positive integer, default 1
86 Slices between two QR factorizations, which must keep
87 :math:`e^{q(\gamma_1-\gamma_{\min})}` well below :math:`10^{16}`.
88 seed : Integer or None, default None
89 Seed of the disorder and of the initial basis.
91 Returns
92 -------
93 result : :class:`Lyapunov`
95 Examples
96 --------
97 Example usage::
99 res = lyapunov_exponents([[0.]], [[1.]], [0.5, 1.], disorder=1., n_slices=10 ** 5)
100 res.localization_length # about 24 (4 - E^2) / W^2 sites (Thouless)
101 '''
102 h0 = np.atleast_2d(np.asarray(h0, dtype='c16'))
103 error_handling.square_matrix(h0, 'h0')
104 error_handling.hermitian_dense(h0)
105 v = np.asarray(v, dtype='c16')
106 vs = list(v) if v.ndim == 3 else [np.atleast_2d(v)]
107 for c in vs:
108 error_handling.lead(h0, c)
109 error_handling.invertible_coupling(vs[-1])
110 error_handling.frequencies(energies, 'energies')
111 energies = np.atleast_1d(np.asarray(energies, dtype='f8'))
112 error_handling.positive_real_zero(disorder, 'disorder')
113 error_handling.positive_int(n_slices, 'n_slices')
114 error_handling.positive_int_zero(n_warmup, 'n_warmup')
115 error_handling.positive_int(qr_every, 'qr_every')
116 error_handling.transfer_chunks(n_chunks, n_slices, qr_every)
118 m, l0, n_e = len(h0), len(vs), len(energies)
119 k = l0 * m
120 rng = np.random.default_rng(seed)
121 # the rows of the state hold A_{i+l0-1}, ..., A_{i-l0} (blocks of m), so
122 # the equation at slice i is one product with [V_{l0-1} .. V_1 h0 V_1^+ .. V_l0^+]
123 lu = LA.lu_factor(vs[-1])
124 row = np.hstack(vs[-2::-1] + [h0] + [c.conj().T for c in vs])
125 prop = LA.lu_solve(lu, row)
126 inv = LA.lu_solve(lu, np.eye(m))
127 q0, _ = LA.qr(rng.standard_normal((2 * k, k)) + 1j * rng.standard_normal((2 * k, k)), mode='economic')
128 q = np.tile(q0, n_e) # one basis per energy, side by side
129 shift = np.repeat(energies, k)
130 mid = slice((l0 - 1) * m, l0 * m)
131 n_warmup = qr_every * -(-n_warmup // qr_every)
132 chunk_len = n_slices // (n_chunks * qr_every) * qr_every
133 logs = np.zeros((n_e, k))
134 ends = []
135 for i in range(n_warmup + chunk_len * n_chunks):
136 eps = disorder * (rng.random(m) - 0.5)
137 top = prop @ q + inv @ ((eps[:, None] - shift) * q[mid])
138 q = np.vstack([-top, q[:-m]])
139 if (i + 1) % qr_every:
140 continue
141 for e in range(n_e):
142 q[:, e * k:(e + 1) * k], r = LA.qr(q[:, e * k:(e + 1) * k], mode='economic', check_finite=False)
143 if i >= n_warmup:
144 logs[e] += np.log(np.abs(np.diag(r)))
145 if i >= n_warmup and (i + 1 - n_warmup) % chunk_len == 0:
146 ends.append(logs[:, -1].copy())
147 n = chunk_len * n_chunks
148 means = np.diff(np.vstack([np.zeros(n_e)] + ends), axis=0) / chunk_len
149 gammas = logs / n
150 error = means.std(axis=0, ddof=1) / np.sqrt(n_chunks)
151 length = 1 / gammas[:, -1]
152 return Lyapunov(gammas, error, length, error * length ** 2)