-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpauli_algebra.py
129 lines (107 loc) · 4.14 KB
/
pauli_algebra.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
import numpy as np
def pauli_product(pauliL,pauliR):
prod = np.zeros(len(pauliL),dtype=int)
coeff = 1
for i in range(len(pauliL)):
if pauliL[i] == 0:
prod[i] = pauliR[i]
elif pauliR[i] == 0:
prod[i] = pauliL[i]
elif pauliL[i] != pauliR[i]:
prod[i] = list( set([1,2,3]).difference([pauliL[i],pauliR[i]]) )[0]
if (pauliR[i] - pauliL[i])%3 == 1:
coeff = coeff*1j
elif (pauliR[i] - pauliL[i])%3 == 2:
coeff = coeff*(-1j)
return prod, coeff
def majorana(whichMajorana,N,encoding):
# e.g. whichMajorana = [5,3,2,0] means \chi_0 \chi_2 \chi_3 \chi_5
# whichPauli = [3,3,0,1] means Z_0 Z_1 I_2 X_3
# N is the number of fermions.
# note: convention is that least significant bit is 0.
if not hasattr(whichMajorana, '__iter__'):
whichMajorana = [whichMajorana]
if encoding == 'jordan_wigner':
pauli_op, coef = jordan_wigner(whichMajorana,N)
elif encoding == 'bravyi_kitaev':
pauli_op, coef = bravyi_kitaev(whichMajorana,N)
if len(pauli_op.shape) > 1:
pauli_op = pauli_op[0]
return pauli_op, coef
def jordan_wigner(whichMajorana,N):
whichPaulis = np.zeros((len(whichMajorana),N//2),dtype=int)
for i in range(len(whichMajorana)):
type = (whichMajorana[i]%2) + 1
qubit = whichMajorana[i]//2
whichPaulis[i,qubit] = type
for j in range(qubit):
whichPaulis[i,j] = 3
if len(whichMajorana) == 1:
whichPauli = whichPaulis
coeff = 1
else:
whichPauli, coeff = pauli_product(whichPaulis[1,:],whichPaulis[0,:])
for i in range(2,len(whichMajorana)):
whichPauli,new_coeff = pauli_product(whichPaulis[i,:],whichPauli);
coeff = coeff*new_coeff
return whichPauli, coeff
def bravyi_kitaev(whichMajorana,N):
def ones_str(num_ones):
if num_ones >= 1:
str = '1'
for _ in range(num_ones-1):
str += '1'
else:
str = ''
return str
def partial_order(i,n):
# returns all j >= i, using the partial order above Eq. 19 in the
# Bravyi-Kitaev paper. n is the number of qubits
j = set()
i_binary = np.binary_repr(i,n)
i_binary = i_binary[::-1] # flip so that the zeroth element is least significant.
for l0 in range(n):
j_l0 = ones_str(l0) + i_binary[l0:]
j_l0 = int(j_l0[::-1],2) # flip back and convert back to int
if j_l0 < n:
j.add(j_l0)
return j
def L_set(i,n):
# returns the elements in the set L from Eq. 21 of the Bravyi-Kitaev paper
i_binary = np.binary_repr(i,n)
i_binary = i_binary[::-1]
k = set()
for l0 in range(n):
if i_binary[l0] == '0':
continue
elif i_binary[l0] == '1':
k_l0 = ones_str(l0) + '0' + i_binary[l0+1:]
k_l0 = int( k_l0[::-1],2 )
if k_l0 < n:
k.add(k_l0)
return k
whichPaulis = np.zeros((len(whichMajorana),N//2),dtype=int)
for i in range(len(whichMajorana)):
type = whichMajorana[i]%2
qubit = whichMajorana[i]//2
x_indices = partial_order(qubit,N//2)
if type == 0:
z_indices = L_set(qubit,N//2)
elif type == 1:
z_indices = L_set(qubit+1,N//2)
y_indices = z_indices.intersection(x_indices)
for x in x_indices:
whichPaulis[i,x] = 1
for z in z_indices:
whichPaulis[i,z] = 3
for y in y_indices:
whichPaulis[i,y] = 2
if len(whichMajorana) == 1:
whichPauli = whichPaulis
coeff = 1
else:
whichPauli, coeff = pauli_product(whichPaulis[1,:],whichPaulis[0,:])
for i in range(2,len(whichMajorana)):
whichPauli,new_coeff = pauli_product(whichPaulis[i,:],whichPauli);
coeff = coeff*new_coeff
return whichPauli, coeff