MCPcopy Create free account
hub / github.com/NVIDIA/cuda-quantum / visit_Compare

Method visit_Compare

python/cudaq/kernel/ast_bridge.py:5021–5188  ·  view source on GitHub ↗

Visit while loop compare operations and translate to equivalent MLIR. Note, Python lets you construct expressions with multiple comparators, here we limit ourselves to just a single comparator.

(self, node)

Source from the content-addressed store, hash-verified

5019 self.emitFatalError(f'unsupported boolean expression {node.op}', node)
5020
5021 def visit_Compare(self, node):
5022 """Visit while loop compare operations and translate to equivalent MLIR.
5023
5024 Note, Python lets you construct expressions with multiple comparators,
5025 here we limit ourselves to just a single comparator.
5026 """
5027 if len(node.ops) > 1:
5028 self.emitFatalError("only single comparators are supported", node)
5029
5030 iTy = self.getIntegerType()
5031 self.visit(node.left)
5032 left = self.popValue()
5033 self.visit(node.comparators[0])
5034 right = self.popValue()
5035 op = node.ops[0]
5036
5037 def convert_arithmetic_types(item1, item2):
5038 superior_type = self.__get_superior_type(item1.type, item2.type)
5039 if superior_type is None:
5040 self.emitFatalError("invalid type in comparison", node)
5041 item1 = self.changeOperandToType(superior_type,
5042 item1,
5043 allowDemotion=False)
5044 item2 = self.changeOperandToType(superior_type,
5045 item2,
5046 allowDemotion=False)
5047 return item1, item2
5048
5049 # To understand the integer attributes used here (the predicates) see
5050 # `arith::CmpIPredicate` and `arith::CmpFPredicate`.
5051
5052 def compare_equality(item1, item2):
5053
5054 # TODO: the In/NotIn case should be recursive such that we can
5055 # search for a list in a list of lists.
5056 # `mz(q1) == mz(q2)` (or with one side being a handle and the
5057 # other a `bool`): discriminate each handle to `i1` first and let
5058 # `convert_arithmetic_types` finish the integer comparison.
5059 item1 = self.__discriminateIfMeasureHandle(item1, node)
5060 item2 = self.__discriminateIfMeasureHandle(item2, node)
5061 item1, item2 = convert_arithmetic_types(item1, item2)
5062 iCondPred = self.getIntegerAttr(iTy, 0)
5063 fCondPred = self.getIntegerAttr(iTy, 1)
5064
5065 if ComplexType.isinstance(item1.type):
5066 reComp = arith.CmpFOp(fCondPred,
5067 complex.ReOp(item1).result,
5068 complex.ReOp(item2).result).result
5069 imComp = arith.CmpFOp(fCondPred,
5070 complex.ImOp(item1).result,
5071 complex.ImOp(item2).result).result
5072 return arith.AndIOp(reComp, imComp).result
5073 elif IntegerType.isinstance(item1.type):
5074 return arith.CmpIOp(iCondPred, item1, item2).result
5075 else:
5076 return arith.CmpFOp(fCondPred, item1, item2).result
5077
5078 if isinstance(op, ast.Gt):

Callers

nothing calls this directly

Calls 9

emitFatalErrorMethod · 0.95
getIntegerTypeMethod · 0.95
visitMethod · 0.95
popValueMethod · 0.95
pushValueMethod · 0.95
getIntegerAttrMethod · 0.95
getConstantIntMethod · 0.95
createForLoopMethod · 0.95
getMethod · 0.45

Tested by

no test coverage detected