{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Quick Guide\n", "To introduce the main functionality of Haarpy consider the following problem: imagine that you can generate unitary matrices at random (from the Haar measure); you would like to estimate the average of $|U_{i,j}|^2$ which we write mathematically as $\\int dU |U_{i,j}|^2 = \\int dU U_{i,j} U_{i,j}^*$. We could obtain this average by using the random matrix functionality of SciPy as follows:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy.stats import unitary_group" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.4964599 , 0.32742463, 0.25429793])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(137)\n", "shots = 1000\n", "\n", "# For unitary matrices of size between 2 and 4 produce 1000 shots and calculate the average\n", "# of the absolute value squared of the 0,1 entry\n", "np.array(\n", " [\n", " np.mean([np.abs(unitary_group.rvs(dim=dim)[0, 1]) ** 2 for _ in range(shots)])\n", " for dim in range(2, 5)\n", " ]\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Haarpy allows you to obtain this average (and many others!) analytically. We first recall that the expression we are trying to calculate is $\\int dU \\left|U_{i,j}\\right|^2 = \\int dU U_{i,j} U_{i,j}^*$. With this expression in mind we can use Sympy to create a symbolic variable $d$ for the dimension of the unitary and write" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from sympy import Symbol\n", "from haarpy import haar_integral_unitary" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{1}{d}$" ], "text/plain": [ "1/d" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = Symbol(\"d\")\n", "haar_integral_unitary((\"i\", \"j\", \"i\", \"j\"), d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also put integers" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Fraction(1, 3)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "haar_integral_unitary((\"i\", \"j\", \"i\", \"j\"), 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice the order of the indices! The first `\"i\"` and `\"j\"` are the indices of $U$ while the second pair of `\"i\"` and `\"j\"` are the indices of $U^*$.\n", "\n", "Imagine that now we want to calculate something like $\\int dU U_{i,m} U_{j,n} U_{k,o} U_{i,m}^* U_{j,n}^* U_{k,p}^*$ $= \\int dU \\left|U_{i,m} U_{j,n} U_{k,o}\\right|^2$ we simply do" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{d^{2} - 2}{d \\left(d - 2\\right) \\left(d - 1\\right) \\left(d + 1\\right) \\left(d + 2\\right)}$" ], "text/plain": [ "(d**2 - 2)/(d*(d - 2)*(d - 1)*(d + 1)*(d + 2))" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "haar_integral_unitary((\"ijk\", \"mno\", \"ijk\", \"mno\"), d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The averages we are calculating are obtained by using so-called [Weingarten calculus](https://doi.org/10.1155/S107379280320917X)." ] } ], "metadata": { "kernelspec": { "display_name": "mtl", "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": 2 }