1
+ {
2
+ "cells" : [
3
+ {
4
+ "cell_type" : " markdown" ,
5
+ "id" : " bc07c2b3" ,
6
+ "metadata" : {},
7
+ "source" : [
8
+ " # 定制收缩"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type" : " markdown" ,
13
+ "id" : " 694cc0e0" ,
14
+ "metadata" : {},
15
+ "source" : [
16
+ " ## 概述\n " ,
17
+ " \n " ,
18
+ " 如果模拟电路的量子比特数很大,我们建议用户尝试自定义收缩设置,而不是使用贪婪的默认设置。"
19
+ ]
20
+ },
21
+ {
22
+ "cell_type" : " markdown" ,
23
+ "id" : " 918c848d" ,
24
+ "metadata" : {},
25
+ "source" : [
26
+ " ## 设置\n " ,
27
+ " \n " ,
28
+ " cotengra请参考[安装文档](https://cotengra.readthedocs.io/en/latest/installation.html),由于没有上传到PyPI,所以无法通过\n " ,
29
+ " pip install 简单获取。最简单的安装方式是 ``pip install -U git+https://github.com/jcmgray/cotengra.git``。"
30
+ ]
31
+ },
32
+ {
33
+ "cell_type" : " code" ,
34
+ "execution_count" : 3 ,
35
+ "id" : " 2ae27e57" ,
36
+ "metadata" : {},
37
+ "outputs" : [],
38
+ "source" : [
39
+ " import tensorcircuit as tc\n " ,
40
+ " import numpy as np\n " ,
41
+ " import cotengra as ctg"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type" : " markdown" ,
46
+ "id" : " 63d88675" ,
47
+ "metadata" : {},
48
+ "source" : [
49
+ " 我们使用以下示例作为收缩的测试平台,真正的 contractor 是为 ``Circuit.expectation`` API 调用的。\n " ,
50
+ " 收缩有两个阶段,第一个是收缩路径搜索,用于在空间和时间方面找到更好的收缩路径。第二阶段是真正的收缩,使用 ML 后端 API 调用矩阵乘法。\n " ,
51
+ " 在本说明中,我们关注第一阶段的性能,并且可以使用任何类型的 [opt-einsum 兼容路径求解器](https://optimized-einsum.readthedocs.io/en/stable/custom_paths.html)\n " ,
52
+ " 自定义收缩路径求解器。"
53
+ ]
54
+ },
55
+ {
56
+ "cell_type" : " code" ,
57
+ "execution_count" : 4 ,
58
+ "id" : " bc3f5c65" ,
59
+ "metadata" : {},
60
+ "outputs" : [],
61
+ "source" : [
62
+ " def testbed():\n " ,
63
+ " n = 40\n " ,
64
+ " d = 6\n " ,
65
+ " param = K.ones([2 * d, n])\n " ,
66
+ " c = tc.Circuit(n)\n " ,
67
+ " c = tc.templates.blocks.example_block(c, param, nlayers=d, is_split=True)\n " ,
68
+ " # 用 SVD 分解对两个量子比特门进行分割和截断\n " ,
69
+ " return c.expectation_ps(z=[n // 2], reuse=False)"
70
+ ]
71
+ },
72
+ {
73
+ "cell_type" : " markdown" ,
74
+ "id" : " 17e9c8bc" ,
75
+ "metadata" : {},
76
+ "source" : [
77
+ " opt-einsum 提供了几个收缩优化器,并随 TensorNetwork 包一起提供。由于 TensorCircuit 建立在 TensorNetwork 之上,我们可以使用这些简单的收缩优化器。\n " ,
78
+ " 尽管对于任何中等系统,只有贪婪优化器有效,但其他优化器具有指数缩放并且在电路模拟场景中失败。\n " ,
79
+ " \n " ,
80
+ " 在本说明中,我们始终为收缩系统设置 ``contraction_info=True``(默认为 ``False``),它将打印收缩信息摘要,包括收缩大小、触发器和写入。\n " ,
81
+ " 有关这些指标的定义,另请参阅 cotengra 文档和 [相应论文](https://quantum-journal.org/papers/q-2021-03-15-410/)。\n " ,
82
+ " \n " ,
83
+ " 衡量收缩路径质量的指标包括\n " ,
84
+ " \n " ,
85
+ " * **FLOPs**:通过给定路径收缩张量网络时涉及的所有矩阵乘法所需的计算操作总数。该指标表征了总的模拟时间。\n " ,
86
+ " \n " ,
87
+ " * **WRITE**:在收缩期间计算的所有张量(包括中间张量)的总大小(元素数量)。\n " ,
88
+ " \n " ,
89
+ " * **SIZE**:存储在内存中的最大中间张量的大小。\n " ,
90
+ " \n " ,
91
+ " 由于 TensorCircuit 中的模拟启用了 AD,所有中间结果都需要缓存和跟踪,因此写入更相关的空间成本指标而不是大小。\n " ,
92
+ " \n " ,
93
+ " 此外,我们将在 ``set_contractor`` 中启用 ``debug_level=2``(不要在实际计算中使用此选项!)通过启用此选项,收缩的第二阶段,即真正的收缩,将不会发生。\n " ,
94
+ " 我们可以关注收缩路径信息,它展示了不同定制 contractor 之间的差异。"
95
+ ]
96
+ },
97
+ {
98
+ "cell_type" : " code" ,
99
+ "execution_count" : 5 ,
100
+ "id" : " 64647063" ,
101
+ "metadata" : {},
102
+ "outputs" : [
103
+ {
104
+ "name" : " stdout" ,
105
+ "output_type" : " stream" ,
106
+ "text" : [
107
+ " ------ contraction cost summary ------\n " ,
108
+ " log10[FLOPs]: 12.393 log2[SIZE]: 30 log2[WRITE]: 35.125\n "
109
+ ]
110
+ },
111
+ {
112
+ "data" : {
113
+ "text/plain" : [
114
+ " <tf.Tensor: shape=(), dtype=complex64, numpy=0j>"
115
+ ]
116
+ },
117
+ "execution_count" : 5 ,
118
+ "metadata" : {},
119
+ "output_type" : " execute_result"
120
+ }
121
+ ],
122
+ "source" : [
123
+ " tc.set_contractor(\" greedy\" , debug_level=2, contraction_info=True)\n " ,
124
+ " # 默认 contractor\n " ,
125
+ " testbed()"
126
+ ]
127
+ },
128
+ {
129
+ "cell_type" : " markdown" ,
130
+ "id" : " 10d590ec" ,
131
+ "metadata" : {},
132
+ "source" : [
133
+ " **cotengra 优化器**:有关超参数调整,请参阅[文档](https://cotengra.readthedocs.io/en/latest/advanced.html)。"
134
+ ]
135
+ },
136
+ {
137
+ "cell_type" : " code" ,
138
+ "execution_count" : 7 ,
139
+ "id" : " a0075260" ,
140
+ "metadata" : {},
141
+ "outputs" : [
142
+ {
143
+ "name" : " stderr" ,
144
+ "output_type" : " stream" ,
145
+ "text" : [
146
+ " log2[SIZE]: 15.00 log10[FLOPs]: 7.56: 45%|██████████████████▊ | 458/1024 [02:03<02:32, 3.70it/s]\n "
147
+ ]
148
+ },
149
+ {
150
+ "name" : " stdout" ,
151
+ "output_type" : " stream" ,
152
+ "text" : [
153
+ " ------ contraction cost summary ------\n " ,
154
+ " log10[FLOPs]: 7.565 log2[SIZE]: 15 log2[WRITE]: 19.192\n "
155
+ ]
156
+ },
157
+ {
158
+ "data" : {
159
+ "text/plain" : [
160
+ " <tf.Tensor: shape=(), dtype=complex64, numpy=0j>"
161
+ ]
162
+ },
163
+ "execution_count" : 7 ,
164
+ "metadata" : {},
165
+ "output_type" : " execute_result"
166
+ }
167
+ ],
168
+ "source" : [
169
+ " opt = ctg.ReusableHyperOptimizer(\n " ,
170
+ " methods=[\" greedy\" , \" kahypar\" ],\n " ,
171
+ " parallel=True,\n " ,
172
+ " minimize=\" write\" ,\n " ,
173
+ " max_time=120,\n " ,
174
+ " max_repeats=1024,\n " ,
175
+ " progbar=True,\n " ,
176
+ " )\n " ,
177
+ " # 注意:目前,parallel 仅适用于较新版本的 python 中的 “ray”\n " ,
178
+ " tc.set_contractor(\n " ,
179
+ " \" custom\" , optimizer=opt, preprocessing=True, contraction_info=True, debug_level=2\n " ,
180
+ " )\n " ,
181
+ " # opt-einsum 兼容函数接口作为优化器的参数传递\\\n " ,
182
+ " # 还要注意 preprocessing=True 如何将单个量子比特门合并到相邻的两个量子比特门中\n " ,
183
+ " testbed()"
184
+ ]
185
+ },
186
+ {
187
+ "cell_type" : " markdown" ,
188
+ "id" : " 871115c1" ,
189
+ "metadata" : {},
190
+ "source" : [
191
+ " 我们甚至可以在路径搜索之后包含收缩重新配置,这进一步大大提高了收缩路径的空间效率。"
192
+ ]
193
+ },
194
+ {
195
+ "cell_type" : " code" ,
196
+ "execution_count" : 8 ,
197
+ "id" : " 7c625596" ,
198
+ "metadata" : {},
199
+ "outputs" : [
200
+ {
201
+ "name" : " stderr" ,
202
+ "output_type" : " stream" ,
203
+ "text" : [
204
+ " log2[SIZE]: 15.00 log10[FLOPs]: 7.46: 32%|█████████████▍ | 329/1024 [02:00<04:13, 2.74it/s]\n " ,
205
+ " log2[SIZE]: 14.00 log10[FLOPs]: 7.02: 100%|█████████████████████████████████████████████| 20/20 [01:05<00:00, 3.30s/it]\n "
206
+ ]
207
+ },
208
+ {
209
+ "name" : " stdout" ,
210
+ "output_type" : " stream" ,
211
+ "text" : [
212
+ " ------ contraction cost summary ------\n " ,
213
+ " log10[FLOPs]: 7.021 log2[SIZE]: 14 log2[WRITE]: 19.953\n "
214
+ ]
215
+ },
216
+ {
217
+ "data" : {
218
+ "text/plain" : [
219
+ " <tf.Tensor: shape=(), dtype=complex64, numpy=0j>"
220
+ ]
221
+ },
222
+ "execution_count" : 8 ,
223
+ "metadata" : {},
224
+ "output_type" : " execute_result"
225
+ }
226
+ ],
227
+ "source" : [
228
+ " opt = ctg.ReusableHyperOptimizer(\n " ,
229
+ " minimize=\" combo\" ,\n " ,
230
+ " max_repeats=1024,\n " ,
231
+ " max_time=120,\n " ,
232
+ " progbar=True,\n " ,
233
+ " )\n " ,
234
+ " \n " ,
235
+ " \n " ,
236
+ " def opt_reconf(inputs, output, size, **kws):\n " ,
237
+ " tree = opt.search(inputs, output, size)\n " ,
238
+ " tree_r = tree.subtree_reconfigure_forest(\n " ,
239
+ " progbar=True, num_trees=10, num_restarts=20, subtree_weight_what=(\" size\" ,)\n " ,
240
+ " )\n " ,
241
+ " return tree_r.get_path()\n " ,
242
+ " \n " ,
243
+ " \n " ,
244
+ " # subtree_reconfigure_forest 还有一个默认的 parallel=True 选项,\n " ,
245
+ " # 对于上面的较新版本的 python,这只能设置为 “ray”\n " ,
246
+ " # 请注意不同版本的 cotengra 在最后一行中如何破坏 API:get_path 或 pat\n " ,
247
+ " # 用户可能需要更改 API 以使示例工作\n " ,
248
+ " \n " ,
249
+ " tc.set_contractor(\n " ,
250
+ " \" custom\" ,\n " ,
251
+ " optimizer=opt_reconf,\n " ,
252
+ " contraction_info=True,\n " ,
253
+ " preprocessing=True,\n " ,
254
+ " debug_level=2,\n " ,
255
+ " )\n " ,
256
+ " testbed()"
257
+ ]
258
+ }
259
+ ],
260
+ "metadata" : {
261
+ "kernelspec" : {
262
+ "display_name" : " Python 3 (ipykernel)" ,
263
+ "language" : " python" ,
264
+ "name" : " python3"
265
+ },
266
+ "language_info" : {
267
+ "codemirror_mode" : {
268
+ "name" : " ipython" ,
269
+ "version" : 3
270
+ },
271
+ "file_extension" : " .py" ,
272
+ "mimetype" : " text/x-python" ,
273
+ "name" : " python" ,
274
+ "nbconvert_exporter" : " python" ,
275
+ "pygments_lexer" : " ipython3" ,
276
+ "version" : " 3.8.0"
277
+ }
278
+ },
279
+ "nbformat" : 4 ,
280
+ "nbformat_minor" : 5
281
+ }
0 commit comments