|
1 | | -from kirin import interp |
| 1 | +from typing import cast |
2 | 2 |
|
3 | | -from bloqade import squin |
| 3 | +from kirin import ir, interp |
4 | 4 |
|
5 | | -""" from .lattice import ( |
6 | | - Shape, |
| 5 | +from bloqade.squin import op |
| 6 | + |
| 7 | +from .lattice import ( |
7 | 8 | NoShape, |
8 | 9 | OpShape, |
9 | 10 | ) |
10 | | -
|
11 | | -from .analysis import ShapeAnalysis """ |
| 11 | +from .analysis import ShapeAnalysis |
12 | 12 |
|
13 | 13 |
|
14 | | -@squin.op.dialect.register(key="op.shape") |
| 14 | +@op.dialect.register(key="op.shape") |
15 | 15 | class SquinOp(interp.MethodTable): |
16 | | - pass |
17 | 16 |
|
18 | | - # Should be using the Sized trait |
19 | | - # that the statements have |
| 17 | + @interp.impl(op.stmts.Kron) |
| 18 | + def kron(self, interp: ShapeAnalysis, frame: interp.Frame, stmt: op.stmts.Kron): |
| 19 | + lhs = frame.get(stmt.lhs) |
| 20 | + rhs = frame.get(stmt.rhs) |
| 21 | + if isinstance(lhs, OpShape) and isinstance(rhs, OpShape): |
| 22 | + new_size = lhs.size + rhs.size |
| 23 | + return (OpShape(size=new_size),) |
| 24 | + else: |
| 25 | + return (NoShape(),) |
| 26 | + |
| 27 | + @interp.impl(op.stmts.Mult) |
| 28 | + def mult(self, interp: ShapeAnalysis, frame: interp.Frame, stmt: op.stmts.Mult): |
| 29 | + lhs = frame.get(stmt.lhs) |
| 30 | + rhs = frame.get(stmt.rhs) |
| 31 | + |
| 32 | + if isinstance(lhs, OpShape) and isinstance(rhs, OpShape): |
| 33 | + lhs_size = lhs.size |
| 34 | + rhs_size = rhs.size |
| 35 | + # Sized trait implicitly enforces that |
| 36 | + # all operators are square matrices, |
| 37 | + # not sure if it's worth raising an exception here |
| 38 | + # or just letting this propagate... |
| 39 | + if lhs_size != rhs_size: |
| 40 | + return (NoShape(),) |
| 41 | + else: |
| 42 | + return (OpShape(size=lhs_size + rhs_size),) |
| 43 | + else: |
| 44 | + return (NoShape(),) |
| 45 | + |
| 46 | + @interp.impl(op.stmts.Control) |
| 47 | + def control( |
| 48 | + self, interp: ShapeAnalysis, frame: interp.Frame, stmt: op.stmts.Control |
| 49 | + ): |
| 50 | + op_shape = frame.get(stmt.op) |
| 51 | + |
| 52 | + if isinstance(op_shape, OpShape): |
| 53 | + op_size = op_shape.size |
| 54 | + n_controls_attr = stmt.get_attr_or_prop("n_controls") |
| 55 | + # raise exception if attribute is NOne |
| 56 | + n_controls = cast(ir.PyAttr[int], n_controls_attr).data |
| 57 | + return (OpShape(size=op_size + n_controls),) |
| 58 | + else: |
| 59 | + return (NoShape(),) |
| 60 | + |
| 61 | + @interp.impl(op.stmts.Rot) |
| 62 | + def rot(self, interp: ShapeAnalysis, frame: interp.Frame, stmt: op.stmts.Rot): |
| 63 | + op_shape = frame.get(stmt.axis) |
| 64 | + return op_shape |
20 | 65 |
|
21 | | - # Need to keep in mind that Identity |
22 | | - # has a HasSize() trait with "size:int" |
23 | | - # as the corresponding attribute to query |
24 | | - # @interp.impl(squin.op.stmts.ConstantUnitary) |
| 66 | + @interp.impl(op.stmts.Scale) |
| 67 | + def scale(self, interp: ShapeAnalysis, frame: interp.Frame, stmt: op.stmts.Scale): |
| 68 | + op_shape = frame.get(stmt.op) |
| 69 | + return op_shape |
0 commit comments