|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 - 2026 Chair for Design Automation, TUM |
| 3 | + * Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: MIT |
| 7 | + * |
| 8 | + * Licensed under the MIT License |
| 9 | + */ |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include "EulerBasis.h" |
| 14 | +#include "GateSequence.h" |
| 15 | + |
| 16 | +#include <Eigen/Core> |
| 17 | + |
| 18 | +#include <array> |
| 19 | +#include <optional> |
| 20 | + |
| 21 | +namespace mlir::qco::decomposition { |
| 22 | + |
| 23 | +/** |
| 24 | + * Decompose a single-qubit unitary into a selected Euler-style gate basis. |
| 25 | + * |
| 26 | + * The returned sequence tracks both the emitted gates and the scalar phase |
| 27 | + * needed to reconstruct the input matrix exactly. This is stronger than the |
| 28 | + * usual "up to global phase" contract and is relied on by downstream |
| 29 | + * canonicalization and testing code. |
| 30 | + */ |
| 31 | +class EulerDecomposition { |
| 32 | +public: |
| 33 | + /** |
| 34 | + * Decompose a 2x2 unitary into the gate alphabet described by |
| 35 | + * `targetBasis`. |
| 36 | + * |
| 37 | + * When `simplify` is true, near-zero angles are removed using `atol` (or |
| 38 | + * `DEFAULT_ATOL` if no override is provided). The returned global phase keeps |
| 39 | + * the decomposition exactly equal to `unitaryMatrix`. |
| 40 | + */ |
| 41 | + [[nodiscard]] static OneQubitGateSequence |
| 42 | + generateCircuit(EulerBasis targetBasis, const Eigen::Matrix2cd& unitaryMatrix, |
| 43 | + bool simplify, std::optional<double> atol); |
| 44 | + |
| 45 | + /** |
| 46 | + * Extract canonical Euler parameters for `matrix` in the requested basis. |
| 47 | + * |
| 48 | + * Some target bases reuse the same parameter extraction routine and differ |
| 49 | + * only during circuit emission. The returned array always contains |
| 50 | + * `(theta, phi, lambda, phase)` in this order. |
| 51 | + */ |
| 52 | + [[nodiscard]] static std::array<double, 4> |
| 53 | + anglesFromUnitary(const Eigen::Matrix2cd& matrix, EulerBasis basis); |
| 54 | + |
| 55 | +private: |
| 56 | + /// Extract parameters for a `RZ(phi) RY(theta) RZ(lambda)` factorization. |
| 57 | + [[nodiscard]] static std::array<double, 4> |
| 58 | + paramsZyz(const Eigen::Matrix2cd& matrix); |
| 59 | + |
| 60 | + /// Extract parameters for a `RZ(phi) RX(theta) RZ(lambda)` factorization. |
| 61 | + [[nodiscard]] static std::array<double, 4> |
| 62 | + paramsZxz(const Eigen::Matrix2cd& matrix); |
| 63 | + |
| 64 | + /// Extract parameters for a `RX(phi) RY(theta) RX(lambda)` factorization. |
| 65 | + [[nodiscard]] static std::array<double, 4> |
| 66 | + paramsXyx(const Eigen::Matrix2cd& matrix); |
| 67 | + |
| 68 | + /// Extract parameters for a `RX(phi) RZ(theta) RX(lambda)` factorization. |
| 69 | + [[nodiscard]] static std::array<double, 4> |
| 70 | + paramsXzx(const Eigen::Matrix2cd& matrix); |
| 71 | + |
| 72 | + /** |
| 73 | + * Extract parameters for a `u1`/`p` + `sx` factorization. |
| 74 | + * |
| 75 | + * The returned angles are identical to `paramsZyz` but the phase is shifted |
| 76 | + * by `-0.5 * (theta + phi + lambda)` so that the `rz`/`sx` circuits emitted |
| 77 | + * by `decomposePsxGen` match the input matrix exactly (not only up to a |
| 78 | + * global phase). |
| 79 | + * |
| 80 | + * @note Adapted from `params_u1x_inner` in the IBM Qiskit framework. |
| 81 | + * (C) Copyright IBM 2022 |
| 82 | + * |
| 83 | + * This code is licensed under the Apache License, Version 2.0. You may |
| 84 | + * obtain a copy of this license in the LICENSE.txt file in the root |
| 85 | + * directory of this source tree or at |
| 86 | + * https://www.apache.org/licenses/LICENSE-2.0. |
| 87 | + * |
| 88 | + * Any modifications or derivative works of this code must retain this |
| 89 | + * copyright notice, and modified files need to carry a notice |
| 90 | + * indicating that they have been altered from the originals. |
| 91 | + */ |
| 92 | + [[nodiscard]] static std::array<double, 4> |
| 93 | + paramsU1x(const Eigen::Matrix2cd& matrix); |
| 94 | + |
| 95 | + /** |
| 96 | + * Emit a K-A-K circuit from already extracted Euler parameters. |
| 97 | + * |
| 98 | + * `kGate` is used for the outer rotations and `aGate` for the middle |
| 99 | + * rotation. |
| 100 | + * |
| 101 | + * @note Adapted from circuit_kak() in the IBM Qiskit framework. |
| 102 | + * (C) Copyright IBM 2022 |
| 103 | + * |
| 104 | + * This code is licensed under the Apache License, Version 2.0. You may |
| 105 | + * obtain a copy of this license in the LICENSE.txt file in the root |
| 106 | + * directory of this source tree or at |
| 107 | + * https://www.apache.org/licenses/LICENSE-2.0. |
| 108 | + * |
| 109 | + * Any modifications or derivative works of this code must retain this |
| 110 | + * copyright notice, and modified files need to carry a notice |
| 111 | + * indicating that they have been altered from the originals. |
| 112 | + */ |
| 113 | + [[nodiscard]] static OneQubitGateSequence |
| 114 | + decomposeKAK(double theta, double phi, double lambda, double phase, |
| 115 | + GateKind kGate, GateKind aGate, bool simplify, |
| 116 | + std::optional<double> atol); |
| 117 | + |
| 118 | + /** |
| 119 | + * Emit an `rz`/`sx`-style circuit for the `ZSX` and `ZSXX` bases. |
| 120 | + * |
| 121 | + * The emitted sequence is structurally identical to the one produced by |
| 122 | + * Qiskit's `circuit_psx_gen`. When `simplify` is enabled the number of `sx` |
| 123 | + * gates shrinks based on `theta`: zero `sx` gates for `theta ~= 0`, one |
| 124 | + * `sx` gate for `theta ~= pi/2`, and two `sx` gates otherwise. |
| 125 | + * |
| 126 | + * When `allowXShortcut` is true (i.e. for `ZSXX`), the general-case 2-`sx` |
| 127 | + * path additionally collapses `sx . rz(+/- pi) . sx` into a single `x` |
| 128 | + * gate when the middle rotation is congruent to +/- pi modulo 2 pi. |
| 129 | + * |
| 130 | + * @note Adapted from `circuit_psx_gen` in the IBM Qiskit framework. |
| 131 | + * (C) Copyright IBM 2022 |
| 132 | + * |
| 133 | + * This code is licensed under the Apache License, Version 2.0. You |
| 134 | + * may obtain a copy of this license in the LICENSE.txt file in the |
| 135 | + * root directory of this source tree or at |
| 136 | + * https://www.apache.org/licenses/LICENSE-2.0. |
| 137 | + * |
| 138 | + * Any modifications or derivative works of this code must retain |
| 139 | + * this copyright notice, and modified files need to carry a notice |
| 140 | + * indicating that they have been altered from the originals. |
| 141 | + */ |
| 142 | + [[nodiscard]] static OneQubitGateSequence |
| 143 | + decomposePsxGen(double theta, double phi, double lambda, double phase, |
| 144 | + bool allowXShortcut, bool simplify, |
| 145 | + std::optional<double> atol); |
| 146 | +}; |
| 147 | +} // namespace mlir::qco::decomposition |
0 commit comments