You are on page 1of 72

launch binder

launch binder

launch binder

launch binder

launch binder

launch binder

launch binder

launch binder

launch binder

launch binder
shift+enter

In [1]: 5+3

Out[1]: 8

shift+enter

In [2]: 5*3

Out[2]: 15

In [3]: print('this is a string of characters')

this is a string of characters

pip pip

pip
In [ ]: pip install qiskit

In [ ]: pip install pennylane

import

In [4]: import numpy as np

np np

In [5]: np.matrix([[1, 2],


[3, 4]])

Out[5]: matrix([[1, 2],


[3, 4]])

A A

In [6]: A = np.matrix([[1, 2],


[3, 4]])

In [7]: print(A)

[[1 2]
[3 4]]
In [1]: import numpy as np

In [2]: (-1j)*(-1j)

Out[2]: (-1+0j)

In [3]: z = 3 + 2j
w = -2 - 5j

shift+enter

In [4]: z

Out[4]: (3+2j)

In [5]: w

Out[5]: (-2-5j)
print() shift+enter

In [6]: print(z)

(3+2j)

In [7]: print(w)

(-2-5j)

In [8]: print("z=", z)

z= (3+2j)

In [9]: print("the complex number w is:", w)

the complex number w is: (-2-5j)

In [10]: print('z=', z)
print('Real(z)=', np.real(z))
print('Imag(Z)=', np.imag(z))

z= (3+2j)
Real(z)= 3.0
Imag(Z)= 2.0

In [11]: print('w=', w)
print('Real(w)=', np.real(w))
print('Imag(w)=', np.imag(w))

w= (-2-5j)
Real(w)= -2.0
Imag(w)= -5.0
In [12]: np.conj(z)

Out[12]: (3-2j)

In [13]: np.conj(w)

Out[13]: (-2+5j)

In [14]: print('||z||=', np.abs(z))

||z||= 3.605551275463989

In [15]: print('||w||=', np.abs(w))

||w||= 5.385164807134504
In [16]: z+w

Out[16]: (1-3j)

In [17]: z-w

Out[17]: (5+7j)

In [18]: w-z

Out[18]: (-5-7j)

In [19]: z*w
Out[19]: (4-19j)

In [20]: z/w

Out[20]: (-0.5517241379310345+0.37931034482758624j)

In [21]: w/z

Out[21]: (-1.2307692307692308-0.8461538461538463j)
In [1]: import numpy as np

In [2]: # Create a vector as a row


row_vector = np.array([2-1j, 7j, -3])
# Create a vector as a column
column_vector = np.array([[2+1j],
[-5],
[2j]])
In [3]: ket_A = np.array([[2-1j],
[5]])
ket_B = np.array([[7],
[-3j]])
print(ket_A + ket_B)

[[9.-1.j]
[5.-3.j]]

In [4]: print(3*ket_A)
[[ 6.-3.j]
[15.+0.j]]

In [5]: bra_B = np.array([[7, 3j]])


print(5*bra_B)

[[35. +0.j 0.+15.j]]


In [1]: import numpy as np
In [2]: # Define the 4x1 matrix version of a column vector (instead of using the np.array() version):
A = np.matrix([[1-1j],
[3],
[2j],
[5+1j]])

# Compute the Hermitian Conjugate:


A.H

Out[2]: matrix([[1.+1.j, 3.-0.j, 0.-2.j, 5.-1.j]])

In [3]: np.dot(A.H, A)

Out[3]: matrix([[41.+0.j]])

In [4]: # Define B as a 1x4 matrix


B = np.matrix([[-3j, 2+2j, -6j, -7]])

#Compute the Hermitian Conjugate of B, which is a ket-vector


B.H

Out[4]: matrix([[-0.+3.j],
[ 2.-2.j],
[-0.+6.j],
[-7.-0.j]])
In [5]: # Compute <B|A>
np.dot(B,A)

Out[5]: matrix([[-20.-4.j]])

In [6]: # Compute <B|B>


np.dot(B, B.H)

Out[6]: matrix([[102.+0.j]])

In [7]: # Compute <A|B>


np.dot(A.H, B.H)

Out[7]: matrix([[-20.+4.j]])

In [8]: np.dot(A, A.H)

Out[8]: matrix([[ 2. +0.j, 3. -3.j, -2. -2.j, 4. -6.j],


[ 3. +3.j, 9. +0.j, 0. -6.j, 15. -3.j],
[-2. +2.j, 0. +6.j, 4. +0.j, 2.+10.j],
[ 4. +6.j, 15. +3.j, 2.-10.j, 26. +0.j]])
In [1]: pip install qiskit

Requirement already satisfied...

In [2]: import numpy as np


from qiskit import *
from qiskit.visualization import plot_bloch_vector
%matplotlib inline
In [3]: plot_bloch_vector([0,0,1], title="Spin-Up")
Out[3]:

In [4]: plot_bloch_vector([0,0,1], title="Spin-Up")


Out[4]:

In [5]: plot_bloch_vector([0,0,-1], title="Spin-Down")


Out[5]:

In [6]: plot_bloch_vector([0,1,0], title="Spin-Right")


Out[6]:

In [7]: plot_bloch_vector([0,-1,0], title="Spin-Left")


Out[7]:

In [8]: plot_bloch_vector([1,0,0], title="Spin-Plus")


Out[8]:

In [9]: plot_bloch_vector([-1,0,0], title="Spin-Minus")


Out[9]:
In [10]: # Define a 3-dimensional vector
vect = np.array([1, -1, 1])

# Divide it by its length (or norm)


psi = vect/(np.linalg.norm(vect))
print(psi)

[ 0.57735027 -0.57735027 0.57735027]

In [11]: # Plot psi on the Bloch sphere using Qiskit


plot_bloch_vector(psi, title="psi")

Out[11]:
In [12]: x = np.sin(0)*np.cos(0)
y = np.sin(0)*np.sin(0)
z = np.cos(0)

psi1 = np.array([x, y, z])


print("x=", x)
print("y=", y)
print("z=", z)
print("psi1=", psi1)

x= 0.0
y= 0.0
z= 1.0
psi1= [0. 0. 1.]

In [13]: # Plot psi on the Bloch sphere using Qiskit


plot_bloch_vector(psi1, title="psi1")
Out[13]:

In [14]: x = np.sin(0)*np.cos(np.pi)
y = np.sin(0)*np.sin(np.pi)
z = np.cos(np.pi)

psi2 = np.array([x, y, z])


print("x=", x)
print("y=", y)
print("z=", z)
print("psi2=", psi2)

x= -0.0
y= 0.0
z= -1.0
psi2= [-0. 0. -1.]
In [15]: # Plot psi on the Bloch sphere using Qiskit
plot_bloch_vector(psi2, title="psi2")

Out[15]:

In [16]: spin_up = np.matrix([[1],


[0]])

spin_down = np.matrix([[0],
[1]])

spin_right = (1/np.sqrt(2))*(spin_up + 1j*spin_down)

spin_left = (1/np.sqrt(2))*(spin_up - 1j*spin_down)

spin_plus = (1/np.sqrt(2))*(spin_up + spin_down)


spin_minus = (1/np.sqrt(2))*(spin_up - spin_down)

print('spin up:', spin_up)


print('spin down:', spin_down)
print('spin right:', spin_right)
print('spin left:', spin_left)
print('spin plus:', spin_plus)
print('spin minus:', spin_minus)

spin up: [[1]


[0]]
spin down: [[0]
[1]]
spin right: [[0.70710678+0.j ]
[0. +0.70710678j]]
spin left: [[0.70710678+0.j ]
[0. -0.70710678j]]
spin plus: [[0.70710678]
[0.70710678]]
spin minus: [[ 0.70710678]
[-0.70710678]]

In [17]: np.dot(spin_down.H, spin_down)

Out[17]: matrix([[1]])

In [18]: np.dot(spin_left.H, spin_right)

Out[18]: matrix([[0.+0.j]])
In [ ]:
In [1]: import numpy as np
In [2]: # Define two ket-vectors

A = np.array([[2],
[5]])

B = np.array([[3],
[1]])

# Take the Kronecker (tensor) product of the two column vectors


np.kron(A,B)

Out[2]: array([[ 6],


[ 2],
[15],
[ 5]])

In [3]: # Define the ket-vectors as 2x1 column matrices:


ket_A = np.matrix([[2],
[5]])

ket_B = np.matrix([[3],
[1]])

# Compute their Kronecker product


np.kron(ket_A, ket_B)

Out[3]: matrix([[ 6],


[ 2],
[15],
[ 5]])
In [4]: # Define the ket-vectors as 2x1 column matrices:
ket_psi = np.matrix([[2-1j],
[3j]])

ket_phi = np.matrix([[-3],
[4-2j]])

# Compute their Kronecker product


np.kron(ket_psi, ket_phi)

Out[4]: matrix([[-6. +3.j],


[ 6. -8.j],
[-0. -9.j],
[ 6.+12.j]])
In [5]: ket_X = np.matrix([[1],
[4]])

ket_Y = np.matrix([[5],
[2],
[4]])

np.kron(ket_X, ket_Y)

Out[5]: matrix([[ 5],


[ 2],
[ 4],
[20],
[ 8],
[16]])
In [ ]: 5. Write Python code to compute $|0\rangle \otimes |0 \rangle $.
6. Write Python code to compute $|0\rangle \otimes |1 \rangle $.
7. Write Python code to compute $|1\rangle \otimes |0 \rangle $.
8. Write Python code to compute $|1\rangle \otimes |1 \rangle $.
In [1]: pip install pennylane

Requirement already satisfied...

In [2]: import pennylane as qml


from pennylane import numpy as np
In [3]: # Define single qubit states spin-up and spin-down
u = np.matrix([[1],
[0]])
d = np.matrix([[0],
[1]])

# Define the basis states:


uu = np.kron(u, u)
ud = np.kron(u, d)
du = np.kron(d, u)
dd = np.kron(d, d)

In [4]: print('|00> =')


print(uu)
|00> =
[[1]
[0]
[0]
[0]]

In [5]: print('|01> =')


print(ud)

|01> =
[[0]
[1]
[0]
[0]]

In [6]: print('|10> =')


print(du)

|10> =
[[0]
[0]
[1]
[0]]

In [7]: print('|11> =')


print(dd)

|11> =
[[0]
[0]
[0]
[1]]
In [8]: dev = qml.device("default.qubit", wires=2, shots=1)

In [9]: uu = np.array([0, 0])

circuit()

In [10]: def circuit():


qml.BasisState(uu, wires=[0, 1])
return qml.sample(qml.PauliZ(0)), qml.sample(qml.PauliZ(1))

qnode

In [11]: @qml.qnode(dev)
def circuit():
qml.BasisState(uu, wires=[0, 1])
return qml.sample(qml.PauliZ(0)), qml.sample(qml.PauliZ(1))

print(circuit())

[[1]
[1]]

In [12]: ud = np.array([0,1])

qnode

In [13]: @qml.qnode(dev)
def circuit():
qml.BasisState(ud, wires=[0, 1])
return qml.sample(qml.PauliZ(0)), qml.sample(qml.PauliZ(1))

print(circuit())

[[ 1]
[-1]]

In [14]: dev2 = qml.device("default.qubit", wires=2, shots=10)


In [15]: @qml.qnode(dev2)
def circuit():
qml.BasisState(ud, wires=[0, 1])
return qml.sample(qml.PauliZ(0)), qml.sample(qml.PauliZ(1))

print(circuit())

[[ 1 1 1 1 1 1 1 1 1 1]
[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1]]

In [16]: dd = np.array([1,1])

In [17]: @qml.qnode(dev2)
def circuit():
qml.BasisState(dd, wires=[0, 1])
return qml.sample(qml.PauliZ(0)), qml.sample(qml.PauliZ(1))

print(circuit())

[[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1]
[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1]]
qnode

qml.sample(qml.PauliZ(0))

qml.sample(qml.PauliZ(1))

qml.sample(qml.PauliZ(2))
In [1]: import numpy as np

In [2]: M = np.array([[2-1j, -3],


[-5j, 2]])

print(M)

[[ 2.-1.j -3.+0.j]
[-0.-5.j 2.+0.j]]

In [3]: M = np.matrix([[2-1j, -3],


[-5j, 2]])
print(M)

[[ 2.-1.j -3.+0.j]
[-0.-5.j 2.+0.j]]

In [4]: print(M.H)

[[ 2.+1.j -0.+5.j]
[-3.-0.j 2.-0.j]]

In [5]: B = np.matrix([[1, -3j, 5, 2],


[1-1j, 1, 3, 7j]])

print(B.H)

[[ 1.-0.j 1.+1.j]
[-0.+3.j 1.-0.j]
[ 5.-0.j 3.-0.j]
[ 2.-0.j 0.-7.j]]

In [6]: # Define a 3x2 matrix:


A = np.matrix([[2j, -5],
[3-5j, 1],
[5, 4j]])

# Define a 2x4 matrix:


B = np.matrix([[1, -3j, 5, 2],
[1-1j, 1, 3, 7j]])
#Taking the product of a (3x2) and (2x4) matrix will give a 3x4 matrix:
np.dot(A, B)

Out[6]: matrix([[ -5. +7.j, 1. +0.j, -15.+10.j, 0.-31.j],


[ 4. -6.j, -14. -9.j, 18.-25.j, 6. -3.j],
[ 9. +4.j, 0.-11.j, 25.+12.j, -18. +0.j]])

In [7]: A*B

Out[7]: matrix([[ -5. +7.j, 1. +0.j, -15.+10.j, 0.-31.j],


[ 4. -6.j, -14. -9.j, 18.-25.j, 6. -3.j],
[ 9. +4.j, 0.-11.j, 25.+12.j, -18. +0.j]])

In [8]: M = np.matrix([[1, 2, 3],


[1j, 2j, 3j]])

N = np.matrix([[2, 4, 6],
[-2j, -4j, -6j]])

In [9]: M+N

