Skip to content

Commit 56663b9

Browse files
committed
Add shift and logical operations
1 parent db9ec10 commit 56663b9

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

oqpy/base.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,36 @@ def __pow__(self, other: AstConvertible) -> OQPyBinaryExpression:
107107
def __rpow__(self, other: AstConvertible) -> OQPyBinaryExpression:
108108
return self._to_binary("**", other, self)
109109

110+
def __lshift__(self, other: AstConvertible) -> OQPyBinaryExpression:
111+
return self._to_binary("<<", self, other)
112+
113+
def __rlshift__(self, other: AstConvertible) -> OQPyBinaryExpression:
114+
return self._to_binary("<<", other, self)
115+
116+
def __rshift__(self, other: AstConvertible) -> OQPyBinaryExpression:
117+
return self._to_binary(">>", self, other)
118+
119+
def __rrshift__(self, other: AstConvertible) -> OQPyBinaryExpression:
120+
return self._to_binary(">>", other, self)
121+
122+
def __and__(self, other: AstConvertible) -> OQPyBinaryExpression:
123+
return self._to_binary("&", self, other)
124+
125+
def __rand__(self, other: AstConvertible) -> OQPyBinaryExpression:
126+
return self._to_binary("&", other, self)
127+
128+
def __or__(self, other: AstConvertible) -> OQPyBinaryExpression:
129+
return self._to_binary("|", self, other)
130+
131+
def __ror__(self, other: AstConvertible) -> OQPyBinaryExpression:
132+
return self._to_binary("|", other, self)
133+
134+
def __xor__(self, other: AstConvertible) -> OQPyBinaryExpression:
135+
return self._to_binary("^", self, other)
136+
137+
def __rxor__(self, other: AstConvertible) -> OQPyBinaryExpression:
138+
return self._to_binary("^", other, self)
139+
110140
def __eq__(self, other: AstConvertible) -> OQPyBinaryExpression: # type: ignore[override]
111141
return self._to_binary("==", self, other)
112142

@@ -125,6 +155,9 @@ def __ge__(self, other: AstConvertible) -> OQPyBinaryExpression:
125155
def __le__(self, other: AstConvertible) -> OQPyBinaryExpression:
126156
return self._to_binary("<=", self, other)
127157

158+
def __invert__(self) -> OQPyUnaryExpression:
159+
return self._to_unary("~", self)
160+
128161
def __bool__(self) -> bool:
129162
raise RuntimeError(
130163
"OQPy expressions cannot be converted to bool. This can occur if you try to check "

tests/test_directives.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,22 +269,56 @@ def test_binary_expressions():
269269
prog = Program()
270270
i = IntVar(5, "i")
271271
j = IntVar(2, "j")
272+
k = IntVar(0, "k")
272273
prog.set(i, 2 * (i + j))
273274
prog.set(j, 2 % (2 - i) % 2)
274275
prog.set(j, 1 + oqpy.pi)
275276
prog.set(j, 1 / oqpy.pi**2 / 2 + 2**oqpy.pi)
276277
prog.set(j, -oqpy.pi * oqpy.pi - i**j)
278+
prog.set(k, i & 51966)
279+
prog.set(k, 51966 & i)
280+
prog.set(k, i & j)
281+
prog.set(k, i | 51966)
282+
prog.set(k, 51966 | i)
283+
prog.set(k, i | j)
284+
prog.set(k, i ^ 51966)
285+
prog.set(k, 51966 & i)
286+
prog.set(k, i ^ j)
287+
prog.set(k, i >> 1)
288+
prog.set(k, 1 >> i)
289+
prog.set(k, i >> j)
290+
prog.set(k, i << 1)
291+
prog.set(k, 1 << j)
292+
prog.set(k, i << j)
293+
prog.set(k, ~k)
277294

278295
expected = textwrap.dedent(
279296
"""
280297
OPENQASM 3.0;
281298
int[32] i = 5;
282299
int[32] j = 2;
300+
int[32] k = 0;
283301
i = 2 * (i + j);
284302
j = 2 % (2 - i) % 2;
285303
j = 1 + pi;
286304
j = 1 / pi ** 2 / 2 + 2 ** pi;
287305
j = -pi * pi - i ** j;
306+
k = i & 51966;
307+
k = 51966 & i;
308+
k = i & j;
309+
k = i | 51966;
310+
k = 51966 | i;
311+
k = i | j;
312+
k = i ^ 51966;
313+
k = 51966 & i;
314+
k = i ^ j;
315+
k = i >> 1;
316+
k = 1 >> i;
317+
k = i >> j;
318+
k = i << 1;
319+
k = 1 << j;
320+
k = i << j;
321+
k = ~k;
288322
"""
289323
).strip()
290324

0 commit comments

Comments
 (0)