|
| 1 | +""" |
| 2 | +qubit.address method table for a few builtin dialects. |
| 3 | +""" |
| 4 | + |
| 5 | +from kirin import interp |
| 6 | +from kirin.dialects import cf, py, func, ilist |
| 7 | + |
| 8 | +from .lattice import NotQubit, AddressReg, AddressQubit, AddressTuple |
| 9 | +from .analysis import AddressAnalysis |
| 10 | + |
| 11 | + |
| 12 | +@py.binop.dialect.register(key="qubit.address") |
| 13 | +class PyBinOp(interp.MethodTable): |
| 14 | + |
| 15 | + @interp.impl(py.Add) |
| 16 | + def add(self, interp: AddressAnalysis, frame: interp.Frame, stmt: py.Add): |
| 17 | + lhs = frame.get(stmt.lhs) |
| 18 | + rhs = frame.get(stmt.rhs) |
| 19 | + |
| 20 | + if isinstance(lhs, AddressTuple) and isinstance(rhs, AddressTuple): |
| 21 | + return (AddressTuple(data=lhs.data + rhs.data),) |
| 22 | + else: |
| 23 | + return (NotQubit(),) |
| 24 | + |
| 25 | + |
| 26 | +@py.tuple.dialect.register(key="qubit.address") |
| 27 | +class PyTuple(interp.MethodTable): |
| 28 | + @interp.impl(py.tuple.New) |
| 29 | + def new_tuple( |
| 30 | + self, |
| 31 | + interp: AddressAnalysis, |
| 32 | + frame: interp.Frame, |
| 33 | + stmt: py.tuple.New, |
| 34 | + ): |
| 35 | + return (AddressTuple(frame.get_values(stmt.args)),) |
| 36 | + |
| 37 | + |
| 38 | +@ilist.dialect.register(key="qubit.address") |
| 39 | +class IList(interp.MethodTable): |
| 40 | + @interp.impl(ilist.New) |
| 41 | + def new_ilist( |
| 42 | + self, |
| 43 | + interp: AddressAnalysis, |
| 44 | + frame: interp.Frame, |
| 45 | + stmt: ilist.New, |
| 46 | + ): |
| 47 | + return (AddressTuple(frame.get_values(stmt.args)),) |
| 48 | + |
| 49 | + |
| 50 | +@py.list.dialect.register(key="qubit.address") |
| 51 | +class PyList(interp.MethodTable): |
| 52 | + @interp.impl(py.list.New) |
| 53 | + def new_ilist( |
| 54 | + self, |
| 55 | + interp: AddressAnalysis, |
| 56 | + frame: interp.Frame, |
| 57 | + stmt: py.list.New, |
| 58 | + ): |
| 59 | + return (AddressTuple(frame.get_values(stmt.args)),) |
| 60 | + |
| 61 | + |
| 62 | +@py.indexing.dialect.register(key="qubit.address") |
| 63 | +class PyIndexing(interp.MethodTable): |
| 64 | + @interp.impl(py.GetItem) |
| 65 | + def getitem(self, interp: AddressAnalysis, frame: interp.Frame, stmt: py.GetItem): |
| 66 | + idx = interp.get_const_value(int, stmt.index) |
| 67 | + obj = frame.get(stmt.obj) |
| 68 | + if isinstance(obj, AddressTuple): |
| 69 | + return (obj.data[idx],) |
| 70 | + elif isinstance(obj, AddressReg): |
| 71 | + return (AddressQubit(obj.data[idx]),) |
| 72 | + else: |
| 73 | + return (NotQubit(),) |
| 74 | + |
| 75 | + |
| 76 | +@py.assign.dialect.register(key="qubit.address") |
| 77 | +class PyAssign(interp.MethodTable): |
| 78 | + @interp.impl(py.Alias) |
| 79 | + def alias(self, interp: AddressAnalysis, frame: interp.Frame, stmt: py.Alias): |
| 80 | + return (frame.get(stmt.value),) |
| 81 | + |
| 82 | + |
| 83 | +@func.dialect.register(key="qubit.address") |
| 84 | +class Func(interp.MethodTable): |
| 85 | + @interp.impl(func.Return) |
| 86 | + def return_(self, _: AddressAnalysis, frame: interp.Frame, stmt: func.Return): |
| 87 | + return interp.ReturnValue(frame.get(stmt.value)) |
| 88 | + |
| 89 | + |
| 90 | +@cf.dialect.register(key="qubit.address") |
| 91 | +class Cf(cf.typeinfer.TypeInfer): |
| 92 | + # NOTE: cf just re-use the type infer method table |
| 93 | + # it's the same process as type infer. |
| 94 | + pass |
0 commit comments