Minimal Matplotlib visualizations for TensorKrowch, TensorNetwork, Quimb, TeNPy, and traced PyTorch/NumPy einsum tensor networks.
Project description
Tensor-Network-Visualization
Minimal Matplotlib visualizations for TensorKrowch, TensorNetwork, Quimb, TeNPy, and traced
PyTorch/NumPy einsum tensor networks.
Repository: https://github.com/DOKOS-TAYOS/Tensor-Network-Visualization
What this project does
Tensor network libraries use different object models and rarely share a single visualization path.
This package normalizes each backend into one graph description, lays out nodes (chains,
grids, trees, planar embeddings, or force-directed fallback), and draws with Matplotlib in
2D (filled disks) or 3D (lightweight octahedra). There is no custom GUI: you get standard
Figure and Axes objects for saving, composing subplots, and further styling.
Audience: Researchers who work with tensor networks and want consistent, publication-friendly
diagrams across Quimb, TeNPy, TensorNetwork, TensorKrowch, or traced einsum contraction graphs.
Installation
PyPI package name: tensor-network-visualization. Import module: tensor_network_viz.
Base install
python -m pip install tensor-network-visualization
Depends on matplotlib and networkx only. You can render einsum traces built from ordered
pair_tensor entries without installing PyTorch.
Optional backends (extras)
| Backend | Pip extra | Notes |
|---|---|---|
| TensorKrowch | tensorkrowch |
pip install "tensor-network-visualization[tensorkrowch]" |
| TensorNetwork | tensornetwork |
pip install "tensor-network-visualization[tensornetwork]" |
| Quimb | quimb |
pip install "tensor-network-visualization[quimb]" |
| TeNPy | tenpy |
Resolves to PyPI package physics-tenpy. |
| Einsum tracing | einsum |
Adds PyTorch for the traced einsum helper. |
| Jupyter widgets | jupyter |
ipympl, widgets, JupyterLab / Notebook 7+ for interactive figures. |
Combine extras, for example:
python -m pip install "tensor-network-visualization[quimb,jupyter]"
Windows and Linux quick setup
From the project root (development) or any environment (end users):
Windows (PowerShell):
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install -U pip
python -m pip install "tensor-network-visualization[quimb]"
Linux / macOS:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -U pip
python -m pip install "tensor-network-visualization[quimb]"
Editable install for contributors:
python -m pip install -e ".[dev]"
Jupyter and interactive figures
For pan, zoom, and (with %matplotlib widget) smoother interaction, install the jupyter
extra and select the ipympl backend before creating figures.
For interactive figures (e.g. rotatable 3D), install [jupyter] (ipympl, ipywidgets,
JupyterLab, and classic Jupyter Notebook 7+) and run jupyter notebook or jupyter lab in
the browser. The Cursor / VS Code notebook tab may not load the full widget stack; if
figures fail to show, use %matplotlib inline or set MPLBACKEND=inline before importing
pyplot, or open the same notebook in the browser with a normal Jupyter install.
Modes and API knobs
Everything below maps to real parameters—there are no hidden mode switches.
| Concept | Where | Meaning |
|---|---|---|
| View mode | show_tensor_network(..., view=...) |
"2d" — disk nodes; "3d" — octahedra. Same normalized graph. |
| Engine mode | engine=... |
Adapter: "tensorkrowch", "tensornetwork", "quimb", "tenpy", "einsum". Invalid → ValueError. |
| Display mode | show=True / False |
If True: Jupyter kernel uses IPython.display.display(fig); otherwise plt.show(). If False: neither runs—use for savefig / batch. |
| Label policy | PlotConfig + overrides |
Defaults: show_tensor_labels, show_index_labels. Per-call: show_tensor_network(..., show_tensor_labels=..., show_index_labels=...). |
| Hover labels | PlotConfig(hover_labels=True) |
Tensor names and bond labels appear on pointer hover (2D axes hit-test; 3D screen-space distance). Needs an interactive Matplotlib window. |
| Einsum workflow | engine="einsum" |
Auto: EinsumTrace + tensor_network_viz.einsum. Manual: ordered list of pair_tensor(...). |
Minimal examples
Quimb 2D, save to PNG
After python -m pip install "tensor-network-visualization[quimb]":
import numpy as np
import quimb.tensor as qtn
from tensor_network_viz import PlotConfig, show_tensor_network
tensors = []
for i in range(3):
inds = ([f"b{i-1}_{i}"] if i > 0 else []) + [f"p{i}"] + ([f"b{i}_{i+1}"] if i < 2 else [])
shape = tuple(2 for _ in inds)
tensors.append(qtn.Tensor(np.ones(shape), inds=inds, tags=(f"A{i}",)))
network = qtn.TensorNetwork(tensors)
fig, ax = show_tensor_network(
network,
engine="quimb",
view="2d",
config=PlotConfig(figsize=(8, 6)),
show=False,
)
fig.savefig("network.png", bbox_inches="tight")
Quimb 3D, hide index labels on this call only
fig, ax = show_tensor_network(
network,
engine="quimb",
view="3d",
config=PlotConfig(figsize=(9, 7)),
show_index_labels=False,
show=False,
)
Hover labels (interactive session only)
fig, ax = show_tensor_network(
network,
engine="quimb",
view="2d",
config=PlotConfig(figsize=(8, 6), hover_labels=True),
)
No effect for headless show=False without a GUI event loop; pair with an interactive backend
(e.g. %matplotlib widget in Jupyter).
TeNPy finite MPS (sketch)
Requires python -m pip install "tensor-network-visualization[tenpy]". See the
extended guide for exact MPS construction; the call shape is always:
from tensor_network_viz import show_tensor_network, PlotConfig
fig, ax = show_tensor_network(
mps,
engine="tenpy",
view="2d",
config=PlotConfig(figsize=(8, 6)),
show=False,
)
show_tensor_network reference
show_tensor_network(
network,
*,
engine: EngineName,
view: ViewName,
config: PlotConfig | None = None,
show_tensor_labels: bool | None = None,
show_index_labels: bool | None = None,
show: bool = True,
) -> tuple[Figure, Axes | Axes3D]
network: Backend-native object or iterable (see guide — supported inputs).engine:"tensorkrowch"|"tensornetwork"|"quimb"|"tenpy"|"einsum".view:"2d"|"3d".config: Styling and layout; defaults toPlotConfig()if omitted.show_tensor_labels/show_index_labels: IfNone, use values fromconfig; else override for this call only.show: Whether to push the figure to the current UI (display/show).- Returns:
(fig, ax)for titles,savefig, colorbars, or embedding in a subplot.
Backend-specific shortcuts (same renderer core) accept ax= and seed= for subplots and
reproducible layout; see the guide.
PlotConfig quick reference
Frozen dataclass; all fields optional beyond defaults. Values shown are constructor defaults.
Numeric fields with None use the corresponding DEFAULT_* on the class (see table footnote).
| Field | Default | Role |
|---|---|---|
node_color |
"#E8E8E8" |
Tensor node fill. |
node_edge_color |
"#2D3748" |
Tensor node outline. |
node_color_degree_one |
"#E8D6D6" |
Fill for tensors with total graph degree 1. |
node_edge_color_degree_one |
"#4A3436" |
Outline for degree-1 tensors. |
tensor_label_color |
"#1A202C" |
Tensor name text. |
label_color |
"#0C1319" |
Index / bond label text. |
bond_edge_color |
"#00008B" |
Contraction edges. |
dangling_edge_color |
"#8B0000" |
Dangling (open) legs. |
figsize |
(8, 6) |
inches; None uses Matplotlib fallback (14, 10) when the renderer creates a new figure. |
show_tensor_labels |
True |
Draw tensor names on nodes. |
show_index_labels |
True |
Draw axis names on bonds / stubs. |
node_radius |
None |
→ 0.08 data units (scaled with layout; multiplies geometric radius). |
stub_length |
None |
→ 0.16 (dangling stub length scale). |
self_loop_radius |
None |
→ 0.2 (self-contraction loops). |
line_width_2d |
None |
→ 0.85 |
line_width_3d |
None |
→ 0.75 |
layout_iterations |
None |
→ automatic: int(min(220, max(45, 14*√n))) with n = max(n_nodes, 1) when unset; explicit int always wins. |
positions |
None |
dict[int, tuple[float, ...]] — custom positions keyed by normalized node id (id of adapter node); partial dicts get layout for missing ids. |
validate_positions |
False |
If True, warn on unknown keys or short coordinates vs view. |
refine_tensor_labels |
True |
Extra passes to fit tensor names inside the node marker (2D or 3D); set False for speed. |
hover_labels |
False |
Hide labels until hover (interactive only). |
Defaults 0.08, 0.16, 0.2, 0.85, 0.75, 220 are also available as
PlotConfig.DEFAULT_NODE_RADIUS, DEFAULT_STUB_LENGTH, DEFAULT_SELF_LOOP_RADIUS,
DEFAULT_LINE_WIDTH_2D, DEFAULT_LINE_WIDTH_3D, DEFAULT_LAYOUT_ITERATIONS.
Public Python API
from tensor_network_viz import (
EngineName,
EinsumTrace,
PlotConfig,
ViewName,
einsum,
pair_tensor,
show_tensor_network,
)
Per-backend plotters (optional; same as show_tensor_network internals):
from tensor_network_viz.tensorkrowch import plot_tensorkrowch_network_2d, plot_tensorkrowch_network_3d
from tensor_network_viz.tensornetwork import plot_tensornetwork_network_2d, plot_tensornetwork_network_3d
from tensor_network_viz.quimb import plot_quimb_network_2d, plot_quimb_network_3d
from tensor_network_viz.tenpy import plot_tenpy_network_2d, plot_tenpy_network_3d
from tensor_network_viz.einsum_module import plot_einsum_network_2d, plot_einsum_network_3d
Accepted inputs (summary)
| Backend | Input |
|---|---|
| tensorkrowch | Network with nodes / leaf_nodes, or iterable of nodes |
| tensornetwork | Iterable of tensornetwork.Node |
| quimb | TensorNetwork or iterable of Tensor |
| tenpy | Finite/segment/infinite MPS, finite/infinite MPO |
| einsum | EinsumTrace or ordered iterable of pair_tensor |
Details, subgraph behavior, and Quimb hyperindex hubs are in docs/guide.md.
Example scripts
Runnable demos live under examples/. From the repo root with the right extra installed:
| Script | Purpose |
|---|---|
demo_cli.py |
Shared helpers (--hover-labels → PlotConfig(hover_labels=True)); imported by demos. |
tensorkrowch_demo.py |
MPS, MPO, PEPS, weird, disconnected; --from-list subset. |
tensornetwork_demo.py |
Same topologies with tensornetwork.Node. |
mera_tree_demo.py |
Large MERA + binary TTN stress test. |
cubic_peps_demo.py |
3D cubic PEPS lattice. |
quimb_demo.py |
Includes hyper-index example; --from-list. |
tenpy_demo.py |
Finite and infinite MPS/MPO. |
einsum_demo.py |
Auto trace vs manual pair_tensor. |
tn_tsp.py |
Larger TensorKrowch TSP construction. |
Catalog and one-liner commands: examples/README.md.
Backend notes
- Quimb hyper-indices (three or more tensors) are drawn via internal virtual hubs.
- Infinite TeNPy MPS/MPO use one periodic unit cell.
- The einsum backend visualizes the fundamental tensor network, not each intermediate contraction tensor.
- Passing a subset of nodes/tensors shows connections outside the subset as dangling legs.
Quick verification (reviewers)
python -m pip install -e ".[quimb]"
python examples/quimb_demo.py mps 2d --save quimb_mps.png --no-show
python -m pytest
Expect quimb_mps.png and all tests passing.
Troubleshooting (short)
| Symptom | What to try |
|---|---|
ModuleNotFoundError for quimb, tenpy, torch, … |
Install the matching extra, e.g. "tensor-network-visualization[quimb]". |
ValueError: Unsupported tensor network engine / view |
Use only listed engine / view literals (see above). |
| Blank or double figure in Jupyter | Assign fig, ax = show_tensor_network(...); avoid bare tuple as last line; try %matplotlib widget or inline. |
| Hover labels do nothing | Requires interactive backend and show path that runs a GUI or widget event loop; not for --no-show PNG only. |
| Huge graphs are slow | PlotConfig(refine_tensor_labels=False); optionally set layout_iterations explicitly. |
Full troubleshooting: docs/guide.md — Troubleshooting.
Documentation index
- docs/guide.md — Installation, backends,
PlotConfigrecipes, layout/draw behavior, architecture, extended troubleshooting. - CHANGELOG.md — Release notes by version.
- examples/README.md — CLI examples per script.
- THIRD_PARTY_LICENSES.md
Support and contributing
- Issues: GitHub Issues
- Contributing: CONTRIBUTING.md
- Code of Conduct: CODE_OF_CONDUCT.md
Development
python -m pip install -e ".[dev]"
ruff check .
ruff format .
pyright
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tensor_network_visualization-1.4.0.tar.gz.
File metadata
- Download URL: tensor_network_visualization-1.4.0.tar.gz
- Upload date:
- Size: 82.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27d8b5b84e1941bd5d1de86d3d005a2e930997e2a5ace45f028dad3f84adec12
|
|
| MD5 |
4ed0b8959082a97c08706d8a22f8c1e9
|
|
| BLAKE2b-256 |
1472dd939ea73b59042fc8160b4ceaeba94e8c431d24bdc5405c1e1632d1fa37
|
File details
Details for the file tensor_network_visualization-1.4.0-py3-none-any.whl.
File metadata
- Download URL: tensor_network_visualization-1.4.0-py3-none-any.whl
- Upload date:
- Size: 74.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f0cc4936a741bc9619cbe01a663b89cb715cfd9c4264fa04e7c69cb3480990b
|
|
| MD5 |
bbf97f0aa8e4adc915cf2a8ea9838497
|
|
| BLAKE2b-256 |
ef587c4ff0ca0eab30ff77848f6b6df1997d1f39e447ee62a8060af246a2ebb7
|