Professional Documents
Culture Documents
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
pip pip
pip
In [ ]: pip install qiskit
import
np np
A A
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 [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)
||z||= 3.605551275463989
||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
[[9.-1.j]
[5.-3.j]]
In [4]: print(3*ket_A)
[[ 6.-3.j]
[15.+0.j]]
In [3]: np.dot(A.H, A)
Out[3]: matrix([[41.+0.j]])
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]])
Out[6]: matrix([[102.+0.j]])
Out[7]: matrix([[-20.+4.j]])
Out[11]:
In [12]: x = np.sin(0)*np.cos(0)
y = np.sin(0)*np.sin(0)
z = np.cos(0)
x= 0.0
y= 0.0
z= 1.0
psi1= [0. 0. 1.]
In [14]: x = np.sin(0)*np.cos(np.pi)
y = np.sin(0)*np.sin(np.pi)
z = np.cos(np.pi)
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]:
spin_down = np.matrix([[0],
[1]])
Out[17]: matrix([[1]])
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]])
ket_B = np.matrix([[3],
[1]])
ket_phi = np.matrix([[-3],
[4-2j]])
ket_Y = np.matrix([[5],
[2],
[4]])
np.kron(ket_X, ket_Y)
|01> =
[[0]
[1]
[0]
[0]]
|10> =
[[0]
[0]
[1]
[0]]
|11> =
[[0]
[0]
[0]
[1]]
In [8]: dev = qml.device("default.qubit", wires=2, shots=1)
circuit()
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]]
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
print(M)
[[ 2.-1.j -3.+0.j]
[-0.-5.j 2.+0.j]]
[[ 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]]
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 [7]: A*B
N = np.matrix([[2, 4, 6],
[-2j, -4j, -6j]])
In [9]: M+N
In [10]: M-N
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
np.kron(A,B)
d = np.matrix([[0],
[1]])
ud = np.kron(u,d)
print(ud)
[[0]
[1]
[0]
[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 [8]: XY = np.kron(X, Y)
XZ = np.kron(X, Z)
HH = np.kron(H, H)
[[0.+0.j]
[0.+0.j]
[0.-1.j]
[0.+0.j]]
[[ 0]
[ 0]
[ 0]
[-1]]
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]])
d = np.matrix([[0],
[1]])
In [ ]:
In [1]: pip install qiskit
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]])
Out[6]:
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]])