{ "cells": [ { "cell_type": "markdown", "id": "8aa8903f", "metadata": {}, "source": [ "# Piquasso program organization\n", "\n", "With Piquasso, one could easily separate programs into multiple `with` statements:" ] }, { "cell_type": "code", "execution_count": 18, "id": "25a54dc1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Result(samples=[(-8.201970710129283, 6.806985248163985)], state=GaussianState(d=5, config=Config(), connector=NumpyConnector()))" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import piquasso as pq\n", "import numpy as np\n", "\n", "\n", "with pq.Program() as preparation:\n", " pq.Q(0, 1) | pq.Squeezing2(r=1, phi=np.pi / 4)\n", " pq.Q(2, 3) | pq.Squeezing2(r=2, phi=np.pi / 3)\n", "\n", "with pq.Program() as interferometer:\n", " pq.Q(0, 1) | pq.Beamsplitter(theta=np.pi / 4, phi=np.pi / 3)\n", " pq.Q(1) | pq.Phaseshifter(phi=np.pi / 2)\n", " pq.Q(1, 2) | pq.Beamsplitter(theta=np.pi / 5, phi=np.pi / 6)\n", "\n", "with pq.Program() as executable_program:\n", " pq.Q(all) | preparation\n", "\n", " pq.Q(0, 1, 2) | interferometer\n", " pq.Q(2, 3, 4) | interferometer\n", "\n", " pq.Q(3) | pq.HeterodyneMeasurement()\n", "\n", "\n", "simulator = pq.GaussianSimulator(d=5)\n", "result = simulator.execute(executable_program)\n", "result" ] }, { "cell_type": "markdown", "id": "be2a4757", "metadata": {}, "source": [ "Using this syntax, one could embed subprograms on different modes. In this example, the `interferometer` subprogram is embedded twice for two different sets of modes. Note, that the subprogram is registered to the specified only via [pq.Q](../api/mode.rst#piquasso.api.mode.Q).\n", "\n", "One can use this syntax to define custom gates as follows:" ] }, { "cell_type": "code", "execution_count": 19, "id": "df8c05c3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Instructions:\n", "\t Squeezing2(r=1, phi=0.7853981633974483, modes=(0, 1))\n", "\t Squeezing2(r=2, phi=1.0471975511965976, modes=(2, 3))\n", "\t Phaseshifter(phi=1.0471975511965976, modes=(0,))\n", "\t Beamsplitter(theta=0.7853981633974483, phi=0.0, modes=(0, 1))\n", "\t Phaseshifter(phi=0.5235987755982988, modes=(1,))\n", "\t Beamsplitter(theta=0.6283185307179586, phi=0.0, modes=(1, 2))\n", "\t ParticleNumberMeasurement(modes=(3,))\n" ] } ], "source": [ "def MyBeamsplitter(theta, phi):\n", " my_beamsplitter = pq.Program(\n", " instructions=[\n", " pq.Phaseshifter(phi=phi).on_modes(0),\n", " pq.Beamsplitter(theta=theta, phi=0.0).on_modes(0, 1),\n", " ]\n", " )\n", "\n", " return my_beamsplitter\n", "\n", "\n", "with pq.Program() as preparation:\n", " pq.Q(0, 1) | pq.Squeezing2(r=1, phi=np.pi / 4)\n", " pq.Q(2, 3) | pq.Squeezing2(r=2, phi=np.pi / 3)\n", "\n", "with pq.Program() as program:\n", " pq.Q(all) | preparation\n", "\n", " pq.Q(0, 1) | MyBeamsplitter(theta=np.pi / 4, phi=np.pi / 3)\n", " pq.Q(1, 2) | MyBeamsplitter(theta=np.pi / 5, phi=np.pi / 6)\n", "\n", " pq.Q(3) | pq.ParticleNumberMeasurement()\n", "\n", "\n", "print(\"Instructions:\")\n", "for instruction in program.instructions:\n", " print(\"\\t\", instruction)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.16" } }, "nbformat": 4, "nbformat_minor": 5 }