-
Notifications
You must be signed in to change notification settings - Fork 369
/
special_operators.py
310 lines (243 loc) · 9.8 KB
/
special_operators.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Commonly used operators (mainly instances of SymbolicOperator)."""
from typing import Optional, Union, Tuple
from openfermion.ops.operators import BosonOperator, FermionOperator
from openfermion.utils.indexing import down_index, up_index
def s_plus_operator(n_spatial_orbitals: int) -> FermionOperator:
r"""Return the s+ operator.
$$
\begin{align}
S^{+} = \sum_{i=1}^{n} a_{i, \alpha}^{\dagger}a_{i, \beta}
\end{align}
$$
Args:
n_spatial_orbitals: number of spatial orbitals (n_qubits + 1 // 2).
Returns:
operator (FermionOperator): corresponding to the s+ operator over
n_spatial_orbitals.
Note:
The indexing convention used is that even indices correspond to
spin-up (alpha) modes and odd indices correspond to spin-down (beta)
modes.
"""
if not isinstance(n_spatial_orbitals, int):
raise TypeError("n_orbitals must be specified as an integer")
operator = FermionOperator()
for ni in range(n_spatial_orbitals):
operator += FermionOperator(((up_index(ni), 1), (down_index(ni), 0)))
return operator
def s_minus_operator(n_spatial_orbitals: int) -> FermionOperator:
r"""Return the s+ operator.
$$
\begin{align}
S^{-} = \sum_{i=1}^{n} a_{i, \beta}^{\dagger}a_{i, \alpha}
\end{align}
$$
Args:
n_spatial_orbitals: number of spatial orbitals (n_qubits + 1 // 2).
Returns:
operator (FermionOperator): corresponding to the s- operator over
n_spatial_orbitals.
Note:
The indexing convention used is that even indices correspond to
spin-up (alpha) modes and odd indices correspond to spin-down (beta)
modes.
"""
if not isinstance(n_spatial_orbitals, int):
raise TypeError("n_orbitals must be specified as an integer")
operator = FermionOperator()
for ni in range(n_spatial_orbitals):
operator += FermionOperator(((down_index(ni), 1), (up_index(ni), 0)))
return operator
def sx_operator(n_spatial_orbitals: int) -> FermionOperator:
r"""Return the sx operator.
$$
\begin{align}
S^{x} = \frac{1}{2}\sum_{i = 1}^{n}(S^{+} + S^{-})
\end{align}
$$
Args:
n_spatial_orbitals: number of spatial orbitals (n_qubits // 2).
Returns:
operator (FermionOperator): corresponding to the sx operator over
n_spatial_orbitals.
Note:
The indexing convention used is that even indices correspond to
spin-up (alpha) modes and odd indices correspond to spin-down (beta)
modes.
"""
if not isinstance(n_spatial_orbitals, int):
raise TypeError("n_orbitals must be specified as an integer")
operator = FermionOperator()
for ni in range(n_spatial_orbitals):
operator += FermionOperator(((up_index(ni), 1), (down_index(ni), 0)), 0.5)
operator += FermionOperator(((down_index(ni), 1), (up_index(ni), 0)), 0.5)
return operator
def sy_operator(n_spatial_orbitals: int) -> FermionOperator:
r"""Return the sy operator.
$$
\begin{align}
S^{y} = \frac{-i}{2}\sum_{i = 1}^{n}(S^{+} - S^{-})
\end{align}
$$
Args:
n_spatial_orbitals: number of spatial orbitals (n_qubits // 2).
Returns:
operator (FermionOperator): corresponding to the sx operator over
n_spatial_orbitals.
Note:
The indexing convention used is that even indices correspond to
spin-up (alpha) modes and odd indices correspond to spin-down (beta)
modes.
"""
if not isinstance(n_spatial_orbitals, int):
raise TypeError("n_orbitals must be specified as an integer")
operator = FermionOperator()
for ni in range(n_spatial_orbitals):
operator += FermionOperator(((up_index(ni), 1), (down_index(ni), 0)), -0.5j)
operator += FermionOperator(((down_index(ni), 1), (up_index(ni), 0)), 0.5j)
return operator
def sz_operator(n_spatial_orbitals: int) -> FermionOperator:
r"""Return the sz operator.
$$
\begin{align}
S^{z} = \frac{1}{2}\sum_{i = 1}^{n}(n_{i, \alpha} - n_{i, \beta})
\end{align}
$$
Args:
n_spatial_orbitals: number of spatial orbitals (n_qubits // 2).
Returns:
operator (FermionOperator): corresponding to the sz operator over
n_spatial_orbitals.
Note:
The indexing convention used is that even indices correspond to
spin-up (alpha) modes and odd indices correspond to spin-down (beta)
modes.
"""
if not isinstance(n_spatial_orbitals, int):
raise TypeError("n_orbitals must be specified as an integer")
operator = FermionOperator()
n_spinless_orbitals = 2 * n_spatial_orbitals
for ni in range(n_spatial_orbitals):
operator += number_operator(n_spinless_orbitals, up_index(ni), 0.5) + number_operator(
n_spinless_orbitals, down_index(ni), -0.5
)
return operator
def s_squared_operator(n_spatial_orbitals: int) -> FermionOperator:
r"""Return the s^{2} operator.
$$
\begin{align}
S^{2} = S^{-} S^{+} + S^{z}( S^{z} + 1)
\end{align}
$$
Args:
n_spatial_orbitals: number of spatial orbitals (n_qubits + 1 // 2).
Returns:
operator (FermionOperator): corresponding to the s+ operator over
n_spatial_orbitals.
Note:
The indexing convention used is that even indices correspond to
spin-up (alpha) modes and odd indices correspond to spin-down (beta)
modes.
"""
if not isinstance(n_spatial_orbitals, int):
raise TypeError("n_orbitals must be specified as an integer")
fermion_identity = FermionOperator(())
operator = s_minus_operator(n_spatial_orbitals) * s_plus_operator(n_spatial_orbitals)
operator += sz_operator(n_spatial_orbitals) * (
sz_operator(n_spatial_orbitals) + fermion_identity
)
return operator
def majorana_operator(
term: Optional[Union[Tuple[int, int], str]] = None, coefficient=1.0
) -> FermionOperator:
r"""Initialize a Majorana operator.
Args:
term(tuple or string): The first element of the tuple indicates the
mode on which the Majorana operator acts, starting from zero.
The second element of the tuple is an integer, either 0 or 1,
indicating which type of Majorana operator it is:
Type 0: $a^\dagger_p + a_p$
Type 1: $i (a^\dagger_p - a_p)$
where the $a^\dagger_p$ and $a_p$ are the usual
fermionic ladder operators.
Alternatively, one can provide a string such as 'c2', which
is a Type 0 operator on mode 2, or 'd3', which is a Type 1
operator on mode 3.
Default will result in the zero operator.
coefficient(complex or float, optional): The coefficient of the term.
Default value is 1.0.
Returns:
FermionOperator
"""
if not isinstance(coefficient, (int, float, complex)):
raise ValueError('Coefficient must be scalar.')
# If term is a string, convert it to a tuple
if isinstance(term, str):
operator_type = term[0]
mode = int(term[1:])
if operator_type == 'c':
operator_type = 0
elif operator_type == 'd':
operator_type = 1
else:
raise ValueError('Invalid operator type: {}'.format(operator_type))
term = (mode, operator_type)
# Process term
# Zero operator
if term is None:
return FermionOperator()
# Tuple
if isinstance(term, tuple):
mode, operator_type = term
if operator_type == 0:
majorana_op = FermionOperator(((mode, 1),), coefficient)
majorana_op += FermionOperator(((mode, 0),), coefficient)
elif operator_type == 1:
majorana_op = FermionOperator(((mode, 1),), 1.0j * coefficient)
majorana_op -= FermionOperator(((mode, 0),), 1.0j * coefficient)
else:
raise ValueError('Invalid operator type: {}'.format(str(operator_type)))
return majorana_op
# Invalid input.
else:
raise ValueError('Operator specified incorrectly.')
def number_operator(
n_modes: int, mode: Optional[int] = None, coefficient=1.0, parity: int = -1
) -> Union[BosonOperator, FermionOperator]:
"""Return a fermionic or bosonic number operator.
Args:
n_modes (int): The number of modes in the system.
mode (int, optional): The mode on which to return the number
operator. If None, return total number operator on all sites.
coefficient (float): The coefficient of the term.
parity (int): Returns the fermionic number operator
if parity=-1 (default),
and returns the bosonic number operator
if parity=1.
Returns:
operator (BosonOperator or FermionOperator)
"""
if parity == -1:
Op = FermionOperator
elif parity == 1:
Op = BosonOperator
else:
raise ValueError('Invalid parity value: {}'.format(parity))
if mode is None:
operator = Op()
for m in range(n_modes):
operator += number_operator(n_modes, m, coefficient, parity)
else:
operator = Op(((mode, 1), (mode, 0)), coefficient)
return operator