File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -165,6 +165,16 @@ def __bool__(self) -> bool:
165
165
)
166
166
167
167
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
+
168
178
def expr_matches (a : Any , b : Any ) -> bool :
169
179
"""Check equality of the given objects.
170
180
Original file line number Diff line number Diff line change 24
24
25
25
import oqpy
26
26
from oqpy import *
27
- from oqpy .base import expr_matches
27
+ from oqpy .base import expr_matches , logical_and , logical_or
28
28
from oqpy .quantum_types import PhysicalQubits
29
29
from oqpy .timing import OQDurationLiteral
30
30
@@ -270,6 +270,9 @@ def test_binary_expressions():
270
270
i = IntVar (5 , "i" )
271
271
j = IntVar (2 , "j" )
272
272
k = IntVar (0 , "k" )
273
+ b1 = BoolVar (False , "b1" )
274
+ b2 = BoolVar (True , "b2" )
275
+ b3 = BoolVar (False , "b3" )
273
276
prog .set (i , 2 * (i + j ))
274
277
prog .set (j , 2 % (2 - i ) % 2 )
275
278
prog .set (j , 1 + oqpy .pi )
@@ -291,13 +294,19 @@ def test_binary_expressions():
291
294
prog .set (k , 1 << j )
292
295
prog .set (k , i << j )
293
296
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 ))
294
300
295
301
expected = textwrap .dedent (
296
302
"""
297
303
OPENQASM 3.0;
298
304
int[32] i = 5;
299
305
int[32] j = 2;
300
306
int[32] k = 0;
307
+ bool b1 = false;
308
+ bool b2 = true;
309
+ bool b3 = false;
301
310
i = 2 * (i + j);
302
311
j = 2 % (2 - i) % 2;
303
312
j = 1 + pi;
@@ -319,6 +328,9 @@ def test_binary_expressions():
319
328
k = 1 << j;
320
329
k = i << j;
321
330
k = ~k;
331
+ b1 = b2 || b3;
332
+ b1 = b2 && true;
333
+ b1 = false || b3;
322
334
"""
323
335
).strip ()
324
336
You can’t perform that action at this time.
0 commit comments