{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Quantum Objects\n", "### Author: Vineeth Thalakottoor\n", "### Email: vineethfrancis.physics@gmail.com" ] }, { "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/PyOR/Source_Doc'\n", "\n", "# Add source path\n", "import sys\n", "sys.path.append(SourcePath)\n", "\n", "# Import PyOR package\n", "from PyOR_QuantumObject import QunObj" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Define State and Operator" ] }, { "cell_type": "code", "execution_count": 70, "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", "Quantum object initialized: shape=(2, 2), type='operator', dtype=complex128\n", "Quantum object initialized: shape=(2, 2), type='operator', dtype=complex128\n", "Quantum object initialized: shape=(2, 2), type='operator', dtype=complex128\n", "Quantum object initialized: shape=(2, 2), type='operator', dtype=complex128\n" ] } ], "source": [ "# Vector (ket)\n", "ket1 = QunObj([[1], [0]],PrintDefault=True) \n", "ket2 = QunObj([[0], [1]],PrintDefault=True) \n", "\n", "# Matrix (Spin operator operator)\n", "Sx = QunObj([[0.0, 0.5],[0.5,0.0]],PrintDefault=True) \n", "Sy = QunObj([[0.0, -0.5j],[0.5j, 0.0]],PrintDefault=True) \n", "Sz = QunObj([[0.5, 0.0],[0.0, -0.5]],PrintDefault=True) \n", "\n", "Id = QunObj([[1., 0.0],[0.0, 1]],PrintDefault=True) " ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Quantum Object: Ket" ] }, { "cell_type": "code", "execution_count": null, "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": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : matrix - show matrix form using sympy\n", "\n", "ket1.matrix " ] }, { "cell_type": "code", "execution_count": null, "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": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : matrix - show matrix form using sympy\n", "\n", "ket2.matrix " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ket'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : type - show the type of the object (ket, bra or operator)\n", "\n", "ket1.type " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1.+0.j],\n", " [0.+0.j]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : data - show matrix form using numpy, as array\n", "\n", "ket1.data " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('complex128')" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : datatype - show the data type of the array\n", "\n", "ket1.datatype " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 1)" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : datatype - show the dimension of the array\n", "\n", "ket1.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Quantum object: Operators" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "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": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : matrix - show matrix form using sympy\n", "\n", "Sx.matrix " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0. +0.j, 0.5+0.j],\n", " [0.5+0.j, 0. +0.j]])" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : data - show matrix form using numpy, as array\n", "\n", "Sx.data " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'operator'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : type - show the type of the object (ket, bra or operator)\n", "\n", "Sx.type " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('complex128')" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : datatype - show the data type of the array\n", "\n", "Sx.datatype " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 2)" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : shape - show the dimension of the array\n", "\n", "Sx.shape" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0 & - 0.5 i\\\\0.5 i & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0, -0.5*I],\n", "[0.5*I, 0]])" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : matrix - show matrix form using sympy\n", "\n", "Sy.matrix " ] }, { "cell_type": "code", "execution_count": 49, "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": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : matrix - show matrix form using sympy\n", "\n", "Sz.matrix " ] }, { "cell_type": "code", "execution_count": 71, "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": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attribute : matrix - show matrix form using sympy\n", "\n", "Id.matrix " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rotate an qunatum object" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0\\\\- 1.0 i\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0],\n", "[-1.0*I]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : Rotate - Rotate the object\n", "\n", "ket3 = ket1.Rotate(180,Sx) \n", "ket3.matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding two objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}2.0\\\\5.0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[2.0],\n", "[5.0]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : Add two objects\n", "\n", "ket4 = 2 * ket1 + 5 * ket2\n", "ket4.matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiply two objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0\\\\0.5\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0],\n", "[0.5]])" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : Multiply two objects\n", "\n", "ket5 = Sx * ket1\n", "ket5.matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add and multiply objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}7.5 & 0\\\\0 & 7.5\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[7.5, 0],\n", "[ 0, 7.5]])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Multiply and add quantum objects\n", "\n", "H = 10 * Sx * Sx + 10 * Sy * Sy + 10 * Sz * Sz\n", "H.matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adjoint" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1.0 & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([[1.0, 0]])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : Adjoint\n", "\n", "bra1 = ket1.Adjoint()\n", "bra1.matrix" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bra'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bra1.type" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Conjugate" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0\\\\1.0 i\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0],\n", "[1.0*I]])" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : Conjugate\n", "\n", "ket3.Conjugate().matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Transpose" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0 & - 1.0 i\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([[0, -1.0*I]])" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Method : Transpose\n", "\n", "ket3.Tranpose().matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Trace" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0j" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : Trace\n", "\n", "Sx.Trace()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Frobenius norm" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7071067811865476" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# method : Norm\n", "\n", "Sx.Norm()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# method : Norm\n", "\n", "ket1.Norm()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exponential of an operator" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1.12762596520638 & 0.521095305493747\\\\0.521095305493747 & 1.12762596520638\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 1.12762596520638, 0.521095305493747],\n", "[0.521095305493747, 1.12762596520638]])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : Expm, Compute the matrix exponential\n", "\n", "Sx.Expm().matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Check for Hermitian" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : Hermitian, Check whether the matrix is Hermitian (A = A†)\n", "\n", "Sx.Hermitian()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Check for commute" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Don't Commute\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : Commute, Check if two QunObj instances commute\n", "\n", "Sx.Commute(Sy)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Commute\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sy.Commute(Sy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tensor Producrt" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0 & 0 & 0.5 & 0\\\\0 & 0 & 0 & 0.5\\\\0.5 & 0 & 0 & 0\\\\0 & 0.5 & 0 & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0, 0, 0.5, 0],\n", "[ 0, 0, 0, 0.5],\n", "[0.5, 0, 0, 0],\n", "[ 0, 0.5, 0, 0]])" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : Tensor Product\n", "\n", "Sx.TensorProduct(Id).matrix" ] }, { "cell_type": "code", "execution_count": null, "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": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : Tensor Product\n", "\n", "Id.TensorProduct(Sx).matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Outer Product" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1.0 & 0\\\\0 & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[1.0, 0],\n", "[ 0, 0]])" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : OuterProduct\n", "\n", "ket1.OuterProduct(ket1).matrix" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0 & 1.0\\\\0 & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[0, 1.0],\n", "[0, 0]])" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : OuterProduct\n", "\n", "ket1.OuterProduct(ket2).matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Inner Product" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1+0j)" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : InnerProduct\n", "\n", "ket1.InnerProduct(ket1)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0.5+0j)" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : InnerProduct\n", "\n", "Sx.InnerProduct(Sx)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Normalize" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}0 & 0.707106781186547\\\\0.707106781186547 & 0\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 0, 0.707106781186547],\n", "[0.707106781186547, 0]])" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method : Normalize, normalize an object\n", "\n", "Sx.Normalize().matrix" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9999999999999999" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sx.Normalize().Norm()" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7071067811865476" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sx.Norm()" ] } ], "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" } }, "nbformat": 4, "nbformat_minor": 4 }