Skip to content

Commit

Permalink
conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
1ucian0 committed Jul 20, 2023
2 parents 25b3e03 + e595066 commit 722095f
Show file tree
Hide file tree
Showing 46 changed files with 1,744 additions and 161 deletions.
29 changes: 17 additions & 12 deletions qiskit/circuit/classical/expr/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,28 +240,30 @@ def logic_not(operand: typing.Any, /) -> Expr:
return Unary(Unary.Op.LOGIC_NOT, operand, operand.type)


def _binary_bitwise(op: Binary.Op, left: typing.Any, right: typing.Any) -> Expr:
# Inference for integer literals is slightly different here as a convenience, since the bitwise
# binary operations require operands of the same width.
if not (isinstance(left, int) or isinstance(right, int)):
def _lift_binary_operands(left: typing.Any, right: typing.Any) -> tuple[Expr, Expr]:
"""Lift two binary operands simultaneously, inferring the widths of integer literals in either
position to match the other operand."""
left_int = isinstance(left, int) and not isinstance(left, bool)
right_int = isinstance(right, int) and not isinstance(right, bool)
if not (left_int or right_int):
left = lift(left)
right = lift(right)
elif isinstance(left, int):
elif not right_int:
right = lift(right)
if right.type.kind is types.Uint:
if left.bit_length() > right.type.width:
raise TypeError(
f"integer literal '{left}' is wider than the other bitwise operand '{right}'"
f"integer literal '{left}' is wider than the other operand '{right}'"
)
left = Value(left, right.type)
else:
left = lift(left)
elif isinstance(right, int):
elif not left_int:
left = lift(left)
if left.type.kind is types.Uint:
if right.bit_length() > left.type.width:
raise TypeError(
f"integer literal '{right}' is wider than the other bitwise operand '{left}'"
f"integer literal '{right}' is wider than the other operand '{left}'"
)
right = Value(right, left.type)
else:
Expand All @@ -271,6 +273,11 @@ def _binary_bitwise(op: Binary.Op, left: typing.Any, right: typing.Any) -> Expr:
uint = types.Uint(max(left.bit_length(), right.bit_length(), 1))
left = Value(left, uint)
right = Value(right, uint)
return left, right


def _binary_bitwise(op: Binary.Op, left: typing.Any, right: typing.Any) -> Expr:
left, right = _lift_binary_operands(left, right)
type: types.Type
if left.type.kind is right.type.kind is types.Bool:
type = types.Bool()
Expand Down Expand Up @@ -381,8 +388,7 @@ def logic_or(left: typing.Any, right: typing.Any, /) -> Expr:


def _equal_like(op: Binary.Op, left: typing.Any, right: typing.Any) -> Expr:
left = lift(left)
right = lift(right)
left, right = _lift_binary_operands(left, right)
if left.type.kind is not right.type.kind:
raise TypeError(f"invalid types for '{op}': '{left.type}' and '{right.type}'")
type = types.greater(left.type, right.type)
Expand Down Expand Up @@ -426,8 +432,7 @@ def not_equal(left: typing.Any, right: typing.Any, /) -> Expr:


def _binary_relation(op: Binary.Op, left: typing.Any, right: typing.Any) -> Expr:
left = lift(left)
right = lift(right)
left, right = _lift_binary_operands(left, right)
if left.type.kind is not right.type.kind or left.type.kind is types.Bool:
raise TypeError(f"invalid types for '{op}': '{left.type}' and '{right.type}'")
type = types.greater(left.type, right.type)
Expand Down
Loading

0 comments on commit 722095f

Please sign in to comment.