-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQubit Transformations (Matrices using Python).py
149 lines (131 loc) · 3.75 KB
/
Qubit Transformations (Matrices using Python).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 18 16:52:19 2019
@author: domin
"""
import numpy as np
from numpy.linalg import inv
#Assignment: Hermitian Matrices using Python
def twobytwo(a, b, c, d):
#MatrixInv= (1/(a*d - c*b))*np.array([[a, -b],[-c, d]])
Matrix = np.matrix([[a, b],[c, d]])
Herm=Matrix.getH()
Sym = np.array([[a, c],[b,d]])
if Matrix[0,0] == Herm[0,0] and Matrix[0,1] == Herm[0,1]:
e=1;
else:
e=0;
if Matrix[1,0] == Herm[1,0] and Matrix[1,1] == Herm[1,1]:
f=1;
else:
f=0;
if e==1 and f ==1:
print("Herm")
#print ("isa Hermitian")
if Matrix[0,0] == Sym[0,0] and Matrix[0,1] == Sym[0,1]:
g=1;
else:
g=0;
if Matrix[1,0] == Sym[1,0] and Matrix[1,1] == Sym[1,1]:
h=1;
else:
h=0;
if g ==1 & h ==1:
print("sym")
#if Sym == Matrix:
#print("isa Symmetric")
return Matrix
print()
#Assignment: Unitary Matrices using Python
def twobytwo2(a, b, c, d):
#MatrixInv= (1/(a*d - c*b))*np.array([[a, -b],[-c, d]])
Matrix2 = np.matrix([[a, b],[c, d]])
MatR= Matrix2.round(decimals=6)
Inv = inv(Matrix2)
InvR = Inv.round(decimals=6)
Trans=np.matrix([[a,c],[b,d]])
TransR= Trans.round(decimals=6)
Uni= Matrix2.getH()
UniR=Uni.round(decimals=6)
print(MatR)
print()
print(InvR)
print()
print(TransR)
print()
print(UniR)
#involution Matrix2=Inv
if MatR[0,0] == InvR[0,0] and MatR[0,1] == InvR[0,1]:
e=1;
else:
e=0;
if MatR[1,0] == InvR[1,0] and MatR[1,1] == InvR[1,1]:
f=1;
else:
f=0;
if e==1 and f ==1:
print("Involution")
#print ("isa Hermitian")
if TransR[0,0] == InvR[0,0] and TransR[0,1] == InvR[0,1]:
g=1;
else:
g=0;
if TransR[1,0] == InvR[1,0] and TransR[1,1] == InvR[1,1]:
h=1;
else:
h=0;
if g ==1 & h ==1:
print("Orthogonal")
if UniR[0,0] == InvR[0,0] and UniR[0,1] == InvR[0,1]:
i=1;
else:
i=0;
if UniR[1,0] == InvR[1,0] and UniR[1,1] == InvR[1,1]:
j=1;
else:
j=0;
if i ==1 & j ==1:
print("Unitary")
#if Sym == Matrix:
#print("isa Symmetric")
return MatR
Y1=twobytwo2(0,-1j, 1j,0)
print()
S1=twobytwo2(1,0,0,1j)
print()
x1=1/np.sqrt(2)
H1=twobytwo2(x1, x1, x1, -x1)
print()
Q1=twobytwo2(x1, x1, 1j*x1, -1j*x1)
print()
#Assignment: Rotation Matrices using Python
angle=45
theta=angle*(np.pi/180)
#Rot=np.matrix([[np.cos(theta), -np.sin(theta)],[np.sin(theta), np.cos(theta)]])
R1=twobytwo2(np.cos(theta), -np.sin(theta), np.sin(theta), np.cos(theta))
V_ket = np.array([[0],[1]])
H_ket = np.array([[1],[0]])
H_ket2 = np.matmul(R1,H_ket)
V_ket2 = np.matmul(R1,V_ket)
print(H_ket)
import matplotlib.pyplot as plt
originx=0
originy=0
V_polar = np.array([[originx, originy, V_ket[0,0], V_ket[1,0]]])
H_polar = np.array([[originx, originy, H_ket[0,0], H_ket[1,0]]])
V_polar2 = np.array([[originx, originy, V_ket2[0,0], V_ket2[1,0]]])
H_polar2 = np.array([[originx, originy, H_ket2[0,0], H_ket2[1,0]]])
X, Y, U, V = zip(*V_polar)
X1, Y1, U1, V1 = zip(*H_polar)
X2, Y2, U2, V2 = zip(*V_polar2)
X3, Y3, U3, V3 = zip(*H_polar2)
plt.figure(figsize=(5,5))
ax = plt.gca()
ax.quiver(X, Y, U, V, angles='xy', scale_units='xy', scale=1, color='k')
ax.quiver(X1, Y1, U1, V1, angles='xy', scale_units='xy', scale=1, color='k')
ax.quiver(X2, Y2, U2, V2, angles='xy', scale_units='xy', scale=1, color='r')
ax.quiver(X3, Y3, U3, V3, angles='xy', scale_units='xy', scale=1, color='r')
ax.set_xlim([-1, 1])
ax.set_ylim([-1, 1])
plt.draw()
plt.show()