TensorCircuit:里面有什么?#
这部分文档主要面向希望了解更多后端原理与并深入研究代码库的高级用户和开发人员。
模块概述#
核心模块:
tensorcircuit.abstractcircuitandtensorcircuit.basecircuit: Hierarchical abstraction of circuit class.tensorcircuit.circuit: 核心对象tensorcircuit.circuit.Circuit.它支持使用蒙特卡洛轨迹方法的无噪声或有噪声的电路构建、仿真、表示和可视化。tensorcircuit.cons: 运行时的机器学习后端、数据类型和 contractor 设置。 我们为全局设置、使用函数装饰器的函数级别设置和使用with上下文管理器的上下文设置提供了三组设置方法.我们还在此模块中提供了定制的 contractor 基础设施。tensorcircuit.gates: 固定或参数化的量子门的定义,以及用于门的tensorcircuit.gates.GateF类。
后端无关抽象:
tensorcircuit.backends提供了一组后端 API 以及在 Numpy、Jax、TensorFlow 和 PyTorch 后端上的对应实现。这些后端继承自 TensorNetwork 包并且是高度定制的。
噪声模拟相关模块:
tensorcircuit.channels: 量子噪声通道的定义。tensorcircuit.densitymatrix: Referenced and highly efficient implementation oftc.DMCircuitclass, with similar set API oftc.Circuitwhile simulating the noise in the full form of the density matrix.tensorcircuit.noisemodel: The global noise configuration and circuit noisy method APIs
机器学习接口相关模块:
tensorcircuit.interfaces: Provide interfaces when quantum simulation backend is different from neural libraries. Currently include PyTorch, TensorFlow, NumPy and SciPy optimizer interfaces.tensorcircuit.keras: 提供 TensorFlow Keras 层,以及可及时编译函数的包装器,从 TensorFlow 端保存/加载.tensorcircuit.torchnn: Provide PyTorch nn Modules.
MPS 和 MPO 实用模块:
tensorcircuit.quantum: 提供矩阵乘积状态以及矩阵乘积算子的定义和类,我们还在这个模块中包含了各种量子物理和量子信息量。
基于 MPS 的模拟器模块:
tensorcircuit.mps_base: 来自 TensorNetwork 包的自定义并且即时编译/自动微分兼容的 MPS 类。tensorcircuit.mpscircuit:tensorcircuit.mpscircuit.MPSCircuit类具有与tc.Circuit,类似(但略有不同)的 API,其中仿真引擎基于 MPS TEBD。
支持模块:
tensorcircuit.simplify: 提供工具和实用函数以在真正收缩之前简化张量网络。tensorcircuit.experimental: 实验函数,不保证有持久且稳定的支持。tensorcircuit.utils: 一些与量子完全无关的通用工具函数。tensorcircuit.vis: 用于电路可视化的代码tensorcircuit.translation: 将电路对象转换为其他量子包中的电路对象。
Processing and error mitigation on sample results:
tensorcircuit.results: Provide tools to process count dict and to apply error mitigation.
Cloud quantum hardware access module:
tensorcircuit.cloud: Provide quantum cloud SDK that can access and program the real quantum hardware.tensorcircuit.compiler: Provide compiler chains to compile and transform quantum circuits.
电路操作的快捷方式和模板:
tensorcircuit.templates: 为期望或电路构建模式提供方便的快捷函数。
应用:
tensorcircuit.applications: 这里的大多数代码都没有维护并且被弃用了,使用风险自负。
注解
推荐阅读顺序——只阅读你关心的部分代码。如果您想了解代码库的概述,之后可以阅读 tc.circuit 后面的 tc.cons 和 tc.gates。
TensorCircuit 和 TensorNetwork 之间的关系#
TensorCircuit 与谷歌发布的 TensorNetwork 有很强的联系。由于 TensorNetwork 包的文档和教程很差,大多数时候,我们需要深入研究 TensorNetwork 的代码库来弄清楚发生了什么。换句话说,要阅读 TensorCircuit 代码库,可能需要经常参考 TensorNetwork 代码库。
在 TensorCircuit 内部,我们大量使用了 TensorNetwork 包中与 TensorNetwork 相关的 API,并通过继承和重写从 TensorNetwork 中高度定制了几个模块:
我们从 TensorNetwork 的后端实现我们自己的后端,方法是添加更多 API,并通过猴子补丁修复 TensorNetwork 在某些后端的实现中的许多错误。(上游是不活跃的,反馈不够灵敏)
我们将 /quantum 中的 TensorNetwork 代码借用到我们的
tc.quantum模块中,因为 TensorNetwork 没有__init__.py文件来导出这些 MPO 和 MPS 相关对象。当然,从那时起,我们已经取得了实质性的代码改进。我们借用 /matrixproductstates 中 TensorNetwork 的代码作为
tc.mps_base用于错误修复和即时编译/自动微分兼容性,以便我们更好地支持基于 MPS 的量子电路模拟器。
Relations of Circuit-like classes#
|- Circuit
|- BaseCircuit --- |
AbstractCircuit ---| |- DMCircuitReference --- DMCircuit
|- MPSCircuit
QuOperator/QuVector 和 MPO/MPS#
tensorcircuit.quantum.QuOperator, tensorcircuit.quantum.QuVector 和 tensorcircuit.quantum.QuAdjointVector 是从 TensorNetwork 包中采用的类。它们的行为类似于与其他成分交互时的矩阵/向量(列或行),而内部结构由张量网络维护以提高效率和紧凑性。
我们使用代码示例和相关的张量图来说明这些对象抽象。
注解
QuOperator 可以表达 MPO,QuVector 可以表达 MPS,但它们可以表达的不仅仅是这些固定的结构化张量网络。
import tensornetwork as tn
n1 = tn.Node(np.ones([2, 2, 2]))
n2 = tn.Node(np.ones([2, 2, 2]))
n3 = tn.Node(np.ones([2, 2]))
n1[2]^n2[2]
n2[1]^n3[0]
matrix = tc.quantum.QuOperator(out_edges=[n1[0], n2[0]], in_edges=[n1[1], n3[1]])
n4 = tn.Node(np.ones([2]))
n5 = tn.Node(np.ones([2]))
vector = tc.quantum.QuVector([n4[0], n5[0]])
nvector = matrix @ vector
assert type(nvector) == tc.quantum.QuVector
nvector.eval_matrix()
# array([[16.], [16.], [16.], [16.]])
请注意,在这个例子中,matrix 不是一个典型的 MPO,但仍然可以表示为 QuOperator。事实上,任何具有两组相同维度的悬边的张量网络都可以被视为 `` QuOperator``。QuVector 更加灵活,因为我们可以将所有悬空边视为向量维度。
还要注意 ^ 是如何被重载为 tn.connect 以连接 TensorNetwork 中不同节点之间的边。索引节点给出了节点的边,例如 n1[0] 意味着 节点 n1 的第一条边。
定义 QuOperator 的惯例是首先给出 ``out_edges``(矩阵的左索引或行索引),然后给出 ``in_edges``(矩阵的右索引或列索引)。边列表包含来自 TensorNetwork 库的边对象。
这样的 QuOperator/QuVector 抽象支持只能在矩阵/向量上进行的各种计算,例如 matmul (@)、伴随 (.adjoint())、标量乘法 (*)、张量乘积(|)和偏迹(.partial_trace(subsystems_to_trace_out))。要提取这些对象的矩阵信息,我们可以使用 .eval() 或 ``.eval_matrix() ``,前者保留了张量网络的形状信息,而后者给出了形状秩为2的矩阵表示。
Quantum Cloud SDK: Layerwise API design#
From lower level to higher level, a view of API layers invoking QPU calls
Vendor specific implementation of functional API in, e.g.,
tensorcircuit.cloud.tencentProvider agnostic functional lower level API for task/device management in
tensorcircuit.cloud.apisObject oriented abstraction for Provider/Device/Task in
tensorcircuit.cloud.abstractionUnified batch submission interface as standarized in
tensorcircuit.cloud.wrapper.batch_submit_template()Numerical and experimental unified all-in-one interface as
tensorcircuit.cloud.wrapper.batch_expectation_ps()Application level code with QPU calls built directly on
batch_expectation_psor more fancy algorithms can be built onbatch_submit_funcso that these algorithms can be reused as long as one functionbatch_submit_funcis defined for a given vendor (cheaper than defining a new provider from lower level).
注解
For compiler, error mitigation and results post-processing parts, they can be carefully designed to decouple with the QPU calls,
so they are separately implemented in tensorcircuit.compiler and tensorcircuit.results,
and they can be independently useful even without tc's cloud access.