{ "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": [ "### Mid-circuit 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 saving the result as `b0`. Then, qubit 0 can be reset and used later in the computation. In this case it is flipped ot 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 expexted." ] }, { "cell_type": "code", "execution_count": 6, "id": "44001a51-3733-472c-8bc1-ee694e957708", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{ \n", " __global__ : { 10:728 11:272 }\n", " b0 : { 0:505 1:495 }\n", "}\n", "\n" ] } ], "source": [ "@cudaq.kernel\n", "def kernel():\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", "print(cudaq.sample(kernel))" ] }, { "cell_type": "code", "execution_count": null, "id": "d525be71-a745-43a5-a7ca-a2720c536f8c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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 }