{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b1c8553d",
   "metadata": {},
   "source": [
    "# Manual QIR Circuit Submission to Azure Quantum\n",
    "\n",
    "This notebook demonstrates how to submit quantum circuits to Azure Quantum via an explicit **OpenQASM 3 → QIR** compilation path, covering both **Qiskit** and **Cirq** circuits.\n",
    "\n",
    "This approach gives full visibility and control over intermediate compilation artifacts. It is useful when you need to inspect or modify OpenQASM or QIR before submission, target backends that require raw QIR, or share a single submission pipeline across frameworks.\n",
    "\n",
    "For the recommended getting-started experience for each framework, see:\n",
    "- `qiskit_submission_to_azure.ipynb`\n",
    "- `cirq_submission_to_azure.ipynb`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "906a9b09",
   "metadata": {},
   "source": [
    "The same compilation pipeline works for both Qiskit and Cirq circuits:\n",
    "\n",
    "1. Build a circuit in Qiskit or Cirq.\n",
    "2. Export it to **OpenQASM 3** text — this intermediate representation can be inspected or modified.\n",
    "3. Compile the OpenQASM 3 source to **QIR** with `qdk.openqasm.compile` (choose a `TargetProfile`).\n",
    "4. Connect to an Azure Quantum workspace with `qdk.azure.Workspace`.\n",
    "5. Select a target (e.g. a simulator such as `rigetti.sim.qvm`).\n",
    "6. Submit the QIR payload (`target.submit(qir, job_name, shots)`) and retrieve results (`job.get_results()`)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "243e083e",
   "metadata": {},
   "source": [
    "## Prerequisites\n",
    "\n",
    "Install the `qdk` package with `azure` and whichever framework extras you need. Install both if you plan to run both sections of this notebook:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "afb12537",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install \"qdk[azure,cirq,qiskit]\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83d2907f",
   "metadata": {},
   "source": [
    "After installing, restart the notebook kernel if it was already running. Verify installation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e3b8778",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cirq, qiskit, qdk, qdk.azure  # should import without errors"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c503ee7",
   "metadata": {},
   "source": [
    "## Configure Azure Quantum workspace connection\n",
    "\n",
    "Replace the placeholder values with your own workspace details. These are shared across both the Qiskit and Cirq examples below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20dc0946",
   "metadata": {},
   "outputs": [],
   "source": [
    "subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'\n",
    "resource_group = 'myresourcegroup'\n",
    "workspace_name = 'myworkspace'\n",
    "location = 'westus'\n",
    "\n",
    "# Replace with any QIR-capable target name from your workspace\n",
    "target_name = \"rigetti.sim.qvm\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbee43f6",
   "metadata": {},
   "source": [
    "---\n",
    "## Part A: Submitting a Qiskit circuit\n",
    "\n",
    "### Build a Qiskit circuit\n",
    "\n",
    "We use a Bell-state circuit — two entangled qubits — to demonstrate the Qiskit path.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d0e8013",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import QuantumCircuit\n",
    "\n",
    "qiskit_circuit = QuantumCircuit(2, 2)\n",
    "qiskit_circuit.h(0)\n",
    "qiskit_circuit.cx(0, 1)\n",
    "qiskit_circuit.measure([0, 1], [0, 1])\n",
    "\n",
    "qiskit_circuit.draw(output=\"text\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5848792",
   "metadata": {},
   "source": [
    "### Export to OpenQASM 3\n",
    "\n",
    "`qiskit.qasm3.dumps` converts a Qiskit circuit to an OpenQASM 3 string. This intermediate text can be inspected or modified before compilation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85413498",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import qasm3\n",
    "\n",
    "qasm3_str_qiskit = qasm3.dumps(qiskit_circuit)\n",
    "print(qasm3_str_qiskit)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43904206",
   "metadata": {},
   "source": [
    "### Compile to QIR and submit\n",
    "\n",
    "Compile the OpenQASM 3 source to QIR, then connect to the workspace and submit the payload."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6f32b4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qdk.openqasm import compile\n",
    "from qdk import TargetProfile\n",
    "from qdk.azure import Workspace\n",
    "\n",
    "qir_qiskit = compile(qasm3_str_qiskit, target_profile=TargetProfile.Base)\n",
    "\n",
    "workspace = Workspace(\n",
    "    subscription_id=subscription_id,\n",
    "    resource_group=resource_group,\n",
    "    name=workspace_name,\n",
    "    location=location,\n",
    ")\n",
    "\n",
    "target = workspace.get_targets(target_name)\n",
    "job = target.submit(qir_qiskit, \"qiskit-manual-job\", shots=100)\n",
    "print(\"Job submitted. Waiting for results...\")\n",
    "\n",
    "results = job.get_results()\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c580ff95",
   "metadata": {},
   "source": [
    "---\n",
    "## Part B: Submitting a Cirq circuit\n",
    "\n",
    "### Build a Cirq circuit\n",
    "\n",
    "We create a simple Bell-state circuit with two named measurement keys."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d3e980e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cirq\n",
    "\n",
    "q0, q1 = cirq.LineQubit.range(2)\n",
    "cirq_circuit = cirq.Circuit(\n",
    "    cirq.H(q0),\n",
    "    cirq.CNOT(q0, q1),\n",
    "    cirq.measure(q0, key=\"q0\"),\n",
    "    cirq.measure(q1, key=\"q1\"),\n",
    ")\n",
    "print(cirq_circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7978d457",
   "metadata": {},
   "source": [
    "### Export to OpenQASM 3\n",
    "\n",
    "`circuit.to_qasm(version=\"3.0\")` converts a Cirq circuit to an OpenQASM 3 string. This intermediate text can be inspected or modified before compilation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8aead8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "qasm3_str_cirq = cirq_circuit.to_qasm(version=\"3.0\")\n",
    "print(qasm3_str_cirq)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba3547ee",
   "metadata": {},
   "source": [
    "### Compile to QIR and submit\n",
    "\n",
    "The compile and submission steps are identical to Part A — `compile` and `workspace` are already available from the cells above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f00236c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "qir_cirq = compile(qasm3_str_cirq, target_profile=TargetProfile.Base)\n",
    "\n",
    "target = workspace.get_targets(target_name)\n",
    "job = target.submit(qir_cirq, \"cirq-manual-job\", shots=100)\n",
    "print(\"Job submitted. Waiting for results...\")\n",
    "\n",
    "results = job.get_results()\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0270d310",
   "metadata": {},
   "source": [
    "## Notes\n",
    "\n",
    "- **TargetProfile**: Use `TargetProfile.Base` for most simulators and hardware targets. Use `TargetProfile.Adaptive_RI` for targets that support adaptive circuits with classical branching.\n",
    "- **Shared compilation step**: The `compile → submit` pipeline is identical for both frameworks — the only difference is how the OpenQASM 3 string is produced.\n",
    "- **QIR inspection**: The `qir` object is LLVM bitcode. You can write it to a `.bc` file for offline inspection with LLVM tools.\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
