Interactive local editor for building tensor networks and generating Python code.
Project description
Tensor Network Editor
tensor-network-editor is a local-first Python package for building tensor networks visually, saving them as versioned JSON, and generating readable Python code for multiple backends.
It is meant for research, teaching, and experimentation workflows where you want a friendly editor without giving up plain Python objects, reproducible files, or offline use. The UI opens in your browser, but the whole session is served locally from your own machine.
Why this project
- Build tensor-network diagrams interactively in a local browser session.
- Save and reload designs as versioned JSON files.
- Generate Python code for
tensornetwork,quimb,tensorkrowch,einsum_numpy, andeinsum_torch. - Reconstruct a
NetworkSpecfrom supported generated Python exports when you need a code-to-spec round trip. - Insert built-in templates for MPS, MPO, PEPS, MERA, and binary-tree layouts.
- Tune template size, bond dimension, and physical dimension before inserting them.
- Add notes and groups so larger diagrams stay easier to understand.
- Inspect and edit manual contraction paths, with optional automatic suggestions through the planner extra.
- Use the project either as a Python library or from the
tensor-network-editorCLI.
Why it is useful in practice
- Local and offline-friendly. No cloud dependency, no CDN requirement, and no Node runtime needed for end users.
- Python-native output. The editor returns
NetworkSpecdata structures and generated Python code you can inspect, save, or post-process. - Backend-flexible. You can keep one abstract network design and target different Python ecosystems from it.
- Cross-platform by default. The project is tested on Windows and Linux with Python
3.11+.
Installation
The distribution name is tensor-network-editor. The import package is tensor_network_editor.
Install from PyPI
PowerShell:
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install -U pip
python -m pip install tensor-network-editor
Bash:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -U pip
python -m pip install tensor-network-editor
Optional extras:
python -m pip install "tensor-network-editor[quimb]"
python -m pip install "tensor-network-editor[tensornetwork]"
python -m pip install "tensor-network-editor[tensorkrowch]"
python -m pip install "tensor-network-editor[planner]"
Use backend extras when you want the generated code to run in the same environment without installing those libraries separately. The planner extra installs opt_einsum for automatic greedy contraction suggestions inside the editor.
You can combine extras when needed, for example:
python -m pip install "tensor-network-editor[quimb,planner]"
Install from source
If you want the current repository version instead of the published package:
python -m pip install .
For development work:
python -m pip install -e ".[dev]"
Quick start
Launch the editor from the CLI
Start a local editing session:
tensor-network-editor --engine einsum_numpy
Open an existing design and write generated code to a file when you confirm the session:
tensor-network-editor --load my_network.json --engine quimb --save-code generated_network.py
Useful flags:
--print-codeprints the generated code to standard output.--no-browserstarts the local server without opening the browser automatically.
Launch the editor from Python
from tensor_network_editor import EngineName, launch_tensor_network_editor
result = launch_tensor_network_editor(default_engine=EngineName.EINSUM_NUMPY)
if result is None:
print("Editor cancelled.")
else:
print(f"Design name: {result.spec.name}")
if result.codegen is not None:
print(result.codegen.code)
The editor blocks until the user presses Done or Cancel. On Done, it returns the abstract NetworkSpec together with the generated code for the selected engine.
Use it as a library
You can also skip the UI and work directly with saved network designs:
from tensor_network_editor import (
CodeGenerationError,
EngineName,
generate_code,
load_spec,
)
spec = load_spec("my_network.json")
try:
result = generate_code(
spec,
engine=EngineName.QUIMB,
path="generated_network.py",
)
except CodeGenerationError as exc:
print(f"Cannot generate this backend: {exc}")
else:
print(result.code)
Main public entry points:
launch_tensor_network_editor(...) -> EditorResult | Nonegenerate_code(spec, engine=..., collection_format=..., path=...) -> CodegenResultsave_spec(spec, path) -> Noneload_spec(path) -> NetworkSpecload_spec_from_python_code(code) -> NetworkSpec
If the NetworkSpec includes a saved manual contraction_plan, generated code
now follows that plan directly instead of collapsing everything into one final
contraction. Complete plans emit a final result. Partial plans emit named
intermediates and a remaining_operands mapping so you can continue from that
state manually.
For einsum_numpy and einsum_torch, partial plans also emit
remaining_operand_labels, which makes the surviving index labels easier to
inspect when you continue the contraction manually.
load_spec(path) accepts saved JSON designs and supported generated .py
exports. If you already have the generated source code in memory, use
load_spec_from_python_code(code) directly.
The package also exposes the main data structures at the top level, including
NetworkSpec, TensorSpec, IndexSpec, EdgeSpec, GroupSpec,
CanvasNoteSpec, ContractionPlanSpec, ContractionStepSpec,
ContractionOperandLayoutSpec, ContractionViewSnapshotSpec, EngineName,
TensorCollectionFormat, CodegenResult, and EditorResult.
Templates and planner
Built-in templates:
MPSMPOPEPSMERABinary Tree
Template controls let you adjust:
- graph size
- bond dimension
- physical dimension
The graph-size control label depends on the template:
MPSandMPOuseSitesPEPSusesSide lengthMERAandBinary TreeuseDepth
The planner tools help with contraction-order work:
- Manual contraction paths are available directly in the editor.
- Automatic greedy suggestions are available when the optional
plannerextra is installed. - Contraction summaries include useful size and cost estimates such as FLOP, MAC, and intermediate sizes.
- Generated code respects the saved manual plan when one is present.
Supported code-generation targets
Available engine names:
tensornetworkquimbtensorkrowcheinsum_numpyeinsum_torch
Generated code can organize created tensors in three collection formats:
listmatrixdict
In practice:
einsum_numpyandeinsum_torchare useful when you want lightweight generated code.tensornetwork,quimb, andtensorkrowchare good fits when you already work in those ecosystems.- The abstract JSON save format stays backend-agnostic, so you can reopen the same design and generate for a different engine later.
tensornetworkandquimbcan export manual contraction plans step by step, including outer products.einsum_numpyandeinsum_torchexport manual plans as severaleinsum(...)calls with intermediate tensors.tensorkrowchalso exports manual plans step by step, but manual outer-product steps are rejected becausecontract_between(...)cannot represent them safely there.
From Python, choose the layout with
TensorCollectionFormat.LIST, TensorCollectionFormat.MATRIX, or
TensorCollectionFormat.DICT through generate_code(...) or
launch_tensor_network_editor(...).
Save format
Designs are stored as plain JSON with a schema wrapper:
{
"schema_version": 3,
"network": {
"...": "..."
}
}
That makes saved files easy to version, inspect, and exchange inside a normal Python workflow.
When a saved design contains a manual contraction plan, the plan can also carry
view_snapshots. Those snapshots store operand positions and sizes for the
contraction-scene UI through ContractionOperandLayoutSpec and
ContractionViewSnapshotSpec.
Development
Set up the project in editable mode:
PowerShell:
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install -U pip
python -m pip install -e ".[dev]"
Bash:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -U pip
python -m pip install -e ".[dev]"
Useful checks:
python -m ruff check .
python -m ruff format .
python -m mypy
python -m pyright
python -m pytest
python -m build
python -m twine check dist/*
Cleanup scripts:
- Windows:
.\scripts\clean.bat - Linux:
./scripts/clean.sh
Bundled third-party notices for redistributed frontend assets are tracked in THIRD_PARTY_LICENSES.
Current limits
- Hyperedges are not supported yet.
- Real tensor data editing is not supported yet; generated tensors are initialized to zeros.
- TenPy code generation is not included in the current release.
- The main supported experience today is the local browser editor.
- Manual outer-product steps cannot be exported to
tensorkrowch;generate_code(...)raisesCodeGenerationErrorfor that backend-specific case.
Project links
- Documentation: docs/README.md
- Source code: github.com/DOKOS-TAYOS/Tensor-Network-Editor
- Changelog: CHANGELOG.md
- Example script: examples/basic_usage.py
- Issue tracker: github.com/DOKOS-TAYOS/Tensor-Network-Editor/issues
License
This project is distributed under the MIT License. See LICENSE for details.
Project details
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_editor-0.2.0.tar.gz.
File metadata
- Download URL: tensor_network_editor-0.2.0.tar.gz
- Upload date:
- Size: 235.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a02e52d3a4adec484722ac58e0062693d92cd0630ed3de8fa5a9688f5a3e571
|
|
| MD5 |
fb8f228e3860cf2d9ba8568c0f69e89c
|
|
| BLAKE2b-256 |
85cd472257ab08789dc0e56282be3f189991a30906d2fe14a315e5722695deb3
|
File details
Details for the file tensor_network_editor-0.2.0-py3-none-any.whl.
File metadata
- Download URL: tensor_network_editor-0.2.0-py3-none-any.whl
- Upload date:
- Size: 264.0 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 |
c179d068c3d8970382609166483bac8818c9701e5abe7570507079d030ee6a5a
|
|
| MD5 |
87d927fc7245127fb332fdba9dbfab59
|
|
| BLAKE2b-256 |
0df038c44de6928acd4ef319f4dafc6134cfe0e1f6bf0f8bb39b28d3aea004ac
|