|
| 1 | +// Copyright 2025 Xanadu Quantum Technologies Inc. |
| 2 | + |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <optional> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include "llvm/ADT/SmallVector.h" |
| 21 | +#include "llvm/ADT/StringMap.h" |
| 22 | +#include "llvm/ADT/StringRef.h" |
| 23 | + |
| 24 | +#include "Ion/IR/IonOps.h" |
| 25 | + |
| 26 | +namespace catalyst { |
| 27 | +namespace ion { |
| 28 | + |
| 29 | +/// Helper class to store and query ion information from an IonOp. |
| 30 | +class IonInfo { |
| 31 | + public: |
| 32 | + struct TransitionInfo { |
| 33 | + std::string level0; |
| 34 | + std::string level1; |
| 35 | + double einstein_a; |
| 36 | + std::string multipole; |
| 37 | + }; |
| 38 | + |
| 39 | + private: |
| 40 | + llvm::StringMap<double> levelEnergyMap; |
| 41 | + llvm::SmallVector<TransitionInfo> transitions; |
| 42 | + |
| 43 | + public: |
| 44 | + explicit IonInfo(ion::IonOp op) |
| 45 | + { |
| 46 | + auto levelAttrs = op.getLevels(); |
| 47 | + auto transitionsAttr = op.getTransitions(); |
| 48 | + |
| 49 | + // Map from Level label to Energy value |
| 50 | + for (auto levelAttr : levelAttrs) { |
| 51 | + auto level = mlir::cast<LevelAttr>(levelAttr); |
| 52 | + std::string label = level.getLabel().getValue().str(); |
| 53 | + double energy = level.getEnergy().getValueAsDouble(); |
| 54 | + levelEnergyMap[label] = energy; |
| 55 | + } |
| 56 | + |
| 57 | + // Store transition information |
| 58 | + for (auto transitionAttr : transitionsAttr) { |
| 59 | + auto transition = mlir::cast<TransitionAttr>(transitionAttr); |
| 60 | + TransitionInfo info; |
| 61 | + info.level0 = transition.getLevel_0().getValue().str(); |
| 62 | + info.level1 = transition.getLevel_1().getValue().str(); |
| 63 | + info.einstein_a = transition.getEinsteinA().getValueAsDouble(); |
| 64 | + info.multipole = transition.getMultipole().getValue().str(); |
| 65 | + transitions.push_back(info); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// Get energy of a level by label |
| 70 | + std::optional<double> getLevelEnergy(llvm::StringRef label) const |
| 71 | + { |
| 72 | + auto it = levelEnergyMap.find(label.str()); |
| 73 | + if (it != levelEnergyMap.end()) { |
| 74 | + return it->second; |
| 75 | + } |
| 76 | + return std::nullopt; |
| 77 | + } |
| 78 | + |
| 79 | + /// Get level energy of a transition by index |
| 80 | + template <int IndexT> |
| 81 | + std::optional<double> getTransitionLevelEnergy(size_t transitionIndex) const |
| 82 | + { |
| 83 | + static_assert(IndexT == 0 || IndexT == 1, "IndexT must be 0 or 1"); |
| 84 | + |
| 85 | + if (transitionIndex >= transitions.size()) { |
| 86 | + return std::nullopt; |
| 87 | + } |
| 88 | + |
| 89 | + const auto &transition = transitions[transitionIndex]; |
| 90 | + if constexpr (IndexT == 0) { |
| 91 | + return getLevelEnergy(transition.level0); |
| 92 | + } |
| 93 | + else { |
| 94 | + return getLevelEnergy(transition.level1); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /// Get energy difference of a transition (level1 energy - level0 energy) |
| 99 | + std::optional<double> getTransitionEnergyDiff(size_t index) const |
| 100 | + { |
| 101 | + if (index >= transitions.size()) { |
| 102 | + return std::nullopt; |
| 103 | + } |
| 104 | + |
| 105 | + auto energy0 = getTransitionLevelEnergy<0>(index); |
| 106 | + auto energy1 = getTransitionLevelEnergy<1>(index); |
| 107 | + |
| 108 | + if (energy0.has_value() && energy1.has_value()) { |
| 109 | + return energy1.value() - energy0.value(); |
| 110 | + } |
| 111 | + |
| 112 | + return std::nullopt; |
| 113 | + } |
| 114 | + |
| 115 | + /// Get number of transitions |
| 116 | + size_t getNumTransitions() const { return transitions.size(); } |
| 117 | + |
| 118 | + /// Get transition info by index |
| 119 | + std::optional<TransitionInfo> getTransition(size_t index) const |
| 120 | + { |
| 121 | + if (index < transitions.size()) { |
| 122 | + return transitions[index]; |
| 123 | + } |
| 124 | + return std::nullopt; |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +} // namespace ion |
| 129 | +} // namespace catalyst |
0 commit comments