-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathneven.patch
166 lines (162 loc) · 6.44 KB
/
neven.patch
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
diff --git chemistry/fermionic_operator.py chemistry/fermionic_operator.py
index 32e61329..3dc0ab25 100644
--- chemistry/fermionic_operator.py
+++ chemistry/fermionic_operator.py
@@ -160,6 +160,139 @@ class FermionicOperator:
self._h2 = temp_ret
+ # relevant part
+ def _neven_mode(self, n):
+
+ def nodeindex(p,l):
+ """Returns index of a node of depth l on the path p on the ternary tree of height h.
+ Ternary tree is just a string of natural numbers starting from 0 ordered in a tree where
+ each node has three children. For image and formula see arXiv:1910.10746, Eq. (3).
+
+
+ Args:
+ p (list): list of strings of integers 0,1 or 2 of length h
+ l (int): depth of tree to calculate the index on, l <= h
+
+ Returns:
+ int: index of a node corresponding to a qubit number
+ """
+ h = len(p)
+ assert l <= h, "should be l <= h, where h = len(p)"
+ for idx in p:
+ assert (idx == '0') or (idx == '1') or (idx == '2'), "indices should be 0,1 or 2"
+
+ prefactor = (3**l - 1)/2
+ sumfactors = [3**(l-1-j)*int(p[j]) for j in range(l)]
+ return int(prefactor + sum(sumfactors))
+
+ def hfromnum_qubits(num_qubits):
+ """Calculates the needed height of the tree from number of qubits.
+
+ Args:
+ num_qubits (int): number of qubits
+
+ Returns:
+ height (int): required height of the ternary tree.
+ """
+ height = np.ceil(np.log(2*num_qubits+1)/np.log(3)) # base 3 logarithm
+
+ return int(height) # integererize the output
+
+ def xyzstring(h):
+ """Generate a list of repeating 'X','Y','Z' pattern to fill the ternary tree.
+
+ Args:
+ h (int): Height of the ternary tree
+
+ Returns:
+ list: List of strings ['null', 'X','Y','Z','X','Y,'Z','X',...]
+ """
+ num_idxs = int((3**h - 1) // 2) # number of indices (qubits) to add
+ num_idxs_triplets = int(num_idxs / 3) # number of triplets
+ output = ['null'] # add an index for the 0th qubit
+ for _ in range(num_idxs_triplets):
+ output += ['X','Y','Z']
+
+ return output
+
+
+ def paulipaths_full(h):
+ """Generate all Pauli paths from a tree of height h.
+
+
+ Args:
+ h (int): height of the ternary tree
+
+ Returns:
+ list: List of Pauli strings
+ """
+
+
+ xyzs = xyzstring(h+1) # generate the xyz string for tree height + 1
+ num_qubits = int((3**(h) - 1) // 2) # get number of qubits from the height
+ # generate all paths by looping over the number of paths in ternary base
+ paths = []
+ for i in range(3**(h)):
+ paths += [np.base_repr(i,base=3).rjust(h,'0')]
+
+ # generate the Pauli strings from paths by substituting I with appropriate Pauli gate
+ paulistrings = []
+ for path in paths:
+ pstring = ['I']*num_qubits # initialize a string with I's
+ # for each depth, get the index at which the IIII.. path should be substituted and
+ # idx2 at which the substitution Pauli is located
+ for depth in range(h):
+ idx = nodeindex(path,depth)
+ idx2 = nodeindex(path,depth+1)
+ pstring[idx] = xyzs[idx2]
+ # add the resulting string to the list, converted from string to list
+ paulistrings += ["".join(pstring)]
+
+ return paulistrings
+
+ def paulipaths(num_qubits):
+ h = hfromnum_qubits(num_qubits) # get tree max height
+
+ # generate two full trees, the larger one should
+ # accommodate all qubits
+ pphm1 = paulipaths_full(h-1)
+ pph = paulipaths_full(h)
+
+ # get number of qubits in the smaller tree
+ # and the number of extra qubits
+ n_qubits_hm1 = len(pphm1[0])
+ n_extraqubits = num_qubits - n_qubits_hm1
+
+
+ paulistrings = []
+ # loop over each gate in the larger tree for extra qubits
+ # to add the extra paths, also truncate the added paths
+ # up to the real number of qubits,
+ # then add the path, converted to a Pauli object
+ for i in range(n_extraqubits * 3):
+ path = pph[i][:len(pphm1[0])+n_extraqubits]
+ # print(path)
+ paulistrings += [Pauli.from_label(path)]
+
+ # paulistrings += path
+
+ # loop over each gate in the smaller tree, skipping over
+ # the first gates of extra qubits, also add extra 'I' gates,
+ # then add the path, converted to a Pauli object
+ for i in range(n_extraqubits, len(pphm1)):
+ path = ''.join([pphm1[i]] + ['I'] * n_extraqubits)
+ paulistrings += [Pauli.from_label(path)]
+ # paulistrings += path
+
+ return paulistrings
+
+ # reshape the pauli strings from a list into a paired list, skipping the very last one
+ allpaths = paulipaths(num_qubits=n)
+ paths = [[allpaths[2*k],allpaths[2*k + 1]] for k in range(int((len(allpaths)-1)//2))]
+
+ return paths
+
+
def _jordan_wigner_mode(self, n):
r"""
Jordan_Wigner mode.
@@ -371,6 +504,8 @@ class FermionicOperator:
a_list = self._parity_mode(n)
elif map_type == 'bravyi_kitaev':
a_list = self._bravyi_kitaev_mode(n)
+ elif map_type == 'neven':
+ a_list = self._neven_mode(n)
elif map_type == 'bksf':
return bksf_mapping(self)
else:
diff --git chemistry/transformations/fermionic_transformation.py chemistry/transformations/fermionic_transformation.py
index 96967709..4eb2fdd4 100644
--- chemistry/transformations/fermionic_transformation.py
+++ chemistry/transformations/fermionic_transformation.py
@@ -49,6 +49,8 @@ class FermionicQubitMappingType(Enum):
JORDAN_WIGNER = 'jordan_wigner'
PARITY = 'parity'
BRAVYI_KITAEV = 'bravyi_kitaev'
+ NEVEN = 'neven'
+
class FermionicTransformation(Transformation):