Quick start
Define a small tensor network, search a contraction path, and verify its numerical result.
On this page
Running a search
After installing a Python package with Light / Heavy, run the three blocks below in order within one Python session to calculate A @ B @ C. A complete package loads its compiled library automatically; source builds first need library configuration. This example separates planning from numerical execution; the homepage demonstration uses a quantum circuit to illustrate tensors, contraction trees, and results.
1. Define the network and arrays
import numpy as np
inputs = [("a", "b"), ("b", "c"), ("c", "d")]
output = ("a", "d")
size_dict = {"a": 2, "b": 3, "c": 4, "d": 2}
a = np.arange(6, dtype=np.float64).reshape(2, 3)
b = np.arange(12, dtype=np.float64).reshape(3, 4)
c = np.arange(8, dtype=np.float64).reshape(4, 2)
arrays = [a, b, c]
inputs[i] lists indices in the axis order of arrays[i]. output selects retained indices and their order; size_dict must contain every used index and its dimension, with no missing or extra entries.
2. Optimize the contraction path
from arctn import arctn_plan
plan = arctn_plan(
inputs, output, size_dict,
preset="light", seed=0,
)
print("SSA path:", plan.ssa_path)
print("sliced indices:", plan.sliced_legs)
print("metrics:", dict(plan.metrics))
plan contains network structure, SSA path, and metrics, not numerical arrays. No slicing was requested, so plan.sliced_legs is empty. preset="light" selects the lower-overhead preset; the default is Heavy.
3. Execute and verify
result, info = plan.execute(
arrays, backend="native", return_info=True
)
np.testing.assert_allclose(result, a @ b @ c)
print(info["execution_backend"])
print(result)
native
[[ 324. 422.]
[1008. 1304.]]
execute() reuses the path without calling Light or Heavy again. backend="native" selects the ArcTN Rust CPU executor; NumPy computes the reference result for numerical verification.
Next steps
- Using Heavy — Inspect the path, objective, and search time.
- Slicing example — Bound intermediate tensor size.
- Saving and reusing paths — Save paths and slicing information; compile unsliced plans for repeated execution.
- Quimb example — Use an existing Circuit or TensorNetwork.