|
| 1 | +from kirin import types as kirin_types, interp |
| 2 | +from kirin.dialects import py, scf, func, ilist |
| 3 | + |
| 4 | +from bloqade.squin import wire, qubit |
| 5 | + |
| 6 | +from .lattice import ( |
| 7 | + AnyMeasureId, |
| 8 | + NotMeasureId, |
| 9 | + MeasureIdBool, |
| 10 | + MeasureIdTuple, |
| 11 | + InvalidMeasureId, |
| 12 | +) |
| 13 | +from .analysis import MeasurementIDAnalysis |
| 14 | + |
| 15 | +## Can't do wire right now because of |
| 16 | +## unresolved RFC on return type |
| 17 | +# from bloqade.squin import wire |
| 18 | + |
| 19 | + |
| 20 | +@qubit.dialect.register(key="measure_id") |
| 21 | +class SquinQubit(interp.MethodTable): |
| 22 | + |
| 23 | + @interp.impl(qubit.MeasureQubit) |
| 24 | + def measure_qubit( |
| 25 | + self, |
| 26 | + interp: MeasurementIDAnalysis, |
| 27 | + frame: interp.Frame, |
| 28 | + stmt: qubit.MeasureQubit, |
| 29 | + ): |
| 30 | + interp.measure_count += 1 |
| 31 | + return (MeasureIdBool(interp.measure_count),) |
| 32 | + |
| 33 | + @interp.impl(qubit.MeasureQubitList) |
| 34 | + def measure_qubit_list( |
| 35 | + self, |
| 36 | + interp: MeasurementIDAnalysis, |
| 37 | + frame: interp.Frame, |
| 38 | + stmt: qubit.MeasureQubitList, |
| 39 | + ): |
| 40 | + |
| 41 | + # try to get the length of the list |
| 42 | + ## "...safely assume the type inference will give you what you need" |
| 43 | + qubits_type = stmt.qubits.type |
| 44 | + # vars[0] is just the type of the elements in the ilist, |
| 45 | + # vars[1] can contain a literal with length information |
| 46 | + num_qubits = qubits_type.vars[1] |
| 47 | + if not isinstance(num_qubits, kirin_types.Literal): |
| 48 | + return (AnyMeasureId(),) |
| 49 | + |
| 50 | + measure_id_bools = [] |
| 51 | + for _ in range(num_qubits.data): |
| 52 | + interp.measure_count += 1 |
| 53 | + measure_id_bools.append(MeasureIdBool(interp.measure_count)) |
| 54 | + |
| 55 | + return (MeasureIdTuple(data=tuple(measure_id_bools)),) |
| 56 | + |
| 57 | + |
| 58 | +@wire.dialect.register(key="measure_id") |
| 59 | +class SquinWire(interp.MethodTable): |
| 60 | + |
| 61 | + @interp.impl(wire.Measure) |
| 62 | + def measure_qubit( |
| 63 | + self, |
| 64 | + interp: MeasurementIDAnalysis, |
| 65 | + frame: interp.Frame, |
| 66 | + stmt: wire.Measure, |
| 67 | + ): |
| 68 | + interp.measure_count += 1 |
| 69 | + return (MeasureIdBool(interp.measure_count),) |
| 70 | + |
| 71 | + |
| 72 | +@ilist.dialect.register(key="measure_id") |
| 73 | +class IList(interp.MethodTable): |
| 74 | + @interp.impl(ilist.New) |
| 75 | + # Because of the way GetItem works, |
| 76 | + # A user could create an ilist of bools that |
| 77 | + # ends up being a mixture of MeasureIdBool and NotMeasureId |
| 78 | + def new_ilist( |
| 79 | + self, |
| 80 | + interp: MeasurementIDAnalysis, |
| 81 | + frame: interp.Frame, |
| 82 | + stmt: ilist.New, |
| 83 | + ): |
| 84 | + |
| 85 | + measure_ids_in_ilist = frame.get_values(stmt.values) |
| 86 | + return (MeasureIdTuple(data=tuple(measure_ids_in_ilist)),) |
| 87 | + |
| 88 | + |
| 89 | +@py.tuple.dialect.register(key="measure_id") |
| 90 | +class PyTuple(interp.MethodTable): |
| 91 | + @interp.impl(py.tuple.New) |
| 92 | + def new_tuple( |
| 93 | + self, interp: MeasurementIDAnalysis, frame: interp.Frame, stmt: py.tuple.New |
| 94 | + ): |
| 95 | + measure_ids_in_tuple = frame.get_values(stmt.args) |
| 96 | + return (MeasureIdTuple(data=tuple(measure_ids_in_tuple)),) |
| 97 | + |
| 98 | + |
| 99 | +@py.indexing.dialect.register(key="measure_id") |
| 100 | +class PyIndexing(interp.MethodTable): |
| 101 | + @interp.impl(py.GetItem) |
| 102 | + def getitem( |
| 103 | + self, interp: MeasurementIDAnalysis, frame: interp.Frame, stmt: py.GetItem |
| 104 | + ): |
| 105 | + idx = interp.get_const_value(int, stmt.index) |
| 106 | + obj = frame.get(stmt.obj) |
| 107 | + if isinstance(obj, MeasureIdTuple): |
| 108 | + return (obj.data[idx],) |
| 109 | + # just propagate these down the line |
| 110 | + elif isinstance(obj, (AnyMeasureId, NotMeasureId)): |
| 111 | + return (obj,) |
| 112 | + else: |
| 113 | + return (InvalidMeasureId(),) |
| 114 | + |
| 115 | + |
| 116 | +@py.binop.dialect.register(key="measure_id") |
| 117 | +class PyBinOp(interp.MethodTable): |
| 118 | + @interp.impl(py.Add) |
| 119 | + def add(self, interp: MeasurementIDAnalysis, frame: interp.Frame, stmt: py.Add): |
| 120 | + lhs = frame.get(stmt.lhs) |
| 121 | + rhs = frame.get(stmt.rhs) |
| 122 | + |
| 123 | + if isinstance(lhs, MeasureIdTuple) and isinstance(rhs, MeasureIdTuple): |
| 124 | + return (MeasureIdTuple(data=lhs.data + rhs.data),) |
| 125 | + else: |
| 126 | + return (InvalidMeasureId(),) |
| 127 | + |
| 128 | + |
| 129 | +@func.dialect.register(key="measure_id") |
| 130 | +class Func(interp.MethodTable): |
| 131 | + @interp.impl(func.Return) |
| 132 | + def return_(self, _: MeasurementIDAnalysis, frame: interp.Frame, stmt: func.Return): |
| 133 | + return interp.ReturnValue(frame.get(stmt.value)) |
| 134 | + |
| 135 | + # taken from Address Analysis implementation from Xiu-zhe (Roger) Luo |
| 136 | + @interp.impl( |
| 137 | + func.Invoke |
| 138 | + ) # we know the callee already, func.Call would mean we don't know the callee @ compile time |
| 139 | + def invoke( |
| 140 | + self, interp_: MeasurementIDAnalysis, frame: interp.Frame, stmt: func.Invoke |
| 141 | + ): |
| 142 | + _, ret = interp_.run_method( |
| 143 | + stmt.callee, |
| 144 | + interp_.permute_values( |
| 145 | + stmt.callee.arg_names, frame.get_values(stmt.inputs), stmt.kwargs |
| 146 | + ), |
| 147 | + ) |
| 148 | + return (ret,) |
| 149 | + |
| 150 | + |
| 151 | +# Just let analysis propagate through |
| 152 | +# scf, particularly IfElse |
| 153 | +@scf.dialect.register(key="measure_id") |
| 154 | +class Scf(scf.absint.Methods): |
| 155 | + pass |
0 commit comments