|
92 | 92 | },
|
93 | 93 | {
|
94 | 94 | "cell_type": "code",
|
95 |
| - "execution_count": 8, |
| 95 | + "execution_count": 2, |
96 | 96 | "metadata": {
|
97 | 97 | "collapsed": true,
|
98 | 98 | "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" |
101 | 101 | }
|
102 | 102 | },
|
103 | 103 | "outputs": [
|
104 | 104 | {
|
105 | 105 | "data": {
|
106 | 106 | "text/plain": "('complex128', 'float64')"
|
107 | 107 | },
|
108 |
| - "execution_count": 8, |
| 108 | + "execution_count": 2, |
109 | 109 | "metadata": {},
|
110 | 110 | "output_type": "execute_result"
|
111 | 111 | }
|
|
149 | 149 | },
|
150 | 150 | {
|
151 | 151 | "cell_type": "code",
|
152 |
| - "execution_count": 9, |
| 152 | + "execution_count": 3, |
153 | 153 | "outputs": [
|
154 | 154 | {
|
155 | 155 | "name": "stdout",
|
|
182 | 182 | "metadata": {
|
183 | 183 | "collapsed": false,
|
184 | 184 | "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" |
187 | 187 | }
|
188 | 188 | }
|
189 | 189 | },
|
|
198 | 198 | },
|
199 | 199 | {
|
200 | 200 | "cell_type": "code",
|
201 |
| - "execution_count": 10, |
| 201 | + "execution_count": 5, |
202 | 202 | "outputs": [],
|
203 | 203 | "source": [
|
204 | 204 | "nlayers = 10\n",
|
|
218 | 218 | "metadata": {
|
219 | 219 | "collapsed": false,
|
220 | 220 | "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" |
223 | 223 | }
|
224 | 224 | }
|
225 | 225 | },
|
|
234 | 234 | },
|
235 | 235 | {
|
236 | 236 | "cell_type": "code",
|
237 |
| - "execution_count": 11, |
| 237 | + "execution_count": 6, |
238 | 238 | "outputs": [],
|
239 | 239 | "source": [
|
240 | 240 | "pauli_strings = BK.convert_to_tensor(np.random.randint(1, 4, size=(nps, n)))\n",
|
|
243 | 243 | "metadata": {
|
244 | 244 | "collapsed": false,
|
245 | 245 | "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" |
248 | 248 | }
|
249 | 249 | }
|
250 | 250 | },
|
|
259 | 259 | },
|
260 | 260 | {
|
261 | 261 | "cell_type": "code",
|
262 |
| - "execution_count": 12, |
| 262 | + "execution_count": 7, |
263 | 263 | "outputs": [
|
264 | 264 | {
|
265 | 265 | "name": "stdout",
|
266 | 266 | "output_type": "stream",
|
267 | 267 | "text": [
|
268 |
| - "(97920, 5, 8, 2, 2)\n" |
| 268 | + "shape of snapshot states: (97920, 5, 8, 2, 2)\n" |
269 | 269 | ]
|
270 | 270 | }
|
271 | 271 | ],
|
|
278 | 278 | "\n",
|
279 | 279 | "\n",
|
280 | 280 | "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)" |
282 | 282 | ],
|
283 | 283 | "metadata": {
|
284 | 284 | "collapsed": false,
|
285 | 285 | "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" |
288 | 288 | }
|
289 | 289 | }
|
290 | 290 | },
|
|
509 | 509 | {
|
510 | 510 | "cell_type": "markdown",
|
511 | 511 | "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." |
513 | 513 | ],
|
514 | 514 | "metadata": {
|
515 | 515 | "collapsed": false
|
516 | 516 | }
|
517 | 517 | },
|
518 | 518 | {
|
519 | 519 | "cell_type": "code",
|
520 |
| - "execution_count": 15, |
| 520 | + "execution_count": 9, |
521 | 521 | "outputs": [
|
522 | 522 | {
|
523 | 523 | "name": "stdout",
|
524 | 524 | "output_type": "stream",
|
525 | 525 | "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" |
528 | 536 | ]
|
529 | 537 | }
|
530 | 538 | ],
|
531 | 539 | "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", |
533 | 552 | "\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", |
538 | 553 | "\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", |
540 | 558 | "\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}\")" |
543 | 569 | ],
|
544 | 570 | "metadata": {
|
545 | 571 | "collapsed": false,
|
546 | 572 | "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" |
549 | 575 | }
|
550 | 576 | }
|
551 | 577 | },
|
|
567 | 593 | },
|
568 | 594 | {
|
569 | 595 | "cell_type": "code",
|
570 |
| - "execution_count": 16, |
| 596 | + "execution_count": 10, |
571 | 597 | "outputs": [
|
572 | 598 | {
|
573 | 599 | "name": "stdout",
|
574 | 600 | "output_type": "stream",
|
575 | 601 | "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" |
577 | 612 | ]
|
578 | 613 | }
|
579 | 614 | ],
|
|
584 | 619 | "status = BK.convert_to_tensor(np.random.rand(nps, r))\n",
|
585 | 620 | "\n",
|
586 | 621 | "snapshots = shadows.shadow_snapshots(psi, pauli_strings, status, measurement_only=True)\n",
|
587 |
| - "ent2 = shadows.renyi_entropy_2(snapshots, sub)\n", |
588 | 622 | "\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" |
590 | 630 | ],
|
591 | 631 | "metadata": {
|
592 | 632 | "collapsed": false,
|
593 | 633 | "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" |
596 | 636 | }
|
597 | 637 | }
|
598 | 638 | },
|
|
608 | 648 | {
|
609 | 649 | "cell_type": "markdown",
|
610 | 650 | "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`." |
612 | 652 | ],
|
613 | 653 | "metadata": {
|
614 | 654 | "collapsed": false
|
615 | 655 | }
|
616 | 656 | },
|
617 | 657 | {
|
618 | 658 | "cell_type": "code",
|
619 |
| - "execution_count": 17, |
| 659 | + "execution_count": 8, |
620 | 660 | "outputs": [
|
621 | 661 | {
|
622 | 662 | "name": "stdout",
|
|
628 | 668 | " [0. +0.j 0. +0.j 0. +0.j 0. +0.j]\n",
|
629 | 669 | " [0.5+0.j 0. +0.j 0. +0.j 0.5+0.j]]\n",
|
630 | 670 | "\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", |
640 | 680 | "\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", |
650 | 690 | "\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" |
660 | 700 | ]
|
661 | 701 | }
|
662 | 702 | ],
|
|
678 | 718 | "sdw_state2 = shadows.global_shadow_state2(lss_states)\n",
|
679 | 719 | "\n",
|
680 | 720 | "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 | + ")" |
684 | 728 | ],
|
685 | 729 | "metadata": {
|
686 | 730 | "collapsed": false,
|
687 | 731 | "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" |
690 | 734 | }
|
691 | 735 | }
|
692 | 736 | },
|
|
0 commit comments