diff --git a/examples/autoqasm/4_Native_programming.ipynb b/examples/autoqasm/4_Native_programming.ipynb index d133598e8..167903871 100644 --- a/examples/autoqasm/4_Native_programming.ipynb +++ b/examples/autoqasm/4_Native_programming.ipynb @@ -20,7 +20,6 @@ "outputs": [], "source": [ "# general imports\n", - "import numpy as np\n", "import IPython\n", "\n", "# AWS imports: Import Braket SDK modules\n", @@ -67,8 +66,7 @@ " measure([0, 1])\n", "\n", "\n", - "bell_state_program = bell_state()\n", - "print(bell_state_program.to_ir())" + "print(bell_state.to_ir())" ] }, { @@ -112,8 +110,7 @@ " measure([\"$0\", \"$1\"])\n", "\n", "\n", - "bell_state_program = bell_state()\n", - "print(bell_state_program.to_ir())" + "print(bell_state.to_ir())" ] }, { @@ -125,7 +122,7 @@ "\n", "## Device-specific validation\n", "\n", - "Bypassing the mapping and compilation is only the first step of native programming. Because native programming is intended for targeting a program to a specific device, we need to specify the target device in the AutoQASM program. We can accomplish this by adding a `device` argument to the `@aq.main` decorator, passing the ARN of the Amazon Braket device (or, optionally, a `braket.devices.device.Device` object) that we want to target.\n", + "Bypassing the mapping and compilation is only the first step of native programming. Because native programming is intended for targeting a program to a specific device, we need to specify the target device in the AutoQASM program. We can accomplish this by adding a `device` argument to the `@aq.main` decorator, passing the ARN of the Amazon Braket device (or, optionally, a `braket.devices.Device` object) that we want to target.\n", "\n", "Here we modify the program to target the `Devices.IonQ.Aria1` device. When building this program, AutoQASM will validate that (among other things) the contents of any `verbatim` blocks respect the native gate set and connectivity of the target device. " ] @@ -145,16 +142,13 @@ } ], "source": [ - "@aq.main(device=Devices.IonQ.Aria1)\n", - "def bell_state():\n", - " with aq.verbatim():\n", - " h(\"$0\")\n", - " cnot(\"$0\", \"$1\")\n", - " measure([\"$0\", \"$1\"])\n", - "\n", - "\n", "try:\n", - " bell_state_program = bell_state()\n", + " @aq.main(device=Devices.IonQ.Aria1)\n", + " def bell_state():\n", + " with aq.verbatim():\n", + " h(\"$0\")\n", + " cnot(\"$0\", \"$1\")\n", + " measure([\"$0\", \"$1\"])\n", "except Exception as e:\n", " print(\"ERROR:\", e)" ] @@ -181,196 +175,9 @@ "outputs": [ { "data": { - "text/html": [ - "
import numpy as np\n",
-       "\n",
-       "import braket.experimental.autoqasm as aq\n",
-       "from braket.experimental.autoqasm.instructions import gpi, gpi2, ms\n",
-       "\n",
-       "\n",
-       "@aq.gate\n",
-       "def h(q: aq.Qubit):\n",
-       "    gpi2(q, np.pi / 2)\n",
-       "    gpi(q, 0)\n",
-       "\n",
-       "\n",
-       "@aq.gate\n",
-       "def u(q: aq.Qubit, a: float, b: float, c: float):\n",
-       "    gpi2(q, a)\n",
-       "    gpi(q, b)\n",
-       "    gpi2(q, c)\n",
-       "\n",
-       "\n",
-       "@aq.gate\n",
-       "def rx(q: aq.Qubit, theta: float):\n",
-       "    u(q, np.pi / 2, theta / 2 + np.pi / 2, np.pi / 2)\n",
-       "\n",
-       "\n",
-       "@aq.gate\n",
-       "def ry(q: aq.Qubit, theta: float):\n",
-       "    u(q, np.pi, theta / 2 + np.pi, np.pi)\n",
-       "\n",
-       "\n",
-       "@aq.gate\n",
-       "def cnot(q0: aq.Qubit, q1: aq.Qubit):\n",
-       "    ry(q0, np.pi / 2)\n",
-       "    ms(q0, q1, 0, 0, np.pi / 2)\n",
-       "    rx(q0, -np.pi / 2)\n",
-       "    rx(q1, -np.pi / 2)\n",
-       "    ry(q0, -np.pi / 2)\n",
-       "
\n" - ], - "text/latex": [ - "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", - "\\PY{k+kn}{import} \\PY{n+nn}{numpy} \\PY{k}{as} \\PY{n+nn}{np}\n", - "\n", - "\\PY{k+kn}{import} \\PY{n+nn}{braket}\\PY{n+nn}{.}\\PY{n+nn}{experimental}\\PY{n+nn}{.}\\PY{n+nn}{autoqasm} \\PY{k}{as} \\PY{n+nn}{aq}\n", - "\\PY{k+kn}{from} \\PY{n+nn}{braket}\\PY{n+nn}{.}\\PY{n+nn}{experimental}\\PY{n+nn}{.}\\PY{n+nn}{autoqasm}\\PY{n+nn}{.}\\PY{n+nn}{instructions} \\PY{k+kn}{import} \\PY{n}{gpi}\\PY{p}{,} \\PY{n}{gpi2}\\PY{p}{,} \\PY{n}{ms}\n", - "\n", - "\n", - "\\PY{n+nd}{@aq}\\PY{o}{.}\\PY{n}{gate}\n", - "\\PY{k}{def} \\PY{n+nf}{h}\\PY{p}{(}\\PY{n}{q}\\PY{p}{:} \\PY{n}{aq}\\PY{o}{.}\\PY{n}{Qubit}\\PY{p}{)}\\PY{p}{:}\n", - " \\PY{n}{gpi2}\\PY{p}{(}\\PY{n}{q}\\PY{p}{,} \\PY{n}{np}\\PY{o}{.}\\PY{n}{pi} \\PY{o}{/} \\PY{l+m+mi}{2}\\PY{p}{)}\n", - " \\PY{n}{gpi}\\PY{p}{(}\\PY{n}{q}\\PY{p}{,} \\PY{l+m+mi}{0}\\PY{p}{)}\n", - "\n", - "\n", - "\\PY{n+nd}{@aq}\\PY{o}{.}\\PY{n}{gate}\n", - "\\PY{k}{def} \\PY{n+nf}{u}\\PY{p}{(}\\PY{n}{q}\\PY{p}{:} \\PY{n}{aq}\\PY{o}{.}\\PY{n}{Qubit}\\PY{p}{,} \\PY{n}{a}\\PY{p}{:} \\PY{n+nb}{float}\\PY{p}{,} \\PY{n}{b}\\PY{p}{:} \\PY{n+nb}{float}\\PY{p}{,} \\PY{n}{c}\\PY{p}{:} \\PY{n+nb}{float}\\PY{p}{)}\\PY{p}{:}\n", - " \\PY{n}{gpi2}\\PY{p}{(}\\PY{n}{q}\\PY{p}{,} \\PY{n}{a}\\PY{p}{)}\n", - " \\PY{n}{gpi}\\PY{p}{(}\\PY{n}{q}\\PY{p}{,} \\PY{n}{b}\\PY{p}{)}\n", - " \\PY{n}{gpi2}\\PY{p}{(}\\PY{n}{q}\\PY{p}{,} \\PY{n}{c}\\PY{p}{)}\n", - "\n", - "\n", - "\\PY{n+nd}{@aq}\\PY{o}{.}\\PY{n}{gate}\n", - "\\PY{k}{def} \\PY{n+nf}{rx}\\PY{p}{(}\\PY{n}{q}\\PY{p}{:} \\PY{n}{aq}\\PY{o}{.}\\PY{n}{Qubit}\\PY{p}{,} \\PY{n}{theta}\\PY{p}{:} \\PY{n+nb}{float}\\PY{p}{)}\\PY{p}{:}\n", - " \\PY{n}{u}\\PY{p}{(}\\PY{n}{q}\\PY{p}{,} \\PY{n}{np}\\PY{o}{.}\\PY{n}{pi} \\PY{o}{/} \\PY{l+m+mi}{2}\\PY{p}{,} \\PY{n}{theta} \\PY{o}{/} \\PY{l+m+mi}{2} \\PY{o}{+} \\PY{n}{np}\\PY{o}{.}\\PY{n}{pi} \\PY{o}{/} \\PY{l+m+mi}{2}\\PY{p}{,} \\PY{n}{np}\\PY{o}{.}\\PY{n}{pi} \\PY{o}{/} \\PY{l+m+mi}{2}\\PY{p}{)}\n", - "\n", - "\n", - "\\PY{n+nd}{@aq}\\PY{o}{.}\\PY{n}{gate}\n", - "\\PY{k}{def} \\PY{n+nf}{ry}\\PY{p}{(}\\PY{n}{q}\\PY{p}{:} \\PY{n}{aq}\\PY{o}{.}\\PY{n}{Qubit}\\PY{p}{,} \\PY{n}{theta}\\PY{p}{:} \\PY{n+nb}{float}\\PY{p}{)}\\PY{p}{:}\n", - " \\PY{n}{u}\\PY{p}{(}\\PY{n}{q}\\PY{p}{,} \\PY{n}{np}\\PY{o}{.}\\PY{n}{pi}\\PY{p}{,} \\PY{n}{theta} \\PY{o}{/} \\PY{l+m+mi}{2} \\PY{o}{+} \\PY{n}{np}\\PY{o}{.}\\PY{n}{pi}\\PY{p}{,} \\PY{n}{np}\\PY{o}{.}\\PY{n}{pi}\\PY{p}{)}\n", - "\n", - "\n", - "\\PY{n+nd}{@aq}\\PY{o}{.}\\PY{n}{gate}\n", - "\\PY{k}{def} \\PY{n+nf}{cnot}\\PY{p}{(}\\PY{n}{q0}\\PY{p}{:} \\PY{n}{aq}\\PY{o}{.}\\PY{n}{Qubit}\\PY{p}{,} \\PY{n}{q1}\\PY{p}{:} \\PY{n}{aq}\\PY{o}{.}\\PY{n}{Qubit}\\PY{p}{)}\\PY{p}{:}\n", - " \\PY{n}{ry}\\PY{p}{(}\\PY{n}{q0}\\PY{p}{,} \\PY{n}{np}\\PY{o}{.}\\PY{n}{pi} \\PY{o}{/} \\PY{l+m+mi}{2}\\PY{p}{)}\n", - " \\PY{n}{ms}\\PY{p}{(}\\PY{n}{q0}\\PY{p}{,} \\PY{n}{q1}\\PY{p}{,} \\PY{l+m+mi}{0}\\PY{p}{,} \\PY{l+m+mi}{0}\\PY{p}{,} \\PY{n}{np}\\PY{o}{.}\\PY{n}{pi} \\PY{o}{/} \\PY{l+m+mi}{2}\\PY{p}{)}\n", - " \\PY{n}{rx}\\PY{p}{(}\\PY{n}{q0}\\PY{p}{,} \\PY{o}{\\PYZhy{}}\\PY{n}{np}\\PY{o}{.}\\PY{n}{pi} \\PY{o}{/} \\PY{l+m+mi}{2}\\PY{p}{)}\n", - " \\PY{n}{rx}\\PY{p}{(}\\PY{n}{q1}\\PY{p}{,} \\PY{o}{\\PYZhy{}}\\PY{n}{np}\\PY{o}{.}\\PY{n}{pi} \\PY{o}{/} \\PY{l+m+mi}{2}\\PY{p}{)}\n", - " \\PY{n}{ry}\\PY{p}{(}\\PY{n}{q0}\\PY{p}{,} \\PY{o}{\\PYZhy{}}\\PY{n}{np}\\PY{o}{.}\\PY{n}{pi} \\PY{o}{/} \\PY{l+m+mi}{2}\\PY{p}{)}\n", - "\\end{Verbatim}\n" - ], - "text/plain": [ - "import numpy as np\n", - "\n", - "import braket.experimental.autoqasm as aq\n", - "from braket.experimental.autoqasm.instructions import gpi, gpi2, ms\n", - "\n", - "\n", - "@aq.gate\n", - "def h(q: aq.Qubit):\n", - " gpi2(q, np.pi / 2)\n", - " gpi(q, 0)\n", - "\n", - "\n", - "@aq.gate\n", - "def u(q: aq.Qubit, a: float, b: float, c: float):\n", - " gpi2(q, a)\n", - " gpi(q, b)\n", - " gpi2(q, c)\n", - "\n", - "\n", - "@aq.gate\n", - "def rx(q: aq.Qubit, theta: float):\n", - " u(q, np.pi / 2, theta / 2 + np.pi / 2, np.pi / 2)\n", - "\n", - "\n", - "@aq.gate\n", - "def ry(q: aq.Qubit, theta: float):\n", - " u(q, np.pi, theta / 2 + np.pi, np.pi)\n", - "\n", - "\n", - "@aq.gate\n", - "def cnot(q0: aq.Qubit, q1: aq.Qubit):\n", - " ry(q0, np.pi / 2)\n", - " ms(q0, q1, 0, 0, np.pi / 2)\n", - " rx(q0, -np.pi / 2)\n", - " rx(q1, -np.pi / 2)\n", - " ry(q0, -np.pi / 2)" - ] + "text/plain": "import numpy as np\n\nimport braket.experimental.autoqasm as aq\nfrom braket.experimental.autoqasm.instructions import gpi, gpi2, ms\n\n\n@aq.gate\ndef h(q: aq.Qubit):\n gpi2(q, np.pi / 2)\n gpi(q, 0)\n\n\n@aq.gate\ndef u(q: aq.Qubit, a: float, b: float, c: float):\n gpi2(q, a)\n gpi(q, b)\n gpi2(q, c)\n\n\n@aq.gate\ndef rx(q: aq.Qubit, theta: float):\n u(q, np.pi / 2, theta / 2 + np.pi / 2, np.pi / 2)\n\n\n@aq.gate\ndef ry(q: aq.Qubit, theta: float):\n u(q, np.pi, theta / 2 + np.pi, np.pi)\n\n\n@aq.gate\ndef cnot(q0: aq.Qubit, q1: aq.Qubit):\n ry(q0, np.pi / 2)\n ms(q0, q1, 0, 0, np.pi / 2)\n rx(q0, -np.pi / 2)\n rx(q1, -np.pi / 2)\n ry(q0, -np.pi / 2)", + "text/html": "
import numpy as np\n\nimport braket.experimental.autoqasm as aq\nfrom braket.experimental.autoqasm.instructions import gpi, gpi2, ms\n\n\n@aq.gate\ndef h(q: aq.Qubit):\n    gpi2(q, np.pi / 2)\n    gpi(q, 0)\n\n\n@aq.gate\ndef u(q: aq.Qubit, a: float, b: float, c: float):\n    gpi2(q, a)\n    gpi(q, b)\n    gpi2(q, c)\n\n\n@aq.gate\ndef rx(q: aq.Qubit, theta: float):\n    u(q, np.pi / 2, theta / 2 + np.pi / 2, np.pi / 2)\n\n\n@aq.gate\ndef ry(q: aq.Qubit, theta: float):\n    u(q, np.pi, theta / 2 + np.pi, np.pi)\n\n\n@aq.gate\ndef cnot(q0: aq.Qubit, q1: aq.Qubit):\n    ry(q0, np.pi / 2)\n    ms(q0, q1, 0, 0, np.pi / 2)\n    rx(q0, -np.pi / 2)\n    rx(q1, -np.pi / 2)\n    ry(q0, -np.pi / 2)\n
\n", + "text/latex": "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n\\PY{k+kn}{import} \\PY{n+nn}{numpy} \\PY{k}{as} \\PY{n+nn}{np}\n\n\\PY{k+kn}{import} \\PY{n+nn}{braket}\\PY{n+nn}{.}\\PY{n+nn}{experimental}\\PY{n+nn}{.}\\PY{n+nn}{autoqasm} \\PY{k}{as} \\PY{n+nn}{aq}\n\\PY{k+kn}{from} \\PY{n+nn}{braket}\\PY{n+nn}{.}\\PY{n+nn}{experimental}\\PY{n+nn}{.}\\PY{n+nn}{autoqasm}\\PY{n+nn}{.}\\PY{n+nn}{instructions} \\PY{k+kn}{import} \\PY{n}{gpi}\\PY{p}{,} \\PY{n}{gpi2}\\PY{p}{,} \\PY{n}{ms}\n\n\n\\PY{n+nd}{@aq}\\PY{o}{.}\\PY{n}{gate}\n\\PY{k}{def} \\PY{n+nf}{h}\\PY{p}{(}\\PY{n}{q}\\PY{p}{:} \\PY{n}{aq}\\PY{o}{.}\\PY{n}{Qubit}\\PY{p}{)}\\PY{p}{:}\n \\PY{n}{gpi2}\\PY{p}{(}\\PY{n}{q}\\PY{p}{,} \\PY{n}{np}\\PY{o}{.}\\PY{n}{pi} \\PY{o}{/} \\PY{l+m+mi}{2}\\PY{p}{)}\n \\PY{n}{gpi}\\PY{p}{(}\\PY{n}{q}\\PY{p}{,} \\PY{l+m+mi}{0}\\PY{p}{)}\n\n\n\\PY{n+nd}{@aq}\\PY{o}{.}\\PY{n}{gate}\n\\PY{k}{def} \\PY{n+nf}{u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i}{2}\\PY{p}{)}\n \\PY{n}{ry}\\PY{p}{(}\\PY{n}{q0}\\PY{p}{,} \\PY{o}{\\PYZhy{}}\\PY{n}{np}\\PY{o}{.}\\PY{n}{pi} \\PY{o}{/} \\PY{l+m+mi}{2}\\PY{p}{)}\n\\end{Verbatim}\n" }, "execution_count": 5, "metadata": {}, @@ -445,8 +252,7 @@ " measure([\"$0\", \"$1\"])\n", "\n", "\n", - "bell_state_program = bell_state()\n", - "print(bell_state_program.to_ir())" + "print(bell_state.to_ir())" ] }, {