microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
samples/notebooks/azure_submission.ipynb
168lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "metadata": {}, |
| 6 | "source": [ |
| 7 | "To connect to an Azure workspace replace the following variables with your own values." |
| 8 | ] |
| 9 | }, |
| 10 | { |
| 11 | "cell_type": "code", |
| 12 | "execution_count": null, |
| 13 | "metadata": {}, |
| 14 | "outputs": [], |
| 15 | "source": [ |
| 16 | "subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'\n", |
| 17 | "resource_group = 'myresourcegroup'\n", |
| 18 | "workspace_name = 'myworkspace'\n", |
| 19 | "location = 'westus'\n" |
| 20 | ] |
| 21 | }, |
| 22 | { |
| 23 | "cell_type": "markdown", |
| 24 | "metadata": {}, |
| 25 | "source": [ |
| 26 | "Import the Q# package. Set the target profile to be the base profile." |
| 27 | ] |
| 28 | }, |
| 29 | { |
| 30 | "cell_type": "code", |
| 31 | "execution_count": null, |
| 32 | "metadata": {}, |
| 33 | "outputs": [], |
| 34 | "source": [ |
| 35 | "from qdk import qsharp\n", |
| 36 | "\n", |
| 37 | "qsharp.init(target_profile=qsharp.TargetProfile.Base)\n" |
| 38 | ] |
| 39 | }, |
| 40 | { |
| 41 | "cell_type": "markdown", |
| 42 | "metadata": {}, |
| 43 | "source": [ |
| 44 | "The following will generate a compiler error because it uses a feature unsupported by the base profile." |
| 45 | ] |
| 46 | }, |
| 47 | { |
| 48 | "cell_type": "code", |
| 49 | "execution_count": null, |
| 50 | "metadata": { |
| 51 | "vscode": { |
| 52 | "languageId": "qsharp" |
| 53 | } |
| 54 | }, |
| 55 | "outputs": [], |
| 56 | "source": [ |
| 57 | "%%qsharp\n", |
| 58 | "\n", |
| 59 | "operation NotAllowed() : Unit {\n", |
| 60 | " use q = Qubit();\n", |
| 61 | " let result = M(q);\n", |
| 62 | " if (result == Zero) {\n", |
| 63 | " Message(\"The result is Zero\");\n", |
| 64 | " }\n", |
| 65 | " Reset(q);\n", |
| 66 | "}\n" |
| 67 | ] |
| 68 | }, |
| 69 | { |
| 70 | "cell_type": "markdown", |
| 71 | "metadata": {}, |
| 72 | "source": [ |
| 73 | "Define some more Q# operations. This should compile without error." |
| 74 | ] |
| 75 | }, |
| 76 | { |
| 77 | "cell_type": "code", |
| 78 | "execution_count": null, |
| 79 | "metadata": { |
| 80 | "vscode": { |
| 81 | "languageId": "qsharp" |
| 82 | } |
| 83 | }, |
| 84 | "outputs": [], |
| 85 | "source": [ |
| 86 | "%%qsharp\n", |
| 87 | "\n", |
| 88 | "operation Random() : Result {\n", |
| 89 | " use q = Qubit();\n", |
| 90 | " H(q);\n", |
| 91 | " let result = M(q);\n", |
| 92 | " Reset(q);\n", |
| 93 | " return result\n", |
| 94 | "}\n", |
| 95 | "\n", |
| 96 | "operation RandomNBits(N: Int): Result[] {\n", |
| 97 | " mutable results = [];\n", |
| 98 | " for i in 0 .. N - 1 {\n", |
| 99 | " let r = Random();\n", |
| 100 | " set results += [r];\n", |
| 101 | " }\n", |
| 102 | " return results\n", |
| 103 | "}\n" |
| 104 | ] |
| 105 | }, |
| 106 | { |
| 107 | "cell_type": "markdown", |
| 108 | "metadata": {}, |
| 109 | "source": [ |
| 110 | "Submit to Azure Quantum with a custom entry expression.\n", |
| 111 | "\n", |
| 112 | "Make sure the `azure-quantum` package is installed." |
| 113 | ] |
| 114 | }, |
| 115 | { |
| 116 | "cell_type": "code", |
| 117 | "execution_count": null, |
| 118 | "metadata": {}, |
| 119 | "outputs": [], |
| 120 | "source": [ |
| 121 | "%pip install azure-quantum\n" |
| 122 | ] |
| 123 | }, |
| 124 | { |
| 125 | "cell_type": "code", |
| 126 | "execution_count": null, |
| 127 | "metadata": {}, |
| 128 | "outputs": [], |
| 129 | "source": [ |
| 130 | "import azure.quantum\n", |
| 131 | "\n", |
| 132 | "operation = qsharp.compile(\"RandomNBits(4)\")\n", |
| 133 | "\n", |
| 134 | "workspace = azure.quantum.Workspace(\n", |
| 135 | " subscription_id=subscription_id,\n", |
| 136 | " resource_group=resource_group,\n", |
| 137 | " name=workspace_name,\n", |
| 138 | " location=location,\n", |
| 139 | ")\n", |
| 140 | "target = workspace.get_targets(\"rigetti.sim.qvm\")\n", |
| 141 | "job = target.submit(operation, \"my-azure-quantum-job\", shots=100)\n", |
| 142 | "job.get_results()\n" |
| 143 | ] |
| 144 | } |
| 145 | ], |
| 146 | "metadata": { |
| 147 | "kernelspec": { |
| 148 | "display_name": "qsharp", |
| 149 | "language": "python", |
| 150 | "name": "python3" |
| 151 | }, |
| 152 | "language_info": { |
| 153 | "codemirror_mode": { |
| 154 | "name": "ipython", |
| 155 | "version": 3 |
| 156 | }, |
| 157 | "file_extension": ".py", |
| 158 | "mimetype": "text/x-python", |
| 159 | "name": "python", |
| 160 | "nbconvert_exporter": "python", |
| 161 | "pygments_lexer": "ipython3", |
| 162 | "version": "3.11.7" |
| 163 | }, |
| 164 | "orig_nbformat": 4 |
| 165 | }, |
| 166 | "nbformat": 4, |
| 167 | "nbformat_minor": 2 |
| 168 | } |