|
| 1 | +from typing import cast |
| 2 | + |
| 3 | +from kirin import ir, interp |
| 4 | + |
| 5 | +from bloqade.squin import op |
| 6 | + |
| 7 | +from .lattice import ( |
| 8 | + NoSites, |
| 9 | + NumberSites, |
| 10 | +) |
| 11 | +from .analysis import NSitesAnalysis |
| 12 | + |
| 13 | + |
| 14 | +@op.dialect.register(key="op.nsites") |
| 15 | +class SquinOp(interp.MethodTable): |
| 16 | + |
| 17 | + @interp.impl(op.stmts.Kron) |
| 18 | + def kron(self, interp: NSitesAnalysis, frame: interp.Frame, stmt: op.stmts.Kron): |
| 19 | + lhs = frame.get(stmt.lhs) |
| 20 | + rhs = frame.get(stmt.rhs) |
| 21 | + if isinstance(lhs, NumberSites) and isinstance(rhs, NumberSites): |
| 22 | + new_n_sites = lhs.sites + rhs.sites |
| 23 | + return (NumberSites(sites=new_n_sites),) |
| 24 | + else: |
| 25 | + return (NoSites(),) |
| 26 | + |
| 27 | + @interp.impl(op.stmts.Mult) |
| 28 | + def mult(self, interp: NSitesAnalysis, frame: interp.Frame, stmt: op.stmts.Mult): |
| 29 | + lhs = frame.get(stmt.lhs) |
| 30 | + rhs = frame.get(stmt.rhs) |
| 31 | + |
| 32 | + if isinstance(lhs, NumberSites) and isinstance(rhs, NumberSites): |
| 33 | + lhs_sites = lhs.sites |
| 34 | + rhs_sites = rhs.sites |
| 35 | + # I originally considered throwing an exception here |
| 36 | + # but Xiu-zhe (Roger) Luo has pointed out it would be |
| 37 | + # a much better UX to add a type element that |
| 38 | + # could explicitly indicate the error. The downside |
| 39 | + # is you'll have some added complexity in the type lattice. |
| 40 | + if lhs_sites != rhs_sites: |
| 41 | + return (NoSites(),) |
| 42 | + else: |
| 43 | + return (NumberSites(sites=lhs_sites + rhs_sites),) |
| 44 | + else: |
| 45 | + return (NoSites(),) |
| 46 | + |
| 47 | + @interp.impl(op.stmts.Control) |
| 48 | + def control( |
| 49 | + self, interp: NSitesAnalysis, frame: interp.Frame, stmt: op.stmts.Control |
| 50 | + ): |
| 51 | + op_sites = frame.get(stmt.op) |
| 52 | + |
| 53 | + if isinstance(op_sites, NumberSites): |
| 54 | + n_sites = op_sites.sites |
| 55 | + n_controls_attr = stmt.get_attr_or_prop("n_controls") |
| 56 | + n_controls = cast(ir.PyAttr[int], n_controls_attr).data |
| 57 | + return (NumberSites(sites=n_sites + n_controls),) |
| 58 | + else: |
| 59 | + return (NoSites(),) |
| 60 | + |
| 61 | + @interp.impl(op.stmts.Rot) |
| 62 | + def rot(self, interp: NSitesAnalysis, frame: interp.Frame, stmt: op.stmts.Rot): |
| 63 | + op_sites = frame.get(stmt.axis) |
| 64 | + return (op_sites,) |
| 65 | + |
| 66 | + @interp.impl(op.stmts.Scale) |
| 67 | + def scale(self, interp: NSitesAnalysis, frame: interp.Frame, stmt: op.stmts.Scale): |
| 68 | + op_sites = frame.get(stmt.op) |
| 69 | + return (op_sites,) |
0 commit comments