Metadata-Version: 2.4
Name: tbkit
Version: 0.9.0
Summary: A Python package to build and solve Tight-Binding models.
Author-email: Charles Poli <cpoli374@gmail.com>
Maintainer-email: Charles Poli <cpoli374@gmail.com>
License-Expression: BSD-3-Clause
Project-URL: Homepage, https://github.com/cpoli/tbkit
Project-URL: Issues, https://github.com/cpoli/tbkit/issues
Project-URL: Changelog, https://github.com/cpoli/tbkit/blob/master/CHANGELOG.md
Project-URL: Documentation, https://cpoli.github.io/tbkit/
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Physics
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Requires-Dist: scipy>=1.10
Requires-Dist: matplotlib>=3.7
Provides-Extra: continuum
Requires-Dist: sympy>=1.12; extra == "continuum"
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Requires-Dist: pytest-cov>=4; extra == "test"
Requires-Dist: pytest-xdist>=3.2; extra == "test"
Requires-Dist: sympy>=1.12; extra == "test"
Provides-Extra: docs
Requires-Dist: sphinx>=7; extra == "docs"
Requires-Dist: pydata-sphinx-theme>=0.15; extra == "docs"
Requires-Dist: sphinx-gallery>=0.17; extra == "docs"
Requires-Dist: joblib>=1.3; extra == "docs"
Requires-Dist: sympy>=1.12; extra == "docs"
Requires-Dist: sphinx-sitemap>=2.6; extra == "docs"
Requires-Dist: sphinxext-opengraph>=0.9; extra == "docs"
Dynamic: license-file

# tbkit — a Tight-Binding package

