{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "13858fc6-edc7-49cb-8b56-152ebec2f729",
   "metadata": {},
   "source": [
    "# Measurements\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "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": 2,
   "id": "7f45a62e-48cb-4705-8081-e59782c91b5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "@cudaq.kernel\n",
    "def kernel():\n",
    "    qubits = cudaq.qvector(2)\n",
    "    mz()"
   ]
  },
  {
   "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": 3,
   "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": [
    "### Midcircuit Measurement and Conditional Logic\n",
    "\n",
    "In certain cases, it 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 savig the result as `b0`. Then, an if statement performs a Hadamard on qubit 1 only if `b0` is 1. Measuring this qubit 1 verifies this process as a 1 is the result 25% of the time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "44001a51-3733-472c-8bc1-ee694e957708",
   "metadata": {},
   "outputs": [],
   "source": [
    "@cudaq.kernel\n",
    "def kernel():\n",
    "    q = cudaq.qvector(2)\n",
    "    h(q[0])\n",
    "    b0 = mz(q[0])\n",
    "    if b0:\n",
    "        h(q[1])\n",
    "        mz(q[1])"
   ]
  }
 ],
 "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}