{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# PyOR Quantum\n", "### Author: Vineeth Thalakottoor\n", "### Introduction to Quantum Library" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Define the source path\n", "SourcePath = '/media/HD2/Vineeth/PostDoc_Simulations/Github/PyOR_V1/PyOR_Combined/Source_Doc'\n", "\n", "# Add source path\n", "import sys\n", "sys.path.append(SourcePath)\n", "\n", "import numpy as np\n", "\n", "# Import PyOR package\n", "from PyOR_QuantumLibrary import QuantumLibrary\n", "QLib = QuantumLibrary()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Make two states ket1 and ket2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Quantum object initialized: shape=(2, 1), type='ket', dtype=complex128\n", "Quantum object initialized: shape=(2, 1), type='ket', dtype=complex128\n" ] } ], "source": [ "# Make two states ket1 and ket2\n", "ket1 = QLib.Basis_Ket(2,0, PrintDefault=True)\n", "ket2 = QLib.Basis_Ket(2,1, PrintDefault=True)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1.0\\\\0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[1.0],\n", "[ 0]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Matrix form of ket1\n", "ket1.matrix" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0\\\\1.0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0],\n", "[1.0]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Matrix form of ket2\n", "ket2.matrix" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Create a state" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1.0\\\\2.0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[1.0],\n", "[2.0]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a state\n", "psi1 = 1 * ket1 + 2 * ket2\n", "\n", "# Matrix form\n", "psi1.matrix" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}10.0\\\\20.0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[10.0],\n", "[20.0]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a state\n", "psi2 = 10 * ket1 + 20 * ket2\n", "psi2.matrix" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Outer Product" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1.0 & 2.0\\\\2.0 & 4.0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[1.0, 2.0],\n", "[2.0, 4.0]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Outer Product\n", "rho1 = QLib.OuterProduct(psi1,psi1)\n", "\n", "# Matrix form\n", "rho1.matrix" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}100.0 & 200.0\\\\200.0 & 400.0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[100.0, 200.0],\n", "[200.0, 400.0]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Outer Product\n", "rho2 = QLib.OuterProduct(psi2,psi2)\n", "\n", "# Matrix form\n", "rho2.matrix" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Conversion from density matrix to vector" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# Density matrix to vector\n", "QLib.RowColOrder = \"C\" # Vectorize by row\n", "#QLib.RowColOrder = \"F\" # Vectorize by col\n", "vec1 = QLib.DMToVec(rho1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1.0\\\\2.0\\\\2.0\\\\4.0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[1.0],\n", "[2.0],\n", "[2.0],\n", "[4.0]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Matrix form\n", "vec1.matrix" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Conversion from vector to density matrix" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# Vector to DM\n", "DM1 = QLib.VecToDM(vec1,shape=(2,2))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1.0 & 2.0\\\\2.0 & 4.0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[1.0, 2.0],\n", "[2.0, 4.0]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Matrix form\n", "DM1.matrix" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Create a bra state" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Quantum object initialized: shape=(1, 2), type='bra', dtype=complex128\n" ] }, { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1.0 & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([[1.0, 0]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Bra state\n", "bra1 = QLib.Basis_Bra(2,0, PrintDefault=True)\n", "\n", "# Print matrix\n", "bra1.matrix" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Import spin operators" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Quantum object initialized: shape=(2, 2), type='operator', dtype=complex128\n" ] }, { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0 & 0.5\\\\0.5 & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0, 0.5],\n", "[0.5, 0]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sx\n", "Sx = QLib.SSpinOp(1/2,\"x\", PrintDefault=True)\n", "\n", "# show matrix form (Sympy)\n", "Sx.matrix" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# Sy\n", "Sy = QLib.SSpinOp(1/2,\"y\")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "# Sz\n", "Sz = QLib.SSpinOp(1/2,\"z\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Creating Hamiltonian" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0.5 & 0.5 - 0.5 i\\\\0.5 + 0.5 i & -0.5\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0.5, 0.5 - 0.5*I],\n", "[0.5 + 0.5*I, -0.5]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H1 = Sx + Sy + Sz\n", "H1.matrix" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0.5 & 0.5 - 0.5 i\\\\0.5 + 0.5 i & -0.5\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0.5, 0.5 - 0.5*I],\n", "[0.5 + 0.5*I, -0.5]])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H2 = np.cos(np.pi) * Sz\n", "H1.matrix" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "H1.Positive()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H1.Hermitian()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Create Identity matrix" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1.0 & 0\\\\0 & 1.0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[1.0, 0],\n", "[ 0, 1.0]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Id = QLib.Identity(2)\n", "Id.matrix" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "Id.Positive()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Tensor Product" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0 & 0.5 & 0 & 0\\\\0.5 & 0 & 0 & 0\\\\0 & 0 & 0 & 0.5\\\\0 & 0 & 0.5 & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0, 0.5, 0, 0],\n", "[0.5, 0, 0, 0],\n", "[ 0, 0, 0, 0.5],\n", "[ 0, 0, 0.5, 0]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sx1 = QLib.TensorProduct(Id,Sx)\n", "Sx1.matrix" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0 & 0 & 0.5 & 0 & 0 & 0 & 0 & 0\\\\0 & 0 & 0 & 0.5 & 0 & 0 & 0 & 0\\\\0.5 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\0 & 0.5 & 0 & 0 & 0 & 0 & 0 & 0\\\\0 & 0 & 0 & 0 & 0 & 0 & 0.5 & 0\\\\0 & 0 & 0 & 0 & 0 & 0 & 0 & 0.5\\\\0 & 0 & 0 & 0 & 0.5 & 0 & 0 & 0\\\\0 & 0 & 0 & 0 & 0 & 0.5 & 0 & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0, 0, 0.5, 0, 0, 0, 0, 0],\n", "[ 0, 0, 0, 0.5, 0, 0, 0, 0],\n", "[0.5, 0, 0, 0, 0, 0, 0, 0],\n", "[ 0, 0.5, 0, 0, 0, 0, 0, 0],\n", "[ 0, 0, 0, 0, 0, 0, 0.5, 0],\n", "[ 0, 0, 0, 0, 0, 0, 0, 0.5],\n", "[ 0, 0, 0, 0, 0.5, 0, 0, 0],\n", "[ 0, 0, 0, 0, 0, 0.5, 0, 0]])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sx2 = QLib.TensorProductMultiple(Id,Sx,Id)\n", "Sx2.matrix" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Direct Sum" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0.5 & 0.5 - 0.5 i & 0 & 0\\\\0.5 + 0.5 i & -0.5 & 0 & 0\\\\0 & 0 & -0.5 & 0\\\\0 & 0 & 0 & 0.5\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0.5, 0.5 - 0.5*I, 0, 0],\n", "[0.5 + 0.5*I, -0.5, 0, 0],\n", "[ 0, 0, -0.5, 0],\n", "[ 0, 0, 0, 0.5]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DM4 = QLib.DirectSum(H1,H2)\n", "DM4.matrix" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1.0\\\\0\\\\0\\\\1.0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[1.0],\n", "[ 0],\n", "[ 0],\n", "[1.0]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ket4 = QLib.DirectSum(ket1,ket2)\n", "ket4.matrix" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0 & 0.5 & 0 & 0 & 0 & 0\\\\0.5 & 0 & 0 & 0 & 0 & 0\\\\0 & 0 & 0 & - 0.5 i & 0 & 0\\\\0 & 0 & 0.5 i & 0 & 0 & 0\\\\0 & 0 & 0 & 0 & 0.5 & 0\\\\0 & 0 & 0 & 0 & 0 & -0.5\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0, 0.5, 0, 0, 0, 0],\n", "[0.5, 0, 0, 0, 0, 0],\n", "[ 0, 0, 0, -0.5*I, 0, 0],\n", "[ 0, 0, 0.5*I, 0, 0, 0],\n", "[ 0, 0, 0, 0, 0.5, 0],\n", "[ 0, 0, 0, 0, 0, -0.5]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DM5 = QLib.DirectSumMultiple(Sx,Sy,Sz)\n", "DM5.matrix" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1.0\\\\0\\\\0\\\\1.0\\\\1.0\\\\0\\\\0\\\\1.0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[1.0],\n", "[ 0],\n", "[ 0],\n", "[1.0],\n", "[1.0],\n", "[ 0],\n", "[ 0],\n", "[1.0]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ket5 = QLib.DirectSumMultiple(ket1,ket2,ket4)\n", "ket5.matrix" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Block Extractor" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 1)" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ket1.shape" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 1)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ket2.shape" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, 1)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ket4.shape" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0\\\\1.0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0],\n", "[1.0]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ket10 = QLib.BlockExtract(ket5,1,[(2,1),(2,1),(4,1)])\n", "ket10.matrix" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0.5 & 0\\\\0 & -0.5\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[0.5, 0],\n", "[ 0, -0.5]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DM10 = QLib.BlockExtract(DM5,2,[(2,2),(2,2),(2,2)])\n", "DM10.matrix" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.12.7" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }