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