{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "13858fc6-edc7-49cb-8b56-152ebec2f729",
   "metadata": {},
   "source": [
    "# Measurements\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec9af8fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cudaq"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa410efc-fcb1-43e1-ab99-25a7fcbab868",
   "metadata": {},
   "source": [
    "\n",
    "Kernel measurement can be specified in the Z, X, or Y basis using `mz`, `mx`, and `my`. If a measurement is specified with no argument, the entire kernel is measured in that basis. Measurement occurs in the Z basis by default."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f45a62e-48cb-4705-8081-e59782c91b5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "@cudaq.kernel\n",
    "def kernel():\n",
    "    qubits = cudaq.qvector(2)\n",
    "    mz(qubits[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39efaac5-5400-4df6-ac5d-80637d9d3082",
   "metadata": {},
   "source": [
    "Specific qubits or registers can be measured rather than the entire kernel."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16a14177-fa79-4eb3-8fc8-f3e937bc21b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "@cudaq.kernel\n",
    "def kernel():\n",
    "    qubits_a = cudaq.qvector(2)\n",
    "    qubit_b = cudaq.qubit()\n",
    "    mz(qubits_a)\n",
    "    mx(qubit_b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb5dd767-5db7-4847-b04e-ae5695066800",
   "metadata": {},
   "source": [
    "### Mid-circuit Measurement and Conditional Logic\n",
    "\n",
    "In certain cases, it is helpful for some operations in a quantum kernel to depend on measurement results following previous operations. This is accomplished in the following example by performing a Hadamard on qubit 0, then measuring qubit 0 and saving the result as `b0`. Then, qubit 0 can be reset and used later in the computation. In this case it is flipped to a 1. Finally, an if statement performs a Hadamard on qubit 1 if `b0` is 1.\n",
    "\n",
    "The results show qubit 0 is one, indicating the reset worked, and qubit 1 has a 75/25 distribution, demonstrating the mid-circuit measurement worked as expected."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44001a51-3733-472c-8bc1-ee694e957708",
   "metadata": {},
   "outputs": [],
   "source": [
    "@cudaq.kernel\n",
    "def kernel() -> list[bool]:\n",
    "    q = cudaq.qvector(2)\n",
    "\n",
    "    h(q[0])\n",
    "    b0 = mz(q[0])\n",
    "    reset(q[0])\n",
    "    x(q[0])\n",
    "\n",
    "    if b0:\n",
    "        h(q[1])\n",
    "\n",
    "    return mz(q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee8a65fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "\n",
    "results = cudaq.run(kernel, shots_count=1000)\n",
    "# Convert results to bitstrings and count\n",
    "bitstring_counts = Counter(\n",
    "    ''.join('1' if bit else '0' for bit in result) for result in results)\n",
    "\n",
    "print(f\"Bitstring counts: {dict(bitstring_counts)}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
