|
| 1 | +# Copyright 2025 Google LLC |
| 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 | +# https://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 | +import numpy as np |
| 16 | + |
| 17 | +import qualtran.rotation_synthesis._typing as rst |
| 18 | +import qualtran.rotation_synthesis.math_config as mc |
| 19 | + |
| 20 | + |
| 21 | +def su_unitary_to_zxz_angles( |
| 22 | + u: np.ndarray, config: mc.MathConfig |
| 23 | +) -> tuple[rst.Real, rst.Real, rst.Real]: |
| 24 | + det = u[0, 0] * u[1, 1] - u[0, 1] * u[1, 0] |
| 25 | + # print(det) |
| 26 | + # assert config.isclose(det, 1) |
| 27 | + |
| 28 | + cos_phi = (u[1, 1] * u[0, 0] + u[1, 0] * u[0, 1]).real |
| 29 | + # clip to the range [-1, 1] to ensure numerical stability. |
| 30 | + cos_phi = min(1, max(-1, cos_phi)) |
| 31 | + phi = config.arccos(cos_phi) |
| 32 | + |
| 33 | + sum_half_theta = config.arctan2(u[1, 1].imag, u[1, 1].real) |
| 34 | + diff_half_theta = config.arctan2(u[1, 0].imag, u[1, 0].real) - 1.5 * config.pi |
| 35 | + |
| 36 | + theta1 = sum_half_theta + diff_half_theta |
| 37 | + theta2 = sum_half_theta - diff_half_theta |
| 38 | + |
| 39 | + return theta1, phi, theta2 |
| 40 | + |
| 41 | + |
| 42 | +def rx(phi: rst.Real, config: mc.MathConfig) -> np.ndarray: |
| 43 | + c = config.cos(phi / 2) |
| 44 | + s = config.sin(phi / 2) |
| 45 | + return np.array([[c, -1j * s], [-1j * s, c]]) |
| 46 | + |
| 47 | + |
| 48 | +def rz(theta: rst.Real, config: mc.MathConfig) -> np.ndarray: |
| 49 | + v = config.cos(theta / 2) + 1j * config.sin(theta / 2) |
| 50 | + return np.diag([v.conjugate(), v]) |
| 51 | + |
| 52 | + |
| 53 | +def unitary_from_zxz( |
| 54 | + theta1: rst.Real, phi: rst.Real, theta2: rst.Real, config: mc.MathConfig |
| 55 | +) -> np.ndarray: |
| 56 | + return rz(theta1, config) @ rx(phi, config) @ rz(theta2, config) |
0 commit comments