mirror of
https://github.com/rodneyosodo/qc-mentorship-program.git
synced 2026-06-23 04:10:29 +00:00
🔨Adding mathematical explanation of finding lowest eigenvalue
This commit is contained in:
@@ -0,0 +1,314 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Eigenvalues and Eigenvectors"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import scipy.linalg as la"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"Let A be a sqaure matrix. A non-zero vector v is an eigenvector for A with eigenvalue if\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"Rearranging the equation, we see v is a solution of homogenous system of equations"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"where I is the identity matrix of size n. Eigenvalues of A are roots of the characteristic polynomial"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"The function `scipy.linalg.eig` computes eigenvalues and eigenvectors of a square matrix "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[ 1 0]\n",
|
||||
" [ 0 -1]]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Let's consider a simple example\n",
|
||||
"A = np.array([[1,0],[0,-1]])\n",
|
||||
"print(A)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# The function la.eig returns eigenvalues and eigenvectors as tuples\n",
|
||||
"eigvals, eigvecs = la.eig(A)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[ 1.+0.j -1.+0.j]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# The eigenvalues of A are\n",
|
||||
"print(eigvals)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[1. 0.]\n",
|
||||
" [0. 1.]]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# The eigenvectors of A are\n",
|
||||
"print(eigvecs)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[ 1. -1.]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# If we know the eigenvalues are real numbers, A is symmetrics, then we use the method `.real` to convert into real numbers\n",
|
||||
"print(eigvals.real)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {
|
||||
"image.png": {
|
||||
"image/png": "iVBORw0KGg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F5KZvaqPixr+gINnl5gDl5POuwGV+r6sSXpKh/60LOKZkypiw/vKcEiaC048BtHP6He+/GpgyF2PfmOxEfaa9slf7Gx52kMFomjxLjrQrXXrqTJXOGmx9lzyW+N7b1+t/QzN5knGOGLZMFNu9zosUf30lNuKTcrNbBM2Gn3j5tlaAAfDNwIA3YwxwsrksQ+6th2I8MfMAcDzxcDjlgS2j3dMcYxc8hgYDBwwwxIandnzyGDgcHAYGAwMBh4Phn4D19WFuvac7N3AAAAAElFTkSuQmCC"
|
||||
}
|
||||
},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Symmetric Matrices\n",
|
||||
"Symmetric matrix is a square matrix that is qual to its taranspose\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"- The entries of a symmetric matrix are symmetrics with respect to the main diagonal\n",
|
||||
"- eigenvalues are real\n",
|
||||
"- eigenvectors are always orthogonal"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[ 1 7 3]\n",
|
||||
" [ 7 4 -5]\n",
|
||||
" [ 3 -5 6]]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"A = np.array([[1, 7, 3],\n",
|
||||
" [7, 4, -5],\n",
|
||||
" [3, -5, 6]])\n",
|
||||
"print(A)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[ 1 7 3]\n",
|
||||
" [ 7 4 -5]\n",
|
||||
" [ 3 -5 6]]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Transpose of A\n",
|
||||
"print(A.T)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"eigvals, eigvecs = la.eig(A)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[-7.00792852+0.j 7.03594585+0.j 10.97198268+0.j]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Eigenvalues have zero imaginary part\n",
|
||||
"print(eigvals)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[ 0.68415383 -0.62878419 -0.36954564]\n",
|
||||
" [-0.61391034 -0.22292725 -0.75724338]\n",
|
||||
" [-0.39376087 -0.74493885 0.53853365]]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(eigvecs)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0.0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Check that eigenvectors are orthogonal\n",
|
||||
"# The dot product of eigenvectors and is zero \n",
|
||||
"v1 = eigvecs[:,0] # First column is the first eigenvector\n",
|
||||
"v2 = eigvecs[:,1] # Second column is the second eigenvector\n",
|
||||
"print(v1 @ v2)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "PyCharm (Qiskit)",
|
||||
"language": "python",
|
||||
"name": "pycharm-f8797ed6"
|
||||
},
|
||||
"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.6.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
Reference in New Issue
Block a user