-
Notifications
You must be signed in to change notification settings - Fork 0
/
qc_gates.py
68 lines (47 loc) · 1.03 KB
/
qc_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
61
62
63
64
65
66
import numpy as np
from qubit import Qubit, ONE_KET, ZERO_KET
# fmt: off
HADAMARD_MATRIX = 1/np.sqrt(2) * np.array(
[
[1, 1],
[1, -1]
])
# fmt: off
def matmul(qubit: Qubit, matrix: np.ndarray) -> Qubit:
q_array = qubit.as_array()
result = np.matmul(matrix, q_array)
return Qubit.from_array(result)
def hadamard(qubit: Qubit) -> Qubit:
return matmul(qubit, HADAMARD_MATRIX)
def H(q: Qubit):
return hadamard(q)
def qc_not(qubit: Qubit) -> Qubit:
return ZERO_KET if qubit == ONE_KET else ONE_KET
def X(q: Qubit):
return qc_not(q)
def identity(qubit: Qubit) -> Qubit:
return qubit
def I(q: Qubit):
return identity(q)
# fmt: off
Z_MATRIX = np.array(
[
[1, 0],
[0, -1]
]
)
# fmt: on
def change_phase(qubit: Qubit) -> Qubit:
return matmul(qubit, Z_MATRIX)
def Z(q: Qubit):
return change_phase(q)
# fmt: off
S_MATRIX = np.array(
[
[1, 0],
[0, 1j]
]
)
# fmt: on
def S(q: Qubit):
return matmul(q, S_MATRIX)