|
17 | 17 | "\n", |
18 | 18 | "The ABA decomposition (or KAK decomposition) is a decomposition method that uses three consecutive Pauli rotation gates to form any unitary gate. Thus, any arbitrary single qubit gate can be expressed as a product of rotations: a rotation around the A axis, followed by a rotation around the B axis, and then another rotation around the A axis. The mathematical formalism for this rotation can be seen in section 3.1.\n", |
19 | 19 | "\n", |
20 | | - "In OpenSquirrel, this can be found in the `get_decomposition_angles` method from the `opensquirrel.decomposer.aba_decomposer` module. The ABA Decomposition can be applied to list of gates or a circuit. Below we will first focus on decomposing a circuit.\n", |
| 20 | + "In OpenSquirrel, this can be found in the `get_decomposition_angles` method from the `opensquirrel.passes.decomposer.aba_decomposer` module. The ABA Decomposition can be applied to list of gates or a circuit. Below we will first focus on decomposing a circuit.\n", |
21 | 21 | "\n", |
22 | 22 | "The circuit chosen is a single qubit circuit with an added Hadamard, Z, Y, and Rx gate. \n", |
23 | 23 | "\n", |
|
62 | 62 | "source": [ |
63 | 63 | "import math\n", |
64 | 64 | "\n", |
65 | | - "from opensquirrel import CircuitBuilder\n", |
66 | | - "from opensquirrel.ir import Float, Qubit\n", |
| 65 | + "from opensquirrel.circuit_builder import CircuitBuilder\n", |
67 | 66 | "\n", |
68 | 67 | "# Build the circuit structure using the CircuitBuilder\n", |
69 | 68 | "builder = CircuitBuilder(qubit_register_size=1)\n", |
70 | | - "builder.H(Qubit(0))\n", |
71 | | - "builder.Z(Qubit(0))\n", |
72 | | - "builder.Y(Qubit(0))\n", |
73 | | - "builder.Rx(Qubit(0), Float(math.pi / 3))\n", |
| 69 | + "builder.H(0)\n", |
| 70 | + "builder.Z(0)\n", |
| 71 | + "builder.Y(0)\n", |
| 72 | + "builder.Rx(0, math.pi / 3)\n", |
74 | 73 | "\n", |
75 | 74 | "# Create a new circuit from the constructed structure\n", |
76 | 75 | "circuit = builder.to_circuit()\n", |
|
81 | 80 | "cell_type": "markdown", |
82 | 81 | "id": "afe111839ab98b22", |
83 | 82 | "metadata": {}, |
84 | | - "source": "Above we can see the current gates in our circuit. Having created a circuit, we can now use an ABA decomposition in `opensquirrel.decomposer.aba_decomposer` to decompose the gates in the circuit. For this example, we will apply the Z-Y-Z decomposition using the `ZYZDecomposer`. " |
| 83 | + "source": [ |
| 84 | + "Above we can see the current gates in our circuit. Having created a circuit, we can now use an ABA decomposition in `opensquirrel.passes.decomposer.aba_decomposer` to decompose the gates in the circuit. For this example, we will apply the Z-Y-Z decomposition using the `ZYZDecomposer`. " |
| 85 | + ] |
85 | 86 | }, |
86 | 87 | { |
87 | 88 | "cell_type": "code", |
|
116 | 117 | } |
117 | 118 | ], |
118 | 119 | "source": [ |
119 | | - "from opensquirrel.passes.decomposer import ZYZDecomposer\n", |
| 120 | + "from opensquirrel.passes.decomposer.aba_decomposer import ZYZDecomposer\n", |
120 | 121 | "\n", |
121 | 122 | "# Decompose the circuit using ZYZDecomposer\n", |
122 | 123 | "circuit.decompose(decomposer=ZYZDecomposer())\n", |
|
170 | 171 | ], |
171 | 172 | "source": [ |
172 | 173 | "from opensquirrel import H\n", |
173 | | - "from opensquirrel.passes.decomposer import XZXDecomposer\n", |
| 174 | + "from opensquirrel.passes.decomposer.aba_decomposer import XZXDecomposer\n", |
174 | 175 | "\n", |
175 | | - "XZXDecomposer().decompose(H(Qubit(0)))" |
| 176 | + "XZXDecomposer().decompose(H(0))" |
176 | 177 | ] |
177 | 178 | }, |
178 | 179 | { |
|
226 | 227 | "source": [ |
227 | 228 | "# Build the circuit structure using the CircuitBuilder\n", |
228 | 229 | "builder = CircuitBuilder(qubit_register_size=1)\n", |
229 | | - "builder.H(Qubit(0))\n", |
230 | | - "builder.Z(Qubit(0))\n", |
231 | | - "builder.X(Qubit(0))\n", |
232 | | - "builder.Rx(Qubit(0), Float(math.pi / 3))\n", |
| 230 | + "builder.H(0)\n", |
| 231 | + "builder.Z(0)\n", |
| 232 | + "builder.X(0)\n", |
| 233 | + "builder.Rx(0, math.pi / 3)\n", |
233 | 234 | "\n", |
234 | 235 | "# Create a new circuit from the constructed structure\n", |
235 | 236 | "circuit = builder.to_circuit()\n", |
|
240 | 241 | "cell_type": "markdown", |
241 | 242 | "id": "69517d8287c1777d", |
242 | 243 | "metadata": {}, |
243 | | - "source": "The `McKayDecomposer` is called from `opensquirrel.decomposer.mckay_decomposer` and used in a similar method to the `ABADecomposer`." |
| 244 | + "source": [ |
| 245 | + "The `McKayDecomposer` is called from `opensquirrel.passes.decomposer` and used in a similar method to the `ABADecomposer`." |
| 246 | + ] |
244 | 247 | }, |
245 | 248 | { |
246 | 249 | "cell_type": "code", |
|
358 | 361 | "source": [ |
359 | 362 | "# Build the circuit structure using the CircuitBuilder\n", |
360 | 363 | "builder = CircuitBuilder(qubit_register_size=2)\n", |
361 | | - "builder.CZ(Qubit(0), Qubit(1))\n", |
362 | | - "builder.CR(Qubit(0), Qubit(1), Float(math.pi / 3))\n", |
363 | | - "builder.CR(Qubit(1), Qubit(0), Float(math.pi / 2))\n", |
| 364 | + "builder.CZ(0, 1)\n", |
| 365 | + "builder.CR(0, 1, math.pi / 3)\n", |
| 366 | + "builder.CR(1, 0, math.pi / 2)\n", |
364 | 367 | "\n", |
365 | 368 | "# Create a new circuit from the constructed structure\n", |
366 | 369 | "circuit = builder.to_circuit()\n", |
|
371 | 374 | "cell_type": "markdown", |
372 | 375 | "id": "8c945ab4572e9f77", |
373 | 376 | "metadata": {}, |
374 | | - "source": "We can import `CNOTDecomposer` from `opensquirrel.decomposer.cnot_decomposer`. The above circuit can then be decomposed using `CNOTDecomposer` as seen below." |
| 377 | + "source": [ |
| 378 | + "We can import `CNOTDecomposer` from `opensquirrel.passes.decomposer`. The above circuit can then be decomposed using `CNOTDecomposer` as seen below." |
| 379 | + ] |
375 | 380 | }, |
376 | 381 | { |
377 | 382 | "cell_type": "code", |
|
475 | 480 | "outputs": [ |
476 | 481 | { |
477 | 482 | "data": { |
478 | | - "text/latex": "$\\displaystyle \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} + - \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\sin{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} i + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} j + \\sin{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} k$", |
| 483 | + "text/latex": [ |
| 484 | + "$\\displaystyle \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} + - \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\sin{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} i + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} j + \\sin{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} k$" |
| 485 | + ], |
479 | 486 | "text/plain": [ |
480 | 487 | "cos(theta_2/2)*cos((theta_1 + theta_3)/2) + (-sin(theta_2/2)*sin((theta_1 - theta_3)/2))*i + sin(theta_2/2)*cos((theta_1 - theta_3)/2)*j + sin((theta_1 + theta_3)/2)*cos(theta_2/2)*k" |
481 | 488 | ] |
|
502 | 509 | "cell_type": "markdown", |
503 | 510 | "id": "6e776f2cb7cae465", |
504 | 511 | "metadata": {}, |
505 | | - "source": "Let us change variables and define $p\\equiv\\theta_1 + \\theta_3$ and $m\\equiv\\theta_1 - \\theta_3$." |
| 512 | + "source": [ |
| 513 | + "Let us change variables and define $p\\equiv\\theta_1 + \\theta_3$ and $m\\equiv\\theta_1 - \\theta_3$." |
| 514 | + ] |
506 | 515 | }, |
507 | 516 | { |
508 | 517 | "cell_type": "code", |
|
517 | 526 | "outputs": [ |
518 | 527 | { |
519 | 528 | "data": { |
520 | | - "text/latex": "$\\displaystyle \\cos{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} + - \\sin{\\left(\\frac{m}{2} \\right)} \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} i + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{m}{2} \\right)} j + \\sin{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} k$", |
| 529 | + "text/latex": [ |
| 530 | + "$\\displaystyle \\cos{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} + - \\sin{\\left(\\frac{m}{2} \\right)} \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} i + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{m}{2} \\right)} j + \\sin{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} k$" |
| 531 | + ], |
521 | 532 | "text/plain": [ |
522 | 533 | "cos(p/2)*cos(theta_2/2) + (-sin(m/2)*sin(theta_2/2))*i + sin(theta_2/2)*cos(m/2)*j + sin(p/2)*cos(theta_2/2)*k" |
523 | 534 | ] |
|
539 | 550 | "cell_type": "markdown", |
540 | 551 | "id": "1ece3d36eb99512b", |
541 | 552 | "metadata": {}, |
542 | | - "source": "The original rotation's quaternion $q$ can be defined in Sympy accordingly:" |
| 553 | + "source": [ |
| 554 | + "The original rotation's quaternion $q$ can be defined in Sympy accordingly:" |
| 555 | + ] |
543 | 556 | }, |
544 | 557 | { |
545 | 558 | "cell_type": "code", |
|
554 | 567 | "outputs": [ |
555 | 568 | { |
556 | 569 | "data": { |
557 | | - "text/latex": "$\\displaystyle \\cos{\\left(\\frac{\\alpha}{2} \\right)} + n_{x} \\sin{\\left(\\frac{\\alpha}{2} \\right)} i + n_{y} \\sin{\\left(\\frac{\\alpha}{2} \\right)} j + n_{z} \\sin{\\left(\\frac{\\alpha}{2} \\right)} k$", |
| 570 | + "text/latex": [ |
| 571 | + "$\\displaystyle \\cos{\\left(\\frac{\\alpha}{2} \\right)} + n_{x} \\sin{\\left(\\frac{\\alpha}{2} \\right)} i + n_{y} \\sin{\\left(\\frac{\\alpha}{2} \\right)} j + n_{z} \\sin{\\left(\\frac{\\alpha}{2} \\right)} k$" |
| 572 | + ], |
558 | 573 | "text/plain": [ |
559 | 574 | "cos(alpha/2) + n_x*sin(alpha/2)*i + n_y*sin(alpha/2)*j + n_z*sin(alpha/2)*k" |
560 | 575 | ] |
|
577 | 592 | "cell_type": "markdown", |
578 | 593 | "id": "7cb50842c8fa111a", |
579 | 594 | "metadata": {}, |
580 | | - "source": "We get the following system of equations, where the unknowns are $p$, $m$, and $\\theta_2$:\n" |
| 595 | + "source": [ |
| 596 | + "We get the following system of equations, where the unknowns are $p$, $m$, and $\\theta_2$:\n" |
| 597 | + ] |
581 | 598 | }, |
582 | 599 | { |
583 | 600 | "cell_type": "code", |
|
592 | 609 | "outputs": [ |
593 | 610 | { |
594 | 611 | "data": { |
595 | | - "text/latex": "$\\displaystyle \\cos{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} = \\cos{\\left(\\frac{\\alpha}{2} \\right)}$", |
| 612 | + "text/latex": [ |
| 613 | + "$\\displaystyle \\cos{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} = \\cos{\\left(\\frac{\\alpha}{2} \\right)}$" |
| 614 | + ], |
596 | 615 | "text/plain": [ |
597 | 616 | "Eq(cos(p/2)*cos(theta_2/2), cos(alpha/2))" |
598 | 617 | ] |
|
602 | 621 | }, |
603 | 622 | { |
604 | 623 | "data": { |
605 | | - "text/latex": "$\\displaystyle - \\sin{\\left(\\frac{m}{2} \\right)} \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} = n_{x} \\sin{\\left(\\frac{\\alpha}{2} \\right)}$", |
| 624 | + "text/latex": [ |
| 625 | + "$\\displaystyle - \\sin{\\left(\\frac{m}{2} \\right)} \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} = n_{x} \\sin{\\left(\\frac{\\alpha}{2} \\right)}$" |
| 626 | + ], |
606 | 627 | "text/plain": [ |
607 | 628 | "Eq(-sin(m/2)*sin(theta_2/2), n_x*sin(alpha/2))" |
608 | 629 | ] |
|
612 | 633 | }, |
613 | 634 | { |
614 | 635 | "data": { |
615 | | - "text/latex": "$\\displaystyle \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{m}{2} \\right)} = n_{y} \\sin{\\left(\\frac{\\alpha}{2} \\right)}$", |
| 636 | + "text/latex": [ |
| 637 | + "$\\displaystyle \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{m}{2} \\right)} = n_{y} \\sin{\\left(\\frac{\\alpha}{2} \\right)}$" |
| 638 | + ], |
616 | 639 | "text/plain": [ |
617 | 640 | "Eq(sin(theta_2/2)*cos(m/2), n_y*sin(alpha/2))" |
618 | 641 | ] |
|
622 | 645 | }, |
623 | 646 | { |
624 | 647 | "data": { |
625 | | - "text/latex": "$\\displaystyle \\sin{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} = n_{z} \\sin{\\left(\\frac{\\alpha}{2} \\right)}$", |
| 648 | + "text/latex": [ |
| 649 | + "$\\displaystyle \\sin{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} = n_{z} \\sin{\\left(\\frac{\\alpha}{2} \\right)}$" |
| 650 | + ], |
626 | 651 | "text/plain": [ |
627 | 652 | "Eq(sin(p/2)*cos(theta_2/2), n_z*sin(alpha/2))" |
628 | 653 | ] |
|
646 | 671 | "cell_type": "markdown", |
647 | 672 | "id": "e6b34ef9cb46f2e4", |
648 | 673 | "metadata": {}, |
649 | | - "source": "Instead, assume $\\sin(p/2) \\neq 0$, then we can substitute in the first equation $\\cos\\left(\\theta_2/2\\right)$ with its value computed from the last equation. We get:" |
| 674 | + "source": [ |
| 675 | + "Instead, assume $\\sin(p/2) \\neq 0$, then we can substitute in the first equation $\\cos\\left(\\theta_2/2\\right)$ with its value computed from the last equation. We get:" |
| 676 | + ] |
650 | 677 | }, |
651 | 678 | { |
652 | 679 | "cell_type": "code", |
|
661 | 688 | "outputs": [ |
662 | 689 | { |
663 | 690 | "data": { |
664 | | - "text/latex": "$\\displaystyle \\frac{n_{z} \\sin{\\left(\\frac{\\alpha}{2} \\right)}}{\\tan{\\left(\\frac{p}{2} \\right)}} = \\cos{\\left(\\frac{\\alpha}{2} \\right)}$", |
| 691 | + "text/latex": [ |
| 692 | + "$\\displaystyle \\frac{n_{z} \\sin{\\left(\\frac{\\alpha}{2} \\right)}}{\\tan{\\left(\\frac{p}{2} \\right)}} = \\cos{\\left(\\frac{\\alpha}{2} \\right)}$" |
| 693 | + ], |
665 | 694 | "text/plain": [ |
666 | 695 | "Eq(n_z*sin(alpha/2)/tan(p/2), cos(alpha/2))" |
667 | 696 | ] |
|
718 | 747 | "cell_type": "markdown", |
719 | 748 | "id": "e3fc4cdb6d15dd1", |
720 | 749 | "metadata": {}, |
721 | | - "source": "The terms are similar to the other decompositions, XZX, YXY, ZXZ, XYX and YZY. However, for ZXZ, XYX and YZY, the $i$ term in the quaternion is positive as seen below in the YZY decomposition." |
| 750 | + "source": [ |
| 751 | + "The terms are similar to the other decompositions, XZX, YXY, ZXZ, XYX and YZY. However, for ZXZ, XYX and YZY, the $i$ term in the quaternion is positive as seen below in the YZY decomposition." |
| 752 | + ] |
722 | 753 | }, |
723 | 754 | { |
724 | 755 | "cell_type": "code", |
|
733 | 764 | "outputs": [ |
734 | 765 | { |
735 | 766 | "data": { |
736 | | - "text/latex": "$\\displaystyle \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\sin{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} i + \\sin{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} j + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} k$", |
| 767 | + "text/latex": [ |
| 768 | + "$\\displaystyle \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\sin{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} i + \\sin{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} j + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} k$" |
| 769 | + ], |
737 | 770 | "text/plain": [ |
738 | 771 | "cos(theta_2/2)*cos((theta_1 + theta_3)/2) + sin(theta_2/2)*sin((theta_1 - theta_3)/2)*i + sin((theta_1 + theta_3)/2)*cos(theta_2/2)*j + sin(theta_2/2)*cos((theta_1 - theta_3)/2)*k" |
739 | 772 | ] |
|
758 | 791 | "cell_type": "markdown", |
759 | 792 | "id": "a41b84ed5f3365b2", |
760 | 793 | "metadata": {}, |
761 | | - "source": "Thus, in order to correct for the orientation of the rotation, $\\theta_1$ and $\\theta_3$ are swapped. " |
| 794 | + "source": [ |
| 795 | + "Thus, in order to correct for the orientation of the rotation, $\\theta_1$ and $\\theta_3$ are swapped. " |
| 796 | + ] |
762 | 797 | }, |
763 | 798 | { |
764 | 799 | "cell_type": "markdown", |
|
793 | 828 | }, |
794 | 829 | { |
795 | 830 | "cell_type": "markdown", |
| 831 | + "id": "866e89a24587f0af", |
| 832 | + "metadata": {}, |
796 | 833 | "source": [ |
797 | 834 | "### 3.3 ABA Decomposition\n", |
798 | 835 | "In a two qubit system, the unitary matrix of a controlled operation is seen below,\n", |
|
844 | 881 | "C &= Rz\\bigg(\\frac{1}{2} \\theta_0 - \\frac{1}{2} \\theta_2 \\bigg)\n", |
845 | 882 | "\\end{split}\n", |
846 | 883 | "$$" |
847 | | - ], |
| 884 | + ] |
| 885 | + }, |
| 886 | + { |
| 887 | + "cell_type": "code", |
| 888 | + "execution_count": 28, |
| 889 | + "id": "3cc1e9963f3e794d", |
848 | 890 | "metadata": { |
849 | | - "collapsed": false |
| 891 | + "ExecuteTime": { |
| 892 | + "end_time": "2024-08-05T11:35:27.882752Z", |
| 893 | + "start_time": "2024-08-05T11:35:27.871794Z" |
| 894 | + } |
850 | 895 | }, |
851 | | - "id": "c084046b0bb17ec9" |
| 896 | + "outputs": [], |
| 897 | + "source": [] |
852 | 898 | } |
853 | 899 | ], |
854 | 900 | "metadata": { |
|
0 commit comments