Skip to content

Add shift and bitwise operations #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions oqpy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,36 @@ def __pow__(self, other: AstConvertible) -> OQPyBinaryExpression:
def __rpow__(self, other: AstConvertible) -> OQPyBinaryExpression:
return self._to_binary("**", other, self)

def __lshift__(self, other: AstConvertible) -> OQPyBinaryExpression:
return self._to_binary("<<", self, other)

def __rlshift__(self, other: AstConvertible) -> OQPyBinaryExpression:
return self._to_binary("<<", other, self)

def __rshift__(self, other: AstConvertible) -> OQPyBinaryExpression:
return self._to_binary(">>", self, other)

def __rrshift__(self, other: AstConvertible) -> OQPyBinaryExpression:
return self._to_binary(">>", other, self)

def __and__(self, other: AstConvertible) -> OQPyBinaryExpression:
return self._to_binary("&", self, other)

def __rand__(self, other: AstConvertible) -> OQPyBinaryExpression:
return self._to_binary("&", other, self)

def __or__(self, other: AstConvertible) -> OQPyBinaryExpression:
return self._to_binary("|", self, other)

def __ror__(self, other: AstConvertible) -> OQPyBinaryExpression:
return self._to_binary("|", other, self)

def __xor__(self, other: AstConvertible) -> OQPyBinaryExpression:
return self._to_binary("^", self, other)

def __rxor__(self, other: AstConvertible) -> OQPyBinaryExpression:
return self._to_binary("^", other, self)

def __eq__(self, other: AstConvertible) -> OQPyBinaryExpression: # type: ignore[override]
return self._to_binary("==", self, other)

Expand All @@ -125,13 +155,26 @@ def __ge__(self, other: AstConvertible) -> OQPyBinaryExpression:
def __le__(self, other: AstConvertible) -> OQPyBinaryExpression:
return self._to_binary("<=", self, other)

def __invert__(self) -> OQPyUnaryExpression:
return self._to_unary("~", self)

def __bool__(self) -> bool:
raise RuntimeError(
"OQPy expressions cannot be converted to bool. This can occur if you try to check "
"the equality of expressions using == instead of expr_matches."
)


def logical_and(first: AstConvertible, second: AstConvertible) -> OQPyBinaryExpression:
"""Logical AND."""
return OQPyBinaryExpression(ast.BinaryOperator["&&"], first, second)


def logical_or(first: AstConvertible, second: AstConvertible) -> OQPyBinaryExpression:
"""Logical OR."""
return OQPyBinaryExpression(ast.BinaryOperator["||"], first, second)


def expr_matches(a: Any, b: Any) -> bool:
"""Check equality of the given objects.

Expand Down
48 changes: 47 additions & 1 deletion tests/test_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import oqpy
from oqpy import *
from oqpy.base import expr_matches
from oqpy.base import expr_matches, logical_and, logical_or
from oqpy.quantum_types import PhysicalQubits
from oqpy.timing import OQDurationLiteral

Expand Down Expand Up @@ -269,22 +269,68 @@ def test_binary_expressions():
prog = Program()
i = IntVar(5, "i")
j = IntVar(2, "j")
k = IntVar(0, "k")
b1 = BoolVar(False, "b1")
b2 = BoolVar(True, "b2")
b3 = BoolVar(False, "b3")
prog.set(i, 2 * (i + j))
prog.set(j, 2 % (2 - i) % 2)
prog.set(j, 1 + oqpy.pi)
prog.set(j, 1 / oqpy.pi**2 / 2 + 2**oqpy.pi)
prog.set(j, -oqpy.pi * oqpy.pi - i**j)
prog.set(k, i & 51966)
prog.set(k, 51966 & i)
prog.set(k, i & j)
prog.set(k, i | 51966)
prog.set(k, 51966 | i)
prog.set(k, i | j)
prog.set(k, i ^ 51966)
prog.set(k, 51966 & i)
prog.set(k, i ^ j)
prog.set(k, i >> 1)
prog.set(k, 1 >> i)
prog.set(k, i >> j)
prog.set(k, i << 1)
prog.set(k, 1 << j)
prog.set(k, i << j)
prog.set(k, ~k)
prog.set(b1, logical_or(b2, b3))
prog.set(b1, logical_and(b2, True))
prog.set(b1, logical_or(False, b3))

expected = textwrap.dedent(
"""
OPENQASM 3.0;
int[32] i = 5;
int[32] j = 2;
int[32] k = 0;
bool b1 = false;
bool b2 = true;
bool b3 = false;
i = 2 * (i + j);
j = 2 % (2 - i) % 2;
j = 1 + pi;
j = 1 / pi ** 2 / 2 + 2 ** pi;
j = -pi * pi - i ** j;
k = i & 51966;
k = 51966 & i;
k = i & j;
k = i | 51966;
k = 51966 | i;
k = i | j;
k = i ^ 51966;
k = 51966 & i;
k = i ^ j;
k = i >> 1;
k = 1 >> i;
k = i >> j;
k = i << 1;
k = 1 << j;
k = i << j;
k = ~k;
b1 = b2 || b3;
b1 = b2 && true;
b1 = false || b3;
"""
).strip()

Expand Down