{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Executing Quantum Circuits \n", "\n", "One can excute CUDA Quantum kernels via the `sample` and `observe` function calls. \n", "\n", "Quantum states collapse upon measurement and hence need to be sampled from many times to gather statistics. The CUDA Quantum `sample` call enables this: \n", "\n", "## Sample" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{ 00:490 11:510 }\n" ] } ], "source": [ "import cudaq\n", "\n", "qubit_count = 2\n", "\n", "# Define the simulation target.\n", "cudaq.set_target(\"qpp-cpu\")\n", "\n", "# Define a quantum kernel function.\n", "kernel = cudaq.make_kernel()\n", "\n", "# Allocate our `qubit_count` to the kernel.\n", "qubits = kernel.qalloc(qubit_count)\n", "\n", "# 2-qubit GHZ state.\n", "kernel.h(qubits[0])\n", "for i in range(1, qubit_count):\n", " kernel.cx(qubits[0], qubits[i])\n", "\n", "# If we dont specify measurements, all qubits are measured in\n", "# the Z-basis by default.\n", "kernel.mz(qubits)\n", "\n", "result = cudaq.sample(kernel, shots_count=1000)\n", "\n", "result.dump()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "In simulation mode, the quantum state is built once and then sampled from $s$ times where $s$ equals the `shots_count` . In hardware execution mode, the quantum state collapses upon measurement and hence needs to be rebuilt over and over again. \n", "\n", "## Observe\n", "\n", "`observe` allows us to gather qubit statistics and calculate expectation values. We must supply a spin operator in the form of a hamiltonian from which we would like to calculate $\\bra{\\psi}H\\ket{\\psi}$." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " = 0.0\n" ] } ], "source": [ "import cudaq\n", "from cudaq import spin\n", "\n", "qubit_count = 2\n", "\n", "# Define the simulation target.\n", "cudaq.set_target(\"qpp-cpu\")\n", "\n", "# Define a quantum kernel function.\n", "kernel = cudaq.make_kernel()\n", "\n", "# Allocate our `qubit_count` to the kernel.\n", "qubits = kernel.qalloc(qubit_count)\n", "\n", "# 2-qubit GHZ state.\n", "kernel.h(qubits[0])\n", "\n", "for i in range(1, qubit_count):\n", " kernel.cx(qubits[0], qubits[i])\n", "\n", "# Define a Hamiltonian in terms of Pauli Spin operators.\n", "hamiltonian = spin.z(0) + spin.y(1) + spin.x(0) * spin.z(0)\n", "\n", "# Compute the expectation value given the state prepared by the kernel.\n", "result = cudaq.observe(kernel, hamiltonian).expectation()\n", "\n", "print(' =', result)" ] } ], "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" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" } } }, "nbformat": 4, "nbformat_minor": 2 }