microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
9831093db0098b3a3e55cbadf3929222d7dd4805

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/notebooks/azure_submission.ipynb

168lines · modepreview

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To connect to an Azure workspace replace the following variables with your own values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'\n",
    "resource_group = 'myresourcegroup'\n",
    "workspace_name = 'myworkspace'\n",
    "location = 'westus'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import the Q# package. Set the target profile to be the base profile."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import qsharp\n",
    "\n",
    "qsharp.init(target_profile=qsharp.TargetProfile.Base)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following will generate a compiler error because it uses a feature unsupported by the base profile."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "qsharp"
    }
   },
   "outputs": [],
   "source": [
    "%%qsharp\n",
    "\n",
    "operation NotAllowed() : Unit {\n",
    "    use q = Qubit();\n",
    "    let result = M(q);\n",
    "    if (result == Zero) {\n",
    "        Message(\"The result is Zero\");\n",
    "    }\n",
    "    Reset(q);\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define some more Q# operations. This should compile without error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "qsharp"
    }
   },
   "outputs": [],
   "source": [
    "%%qsharp\n",
    "\n",
    "operation Random() : Result {\n",
    "    use q = Qubit();\n",
    "    H(q);\n",
    "    let result = M(q);\n",
    "    Reset(q);\n",
    "    return result\n",
    "}\n",
    "\n",
    "operation RandomNBits(N: Int): Result[] {\n",
    "    mutable results = [];\n",
    "    for i in 0 .. N - 1 {\n",
    "        let r = Random();\n",
    "        set results += [r];\n",
    "    }\n",
    "    return results\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Submit to Azure Quantum with a custom entry expression.\n",
    "\n",
    "Make sure the `azure-quantum` package is installed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install azure-quantum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import azure.quantum\n",
    "\n",
    "operation = qsharp.compile(\"RandomNBits(4)\")\n",
    "\n",
    "workspace = azure.quantum.Workspace(\n",
    "    subscription_id=subscription_id,\n",
    "    resource_group=resource_group,\n",
    "    name=workspace_name,\n",
    "    location=location,\n",
    ")\n",
    "target = workspace.get_targets(\"rigetti.sim.qvm\")\n",
    "job = target.submit(operation, \"my-azure-quantum-job\", shots=100)\n",
    "job.get_results()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "qsharp",
   "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.11.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}