-
Notifications
You must be signed in to change notification settings - Fork 5
/
quantum.py
58 lines (42 loc) · 1.04 KB
/
quantum.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
"""Simple quantum computations simulation."""
import numpy as np
def I():
"""Identity operator."""
return np.identity(2)
def X():
"""X-rotation, negation operator."""
return np.identity(2)[..., ::-1]
def H():
"""Adamara operator, superposition."""
return np.array([[1, 1], [1, -1]]) / np.sqrt(2)
def SWAP():
"""Swap 2 qubits"""
m = np.identity(4)
m[[1, 2]] = m[[2, 1]]
return m
def CX():
"""Controlled negation."""
m = np.identity(4)
m[[3, 2]] = m[[2, 3]]
return m
def apply(v, *gates):
m = gates[0]
gates = gates[1:]
for gate in gates:
m = np.kron(gate, m)
return m.dot(v)
def observe(v):
v2 = np.absolute(v) ** 2
c = np.random.choice(v.size, 1, p=v2)
return c[0]
# Usage example
# create 3 qubits in state 000, array size 2 ^ n
a = np.array([1, 0, 0, 0, 0, 0, 0, 0])
# transform the 2nd qubit into a superposition of 0 and 1
a = apply(a, I(), H(), I())
# entangle the 1st and 2nd qubit
a = apply(a, CX(), I())
# swap the 2nd and 3rd qubit
a = apply(a, I(), SWAP())
# observe the state
observe(a)