diff --git a/team_solutions/Quantum Wranglers/iQuHack-challenge-2023-task1.ipynb b/team_solutions/Quantum Wranglers/iQuHack-challenge-2023-task1.ipynb new file mode 100644 index 0000000..afbfe30 --- /dev/null +++ b/team_solutions/Quantum Wranglers/iQuHack-challenge-2023-task1.ipynb @@ -0,0 +1,431 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "# MIT iQuHack Microsoft Challenge: Optimizing Quantum Oracles, Task 1\n", + "\n", + "To work on this task,\n", + "1. Use the notebook for this task. Each of the notebooks in the repository has the code of the corresponding task.\n", + "2. Update your team name and Slack ID variables in the next code cell (you can use different Slack IDs for different tasks if different team members work on them, but make sure to use the same team name throughout the Hackathon). Do not change the task variable!\n", + "3. Work on your task in the cell that contains operation `Task1`! Your goal is to rewrite the code so that it maintains its correctness, but requires as few resources as possible. See `evaluate_results` function for details on how your absolute score for the task is calculated.\n", + "4. Submit your task using qBraid. Use the Share Notebook feature on qBraid (See File > Share Notebook) and enter the email rickyyoung@qbraid.com. Once you click submit, if the share notebook feature works correctly, it should show that you receive no errors and the email you entered will disappear. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "Log in to Azure (once per session, don't need to do it if running from Azure workspace)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "!az login" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "## Step 1. Write the code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# Run this code cell to import the modules required to work with Q# and Azure\n", + "import qsharp\n", + "from qsharp import azure" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "teamname=\"Quantum Wranglers\" # Update this field with your team name\n", + "task=[\"task1\"]\n", + "slack_id=\"U04JVDTAG4E\" # Update this field with Slack ID of the person who worked on this task as the troubleshooting contact" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# You don't need to run this cell, it defines Q# operations as Python types to keep IntelliSense happy\n", + "Task1_DumpMachineWrapper : qsharp.QSharpCallable = None\n", + "Task1_ResourceEstimationWrapper : qsharp.QSharpCallable = None" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "**The complete code for Task 1 should be in this cell.** \n", + "This cell can include additional open statements and helper operations and functions if your solution needs them. \n", + "If you define helper operations in other cells, they will not be picked up by the grader!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "microsoft": { + "language": "qsharp" + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "%%qsharp\n", + "open Microsoft.Quantum.Canon;\n", + "open Microsoft.Quantum.Diagnostics;\n", + "\n", + "// Task 1. Warm up with simple bit manipulation\n", + "// (input will contain 3 qubits)\n", + "operation Task1(input : Qubit[], target : Qubit) : Unit is Adj {\n", + " within {\n", + " CNOT(input[2], input[0]);\n", + " CNOT(input[2], input[1]);\n", + " CNOT(input[1], input[0]);\n", + " } apply {\n", + " Controlled X([input[1], input[0]], target);\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "microsoft": { + "language": "qsharp" + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "%%qsharp\n", + "// Wrapper operation that allows you to observe the effects of the marking oracle by running it on a simulator.\n", + "operation Task1_DumpMachineWrapper() : Unit {\n", + " let N = 3;\n", + " use (input, target) = (Qubit[N], Qubit());\n", + " // Prepare an equal superposition of all input states in the input register.\n", + " ApplyToEach(H, input);\n", + " // Apply the oracle.\n", + " Task1(input, target);\n", + " // Print the state of the system after the oracle application.\n", + " DumpMachine();\n", + " ResetAll(input + [target]);\n", + "}\n", + "\n", + "// Wrapper operation that allows to run resource estimation for the task.\n", + "// This operation only allocates the qubits and applies the oracle once, not using any additional gates or measurements.\n", + "operation Task1_ResourceEstimationWrapper() : Unit {\n", + " let N = 3;\n", + " use (input, target) = (Qubit[N], Qubit());\n", + " Task1(input, target);\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "## Step 2. Run the code on a simulator to see what it does\n", + "You can also write your own code to explore the effects of the oracle (for example, applying it to different basis states and measuring the results)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# Note that in the output of this cell the target qubit corresponds to the rightmost bit\n", + "qsharp.config[\"dump.basisStateLabelingConvention\"]=\"Bitstring\"\n", + "qsharp.config[\"dump.phaseDisplayStyle\"]=\"None\"\n", + "# Uncomment the following line if you want to see only the entries with non-zero amplitudes\n", + "# qsharp.config[\"dump.truncateSmallAmplitudes\"]=True\n", + "Task1_DumpMachineWrapper.simulate()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "## Step 3. Evaluate the code using resource estimation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# If you're using this notebook in Azure Quantum hosted notebooks, remove the credential=\"CLI\" parameter!\n", + "# If you're using this notebook in qBraid, keep it\n", + "qsharp.azure.connect(\n", + " resourceId=\"/subscriptions/6d61051a-6e40-4845-a03a-3c5160bb5629/resourceGroups/AzureQuantum/providers/Microsoft.Quantum/Workspaces/iQuHack-Workspace-micpap25\",\n", + " location=\"East US\",\n", + " credential=\"CLI\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "qsharp.azure.target(\"microsoft.estimator\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# Update job name to a more descriptive string to make it easier to find it in Job Management tab later\n", + "result = qsharp.azure.execute(Task1_ResourceEstimationWrapper, jobName=\"RE for the task 1\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# If you need to pull up the results of an old job, use its job ID with qsharp.azure.output command\n", + "# result = qsharp.azure.output(\"...\")\n", + "result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# The function that extracts the relevant resource information from the resource estimation job results and produces your absolute score.\n", + "def evaluate_results(res) : \n", + " width = res['physicalCounts']['breakdown']['algorithmicLogicalQubits']\n", + " depth = res['physicalCounts']['breakdown']['algorithmicLogicalDepth']\n", + " print(f\"Logical algorithmic qubits = {width}\")\n", + " print(f\"Algorithmic depth = {depth}\")\n", + " print(f\"Score = {width * depth}\")\n", + " return width * depth\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "evaluate_results(result)" + ] + } + ], + "metadata": { + "kernel_info": { + "name": "python3" + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9" + }, + "nteract": { + "version": "nteract-front-end@1.0.0" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/team_solutions/Quantum Wranglers/iQuHack-challenge-2023-task2.ipynb b/team_solutions/Quantum Wranglers/iQuHack-challenge-2023-task2.ipynb new file mode 100644 index 0000000..bc06808 --- /dev/null +++ b/team_solutions/Quantum Wranglers/iQuHack-challenge-2023-task2.ipynb @@ -0,0 +1,469 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "# MIT iQuHack Microsoft Challenge: Optimizing Quantum Oracles, Task 2\n", + "\n", + "To work on this task,\n", + "1. Use the notebook for this task. Each of the notebooks in the repository has the code of the corresponding task.\n", + "2. Update your team name and Slack ID variables in the next code cell (you can use different Slack IDs for different tasks if different team members work on them, but make sure to use the same team name throughout the Hackathon). Do not change the task variable!\n", + "3. Work on your task in the cell that contains operation `Task2`! Your goal is to rewrite the code so that it maintains its correctness, but requires as few resources as possible. See `evaluate_results` function for details on how your absolute score for the task is calculated.\n", + "4. Submit your task using qBraid. Use the Share Notebook feature on qBraid (See File > Share Notebook) and enter the email rickyyoung@qbraid.com. Once you click submit, if the share notebook feature works correctly, it should show that you receive no errors and the email you entered will disappear. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "Log in to Azure (once per session, don't need to do it if running from Azure workspace)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "!az login" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "## Step 1. Write the code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# Run this code cell to import the modules required to work with Q# and Azure\n", + "import qsharp\n", + "from qsharp import azure" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "teamname=\"Quantum Wranglers\" # Update this field with your team name\n", + "task=[\"task2\"]\n", + "slack_id=\"U04JVDTAG4E\" # Update this field with Slack ID of the person who worked on this task as the troubleshooting contact" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# You don't need to run this cell, it defines Q# operations as Python types to keep IntelliSense happy\n", + "Task2_DumpMachineWrapper : qsharp.QSharpCallable = None\n", + "Task2_ResourceEstimationWrapper : qsharp.QSharpCallable = None" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "**The complete code for Task 2 should be in this cell.** \n", + "This cell can include additional open statements and helper operations and functions if your solution needs them. \n", + "If you define helper operations in other cells, they will not be picked up by the grader!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "microsoft": { + "language": "qsharp" + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "%%qsharp\n", + "open Microsoft.Quantum.Canon;\n", + "open Microsoft.Quantum.Diagnostics;\n", + "\n", + "// Task 2. Celebrate MIT iQuHack!\n", + "// (input will contain 5 qubits)\n", + "operation Task2(input : Qubit[], target : Qubit) : Unit is Adj {\n", + " // Necessary\n", + " \n", + " ControlledOnInt(13, X)(input, target); // M\n", + " ControlledOnInt( 8, X)(input, target); // H\n", + " \n", + " // Can be simplified (pairs)\n", + " \n", + " //ControlledOnInt(20, X)(input, target); // T\n", + " //ControlledOnInt(21, X)(input, target); // U\n", + " \n", + " X(input[1]);\n", + " X(input[3]);\n", + " Controlled X([input[1], input[2], input[3], input[4]], target);\n", + " X(input[1]);\n", + " X(input[3]);\n", + " \n", + " \n", + " //ControlledOnInt(17, X)(input, target); // Q\n", + " //ControlledOnInt( 1, X)(input, target); // A\n", + " \n", + " X(input[1]);\n", + " X(input[2]);\n", + " X(input[3]);\n", + " Controlled X([input[0], input[1], input[2], input[3]], target);\n", + " X(input[1]);\n", + " X(input[2]);\n", + " X(input[3]);\n", + " \n", + " //ControlledOnInt( 3, X)(input, target); // C\n", + " //ControlledOnInt(11, X)(input, target); // K\n", + " \n", + " X(input[2]);\n", + " X(input[4]);\n", + " Controlled X([input[0], input[1], input[2], input[4]], target);\n", + " X(input[2]);\n", + " X(input[4]);\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "microsoft": { + "language": "qsharp" + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "%%qsharp\n", + "// Wrapper operation that allows you to observe the effects of the marking oracle by running it on a simulator.\n", + "operation Task2_DumpMachineWrapper() : Unit {\n", + " let N = 5;\n", + " use (input, target) = (Qubit[N], Qubit());\n", + " // Prepare an equal superposition of all input states in the input register.\n", + " ApplyToEach(H, input);\n", + " // Apply the oracle.\n", + " Task2(input, target);\n", + " // Print the state of the system after the oracle application.\n", + " DumpMachine();\n", + " ResetAll(input + [target]);\n", + "}\n", + "\n", + "// Wrapper operation that allows to run resource estimation for the task.\n", + "// This operation only allocates the qubits and applies the oracle once, not using any additional gates or measurements.\n", + "operation Task2_ResourceEstimationWrapper() : Unit {\n", + " let N = 5;\n", + " use (input, target) = (Qubit[N], Qubit());\n", + " Task2(input, target);\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "## Step 2. Run the code on a simulator to see what it does\n", + "You can also write your own code to explore the effects of the oracle (for example, applying it to different basis states and measuring the results)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# Note that in the output of this cell the target qubit corresponds to the rightmost bit\n", + "qsharp.config[\"dump.basisStateLabelingConvention\"]=\"Bitstring\"\n", + "qsharp.config[\"dump.phaseDisplayStyle\"]=\"None\"\n", + "# Uncomment the following line if you want to see only the entries with non-zero amplitudes\n", + "# qsharp.config[\"dump.truncateSmallAmplitudes\"]=True\n", + "Task2_DumpMachineWrapper.simulate()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "## Step 3. Evaluate the code using resource estimation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# If you're using this notebook in Azure Quantum hosted notebooks, remove the credential=\"CLI\" parameter!\n", + "# If you're using this notebook in qBraid, keep it\n", + "qsharp.azure.connect(\n", + " resourceId=\"/subscriptions/6d61051a-6e40-4845-a03a-3c5160bb5629/resourceGroups/AzureQuantum/providers/Microsoft.Quantum/Workspaces/iQuHack-Workspace-micpap25\",\n", + " location=\"East US\",\n", + " credential=\"CLI\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "qsharp.azure.target(\"microsoft.estimator\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# Update job name to a more descriptive string to make it easier to find it in Job Management tab later\n", + "result = qsharp.azure.execute(Task2_ResourceEstimationWrapper, jobName=\"RE for the task 2\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# If you need to pull up the results of an old job, use its job ID with qsharp.azure.output command\n", + "# result = qsharp.azure.output(\"...\")\n", + "result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# The function that extracts the relevant resource information from the resource estimation job results and produces your absolute score.\n", + "def evaluate_results(res) : \n", + " width = res['physicalCounts']['breakdown']['algorithmicLogicalQubits']\n", + " depth = res['physicalCounts']['breakdown']['algorithmicLogicalDepth']\n", + " print(f\"Logical algorithmic qubits = {width}\")\n", + " print(f\"Algorithmic depth = {depth}\")\n", + " print(f\"Score = {width * depth}\")\n", + " return width * depth\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "evaluate_results(result)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%trace Task2_ResourceEstimationWrapper" + ] + } + ], + "metadata": { + "kernel_info": { + "name": "python3" + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9" + }, + "nteract": { + "version": "nteract-front-end@1.0.0" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/team_solutions/Quantum Wranglers/iQuHack-challenge-2023-task3.ipynb b/team_solutions/Quantum Wranglers/iQuHack-challenge-2023-task3.ipynb new file mode 100644 index 0000000..c5d3c7e --- /dev/null +++ b/team_solutions/Quantum Wranglers/iQuHack-challenge-2023-task3.ipynb @@ -0,0 +1,508 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "# MIT iQuHack Microsoft Challenge: Optimizing Quantum Oracles, Task 3\n", + "\n", + "To work on this task,\n", + "1. Use the notebook for this task. Each of the notebooks in the repository has the code of the corresponding task.\n", + "2. Update your team name and Slack ID variables in the next code cell (you can use different Slack IDs for different tasks if different team members work on them, but make sure to use the same team name throughout the Hackathon). Do not change the task variable!\n", + "3. Work on your task in the cell that contains operation `Task3`! Your goal is to rewrite the code so that it maintains its correctness, but requires as few resources as possible. See `evaluate_results` function for details on how your absolute score for the task is calculated.\n", + "4. Submit your task using qBraid. Use the Share Notebook feature on qBraid (See File > Share Notebook) and enter the email rickyyoung@qbraid.com. Once you click submit, if the share notebook feature works correctly, it should show that you receive no errors and the email you entered will disappear. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "Log in to Azure (once per session, don't need to do it if running from Azure workspace)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "!az login" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "## Step 1. Write the code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# Run this code cell to import the modules required to work with Q# and Azure\n", + "import qsharp\n", + "from qsharp import azure" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "teamname=\"Quantum Wranglers\" # Update this field with your team name\n", + "task=[\"task3\"]\n", + "slack_id=\"U04JZ2Q9Z9B\" # Update this field with Slack ID of the person who worked on this task as the troubleshooting contact" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# You don't need to run this cell, it defines Q# operations as Python types to keep IntelliSense happy\n", + "Task3_DumpMachineWrapper : qsharp.QSharpCallable = None\n", + "Task3_ResourceEstimationWrapper : qsharp.QSharpCallable = None" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "**The complete code for Task 3 should be in this cell.** \n", + "This cell can include additional open statements and helper operations and functions if your solution needs them. \n", + "If you define helper operations in other cells, they will not be picked up by the grader!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "microsoft": { + "language": "qsharp" + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "%%qsharp\n", + "open Microsoft.Quantum.Canon;\n", + "open Microsoft.Quantum.Diagnostics;\n", + "\n", + "// Task 3. \n", + "// (input will contain 6 qubits)\n", + "operation Task3(input : Qubit[], target : Qubit) : Unit is Adj {\n", + " //for i in [7, 11, 13, 14, 19, 21, 22, 25, 26, 28, 35, 37, 38, 41, 42, 44, 49, 50, 52, 56] {\n", + " // ControlledOnInt(i, X)(input, target);\n", + " //}\n", + " \n", + " // optimization 1:\n", + " //X(input[5]);\n", + " //Controlled Task3_0([input[5]], (input[...4], target));\n", + " //X(input[5]);\n", + " //Controlled Task3_1([input[5]], (input[...4], target));\n", + " \n", + " // optimization 2:\n", + " within {\n", + " X(input[5]);\n", + " X(input[4]);\n", + " }\n", + " apply {\n", + " Controlled Task3_00([input[5], input[4]], (input[...3], target)); // top bits: 00\n", + " }\n", + " Controlled Task3_11([input[5], input[4]], (input[...3], target)); // top bits: 11\n", + " \n", + " within {\n", + " CNOT(input[5], input[4]); // input[4] = input[5] ^ input[4]\n", + " }\n", + " apply {\n", + " Controlled Task3_01([input[4]], (input[...3], target)); // top bits: 01 or 10\n", + " }\n", + "}\n", + "\n", + "operation Task3_0(input : Qubit[], target : Qubit) : Unit is Adj + Ctl {\n", + " //for i in [7, 11, 13, 14, 19, 21, 22, 25, 26, 28] {\n", + " // ControlledOnInt(i, X)(input, target);\n", + " //}\n", + " X(input[4]);\n", + " Controlled Task3_00([input[4]], (input[...3], target));\n", + " X(input[4]);\n", + " Controlled Task3_01([input[4]], (input[...3], target));\n", + "}\n", + "\n", + "operation Task3_00(input : Qubit[], target : Qubit) : Unit is Adj + Ctl {\n", + " for i in [7, 11, 13, 14] {\n", + " ControlledOnInt(i, X)(input, target);\n", + " }\n", + "}\n", + "\n", + "operation Task3_01(input : Qubit[], target : Qubit) : Unit is Adj + Ctl {\n", + " for i in [3, 5, 6, 9, 10, 12] {\n", + " ControlledOnInt(i, X)(input, target);\n", + " }\n", + "}\n", + "\n", + "operation Task3_1(input : Qubit[], target : Qubit) : Unit is Adj + Ctl {\n", + " //for i in [3, 5, 6, 9, 10, 12, 17, 18, 20, 24] {\n", + " // ControlledOnInt(i, X)(input, target);\n", + " //}\n", + " X(input[4]);\n", + " Controlled Task3_01([input[4]], (input[...3], target));\n", + " X(input[4]);\n", + " Controlled Task3_11([input[4]], (input[...3], target));\n", + "}\n", + "\n", + "// same as Task3_01, we can optimize by re-using the circuit\n", + "//operation Task3_10(input : Qubit[], target : Qubit) : Unit is Adj + Ctl {\n", + "// for i in [3, 5, 6, 9, 10, 12] {\n", + "// ControlledOnInt(i, X)(input, target);\n", + "// }\n", + "//}\n", + "\n", + "operation Task3_11(input : Qubit[], target : Qubit) : Unit is Adj + Ctl {\n", + " for i in [1, 2, 4, 8] {\n", + " ControlledOnInt(i, X)(input, target);\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "microsoft": { + "language": "qsharp" + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "%%qsharp\n", + "// Wrapper operation that allows you to observe the effects of the marking oracle by running it on a simulator.\n", + "operation Task3_DumpMachineWrapper() : Unit {\n", + " let N = 6;\n", + " use (input, target) = (Qubit[N], Qubit());\n", + " // Prepare an equal superposition of all input states in the input register.\n", + " ApplyToEach(H, input);\n", + " // Apply the oracle.\n", + " Task3(input, target);\n", + " // Print the state of the system after the oracle application.\n", + " DumpMachine();\n", + " ResetAll(input + [target]);\n", + "}\n", + "\n", + "// Wrapper operation that allows to run resource estimation for the task.\n", + "// This operation only allocates the qubits and applies the oracle once, not using any additional gates or measurements.\n", + "operation Task3_ResourceEstimationWrapper() : Unit {\n", + " let N = 6;\n", + " use (input, target) = (Qubit[N], Qubit());\n", + " Task3(input, target);\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "## Step 2. Run the code on a simulator to see what it does\n", + "You can also write your own code to explore the effects of the oracle (for example, applying it to different basis states and measuring the results)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# Note that in the output of this cell the target qubit corresponds to the rightmost bit\n", + "qsharp.config[\"dump.basisStateLabelingConvention\"]=\"Bitstring\"\n", + "qsharp.config[\"dump.phaseDisplayStyle\"]=\"None\"\n", + "# Uncomment the following line if you want to see only the entries with non-zero amplitudes\n", + "# qsharp.config[\"dump.truncateSmallAmplitudes\"]=True\n", + "Task3_DumpMachineWrapper.simulate()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "## Step 3. Evaluate the code using resource estimation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# If you're using this notebook in Azure Quantum hosted notebooks, remove the credential=\"CLI\" parameter!\n", + "# If you're using this notebook in qBraid, keep it\n", + "qsharp.azure.connect(\n", + " resourceId=\"/subscriptions/1b6547eb-50f1-4733-bd8b-4b8c52ff49bd/resourceGroups/AzureQuantum/providers/Microsoft.Quantum/Workspaces/QuantumTests\",\n", + " location=\"West US\",\n", + " credential=\"CLI\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "qsharp.azure.target(\"microsoft.estimator\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# Update job name to a more descriptive string to make it easier to find it in Job Management tab later\n", + "result = qsharp.azure.execute(Task3_ResourceEstimationWrapper, jobName=\"RE for the task 3\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# If you need to pull up the results of an old job, use its job ID with qsharp.azure.output command\n", + "# result = qsharp.azure.output(\"...\")\n", + "result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# The function that extracts the relevant resource information from the resource estimation job results and produces your absolute score.\n", + "def evaluate_results(res) : \n", + " width = res['physicalCounts']['breakdown']['algorithmicLogicalQubits']\n", + " depth = res['physicalCounts']['breakdown']['algorithmicLogicalDepth']\n", + " print(f\"Logical algorithmic qubits = {width}\")\n", + " print(f\"Algorithmic depth = {depth}\")\n", + " print(f\"Score = {width * depth}\")\n", + " return width * depth\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "evaluate_results(result)" + ] + } + ], + "metadata": { + "kernel_info": { + "name": "python3" + }, + "kernelspec": { + "display_name": "Python 3 [Q#]", + "language": "python", + "name": "python3_qsharp_b54crn" + }, + "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.9.10" + }, + "nteract": { + "version": "nteract-front-end@1.0.0" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/team_solutions/Quantum Wranglers/iQuHack-challenge-2023-task7.ipynb b/team_solutions/Quantum Wranglers/iQuHack-challenge-2023-task7.ipynb new file mode 100644 index 0000000..495a6ed --- /dev/null +++ b/team_solutions/Quantum Wranglers/iQuHack-challenge-2023-task7.ipynb @@ -0,0 +1,433 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "# MIT iQuHack Microsoft Challenge: Optimizing Quantum Oracles, Task 7\n", + "\n", + "To work on this task,\n", + "1. Use the notebook for this task. Each of the notebooks in the repository has the code of the corresponding task.\n", + "2. Update your team name and Slack ID variables in the next code cell (you can use different Slack IDs for different tasks if different team members work on them, but make sure to use the same team name throughout the Hackathon). Do not change the task variable!\n", + "3. Work on your task in the cell that contains operation `Task7`! Your goal is to rewrite the code so that it maintains its correctness, but requires as few resources as possible. See `evaluate_results` function for details on how your absolute score for the task is calculated.\n", + "4. Submit your task using qBraid. Use the Share Notebook feature on qBraid (See File > Share Notebook) and enter the email rickyyoung@qbraid.com. Once you click submit, if the share notebook feature works correctly, it should show that you receive no errors and the email you entered will disappear. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "Log in to Azure (once per session, don't need to do it if running from Azure workspace)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "!az login" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "## Step 1. Write the code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# Run this code cell to import the modules required to work with Q# and Azure\n", + "import qsharp\n", + "from qsharp import azure" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "teamname=\"Quantum Wranglers\" # Update this field with your team name\n", + "task=[\"task7\"]\n", + "slack_id=\"U04JVDTAG4E\" # Update this field with Slack ID of the person who worked on this task as the troubleshooting contact" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# You don't need to run this cell, it defines Q# operations as Python types to keep IntelliSense happy\n", + "Task7_DumpMachineWrapper : qsharp.QSharpCallable = None\n", + "Task7_ResourceEstimationWrapper : qsharp.QSharpCallable = None" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "**The complete code for Task 7 should be in this cell.** \n", + "This cell can include additional open statements and helper operations and functions if your solution needs them. \n", + "If you define helper operations in other cells, they will not be picked up by the grader!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "microsoft": { + "language": "qsharp" + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "%%qsharp\n", + "open Microsoft.Quantum.Canon;\n", + "open Microsoft.Quantum.Diagnostics;\n", + "\n", + "// Task 7. \n", + "// (input will contain 7 qubits)\n", + "operation Task7(input : Qubit[], target : Qubit) : Unit is Adj {\n", + " for i in [9,18,19,25,36,37,38,39,41,50,51,57,72,73,74,75,76,77,78,79,82,83,89,100,101,102,103,105,114,115,121] {\n", + " ControlledOnInt(i, X)(input, target);\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "microsoft": { + "language": "qsharp" + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "%%qsharp\n", + "// Wrapper operation that allows you to observe the effects of the marking oracle by running it on a simulator.\n", + "operation Task7_DumpMachineWrapper() : Unit {\n", + " let N = 7;\n", + " use (input, target) = (Qubit[N], Qubit());\n", + " // Prepare an equal superposition of all input states in the input register.\n", + " ApplyToEach(H, input);\n", + " // Apply the oracle.\n", + " Task7(input, target);\n", + " // Print the state of the system after the oracle application.\n", + " DumpMachine();\n", + " ResetAll(input + [target]);\n", + "}\n", + "\n", + "// Wrapper operation that allows to run resource estimation for the task.\n", + "// This operation only allocates the qubits and applies the oracle once, not using any additional gates or measurements.\n", + "operation Task7_ResourceEstimationWrapper() : Unit {\n", + " let N = 7;\n", + " use (input, target) = (Qubit[N], Qubit());\n", + " Task7(input, target);\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "## Step 2. Run the code on a simulator to see what it does\n", + "You can also write your own code to explore the effects of the oracle (for example, applying it to different basis states and measuring the results)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# Note that in the output of this cell the target qubit corresponds to the rightmost bit\n", + "qsharp.config[\"dump.basisStateLabelingConvention\"]=\"Bitstring\"\n", + "qsharp.config[\"dump.phaseDisplayStyle\"]=\"None\"\n", + "# Uncomment the following line if you want to see only the entries with non-zero amplitudes\n", + "qsharp.config[\"dump.truncateSmallAmplitudes\"]=True\n", + "Task7_DumpMachineWrapper.simulate()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nteract": { + "transient": { + "deleting": false + } + } + }, + "source": [ + "## Step 3. Evaluate the code using resource estimation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# If you're using this notebook in Azure Quantum hosted notebooks, remove the credential=\"CLI\" parameter!\n", + "# If you're using this notebook in qBraid, keep it\n", + "qsharp.azure.connect(\n", + " resourceId=\"/subscriptions/6d61051a-6e40-4845-a03a-3c5160bb5629/resourceGroups/AzureQuantum/providers/Microsoft.Quantum/Workspaces/iQuHack-Workspace-micpap25\",\n", + " location=\"East US\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "qsharp.azure.target(\"microsoft.estimator\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# Update job name to a more descriptive string to make it easier to find it in Job Management tab later\n", + "result = qsharp.azure.execute(Task7_ResourceEstimationWrapper, jobName=\"RE for the task 7\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# If you need to pull up the results of an old job, use its job ID with qsharp.azure.output command\n", + "# result = qsharp.azure.output(\"...\")\n", + "result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "# The function that extracts the relevant resource information from the resource estimation job results and produces your absolute score.\n", + "def evaluate_results(res) : \n", + " width = res['physicalCounts']['breakdown']['algorithmicLogicalQubits']\n", + " depth = res['physicalCounts']['breakdown']['algorithmicLogicalDepth']\n", + " print(f\"Logical algorithmic qubits = {width}\")\n", + " print(f\"Algorithmic depth = {depth}\")\n", + " print(f\"Score = {width * depth}\")\n", + " return width * depth\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "outputs_hidden": false, + "source_hidden": false + }, + "nteract": { + "transient": { + "deleting": false + } + } + }, + "outputs": [], + "source": [ + "evaluate_results(result)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernel_info": { + "name": "python3" + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9" + }, + "nteract": { + "version": "nteract-front-end@1.0.0" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}