{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fae381bf",
   "metadata": {},
   "source": [
    "# Submitting Cirq Circuits to Azure Quantum with the QDK\n",
    "\n",
    "This notebook shows how to take a Cirq `Circuit`, export it to OpenQASM 3, compile that OpenQASM 3 source to QIR using the Quantum Development Kit (QDK) Python APIs, and submit it as a job to an Azure Quantum target."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d696a7c",
   "metadata": {},
   "source": [
    "The workflow demonstrated here:\n",
    "\n",
    "1. Build (or load) a Cirq `Circuit`.\n",
    "2. Convert it to OpenQASM 3 text via `circuit.to_qasm(version=\"3.0\")`.\n",
    "3. Compile the OpenQASM 3 source to QIR with `qdk.openqasm.compile`.\n",
    "4. Connect to (or create) an Azure Quantum workspace using `qdk.azure.Workspace`.\n",
    "5. Pick a target (e.g., a simulator like `rigetti.sim.qvm`).\n",
    "6. Submit the QIR payload and retrieve measurement results."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22d78680",
   "metadata": {},
   "source": [
    "## Prerequisites\n",
    "\n",
    "Ensure the `qdk` package is installed with `azure` and `cirq` extras. If not, install dependencies below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb556730",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install \"qdk[azure,cirq]\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20b9ed32",
   "metadata": {},
   "source": [
    "After installing, restart the kernel if necessary. Verify imports:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7531d5a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cirq, qdk, qdk.azure  # should import without errors"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2b111db",
   "metadata": {},
   "source": [
    "## Submitting a simple Cirq circuit\n",
    "We'll build a small circuit creating a superposition on one qubit and flipping another, then measuring both. Afterwards we submit it to an Azure Quantum target."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db4e96e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Build a simple circuit\n",
    "q0, q1 = cirq.LineQubit.range(2)\n",
    "simple_circuit = cirq.Circuit(\n",
    "    cirq.H(q0),\n",
    "    cirq.measure(q0, key='m0'),\n",
    "    cirq.X(q1),\n",
    "    cirq.measure(q1, key='m1'),\n",
    ")\n",
    "print(simple_circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f582bc13",
   "metadata": {},
   "source": [
    "## Configure Azure Quantum workspace connection\n",
    "Replace the placeholder values below with your own subscription, resource group, workspace name, and location."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0344527e",
   "metadata": {},
   "outputs": [],
   "source": [
    "subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'\n",
    "resource_group = 'myresourcegroup'\n",
    "workspace_name = 'myworkspace'\n",
    "location = 'westus'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f336702",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qdk.openqasm import compile\n",
    "from qdk.azure import Workspace\n",
    "from qdk import TargetProfile\n",
    "\n",
    "def submit_cirq_circuit_to_azure(circuit: cirq.Circuit, target_name: str, name: str, shots: int = 100):\n",
    "    # 1. Export to OpenQASM 3\n",
    "    qasm3_str = circuit.to_qasm(version='3.0')\n",
    "    # 2. Compile to QIR with a base target profile\n",
    "    qir = compile(qasm3_str, target_profile=TargetProfile.Base)\n",
    "    # 3. Connect workspace\n",
    "    workspace = Workspace(\n",
    "        subscription_id=subscription_id,\n",
    "        resource_group=resource_group,\n",
    "        name=workspace_name,\n",
    "        location=location,\n",
    "    )\n",
    "    # 4. Select target (string e.g. 'rigetti.sim.qvm' or other available target)\n",
    "    target = workspace.get_targets(target_name)\n",
    "    # 5. Submit QIR payload\n",
    "    job = target.submit(qir, name, shots=shots)\n",
    "    return job.get_results()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0436202",
   "metadata": {},
   "source": [
    "### Submit the simple circuit\n",
    "(Make sure you changed the workspace credentials above.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "417d7818",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Uncomment after setting workspace credentials\n",
    "# results = submit_cirq_circuit_to_azure(simple_circuit, 'rigetti.sim.qvm', 'cirq-simple-job')\n",
    "# print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1478e88a",
   "metadata": {},
   "source": [
    "## Notes\n",
    "- Ensure all measurement keys appear before any classical condition usage if you introduce classical controls.\n",
    "- Multi-target or custom gates may need full decomposition before submission if they produce unsupported classical constructs."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
