---
title: "快速开始"
description: "定义一个小型张量网络，搜索收缩路径，并核对数值结果。"
eyebrow: "开始使用"
---

## 执行搜索 {#choose-route}

[安装包含 Light / Heavy 的 Python 包](/docs/installation#python)后，在同一个 Python 会话中依次运行下面三段代码，计算 `A @ B @ C`。完整安装包会自动加载编译库；源码构建版本需要先完成[编译库配置](/docs/installation#engine)。这里分别进行收缩序优化和数值执行；[首页演示](/#contraction-example)则用一个量子线路展示张量转换、收缩树与数值结果。

## 1. 定义网络与数组 {#network}

```python
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]` 按 `arrays[i]` 的轴顺序列出 index。`output` 决定结果保留哪些 index 及其轴顺序；`size_dict` 必须列出所有使用到的 index 及其维度，不能缺少或多出条目。

## 2. 执行收缩序优化 {#plan}

```python
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` 包含网络结构、SSA path 和指标，不包含数值数组。这里没有请求切片，因此 `plan.sliced_legs` 为空。`preset="light"` 选择规划开销较低的预设；默认值是 Heavy。

## 3. 执行并核对结果 {#execute}

```python
result, info = plan.execute(
    arrays, backend="native", return_info=True
)

np.testing.assert_allclose(result, a @ b @ c)
print(info["execution_backend"])
print(result)
```

```text
native
[[ 324.  422.]
 [1008. 1304.]]
```

`execute()` 复用已有路径，不会重新调用 Light 或 Heavy。这里的 `backend="native"` 使用 ArcTN 的 Rust CPU 执行器；NumPy 计算参考结果，用于数值核对。

## 下一步 {#next}

- [使用 Heavy](/docs/tutorial-heavy) — 读取收缩路径、目标值与搜索耗时。
- [切片示例](/docs/tutorial-slicing) — 限制中间张量大小。
- [保存与复用收缩序](/docs/tutorial-reuse) — 保存收缩序与切片信息；无切片时可编译后重复执行。
- [Quimb 示例](/docs/tutorial-quimb) — 使用已有的 Circuit 或 TensorNetwork。
