microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/pip/qsharp/_device/_atom/_validate.py
45lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | from pyqir import QirModuleVisitor, is_entry_point, Opcode |
| 5 | |
| 6 | |
| 7 | class ValidateAllowedIntrinsics(QirModuleVisitor): |
| 8 | """ |
| 9 | Ensure that the module only contains allowed intrinsics. |
| 10 | """ |
| 11 | |
| 12 | def _on_function(self, function): |
| 13 | name = function.name |
| 14 | if ( |
| 15 | not is_entry_point(function) |
| 16 | and not name.endswith("_record_output") |
| 17 | and name |
| 18 | not in [ |
| 19 | "__quantum__rt__begin_parallel", |
| 20 | "__quantum__rt__end_parallel", |
| 21 | "__quantum__qis__read_result__body", |
| 22 | "__quantum__rt__read_result", |
| 23 | "__quantum__qis__move__body", |
| 24 | "__quantum__qis__cz__body", |
| 25 | "__quantum__qis__sx__body", |
| 26 | "__quantum__qis__rz__body", |
| 27 | "__quantum__qis__mresetz__body", |
| 28 | ] |
| 29 | ): |
| 30 | raise ValueError(f"{name} is not a supported intrinsic") |
| 31 | |
| 32 | |
| 33 | class ValidateNoConditionalBranches(QirModuleVisitor): |
| 34 | """ |
| 35 | Ensure that the function(s) only use unconditional branches. |
| 36 | """ |
| 37 | |
| 38 | def _on_block(self, block): |
| 39 | if ( |
| 40 | block.terminator |
| 41 | and block.terminator.opcode == Opcode.BR |
| 42 | and len(block.terminator.operands) > 1 |
| 43 | ): |
| 44 | raise ValueError("programs with branching control flow are not supported") |
| 45 | super()._on_block(block) |
| 46 | |