%autosave 0
%matplotlib inline
from sglib import *
Autosave disabled
You've already been using the col()
and row()
functions to create column vectors and row vectors:
col(1,2,3)
row(4,5,6)
I've added one more
"creation" function, mat()
which will create square
matrices:
mat(1,0,0,-1)
mat(1,2,3,4,5,6,7,8,9)
I've also "predefined" a number of vectors and matrices that we care about:
sigma_x, sigma_y, sigma_z
pX, mX, pY, mY, pZ, mZ
As you can see above, when you simply put the name of an object in a cell and execute it, python displays the object. But the way it's displayed, is not always the way we would like to see it. For example, in the vectors above the denominators are "rationalized" instead of leaving the $\sqrt{2}$ in the denominator.
Here's a function called sg_print()
which will attempt
to display vectors and matrices in a way that is similar to what we do
on the board. It works pretty well, but it is not always successful.
sg_print(pX)
sg_print(mX)
sg_print(mY)
M = mat(1/sqrt(2), 1/sqrt(2), 1/sqrt(2), -1/sqrt(2))
M
sg_print(M)
sg_print()
can also do decimal approximations, which can
be handy when you're deailing with an object too complicated in its
exact format:
M2 = M/(1+sqrt(3))
M2
sg_print(M, exact=False, ndigs=3)
If you set the "exact" parameter to False, then you will get a decimal approximation. Note that python requires "True" and "False" to begin with a capital "T" and "F." The "ndigs" parameter tells how many "digits of accuracy" you want. In many cases, this will end up meaning the number of digits after the decimal point. There is a limit to how many digits you can get, but we are mainly interested in making the number small, so that things print out in a readable way.
The reason I used the name sg_print()
is because python
already has a print()
function:
print(M)
Matrix([[sqrt(2)/2, sqrt(2)/2], [sqrt(2)/2, -sqrt(2)/2]])
Python's print tends to prints thing out the way you would enter them into a program. This can be useful if you're trying to figure out how to enter something.
The print_eigenvectors()
function finds the eigenvalues and
eigenvectors of an operator (a matrix) and prints them out in a readable
format:
print_eigenvectors(sigma_y)
But if you want to get the actual eigenvectors and assign them to
variables for further calculation, then use the
find_eigenvectors()
function:
eval, evec = find_eigenvectors(sigma_y)
"eval" and "evec" are just variables. You can name them anything you want. The first variable will end up with a list of all the eigenvalues, and the second one will end up with a list of all the eigenvectors. You can get a specific one by supplying an index in square brackets. Note that python, like most computer languages, uses zero for the first index, rather than one.
eval
eval[0]
eval[1]
evec
sg_print(evec[0])
sg_print(evec[1])