Skip to content

Commit 2fe8f4d

Browse files
committed
Add logical OR and AND
1 parent 56663b9 commit 2fe8f4d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

oqpy/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ def __bool__(self) -> bool:
165165
)
166166

167167

168+
def logical_and(first: AstConvertible, second: AstConvertible) -> OQPyBinaryExpression:
169+
"""Logical AND."""
170+
return OQPyBinaryExpression(ast.BinaryOperator["&&"], first, second)
171+
172+
173+
def logical_or(first: AstConvertible, second: AstConvertible) -> OQPyBinaryExpression:
174+
"""Logical OR."""
175+
return OQPyBinaryExpression(ast.BinaryOperator["||"], first, second)
176+
177+
168178
def expr_matches(a: Any, b: Any) -> bool:
169179
"""Check equality of the given objects.
170180

tests/test_directives.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import oqpy
2626
from oqpy import *
27-
from oqpy.base import expr_matches
27+
from oqpy.base import expr_matches, logical_and, logical_or
2828
from oqpy.quantum_types import PhysicalQubits
2929
from oqpy.timing import OQDurationLiteral
3030

@@ -270,6 +270,9 @@ def test_binary_expressions():
270270
i = IntVar(5, "i")
271271
j = IntVar(2, "j")
272272
k = IntVar(0, "k")
273+
b1 = BoolVar(False, "b1")
274+
b2 = BoolVar(True, "b2")
275+
b3 = BoolVar(False, "b3")
273276
prog.set(i, 2 * (i + j))
274277
prog.set(j, 2 % (2 - i) % 2)
275278
prog.set(j, 1 + oqpy.pi)
@@ -291,13 +294,19 @@ def test_binary_expressions():
291294
prog.set(k, 1 << j)
292295
prog.set(k, i << j)
293296
prog.set(k, ~k)
297+
prog.set(b1, logical_or(b2, b3))
298+
prog.set(b1, logical_and(b2, True))
299+
prog.set(b1, logical_or(False, b3))
294300

295301
expected = textwrap.dedent(
296302
"""
297303
OPENQASM 3.0;
298304
int[32] i = 5;
299305
int[32] j = 2;
300306
int[32] k = 0;
307+
bool b1 = false;
308+
bool b2 = true;
309+
bool b3 = false;
301310
i = 2 * (i + j);
302311
j = 2 % (2 - i) % 2;
303312
j = 1 + pi;
@@ -319,6 +328,9 @@ def test_binary_expressions():
319328
k = 1 << j;
320329
k = i << j;
321330
k = ~k;
331+
b1 = b2 || b3;
332+
b1 = b2 && true;
333+
b1 = false || b3;
322334
"""
323335
).strip()
324336

0 commit comments

Comments
 (0)