{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "90f546a9",
   "metadata": {},
   "source": [
    "# Submitting PennyLane Circuits to Azure Quantum with the QDK\n",
    "\n",
    "This notebook demonstrates end‑to‑end submission of a PennyLane `QNode` to Azure Quantum using the QDK compiler pipeline (OpenQASM 2 → QIR → job submission).\n",
    "\n",
    "**Workflow overview:**\n",
    "\n",
    "1. Create a `default.qubit` device and define a parameterized circuit (Hadamard + CNOT entanglement, then `RY(θ)` rotation).\n",
    "2. Draw the circuit for a quick structural sanity check.\n",
    "3. Export the bound PennyLane circuit to OpenQASM 2 using `qml.to_openqasm`.\n",
    "4. Compile the OpenQASM source to QIR with `qdk.openqasm.compile` (selecting a target profile).\n",
    "5. Submit the QIR payload to a chosen Azure Quantum target (e.g. `rigetti.sim.qvm`) with a shot count.\n",
    "6. Retrieve and display measurement results returned by the job."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "004e5982",
   "metadata": {},
   "source": [
    "## 1. Prerequisites\n",
    "\n",
    "This notebook assumes the `qdk`, Azure Quantum integration, and PennyLane packages are installed. You can install these with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfa160e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install \"qdk[azure]\" pennylane"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7f37bcd",
   "metadata": {},
   "source": [
    "This installs:\n",
    "- The base qdk package (compiler, OpenQASM/QIR tooling)\n",
    "- Azure Quantum client dependencies for submission\n",
    "- The PennyLane package\n",
    "\n",
    "After installing, restart the notebook kernel if it was already running. The following imports should run successfully:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7446ed1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pennylane as qml\n",
    "from qdk.openqasm import compile\n",
    "from qdk.azure import Workspace\n",
    "from qdk import TargetProfile"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f761649",
   "metadata": {},
   "source": [
    "## 2. Define a Simple Circuit\n",
    "We'll create a small parameterized circuit destined for export."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e91dd2d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "dev = qml.device('default.qubit', wires=2)\n",
    "\n",
    "@qml.qnode(dev)\n",
    "def small_ansatz(theta):\n",
    "    qml.H(0); qml.CNOT(wires=[0,1])\n",
    "    qml.RY(theta, wires=1)\n",
    "    return qml.expval(qml.PauliZ(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f94b7ac3",
   "metadata": {},
   "source": [
    "## 3. Draw for Inspection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2706bb1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(qml.draw(small_ansatz)(0.3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efd50528",
   "metadata": {},
   "source": [
    "## 4. Configure Azure Quantum workspace connection\n",
    "\n",
    "To connect to an Azure workspace replace the following variables with your own values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54206752",
   "metadata": {},
   "outputs": [],
   "source": [
    "subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'\n",
    "resource_group = 'myresourcegroup'\n",
    "workspace_name = 'myworkspace'\n",
    "location = 'westus'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "552ec2b0",
   "metadata": {},
   "source": [
    "## 5. Submitting the Circuit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9a0f59b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define parameters for the job submission\n",
    "target_name = 'rigetti.sim.qvm'\n",
    "target_profile = TargetProfile.Base\n",
    "job_name = 'pennylane-job'\n",
    "shots = 100\n",
    "\n",
    "# Define parameter for the circuit\n",
    "theta = 0.3\n",
    "\n",
    "# Specify Azure Quantum workspace\n",
    "workspace = Workspace(\n",
    "    subscription_id=subscription_id,\n",
    "    resource_group=resource_group,\n",
    "    name=workspace_name,\n",
    "    location=location,\n",
    ")\n",
    "\n",
    "# Compile the PennyLane circuit to QIR via OpenQASM\n",
    "qasm_str = qml.to_openqasm(small_ansatz)(theta)\n",
    "qir = compile(qasm_str, target_profile)\n",
    "\n",
    "# Submit the job to Azure Quantum\n",
    "target = workspace.get_targets(target_name)\n",
    "job = target.submit(qir, job_name, shots)\n",
    "\n",
    "# Print the results\n",
    "results = job.get_results()\n",
    "print(results)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
