From 07d39c205fe894a2c36036ea55ad0fe1aacf2b1f Mon Sep 17 00:00:00 2001 From: ziofil Date: Tue, 21 Nov 2023 19:12:57 +0000 Subject: [PATCH 1/8] added comment --- mrmustard/math/backend_tensorflow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mrmustard/math/backend_tensorflow.py b/mrmustard/math/backend_tensorflow.py index 8930bf46a..fc75ef3f4 100644 --- a/mrmustard/math/backend_tensorflow.py +++ b/mrmustard/math/backend_tensorflow.py @@ -467,7 +467,7 @@ def reorder_AB_bargmann(self, A: tf.Tensor, B: tf.Tensor) -> Tuple[tf.Tensor, tf r"""In mrmustard.math.compactFock.compactFock~ dimensions of the Fock representation are ordered like [mode0,mode0,mode1,mode1,...] while in mrmustard.physics.bargmann the ordering is [mode0,mode1,...,mode0,mode1,...]. Here we reorder A and B. """ - ordering = np.arange(2 * A.shape[0] // 2).reshape(2, -1).T.flatten() + ordering = np.arange(2 * A.shape[0] // 2).reshape(2, -1).T.flatten() # ordering is [0,2,4,...,1,3,5,...] A = tf.gather(A, ordering, axis=1) A = tf.gather(A, ordering) B = tf.gather(B, ordering) From 6b33cd6a59236783e61df2d23e29194c722d42ff Mon Sep 17 00:00:00 2001 From: ziofil Date: Tue, 21 Nov 2023 19:13:08 +0000 Subject: [PATCH 2/8] use real rather than cast --- mrmustard/physics/fock.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mrmustard/physics/fock.py b/mrmustard/physics/fock.py index cef6834cd..9724dd73a 100644 --- a/mrmustard/physics/fock.py +++ b/mrmustard/physics/fock.py @@ -713,7 +713,7 @@ def f_hermite_polys(xi): poly = math.hermite_renormalized( R, 2 * math.astensor([xi], "complex128"), 1 + 0j, (cutoff,) ) - return math.cast(poly, "float64") + return math.real(poly) hermite_polys = math.map_fn(f_hermite_polys, x_tensor) @@ -844,7 +844,7 @@ def quadrature_distribution( else math.abs(math.einsum("n,nj->j", state, psi_x)) ** 2 ) - return x, math.cast(pdf, "float64") + return x, math.real(pdf) def sample_homodyne(state: Tensor, quadrature_angle: float = 0.0) -> Tuple[float, float]: From be2027079e25539967739b288ab9563e465cd725 Mon Sep 17 00:00:00 2001 From: SamFerracin Date: Wed, 22 Nov 2023 10:26:48 -0500 Subject: [PATCH 3/8] some prog --- mrmustard/math/backend_numpy.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mrmustard/math/backend_numpy.py b/mrmustard/math/backend_numpy.py index cabf9e245..2ce393529 100644 --- a/mrmustard/math/backend_numpy.py +++ b/mrmustard/math/backend_numpy.py @@ -43,10 +43,11 @@ class BackendNumpy(BackendBase): # pragma: no cover """ int32 = np.int32 - float64 = np.float64 float32 = np.float32 + float64 = np.float64 complex64 = np.complex64 complex128 = np.complex128 + allowed_types = [int32, float32, float64, complex64, complex128] def __init__(self): super().__init__(name="numpy") @@ -89,7 +90,10 @@ def boolean_mask(self, tensor: np.array, mask: np.array) -> np.ndarray: def cast(self, array: np.array, dtype=None) -> np.array: if dtype is None: return array - + # return np.array(array, dtype=dtype) + + if dtype not in [self.complex64, self.complex128, "complex64", "complex128"]: + array = self.real(array) return np.array(array, dtype=dtype) def clip(self, array, a_min, a_max) -> np.array: From 41a7e9798b4e663a1c9cc07a2d497a9f1a8cb081 Mon Sep 17 00:00:00 2001 From: SamFerracin Date: Wed, 22 Nov 2023 10:53:50 -0500 Subject: [PATCH 4/8] done --- .github/CHANGELOG.md | 2 ++ mrmustard/math/backend_numpy.py | 1 - mrmustard/math/backend_tensorflow.py | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 851f9025a..10773ae50 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -53,6 +53,8 @@ which uses the old Numba code. When setting to a higher value, the new Julia cod [(#303)](https://github.com/XanaduAI/MrMustard/pull/303) [(#304)](https://github.com/XanaduAI/MrMustard/pull/304) +* Changed the ``cast`` functions in the numpy and tensorflow backends to avoid ``ComplexWarning``s. + ### Bug fixes * Added the missing `shape` input parameters to all methods `U` in the `gates.py` file. diff --git a/mrmustard/math/backend_numpy.py b/mrmustard/math/backend_numpy.py index 2ce393529..b063dd7a2 100644 --- a/mrmustard/math/backend_numpy.py +++ b/mrmustard/math/backend_numpy.py @@ -90,7 +90,6 @@ def boolean_mask(self, tensor: np.array, mask: np.array) -> np.ndarray: def cast(self, array: np.array, dtype=None) -> np.array: if dtype is None: return array - # return np.array(array, dtype=dtype) if dtype not in [self.complex64, self.complex128, "complex64", "complex128"]: array = self.real(array) diff --git a/mrmustard/math/backend_tensorflow.py b/mrmustard/math/backend_tensorflow.py index 8930bf46a..cc2c78dbd 100644 --- a/mrmustard/math/backend_tensorflow.py +++ b/mrmustard/math/backend_tensorflow.py @@ -87,6 +87,9 @@ def boolean_mask(self, tensor: tf.Tensor, mask: tf.Tensor) -> Tensor: def cast(self, array: tf.Tensor, dtype=None) -> tf.Tensor: if dtype is None: return array + + if dtype not in [self.complex64, self.complex128, "complex64", "complex128"]: + array = self.real(array) return tf.cast(array, dtype) def clip(self, array, a_min, a_max) -> tf.Tensor: From be08051c3da1a65132322a924d1b5c6393a2ec4d Mon Sep 17 00:00:00 2001 From: SamFerracin Date: Wed, 22 Nov 2023 10:56:02 -0500 Subject: [PATCH 5/8] revert --- mrmustard/physics/fock.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mrmustard/physics/fock.py b/mrmustard/physics/fock.py index 9724dd73a..cef6834cd 100644 --- a/mrmustard/physics/fock.py +++ b/mrmustard/physics/fock.py @@ -713,7 +713,7 @@ def f_hermite_polys(xi): poly = math.hermite_renormalized( R, 2 * math.astensor([xi], "complex128"), 1 + 0j, (cutoff,) ) - return math.real(poly) + return math.cast(poly, "float64") hermite_polys = math.map_fn(f_hermite_polys, x_tensor) @@ -844,7 +844,7 @@ def quadrature_distribution( else math.abs(math.einsum("n,nj->j", state, psi_x)) ** 2 ) - return x, math.real(pdf) + return x, math.cast(pdf, "float64") def sample_homodyne(state: Tensor, quadrature_angle: float = 0.0) -> Tuple[float, float]: From ec3fa2afc2b48dd5039832519ba9b9c2ee6763f8 Mon Sep 17 00:00:00 2001 From: SamFerracin Date: Wed, 22 Nov 2023 10:57:29 -0500 Subject: [PATCH 6/8] allowed types --- mrmustard/math/backend_numpy.py | 1 - mrmustard/math/backend_tensorflow.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/mrmustard/math/backend_numpy.py b/mrmustard/math/backend_numpy.py index b063dd7a2..2ae003a39 100644 --- a/mrmustard/math/backend_numpy.py +++ b/mrmustard/math/backend_numpy.py @@ -47,7 +47,6 @@ class BackendNumpy(BackendBase): # pragma: no cover float64 = np.float64 complex64 = np.complex64 complex128 = np.complex128 - allowed_types = [int32, float32, float64, complex64, complex128] def __init__(self): super().__init__(name="numpy") diff --git a/mrmustard/math/backend_tensorflow.py b/mrmustard/math/backend_tensorflow.py index 32db681bc..4f86487c2 100644 --- a/mrmustard/math/backend_tensorflow.py +++ b/mrmustard/math/backend_tensorflow.py @@ -42,8 +42,8 @@ class BackendTensorflow(BackendBase): # pragma: no cover """ int32 = tf.int32 - float64 = tf.float64 float32 = tf.float32 + float64 = tf.float64 complex64 = tf.complex64 complex128 = tf.complex128 From a4b9597d91bde8dcc419b2861d70751cdcfa36e7 Mon Sep 17 00:00:00 2001 From: SamFerracin Date: Wed, 22 Nov 2023 11:02:40 -0500 Subject: [PATCH 7/8] fromat --- mrmustard/math/backend_numpy.py | 2 +- mrmustard/math/backend_tensorflow.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/mrmustard/math/backend_numpy.py b/mrmustard/math/backend_numpy.py index 2ae003a39..36452835d 100644 --- a/mrmustard/math/backend_numpy.py +++ b/mrmustard/math/backend_numpy.py @@ -89,7 +89,7 @@ def boolean_mask(self, tensor: np.array, mask: np.array) -> np.ndarray: def cast(self, array: np.array, dtype=None) -> np.array: if dtype is None: return array - + if dtype not in [self.complex64, self.complex128, "complex64", "complex128"]: array = self.real(array) return np.array(array, dtype=dtype) diff --git a/mrmustard/math/backend_tensorflow.py b/mrmustard/math/backend_tensorflow.py index 4f86487c2..8534717c6 100644 --- a/mrmustard/math/backend_tensorflow.py +++ b/mrmustard/math/backend_tensorflow.py @@ -87,7 +87,7 @@ def boolean_mask(self, tensor: tf.Tensor, mask: tf.Tensor) -> Tensor: def cast(self, array: tf.Tensor, dtype=None) -> tf.Tensor: if dtype is None: return array - + if dtype not in [self.complex64, self.complex128, "complex64", "complex128"]: array = self.real(array) return tf.cast(array, dtype) @@ -470,7 +470,9 @@ def reorder_AB_bargmann(self, A: tf.Tensor, B: tf.Tensor) -> Tuple[tf.Tensor, tf r"""In mrmustard.math.compactFock.compactFock~ dimensions of the Fock representation are ordered like [mode0,mode0,mode1,mode1,...] while in mrmustard.physics.bargmann the ordering is [mode0,mode1,...,mode0,mode1,...]. Here we reorder A and B. """ - ordering = np.arange(2 * A.shape[0] // 2).reshape(2, -1).T.flatten() # ordering is [0,2,4,...,1,3,5,...] + ordering = ( + np.arange(2 * A.shape[0] // 2).reshape(2, -1).T.flatten() + ) # ordering is [0,2,4,...,1,3,5,...] A = tf.gather(A, ordering, axis=1) A = tf.gather(A, ordering) B = tf.gather(B, ordering) From 7e9c9687803ccc1603443686748b09383b526edb Mon Sep 17 00:00:00 2001 From: SamFerracin Date: Mon, 27 Nov 2023 16:46:50 -0500 Subject: [PATCH 8/8] empty