/
gates.py
60 lines (51 loc) · 1.57 KB
/
gates.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
"""Defines the Gate class"""
import numpy as np
class Gates:
""""Contains basic quantum gates"""
# Rotation
def RX(self, theta):
return np.matrix([[np.cos(theta / 2), -1j * np.sin(theta / 2)],
[-1j * np.sin(theta / 2), np.cos(theta / 2)]])
def RY(self, theta):
return np.matrix([[np.cos(theta / 2), -np.sin(theta / 2)],
[np.sin(theta / 2), np.cos(theta / 2)]])
def RZ(self, theta):
return np.matrix([[np.exp(-1j * theta / 2), 0],
[0, np.exp(1j * theta / 2)]])
# pauli
I = np.matrix([[1, 0], [0, 1]])
X = np.matrix([[0, 1], [1, 0]])
Y = np.matrix([[0, 0 - 1j], [0 + 1j, 0]])
Z = np.matrix([[1, 0], [0, -1]])
# universal gates
H = (X + Z)/np.sqrt(2)
T = np.matrix([[1, 0], [0, np.exp(1j * np.pi / 4)]])
S = np.matrix([[1.0, 0.0], [0.0, 1.0j]])
CNOT = np.matrix([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0]])
# miscellaneous
CZ = np.matrix([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, -1]])
SWAP = np.matrix([[1, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 1]])
gates_set = {
"I": I,
"X": X,
"Y": Y,
"Z": Z,
"H": H,
"T": T,
"S": S,
"CNOT": CNOT,
"CZ": CZ,
"SWAP": SWAP,
"RX": RX,
"RY": RY,
"RZ": RZ
}