{ "cells": [ { "cell_type": "markdown", "id": "9a37411d-09c5-42f4-9d62-132715cead3e", "metadata": {}, "source": [ "# Operators\n", "\n", "Operators are important constructs for many quantum applications. This section covers how to define and use spin operators as well as additional tools for defining more sophisticated operators." ] }, { "cell_type": "markdown", "id": "a3695762-03b5-46d6-9aac-853d1ebca9d1", "metadata": {}, "source": [ "### Constructing Spin Operators\n", "\n", "The `spin_op` type provides an abstraction for a general tensor product of Pauli spin operators, and their sums.\n", "\n", "Spin operators are constructed using the `spin.z()`, `spin.y()`, `spin.x()`, and `spin.i()` functions, corresponding to the $Z$, $Y$, $X$, and $I$ Pauli operators. For example, `spin.z(0)` corresponds to a Pauli $Z$ operation acting on qubit 0. The example below demonstrates how to construct the following operator $2XYX - 3ZZY$." ] }, { "cell_type": "code", "execution_count": 3, "id": "29ca747b-ee90-4a83-a41b-6480e0c7b6c8", "metadata": {}, "outputs": [], "source": [ "import cudaq\n", "from cudaq import spin\n", "\n", "operator = 2 * spin.x(0) * spin.y(1) * spin.x(2) - 3 * spin.z(0) * spin.z( 1) * spin.y(2)" ] }, { "cell_type": "markdown", "id": "222ea088-ea18-46cf-872d-5e76d25e5e93", "metadata": {}, "source": [ "There are a number of convenient methods for combining, comparing, iterating through, and extracting information from spin operators and can be referenced [here](https://nvidia.github.io/cuda-quantum/latest/api/languages/python_api.html#cudaq.SpinOperator) in the API." ] }, { "cell_type": "markdown", "id": "970e6f09-f0db-4aa3-b611-c33e8436aa55", "metadata": {}, "source": [ "### Pauli Words and Exponentiating Pauli Words\n", "\n", "The `pauli_word` type specifies a string of Pauli operations (e.g. ‘XYXZ’) and is convenient for applying operations based on exponentiated Pauli words. The code below demonstrates how a list of Pauli words, along with their coefficients, are provided as kernel inputs and converted into operators by the `exp_pauli` function.\n", "\n", "The cell below applies the following operation: $e^{i(0.432XZY +0.324IXX)}$" ] }, { "cell_type": "code", "execution_count": 4, "id": "3fe49bcb-0fe1-48e3-aee2-bff882f03fe6", "metadata": {}, "outputs": [], "source": [ "words = ['XYZ', 'IXX']\n", "coefficients = [0.432, 0.324]\n", "\n", "\n", "@cudaq.kernel\n", "def kernel(coefficients: list[float], words: list[cudaq.pauli_word]):\n", " q = cudaq.qvector(3)\n", "\n", " for i in range(len(coefficients)):\n", " exp_pauli(coefficients[i], q, words[i])" ] } ], "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 }