-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq_math.py
45 lines (37 loc) · 1014 Bytes
/
q_math.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
import math
from numpy import dot as npdot, kron as npkron, array as array
# matrices used by controlled gates
Up_l = [[1,0],
[0,0]]
Lo_r = [[0,0],
[0,1]]
def kron(matrices):
res = matrices[0]
for m in matrices[1:]:
res = npkron(res, m)
return res
def dot(matrices):
res = matrices[0]
for m in matrices[1:]:
res = npdot(res, m)
return res
def shuffle(x, order=None):
"""Reorganizes the given string according the order - list of character indices.
"""
if order == None:
return x
res = ""
for o in order:
res += x[o]
return res
def indices(order):
"""Returns list of all 2^n binary indices sorted by given order - positions of digits.
"""
n = len(order)
states = [format(x, '0%sb' % n) for x in range(2**n)]
return [int(x, base=2) for x in sorted(states, key=lambda x: shuffle(x, order))]
def swap(order, a, b):
tmp = order[a]
order[a] = order[b]
order[b] = tmp
return order