|
| 1 | +from kirin import ir, interp as _interp |
| 2 | +from kirin.analysis import const |
| 3 | +from kirin.dialects import scf, func |
| 4 | + |
| 5 | +from bloqade.squin import gate |
| 6 | +from bloqade.validation.analysis import ValidationFrame |
| 7 | +from bloqade.validation.analysis.lattice import Error |
| 8 | + |
| 9 | +from .analysis import GeminiLogicalValidationAnalysis |
| 10 | + |
| 11 | + |
| 12 | +@scf.dialect.register(key="gemini.validate.logical") |
| 13 | +class __ScfGeminiLogicalValidation(_interp.MethodTable): |
| 14 | + |
| 15 | + @_interp.impl(scf.IfElse) |
| 16 | + def if_else( |
| 17 | + self, |
| 18 | + interp: GeminiLogicalValidationAnalysis, |
| 19 | + frame: ValidationFrame, |
| 20 | + stmt: scf.IfElse, |
| 21 | + ): |
| 22 | + frame.errors.append( |
| 23 | + ir.ValidationError( |
| 24 | + stmt, "If statements are not supported in logical Gemini programs!" |
| 25 | + ) |
| 26 | + ) |
| 27 | + return ( |
| 28 | + Error( |
| 29 | + message="If statements are not supported in logical Gemini programs!" |
| 30 | + ), |
| 31 | + ) |
| 32 | + |
| 33 | + @_interp.impl(scf.For) |
| 34 | + def for_loop( |
| 35 | + self, |
| 36 | + interp: GeminiLogicalValidationAnalysis, |
| 37 | + frame: ValidationFrame, |
| 38 | + stmt: scf.For, |
| 39 | + ): |
| 40 | + if isinstance(stmt.iterable.hints.get("const"), const.Value): |
| 41 | + return (interp.lattice.top(),) |
| 42 | + |
| 43 | + frame.errors.append( |
| 44 | + ir.ValidationError( |
| 45 | + stmt, |
| 46 | + "Non-constant iterable in for loop is not supported in Gemini logical programs!", |
| 47 | + ) |
| 48 | + ) |
| 49 | + |
| 50 | + return ( |
| 51 | + Error( |
| 52 | + message="Non-constant iterable in for loop is not supported in Gemini logical programs!" |
| 53 | + ), |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +@func.dialect.register(key="gemini.validate.logical") |
| 58 | +class __FuncGeminiLogicalValidation(_interp.MethodTable): |
| 59 | + @_interp.impl(func.Invoke) |
| 60 | + def invoke( |
| 61 | + self, |
| 62 | + interp: GeminiLogicalValidationAnalysis, |
| 63 | + frame: ValidationFrame, |
| 64 | + stmt: func.Invoke, |
| 65 | + ): |
| 66 | + frame.errors.append( |
| 67 | + ir.ValidationError( |
| 68 | + stmt, |
| 69 | + "Function invocations not supported in logical Gemini program!", |
| 70 | + help="Make sure to decorate your function with `@logical(inline = True)` or `@logical(aggressive_unroll = True)` to inline function calls", |
| 71 | + ) |
| 72 | + ) |
| 73 | + |
| 74 | + return tuple( |
| 75 | + Error( |
| 76 | + message="Function invocations not supported in logical Gemini program!" |
| 77 | + ) |
| 78 | + for _ in stmt.results |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +@gate.dialect.register(key="gemini.validate.logical") |
| 83 | +class __GateGeminiLogicalValidation(_interp.MethodTable): |
| 84 | + @_interp.impl(gate.stmts.U3) |
| 85 | + def u3( |
| 86 | + self, |
| 87 | + interp: GeminiLogicalValidationAnalysis, |
| 88 | + frame: ValidationFrame, |
| 89 | + stmt: gate.stmts.U3, |
| 90 | + ): |
| 91 | + if interp.first_gate: |
| 92 | + interp.first_gate = False |
| 93 | + return () |
| 94 | + |
| 95 | + frame.errors.append( |
| 96 | + ir.ValidationError( |
| 97 | + stmt, |
| 98 | + "U3 gate can only be used for initial state preparation, i.e. as the first gate!", |
| 99 | + ) |
| 100 | + ) |
| 101 | + return () |
0 commit comments