Out[9]: matrix([[3.+0.j, 6.+0.j, 9.+0.j],


[0.-1.j, 0.-2.j, 0.-3.j]])

In [10]: M-N

Out[10]: matrix([[-1.+0.j, -2.+0.j, -3.+0.j],


[ 0.+3.j, 0.+6.j, 0.+9.j]])

In [11]: 3*M
Out[11]: matrix([[3.+0.j, 6.+0.j, 9.+0.j],
[0.+3.j, 0.+6.j, 0.+9.j]])
In [1]: import numpy as np

In [2]: A = np.matrix([[2, 3],


[6, 1]])
B = np.matrix([[7, 10],
[9, 4]])

np.kron(A,B)

Out[2]: matrix([[14, 20, 21, 30],


[18, 8, 27, 12],
[42, 60, 7, 10],
[54, 24, 9, 4]])

In [3]: #Define the basis state |01>


u = np.matrix([[1],
[0]])

d = np.matrix([[0],
[1]])

ud = np.kron(u,d)

print(ud)

[[0]
[1]
[0]
[0]]

In [4]: # Define the matrices X, Y, Z, and H


X = np.matrix([[0, 1],
[1, 0]])

Y = np.matrix([[0, -1j],
[1j, 0]])

Z = np.matrix([[1, 0],
[0, -1]])

H = (1/np.sqrt(2))*np.matrix([[1, 1],
[1, -1]])

In [5]: print(np.kron(X, Y))

[[0.+0.j 0.-0.j 0.+0.j 0.-1.j]


[0.+0.j 0.+0.j 0.+1.j 0.+0.j]
[0.+0.j 0.-1.j 0.+0.j 0.-0.j]
[0.+1.j 0.+0.j 0.+0.j 0.+0.j]]

In [6]: print(np.kron(X, Z))


[[ 0 0 1 0]
[ 0 0 0 -1]
[ 1 0 0 0]
[ 0 -1 0 0]]

In [7]: print(np.kron(H, H))

[[ 0.5 0.5 0.5 0.5]


[ 0.5 -0.5 0.5 -0.5]
[ 0.5 0.5 -0.5 -0.5]
[ 0.5 -0.5 -0.5 0.5]]

In [8]: XY = np.kron(X, Y)
XZ = np.kron(X, Z)
HH = np.kron(H, H)

In [9]: print(XY * ud)

[[0.+0.j]
[0.+0.j]
[0.-1.j]
[0.+0.j]]

In [10]: print(XZ * ud)

[[ 0]
[ 0]
[ 0]
[-1]]

In [11]: print(HH * ud)


[[ 0.5]
[-0.5]
[ 0.5]
[-0.5]]
In [12]: # Define the matrices X, Y, Z, and H:
X = np.matrix([[0, 1],
[1, 0]])

Y = np.matrix([[0, -1j],
[1j, 0]])

Z = np.matrix([[1, 0],
[0, -1]])

H = (1/np.sqrt(2))*np.matrix([[1, 1],
[1, -1]])

# Define the following tensor products:


XY = np.kron(X, Y)
XZ = np.kron(X, Z)
HH = np.kron(H, H)

# Define the following additional tensor products:


XXY = np.kron(X, XY)
XZH = np.kron(XZ, H)
HHH = np.kron(HH, H)

# spin-up and spin-down


u = np.matrix([[1],
[0]])

d = np.matrix([[0],
[1]])

# Define the basis state |01


ud = np.kron(u,d)

# Define the basis state |010>:


udu = np.kron(ud, u)

In [ ]:
In [1]: pip install qiskit

Requirement already satisfied...

In [2]: import numpy as np


from qiskit import *
from qiskit.visualization import plot_bloch_vector
%matplotlib inline
In [3]: # Define spin-up and spin down
u = np.matrix([[1],
[0]])

d = np.matrix([[0],
[1]])

X = np.matrix([[0, 1],
[1, 0]])

Y = np.matrix([[0, -1j],
[1j, 0]])

Z = np.matrix([[1, 0],
[0, -1]])

In [4]: X*u

Out[4]: matrix([[0],
[1]])

In [5]: X*d
Out[5]: matrix([[1],
[0]])

In [6]: plot_bloch_vector([0,0,1], title="Spin-Up")

Out[6]:

In [7]: plot_bloch_vector([0,0,-1], title="Spin-Down")


Out[7]:

In [8]: Y*u

Out[8]: matrix([[0.+0.j],
[0.+1.j]])

In [9]: Z*u

Out[9]: matrix([[1],
[0]])
In [10]: H = (1/np.sqrt(2))*np.matrix([[1, 1],
[1, -1]])

In [11]: H*u

Out[11]: matrix([[0.70710678],
[0.70710678]])

You might also like