| | |
|:--|:-:|
| Package | [![PyPI version](https://badgen.net/pypi/v/tbkit)](https://pypi.org/project/tbkit/) [![conda-forge](https://img.shields.io/conda/vn/conda-forge/tbkit)](https://anaconda.org/conda-forge/tbkit) [![Python versions](https://badgen.net/pypi/python/tbkit)](https://pypi.org/project/tbkit/) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.24285819.svg)](https://doi.org/10.5281/zenodo.24285819) |
| Quality | [![License](https://img.shields.io/github/license/cpoli/tbkit)](https://github.com/cpoli/tbkit/blob/master/LICENSE) [![CI](https://github.com/cpoli/tbkit/actions/workflows/ci.yml/badge.svg)](https://github.com/cpoli/tbkit/actions/workflows/ci.yml) [![Coverage](https://codecov.io/gh/cpoli/tbkit/branch/master/graph/badge.svg)](https://codecov.io/gh/cpoli/tbkit) |
| Documentation | [![Docs](https://badgen.net/badge/docs/cpoli.github.io%2Ftbkit/blue)](https://cpoli.github.io/tbkit/) |
| Downloads | [![Downloads](https://static.pepy.tech/badge/tbkit)](https://pepy.tech/project/tbkit) [![Downloads/Month](https://static.pepy.tech/badge/tbkit/month)](https://pepy.tech/project/tbkit) |
| Community | [![GitHub Stars](https://img.shields.io/github/stars/cpoli/tbkit)](https://github.com/cpoli/tbkit) [![GitHub Forks](https://img.shields.io/github/forks/cpoli/tbkit)](https://github.com/cpoli/tbkit) [![Contributors](https://img.shields.io/github/contributors/cpoli/tbkit)](https://github.com/cpoli/tbkit/graphs/contributors) [![Last Commit](https://img.shields.io/github/last-commit/cpoli/tbkit/master)](https://github.com/cpoli/tbkit/commits/master) |

![tbkit logo](https://raw.githubusercontent.com/cpoli/tbkit/master/docs/source/_static/image/tbkit_logo.png)

**tbkit** is a Python package to build and solve Tight-Binding models, written
in vectorized NumPy/SciPy. It aims to make the mechanics of Tight-Binding
models — lattices, hoppings, Hamiltonians, spectra, band structures —
explicit and easy to inspect, so it works as well for teaching as for
research prototyping.

![Graphene's Dirac cone, Hofstadter's butterfly, and helical edge states of a Kane-Mele ribbon, all computed with tbkit](https://raw.githubusercontent.com/cpoli/tbkit/master/docs/source/_static/images/readme_hero.png)

For computational physics beyond tight-binding — quantum mechanics,
classical mechanics, statistical physics, general relativity, and more —
see [physicskit](https://github.com/cpoli/physicskit), whose
`physicskit.condensed` subpackage covers tight-binding band theory and
topology alongside superconductivity.

## Install

Requires Python >= 3.10 (tested on 3.10-3.15).

```bash
pip install tbkit
# or
conda install -c conda-forge tbkit
```

`tbkit.continuum` (k·p models to tight-binding) also needs sympy:

```bash
pip install "tbkit[continuum]"
# or
conda install -c conda-forge tbkit sympy
```

Transport of large 2D and 3D devices is several times faster with the
MUMPS solver, which `Transport` uses when python-mumps is installed (from
conda-forge; PyPI has no binary of it):

```bash
conda install -c conda-forge python-mumps
```

or, for an editable install from a clone (e.g. to run the test suite or
work on tbkit itself):

```bash
git clone https://github.com/cpoli/tbkit
cd tbkit
pip install -e .
```

## Quick start

Real-space flake, nearest-neighbor square lattice:

```python
from tbkit.lattice import Lattice
from tbkit.system import System

lat = Lattice(unit_cell=[{'tag': 'a', 'r0': (0., 0.)}],
                       prim_vec=[(1., 0.), (0., 1.)])
lat.get_lattice(n1=10, n2=10)

system = System(lat)
system.set_hopping([{'n': 1, 't': 1.}])
system.get_ham()
system.get_eig()
print(system.en)
```

Named models in one call (`tbkit.models`: SSH, Rice-Mele, Kitaev chain,
Haldane, Qi-Wu-Zhang, Kane-Mele, BHZ, the three-band TMD model, a 3D
topological insulator, a Weyl semimetal), with the parameters of the
original papers. Each is a `KSpace`, and `bridges.finite_system` cuts the
same model into a finite `System`:

```python
import numpy as np
from tbkit import models
from tbkit.bridges import finite_system

hal = models.haldane(t1=1., t2=0.1, phi=np.pi / 2, M=0.2)
print(hal.chern_number(bands=[0]))       # 1.0
flake = finite_system(hal, (20, 20))     # the same model on a 20 x 20-cell flake
```

Graphene band structure (reciprocal space):

```python
import numpy as np
from tbkit.lattice import Lattice
from tbkit.kspace import KSpace, reciprocal_vectors

DX, DY = 0.5 * 3 ** 0.5, 0.5
unit_cell = [{'tag': 'a', 'r0': (0., 0.)}, {'tag': 'b', 'r0': (DX, DY)}]
prim_vec = [(2 * DX, 0.), (DX, 1.5)]
lat = Lattice(unit_cell=unit_cell, prim_vec=prim_vec)

gra = KSpace(lat)
gra.set_hopping([{'i': 0, 'j': 1, 'R': (0, 0), 't': 1.},
                        {'i': 0, 'j': 1, 'R': (-1, 0), 't': 1.},
                        {'i': 0, 'j': 1, 'R': (0, -1), 't': 1.}])

b1, b2 = (np.array(v) for v in reciprocal_vectors(prim_vec))
Gamma, K, M = np.zeros(2), (b1 - b2) / 3, b1 / 2
gra.k_path([Gamma, K, M, Gamma], nk=60)
fig = gra.plot_bands(node_labels=[r'$\Gamma$', 'K', 'M', r'$\Gamma$'])
fig.savefig('graphene_bands.png')
```

A magnetic field is added with `System.set_magnetic_field` (uniform field) or
`System.set_peierls_phase` (arbitrary vector potential), applied to the
hoppings *after* `set_hopping`/`set_hopping_manual`:

```python
system.set_hopping_manual(hop_dict)
system.set_magnetic_field(alpha=0.01)  # flux quanta per unit area
system.get_ham()
```

See [`examples/magnetic_field/plot_magnetic_field.py`](examples/magnetic_field/plot_magnetic_field.py) for a full
worked example (an Aharonov-Bohm ring, reproducing the textbook result that
the spectrum is periodic in the enclosed flux with period one flux quantum).

A Chern number is the Berry curvature of a group of bands, integrated over
the Brillouin zone:

```python
chern = gra.chern_number(bands=[0], nk=40)   # ~0 for plain graphene
```

See [`examples/topology/plot_haldane_topology.py`](examples/topology/plot_haldane_topology.py) for the
Haldane model (the first Chern insulator), reproducing its topological
phase transition (Chern number 1 -> 0) and Berry-curvature map.

A spin-1/2 degree of freedom is added with `KSpace(lat, spin=True)`; onsite
values and hoppings then also accept 2x2 (Pauli) matrices:

```python
from tbkit.kspace import KSpace, PAULI

kmele = KSpace(lat, spin=True)
kmele.set_hopping([{'i': 0, 'j': 0, 'R': (1, 0), 't': 1j*lam*PAULI['z']}])  # intrinsic SOC
```

A ribbon -- periodic in one direction, finite in the other, the standard
way to see edge states -- is cut out of a periodic model with
`tbkit.kspace.ribbon`:

```python
from tbkit.kspace import ribbon

rib = ribbon(lat, list_hop, width=30, direction=1)   # 30 unit cells wide
fig = rib.plot_bands()
```

See [`examples/topology/plot_edge_states.py`](examples/topology/plot_edge_states.py) for the zigzag
graphene ribbon's zero-energy edge band, and the Kane-Mele ribbon's helical
edge states crossing a spin-orbit gap.

## Examples

### Building and solving models

Finite lattices, defects, several orbitals per site, real space to k-space and back, k·p models discretized into lattice models, saved models, Wannier90 files (`_hr.dat` and `_tb.dat` in, both out), models exchanged with ASE, PythTB, TBmodels, Z2Pack, qsymm, pybinding and Kwant (`tbkit.interop`), maximally localized Wannier functions, and solvers for very large lattices.

![An impurity bound state, Slater-Koster sp3 graphene bands with overlaps, and the kernel polynomial method on 45,000 sites](https://raw.githubusercontent.com/cpoli/tbkit/master/docs/source/_static/images/readme_models.png)

### Fields, strain and disorder

What happens to a lattice under a magnetic field (Peierls substitution), under strain, and with disorder.

![Graphene's sqrt(n) Landau ladder, triaxial strain as a pseudo-magnetic field, and Anderson localization](https://raw.githubusercontent.com/cpoli/tbkit/master/docs/source/_static/images/readme_fields.png)

### Band topology

Chern numbers, Berry phases, the Z2 invariant (and the four 3D indices), spin and mirror Chern numbers, the Bott index and entanglement spectrum, Weyl points and their chirality, Kitaev's Majorana number, quantum geometry, symmetry classes, higher-order and 3D topological phases.

![Berry curvature of the Haldane model, the Kane-Mele Z2 Wannier flow, and the four corner states of a quadrupole insulator](https://raw.githubusercontent.com/cpoli/tbkit/master/docs/source/_static/images/readme_topology.png)

### Response and transport

Hall conductivities at any Fermi level (with the orbital magnetization, Nernst and thermal Hall responses), Landauer transport between leads, and linear and nonlinear optical response.

![The anomalous Hall conductivity of the Haldane model, the conductance steps of a quantum point contact, and graphene's universal absorption](https://raw.githubusercontent.com/cpoli/tbkit/master/docs/source/_static/images/readme_response.png)

### Flat bands and interactions

Flat bands from lattice geometry and from a moiré twist, the Hubbard model in mean field, and superconductivity.

![The flat bands of twisted bilayer graphene, magnetic zigzag edges in Hubbard mean field, and the Majorana end modes of a Kitaev chain](https://raw.githubusercontent.com/cpoli/tbkit/master/docs/source/_static/images/readme_interactions.png)

### Driven and open systems

Wavepacket dynamics, periodically driven (Floquet) lattices, and non-Hermitian bands with exceptional points.

![Bloch oscillations in a tilted chain, the Floquet bands of graphene in circularly polarized light, and the non-Hermitian skin effect](https://raw.githubusercontent.com/cpoli/tbkit/master/docs/source/_static/images/readme_driven.png)

## Documentation

Rendered docs (tutorial, API reference, example gallery): https://cpoli.github.io/tbkit/

* [`docs/source/tutorial.rst`](docs/source/tutorial.rst) -- a narrative walkthrough of the
  package, from building a lattice through topology, spin-orbit coupling,
  and edge states.
* [`docs/source/history/`](docs/source/history/) -- a chronology of the breakthroughs
  behind Tight-Binding theory (Bloch's theorem through higher-order topology),
  one page per theme,
  each one linked to the corresponding **tbkit** functionality and example
  above.
* `docs/source/tbkit.rst` -- the API reference (auto-generated from docstrings).
* [`ROADMAP.md`](ROADMAP.md) -- what is planned next, and what tbkit
  does not do yet (compared with Kwant and PythTB).

Build the HTML docs with `cd docs && make html` (output in `docs/build/html`),
or `make html-noplot` to skip running the gallery examples. `make publish`
builds them in full and pushes them to the gh-pages branch.

## Test

```bash
pip install -e ".[test]"
pytest tests/
```

The 781 tests cover 100% of the lines of the `tbkit` package; see the
[HTML coverage report](https://cpoli.github.io/tbkit/coverage/). CI runs them
on Python 3.10-3.15, and its docs job re-runs every gallery example, each
of which asserts its key numbers.

## A note on the API

Version 0.2 modernized the package to run on current Python/NumPy/SciPy and
cleaned up the API:

* Sublattice tags are plain one-character **strings** (`'a'`) rather than
  byte strings (`b'a'`).
* Classes are named in `PascalCase` (`Lattice`, `System`, ...) rather than
  lowercase names identical to their module (`lattice.lattice`,
  `system.system`, ...), which used to make `import tbkit.lattice as lattice`
  silently bind the wrong object.

For continuity, the pre-0.2 lowercase class names (`lattice`, `system`,
`plot`, `propagation`, `save`) remain available as aliases of the new
classes, so `from tbkit.lattice import lattice` still works.

## Related toolkits

tbkit belongs to a family of Python toolkits for learning and teaching,
built the same way:

- [blockchainkit](https://github.com/cpoli/blockchainkit) -- cryptography, consensus and blockchain systems
- [chemistrykit](https://github.com/cpoli/chemistrykit) -- computational chemistry
- [earthsciencekit](https://github.com/cpoli/earthsciencekit) -- Earth-system science
- [engineeringkit](https://github.com/cpoli/engineeringkit) -- computational engineering
- [financialkit](https://github.com/cpoli/financialkit) -- quantitative finance
- [mathematicskit](https://github.com/cpoli/mathematicskit) -- computational mathematics
- [physicskit](https://github.com/cpoli/physicskit) -- computational physics

## Citation

If you use tbkit in your research, please cite it; see
[CITATION.cff](CITATION.cff). Each release is archived on Zenodo with its own
DOI; the DOI badge above links the latest release.

## License

BSD 3-Clause -- see [LICENSE](LICENSE).
