Skip to content

Commit a0948b4

Browse files
committed
Update classical_shadows.ipynb, add the jit example when calculate the entanglement entropy.
1 parent 9de1501 commit a0948b4

File tree

1 file changed

+116
-72
lines changed

1 file changed

+116
-72
lines changed

docs/source/tutorials/classical_shadows.ipynb

+116-72
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,20 @@
9292
},
9393
{
9494
"cell_type": "code",
95-
"execution_count": 8,
95+
"execution_count": 2,
9696
"metadata": {
9797
"collapsed": true,
9898
"ExecuteTime": {
99-
"end_time": "2023-08-08T12:07:59.787548200Z",
100-
"start_time": "2023-08-08T12:07:59.689077500Z"
99+
"end_time": "2023-08-09T02:30:37.665201100Z",
100+
"start_time": "2023-08-09T02:30:37.638636Z"
101101
}
102102
},
103103
"outputs": [
104104
{
105105
"data": {
106106
"text/plain": "('complex128', 'float64')"
107107
},
108-
"execution_count": 8,
108+
"execution_count": 2,
109109
"metadata": {},
110110
"output_type": "execute_result"
111111
}
@@ -149,7 +149,7 @@
149149
},
150150
{
151151
"cell_type": "code",
152-
"execution_count": 9,
152+
"execution_count": 3,
153153
"outputs": [
154154
{
155155
"name": "stdout",
@@ -182,8 +182,8 @@
182182
"metadata": {
183183
"collapsed": false,
184184
"ExecuteTime": {
185-
"end_time": "2023-08-08T12:07:59.787548200Z",
186-
"start_time": "2023-08-08T12:07:59.701762Z"
185+
"end_time": "2023-08-09T02:30:41.331246200Z",
186+
"start_time": "2023-08-09T02:30:41.324070Z"
187187
}
188188
}
189189
},
@@ -198,7 +198,7 @@
198198
},
199199
{
200200
"cell_type": "code",
201-
"execution_count": 10,
201+
"execution_count": 5,
202202
"outputs": [],
203203
"source": [
204204
"nlayers = 10\n",
@@ -218,8 +218,8 @@
218218
"metadata": {
219219
"collapsed": false,
220220
"ExecuteTime": {
221-
"end_time": "2023-08-08T12:07:59.893829700Z",
222-
"start_time": "2023-08-08T12:07:59.708422100Z"
221+
"end_time": "2023-08-09T02:30:48.458936400Z",
222+
"start_time": "2023-08-09T02:30:48.245303100Z"
223223
}
224224
}
225225
},
@@ -234,7 +234,7 @@
234234
},
235235
{
236236
"cell_type": "code",
237-
"execution_count": 11,
237+
"execution_count": 6,
238238
"outputs": [],
239239
"source": [
240240
"pauli_strings = BK.convert_to_tensor(np.random.randint(1, 4, size=(nps, n)))\n",
@@ -243,8 +243,8 @@
243243
"metadata": {
244244
"collapsed": false,
245245
"ExecuteTime": {
246-
"end_time": "2023-08-08T12:07:59.919435300Z",
247-
"start_time": "2023-08-08T12:07:59.908781200Z"
246+
"end_time": "2023-08-09T02:30:57.734884200Z",
247+
"start_time": "2023-08-09T02:30:57.692043Z"
248248
}
249249
}
250250
},
@@ -259,13 +259,13 @@
259259
},
260260
{
261261
"cell_type": "code",
262-
"execution_count": 12,
262+
"execution_count": 7,
263263
"outputs": [
264264
{
265265
"name": "stdout",
266266
"output_type": "stream",
267267
"text": [
268-
"(97920, 5, 8, 2, 2)\n"
268+
"shape of snapshot states: (97920, 5, 8, 2, 2)\n"
269269
]
270270
}
271271
],
@@ -278,13 +278,13 @@
278278
"\n",
279279
"\n",
280280
"ss_states = shadow_ss(psi, pauli_strings, status) # jit is not necessary here\n",
281-
"print(ss_states.shape)"
281+
"print(\"shape of snapshot states:\", ss_states.shape)"
282282
],
283283
"metadata": {
284284
"collapsed": false,
285285
"ExecuteTime": {
286-
"end_time": "2023-08-08T12:08:03.113557900Z",
287-
"start_time": "2023-08-08T12:08:00.167642400Z"
286+
"end_time": "2023-08-09T02:32:13.230300600Z",
287+
"start_time": "2023-08-09T02:32:10.257220400Z"
288288
}
289289
}
290290
},
@@ -509,43 +509,69 @@
509509
{
510510
"cell_type": "markdown",
511511
"source": [
512-
"We can also use classical shadows to calculate entanglement entropy. `entropy_shadow` first reconstructs the reduced density matrix, then solves the eigenvalues and finally calculates the entanglement entropy from non-negative eigenvalues. Since the time and space complexity of reconstructing the density matrix is exponential with respect to the system size, this method is only efficient when the reduced system size is constant. `entropy_shadow` is jitable, but since we only calculate the entanglement entropy once here, it is not wrapped."
512+
"We can also use classical shadows to calculate entanglement entropy. `entropy_shadow` first reconstructs the reduced density matrix, then solves the eigenvalues and finally calculates the entanglement entropy from non-negative eigenvalues. Since the time and space complexity of reconstructing the density matrix is exponential with respect to the system size, this method is only efficient when the reduced system size is constant. `entropy_shadow` is jitable, but it will only accelerate when the reduced sub systems have the same shape."
513513
],
514514
"metadata": {
515515
"collapsed": false
516516
}
517517
},
518518
{
519519
"cell_type": "code",
520-
"execution_count": 15,
520+
"execution_count": 9,
521521
"outputs": [
522522
{
523523
"name": "stdout",
524524
"output_type": "stream",
525525
"text": [
526-
"exact: 1.326616283813521\n",
527-
"shadow entropy: 1.328468470127564\n"
526+
"sub: [1, 4]\ttime: 0.17648577690124512\texact: 1.3640689598449087\tshadow entropy: 1.3640570121867708\n",
527+
"sub: [2, 7]\ttime: 0.039922475814819336\texact: 1.3278771521680572\tshadow entropy: 1.329313690500214\n",
528+
"sub: [3, 6]\ttime: 0.03967714309692383\texact: 1.3592729872413634\tshadow entropy: 1.358544872130723\n",
529+
"sub: [0, 5]\ttime: 0.039933204650878906\texact: 1.3431941549633575\tshadow entropy: 1.3434161532641598\n",
530+
"sub: [7, 0]\ttime: 0.04005789756774902\texact: 1.339905097299989\tshadow entropy: 1.3412301752249673\n",
531+
"sub: [1, 4, 7]\ttime: 0.25884079933166504\texact: 1.8441504433475884\tshadow entropy: 1.8320316624636186\n",
532+
"sub: [0, 3, 6]\ttime: 0.11394333839416504\texact: 1.8720104442154792\tshadow entropy: 1.8695589673614292\n",
533+
"sub: [5, 4, 2]\ttime: 0.11730098724365234\texact: 1.863627580838848\tshadow entropy: 1.860649003850145\n",
534+
"sub: [7, 2, 5]\ttime: 0.1133573055267334\texact: 1.8446309930730256\tshadow entropy: 1.8449136080697077\n",
535+
"sub: [0, 1, 2]\ttime: 0.11353397369384766\texact: 1.8514608539755246\tshadow entropy: 1.8518607742823925\n"
528536
]
529537
}
530538
],
531539
"source": [
532-
"sub = [1, 4]\n",
540+
"subs = [\n",
541+
" [1, 4],\n",
542+
" [2, 7],\n",
543+
" [3, 6],\n",
544+
" [0, 5],\n",
545+
" [7, 0],\n",
546+
" [1, 4, 7],\n",
547+
" [0, 3, 6],\n",
548+
" [5, 4, 2],\n",
549+
" [7, 2, 5],\n",
550+
" [0, 1, 2],\n",
551+
"]\n",
533552
"\n",
534-
"exact_rdm = tc.quantum.reduced_density_matrix(\n",
535-
" psi, cut=[i for i in range(n) if i not in sub]\n",
536-
")\n",
537-
"exact_ent = tc.quantum.renyi_entropy(exact_rdm, k=2)\n",
538553
"\n",
539-
"ent = shadows.entropy_shadow(ss_states, sub=sub, alpha=2)\n",
554+
"@BK.jit\n",
555+
"def shadow_ent(snapshots_states, sub, alpha=2):\n",
556+
" return shadows.entropy_shadow(snapshots_states, sub=sub, alpha=alpha)\n",
557+
"\n",
540558
"\n",
541-
"print(\"exact:\", exact_ent)\n",
542-
"print(\"shadow entropy:\", ent)"
559+
"for sub in subs:\n",
560+
" exact_rdm = tc.quantum.reduced_density_matrix(\n",
561+
" psi, cut=[i for i in range(n) if i not in sub]\n",
562+
" )\n",
563+
" exact_ent = tc.quantum.renyi_entropy(exact_rdm, k=2)\n",
564+
"\n",
565+
" t0 = time.time()\n",
566+
" ent = shadow_ent(ss_states, sub)\n",
567+
" t = time.time()\n",
568+
" print(f\"sub: {sub}\\ttime: {t - t0}\\texact: {exact_ent}\\tshadow entropy: {ent}\")"
543569
],
544570
"metadata": {
545571
"collapsed": false,
546572
"ExecuteTime": {
547-
"end_time": "2023-08-08T12:10:35.850421Z",
548-
"start_time": "2023-08-08T12:10:35.292528800Z"
573+
"end_time": "2023-08-09T02:33:00.022682Z",
574+
"start_time": "2023-08-09T02:32:58.952538800Z"
549575
}
550576
}
551577
},
@@ -567,13 +593,22 @@
567593
},
568594
{
569595
"cell_type": "code",
570-
"execution_count": 16,
596+
"execution_count": 10,
571597
"outputs": [
572598
{
573599
"name": "stdout",
574600
"output_type": "stream",
575601
"text": [
576-
"shadow entropy 2: 1.3039560994783876\n"
602+
"sub: [1, 4]\ttime: 4.649497032165527\tshadow entropy 2: 1.3357892188426321\n",
603+
"sub: [2, 7]\ttime: 3.7085328102111816\tshadow entropy 2: 1.2962030413604921\n",
604+
"sub: [3, 6]\ttime: 3.7152998447418213\tshadow entropy 2: 1.3329300192325162\n",
605+
"sub: [0, 5]\ttime: 3.7029261589050293\tshadow entropy 2: 1.3142350322893441\n",
606+
"sub: [7, 0]\ttime: 3.701401472091675\tshadow entropy 2: 1.3078303019333588\n",
607+
"sub: [1, 4, 7]\ttime: 4.517441034317017\tshadow entropy 2: 1.7488599465846058\n",
608+
"sub: [0, 3, 6]\ttime: 3.7792038917541504\tshadow entropy 2: 1.7752035184515997\n",
609+
"sub: [5, 4, 2]\ttime: 3.8193397521972656\tshadow entropy 2: 1.766904637600782\n",
610+
"sub: [7, 2, 5]\ttime: 3.86617374420166\tshadow entropy 2: 1.7459976573206142\n",
611+
"sub: [0, 1, 2]\ttime: 3.7713708877563477\tshadow entropy 2: 1.747717244419407\n"
577612
]
578613
}
579614
],
@@ -584,15 +619,20 @@
584619
"status = BK.convert_to_tensor(np.random.rand(nps, r))\n",
585620
"\n",
586621
"snapshots = shadows.shadow_snapshots(psi, pauli_strings, status, measurement_only=True)\n",
587-
"ent2 = shadows.renyi_entropy_2(snapshots, sub)\n",
588622
"\n",
589-
"print(\"shadow entropy 2:\", ent2)"
623+
"t0 = time.time()\n",
624+
"for sub in subs:\n",
625+
" ent2 = shadows.renyi_entropy_2(snapshots, sub)\n",
626+
"\n",
627+
" t = time.time()\n",
628+
" print(f\"sub: {sub}\\ttime: {t - t0}\\tshadow entropy 2: {ent2}\")\n",
629+
" t0 = t"
590630
],
591631
"metadata": {
592632
"collapsed": false,
593633
"ExecuteTime": {
594-
"end_time": "2023-08-08T12:10:41.738126600Z",
595-
"start_time": "2023-08-08T12:10:35.851060800Z"
634+
"end_time": "2023-08-09T02:33:55.215104800Z",
635+
"start_time": "2023-08-09T02:33:14.638433400Z"
596636
}
597637
}
598638
},
@@ -608,15 +648,15 @@
608648
{
609649
"cell_type": "markdown",
610650
"source": [
611-
"We can use `global_shadow_state`, `global_shadow_state1` or `global_shadow_state2` to reconstruct the density matrix. These three functions use different methods, but the results are exactly the same and all of them are jitable. In specific, `global_shadow_state` uses `kron` and is recommended, the other two use `einsum`."
651+
"We can use `global_shadow_state`, `global_shadow_state1` or `global_shadow_state2` to reconstruct the density matrix. These three functions use different methods, but the results are exactly the same. All functions are jitable, but since we only use each of them once here, they are not wrapped. In terms of implementation details, `global_shadow_state` uses `kron` and is recommended, the other two use `einsum`."
612652
],
613653
"metadata": {
614654
"collapsed": false
615655
}
616656
},
617657
{
618658
"cell_type": "code",
619-
"execution_count": 17,
659+
"execution_count": 8,
620660
"outputs": [
621661
{
622662
"name": "stdout",
@@ -628,35 +668,35 @@
628668
" [0. +0.j 0. +0.j 0. +0.j 0. +0.j]\n",
629669
" [0.5+0.j 0. +0.j 0. +0.j 0.5+0.j]]\n",
630670
"\n",
631-
"shadow state:\n",
632-
" [[ 0.49579 +0.j 0.003945-0.003675j 0.00729 -0.00345j\n",
633-
" 0.498825+0.001125j]\n",
634-
" [ 0.003945+0.003675j 0.00457 +0.j 0.007875+0.003465j\n",
635-
" -0.0081 +0.00204j ]\n",
636-
" [ 0.00729 +0.00345j 0.007875-0.003465j 0.00898 +0.j\n",
637-
" -0.000645+0.002085j]\n",
638-
" [ 0.498825-0.001125j -0.0081 -0.00204j -0.000645-0.002085j\n",
639-
" 0.49066 +0.j ]]\n",
671+
"shadow state: error: 0.019630578188122815\n",
672+
" [[ 5.10880e-01+0.000e+00j -7.35000e-04-2.235e-03j 2.25000e-03+3.150e-03j\n",
673+
" 4.98375e-01-4.005e-03j]\n",
674+
" [-7.35000e-04+2.235e-03j -2.69000e-03+0.000e+00j -2.25000e-04+5.850e-04j\n",
675+
" 5.40000e-04+9.000e-05j]\n",
676+
" [ 2.25000e-03-3.150e-03j -2.25000e-04-5.850e-04j -1.25600e-02+0.000e+00j\n",
677+
" 7.50000e-05-1.245e-03j]\n",
678+
" [ 4.98375e-01+4.005e-03j 5.40000e-04-9.000e-05j 7.50000e-05+1.245e-03j\n",
679+
" 5.04370e-01+0.000e+00j]]\n",
640680
"\n",
641-
"shadow state 1:\n",
642-
" [[ 0.49579 +0.j 0.003945-0.003675j 0.00729 -0.00345j\n",
643-
" 0.498825+0.001125j]\n",
644-
" [ 0.003945+0.003675j 0.00457 +0.j 0.007875+0.003465j\n",
645-
" -0.0081 +0.00204j ]\n",
646-
" [ 0.00729 +0.00345j 0.007875-0.003465j 0.00898 +0.j\n",
647-
" -0.000645+0.002085j]\n",
648-
" [ 0.498825-0.001125j -0.0081 -0.00204j -0.000645-0.002085j\n",
649-
" 0.49066 +0.j ]]\n",
681+
"shadow state 1: error: 0.019630578188122815\n",
682+
" [[ 5.10880e-01+0.000e+00j -7.35000e-04-2.235e-03j 2.25000e-03+3.150e-03j\n",
683+
" 4.98375e-01-4.005e-03j]\n",
684+
" [-7.35000e-04+2.235e-03j -2.69000e-03+0.000e+00j -2.25000e-04+5.850e-04j\n",
685+
" 5.40000e-04+9.000e-05j]\n",
686+
" [ 2.25000e-03-3.150e-03j -2.25000e-04-5.850e-04j -1.25600e-02+0.000e+00j\n",
687+
" 7.50000e-05-1.245e-03j]\n",
688+
" [ 4.98375e-01+4.005e-03j 5.40000e-04-9.000e-05j 7.50000e-05+1.245e-03j\n",
689+
" 5.04370e-01+0.000e+00j]]\n",
650690
"\n",
651-
"shadow state 2:\n",
652-
" [[ 0.49579 +0.j 0.003945-0.003675j 0.00729 -0.00345j\n",
653-
" 0.498825+0.001125j]\n",
654-
" [ 0.003945+0.003675j 0.00457 +0.j 0.007875+0.003465j\n",
655-
" -0.0081 +0.00204j ]\n",
656-
" [ 0.00729 +0.00345j 0.007875-0.003465j 0.00898 +0.j\n",
657-
" -0.000645+0.002085j]\n",
658-
" [ 0.498825-0.001125j -0.0081 -0.00204j -0.000645-0.002085j\n",
659-
" 0.49066 +0.j ]]\n"
691+
"shadow state 2: error: 0.019630578188122815\n",
692+
" [[ 5.10880e-01+0.000e+00j -7.35000e-04-2.235e-03j 2.25000e-03+3.150e-03j\n",
693+
" 4.98375e-01-4.005e-03j]\n",
694+
" [-7.35000e-04+2.235e-03j -2.69000e-03+0.000e+00j -2.25000e-04+5.850e-04j\n",
695+
" 5.40000e-04+9.000e-05j]\n",
696+
" [ 2.25000e-03-3.150e-03j -2.25000e-04-5.850e-04j -1.25600e-02+0.000e+00j\n",
697+
" 7.50000e-05-1.245e-03j]\n",
698+
" [ 4.98375e-01+4.005e-03j 5.40000e-04-9.000e-05j 7.50000e-05+1.245e-03j\n",
699+
" 5.04370e-01+0.000e+00j]]\n"
660700
]
661701
}
662702
],
@@ -678,15 +718,19 @@
678718
"sdw_state2 = shadows.global_shadow_state2(lss_states)\n",
679719
"\n",
680720
"print(\"exact:\\n\", bell_state)\n",
681-
"print(\"\\nshadow state:\\n\", sdw_state)\n",
682-
"print(\"\\nshadow state 1:\\n\", sdw_state1)\n",
683-
"print(\"\\nshadow state 2:\\n\", sdw_state2)"
721+
"print(f\"\\nshadow state: error: {np.linalg.norm(bell_state - sdw_state)}\\n\", sdw_state)\n",
722+
"print(\n",
723+
" f\"\\nshadow state 1: error: {np.linalg.norm(bell_state - sdw_state)}\\n\", sdw_state1\n",
724+
")\n",
725+
"print(\n",
726+
" f\"\\nshadow state 2: error: {np.linalg.norm(bell_state - sdw_state)}\\n\", sdw_state2\n",
727+
")"
684728
],
685729
"metadata": {
686730
"collapsed": false,
687731
"ExecuteTime": {
688-
"end_time": "2023-08-08T12:10:43.360675500Z",
689-
"start_time": "2023-08-08T12:10:41.746265700Z"
732+
"end_time": "2023-08-09T02:19:18.141586600Z",
733+
"start_time": "2023-08-09T02:19:16.434910100Z"
690734
}
691735
}
692736
},

0 commit comments

Comments
 (0)