|
| 1 | +from kirin import ir, interp as _interp |
| 2 | +from kirin.analysis import ForwardFrame |
| 3 | +from kirin.dialects import func |
| 4 | + |
| 5 | +from bloqade import qubit, gemini |
| 6 | +from bloqade.analysis.address.impls import Func as AddressFuncMethodTable |
| 7 | +from bloqade.analysis.measure_id.lattice import MeasureIdTuple |
| 8 | + |
| 9 | +from .analysis import _GeminiTerminalMeasurementValidationAnalysis |
| 10 | + |
| 11 | + |
| 12 | +@qubit.dialect.register(key="gemini.validate.terminal_measurement") |
| 13 | +class __QubitGeminiMeasurementValidation(_interp.MethodTable): |
| 14 | + |
| 15 | + # This is a non-logical measurement, can safely flag as invalid |
| 16 | + @_interp.impl(qubit.stmts.Measure) |
| 17 | + def measure( |
| 18 | + self, |
| 19 | + interp: _GeminiTerminalMeasurementValidationAnalysis, |
| 20 | + frame: ForwardFrame, |
| 21 | + stmt: qubit.stmts.Measure, |
| 22 | + ): |
| 23 | + |
| 24 | + interp.add_validation_error( |
| 25 | + stmt, |
| 26 | + ir.ValidationError( |
| 27 | + stmt, |
| 28 | + "Non-terminal measurements are not allowed in Gemini programs!", |
| 29 | + ), |
| 30 | + ) |
| 31 | + |
| 32 | + return (interp.lattice.bottom(),) |
| 33 | + |
| 34 | + |
| 35 | +@gemini.logical.dialect.register(key="gemini.validate.terminal_measurement") |
| 36 | +class __GeminiLogicalMeasurementValidation(_interp.MethodTable): |
| 37 | + |
| 38 | + @_interp.impl(gemini.logical.stmts.TerminalLogicalMeasurement) |
| 39 | + def terminal_measure( |
| 40 | + self, |
| 41 | + interp: _GeminiTerminalMeasurementValidationAnalysis, |
| 42 | + frame: ForwardFrame, |
| 43 | + stmt: gemini.logical.stmts.TerminalLogicalMeasurement, |
| 44 | + ): |
| 45 | + |
| 46 | + # should only be one terminal measurement EVER |
| 47 | + if not interp.terminal_measurement_encountered: |
| 48 | + interp.terminal_measurement_encountered = True |
| 49 | + else: |
| 50 | + interp.add_validation_error( |
| 51 | + stmt, |
| 52 | + ir.ValidationError( |
| 53 | + stmt, |
| 54 | + "Multiple terminal measurements are not allowed in Gemini logical programs!", |
| 55 | + ), |
| 56 | + ) |
| 57 | + return (interp.lattice.bottom(),) |
| 58 | + |
| 59 | + measurement_analysis_results = interp.measurement_analysis_results |
| 60 | + total_qubits_allocated = interp.unique_qubits_allocated |
| 61 | + |
| 62 | + measure_lattice_element = measurement_analysis_results.get(stmt.result) |
| 63 | + if not isinstance(measure_lattice_element, MeasureIdTuple): |
| 64 | + interp.add_validation_error( |
| 65 | + stmt, |
| 66 | + ir.ValidationError( |
| 67 | + stmt, |
| 68 | + "Measurement ID Analysis failed to produce the necessary results needed for validation.", |
| 69 | + ), |
| 70 | + ) |
| 71 | + return (interp.lattice.bottom(),) |
| 72 | + |
| 73 | + if len(measure_lattice_element.data) != total_qubits_allocated: |
| 74 | + interp.add_validation_error( |
| 75 | + stmt, |
| 76 | + ir.ValidationError( |
| 77 | + stmt, |
| 78 | + "The number of qubits in the terminal measurement does not match the number of total qubits allocated! " |
| 79 | + + f"{total_qubits_allocated} qubits were allocated but only {len(measure_lattice_element.data)} were measured.", |
| 80 | + ), |
| 81 | + ) |
| 82 | + return (interp.lattice.bottom(),) |
| 83 | + |
| 84 | + return (interp.lattice.bottom(),) |
| 85 | + |
| 86 | + |
| 87 | +@func.dialect.register(key="gemini.validate.terminal_measurement") |
| 88 | +class Func(AddressFuncMethodTable): |
| 89 | + pass |
0 commit comments