microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/pip/qsharp/_device/_atom/_utils.py
78lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | from pyqir import ( |
| 5 | Instruction, |
| 6 | Call, |
| 7 | Constant, |
| 8 | Value, |
| 9 | qubit_id, |
| 10 | is_qubit_type, |
| 11 | result_id, |
| 12 | is_result_type, |
| 13 | ) |
| 14 | from typing import Dict |
| 15 | |
| 16 | TOLERANCE: float = 1.1920929e-7 # Machine epsilon for 32-bit IEEE FP numbers. |
| 17 | |
| 18 | |
| 19 | # If this is a call to a __qis__ gate, return a dict describing the gate and its arguments. |
| 20 | def as_qis_gate(instr: Instruction) -> Dict: |
| 21 | if isinstance(instr, Call) and instr.callee.name.startswith("__quantum__qis__"): |
| 22 | parts = instr.callee.name.split("__") |
| 23 | return { |
| 24 | "gate": parts[3] + ("_adj" if parts[4] == "adj" else ""), |
| 25 | "qubit_args": [ |
| 26 | qubit_id(arg) for arg in instr.args if qubit_id(arg) is not None |
| 27 | ], |
| 28 | "result_args": [ |
| 29 | result_id(arg) for arg in instr.args if result_id(arg) is not None |
| 30 | ], |
| 31 | "other_args": [ |
| 32 | arg |
| 33 | for arg in instr.args |
| 34 | if qubit_id(arg) is None and result_id(arg) is None |
| 35 | ], |
| 36 | } |
| 37 | return {} |
| 38 | |
| 39 | |
| 40 | # Returns all values and, separately, all measurement results used by the instruction. |
| 41 | def get_used_values(instr: Instruction) -> tuple[list[Value], list[Value]]: |
| 42 | vals = [] |
| 43 | meas_results = [] |
| 44 | if isinstance(instr, Call): |
| 45 | vals = instr.args |
| 46 | if ( |
| 47 | instr.callee.name == "__quantum__qis__mresetz__body" |
| 48 | or instr.callee.name == "__quantum__qis__m__body" |
| 49 | or instr.callee.name == "__quantum__qis__mz__body" |
| 50 | ): |
| 51 | # Measurement uses a result as the second argument |
| 52 | meas_results += vals[1:] |
| 53 | vals = vals[:1] |
| 54 | elif ( |
| 55 | instr.callee.name == "__quantum__qis__read_result__body" |
| 56 | or instr.callee.name == "__quantum__rt__read_result" |
| 57 | or instr.callee.name == "__quantum__rt__read_atom_result" |
| 58 | ): |
| 59 | # Read result uses a result as the first argument |
| 60 | meas_results += vals |
| 61 | vals = [] |
| 62 | else: |
| 63 | vals = instr.operands |
| 64 | vals.append(instr) |
| 65 | return (vals, meas_results) |
| 66 | |
| 67 | |
| 68 | # Returns true if any of the used values are in the existing values. |
| 69 | # Useful for determining if an instruction depends on any instructions in a set. |
| 70 | def uses_any_value(used_values, existing_values) -> bool: |
| 71 | return any( |
| 72 | [ |
| 73 | val in existing_values |
| 74 | for val in used_values |
| 75 | if not isinstance(val, Constant) |
| 76 | or (is_qubit_type(val.type) or is_result_type(val.type)) |
| 77 | ] |
| 78 | ) |
| 79 | |