From b11dd94844242859cce796e86312da5f1d6ec1b2 Mon Sep 17 00:00:00 2001 From: PaulaGarciaMolina Date: Wed, 3 Apr 2024 10:00:40 +0200 Subject: [PATCH 1/4] Add return to CANONICAL_FORM and DO_NOT_SIMPLIFY in simplify_mps_sum --- src/seemps/truncate/simplify.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/seemps/truncate/simplify.py b/src/seemps/truncate/simplify.py index 92af321e..76fa8d67 100644 --- a/src/seemps/truncate/simplify.py +++ b/src/seemps/truncate/simplify.py @@ -1,6 +1,9 @@ from __future__ import annotations + from typing import Optional, Union + import numpy as np + from .. import tools from ..state import ( DEFAULT_TOLERANCE, @@ -196,6 +199,7 @@ def simplify_mps_sum( f"produces error {mps.error():5e}", debug_level=2, ) + return mps # TODO: DO_NOT_SIMPLIFY should do nothing. However, since the # output is expected to be a CanonicalMPS, we must use the @@ -209,6 +213,7 @@ def simplify_mps_sum( f"produces error {mps.error():5e}", debug_level=2, ) + return mps # Prepare initial guess if guess is None: From bcf8ac96126aa190974298aebbd260e4eba8f1fb Mon Sep 17 00:00:00 2001 From: PaulaGarciaMolina Date: Thu, 25 Apr 2024 08:57:57 +0200 Subject: [PATCH 2/4] Correct normalization in Fourier interpolation --- src/seemps/analysis/interpolation.py | 51 ++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/seemps/analysis/interpolation.py b/src/seemps/analysis/interpolation.py index c1a74fc4..3acb3560 100644 --- a/src/seemps/analysis/interpolation.py +++ b/src/seemps/analysis/interpolation.py @@ -1,10 +1,13 @@ from __future__ import annotations + import copy -import numpy as np from math import sqrt -from ..qft import qft_mpo -from ..state import MPS, MPSSum, Strategy, DEFAULT_STRATEGY + +import numpy as np + from ..operators import MPO +from ..qft import qft_mpo +from ..state import DEFAULT_STRATEGY, MPS, CanonicalMPS, MPSSum, Strategy from ..truncate import simplify from .finite_differences import mpo_combined from .space import Space, mpo_flip @@ -24,7 +27,14 @@ def twoscomplement(L, **kwdargs): return MPO([A0] + [A] * (L - 2) + [Aend], **kwdargs) -def fourier_interpolation_1D(ψ0mps, space, M0, Mf, dim, strategy=DEFAULT_STRATEGY): +def fourier_interpolation_1D( + ψ0mps: MPS, + space: Space, + M0: int, + Mf: int, + dim: int, + strategy: Strategy = DEFAULT_STRATEGY, +): """Obtain the Fourier interpolated MPS over the chosen dimension with a new number of sites Mf. @@ -73,12 +83,19 @@ def fourier_interpolation_1D(ψ0mps, space, M0, Mf, dim, strategy=DEFAULT_STRATE ) U2c = new_space.extend(mpo_flip(twoscomplement(Mf, strategy=strategy)), dim) ψfmps = iQFT_op @ (U2c @ Fψfmps) - ψfmps = ψfmps * (1 / sqrt(ψfmps.norm_squared())) - + ψfmps = (Mf / M0) * ψfmps + if strategy.get_normalize_flag(): + ψfmps = ψfmps * (1 / ψfmps.norm()) return ψfmps, new_space -def fourier_interpolation(ψmps, space, old_sites, new_sites, **kwargs): +def fourier_interpolation( + ψmps: MPS, + space: Space, + old_sites: List, + new_sites: List, + strategy: Strategy = DEFAULT_STRATEGY, +): """Fourier interpolation on an MPS. Parameters @@ -91,8 +108,8 @@ def fourier_interpolation(ψmps, space, old_sites, new_sites, **kwargs): List of integers with the original number of sites for each dimension. new_sites : list[int] List of integers with the new number of sites for each dimension. - **kwargs : - Arguments accepted by :class:`MPO` + strategy : Strategy, optional + Truncation strategy, defaults to DEFAULT_STRATEGY. Returns ------- @@ -101,9 +118,11 @@ def fourier_interpolation(ψmps, space, old_sites, new_sites, **kwargs): """ space = copy.copy(space) + if not isinstance(ψmps, CanonicalMPS): + ψmps = CanonicalMPS(ψmps, strategy=strategy) for i, sites in enumerate(new_sites): ψmps, space = fourier_interpolation_1D( - ψmps, space, old_sites[i], sites, dim=i, **kwargs + ψmps, space, old_sites[i], sites, dim=i, strategy=strategy ) return ψmps @@ -232,7 +251,9 @@ def finite_differences_interpolation_1D( return simplify(odd + even, strategy=strategy), new_space -def finite_differences_interpolation(ψmps, space, **kwargs): +def finite_differences_interpolation( + ψmps: MPS, space: Space, strategy: Strategy = DEFAULT_STRATEGY +): """Finite differences interpolation of an MPS representing a multidimensional function. @@ -242,8 +263,8 @@ def finite_differences_interpolation(ψmps, space, **kwargs): MPS representing a multidimensional function. space : Space Space on which the function is defined. - **kwargs : - Other arguments accepted by :class:`MPO` + strategy : Strategy, optional + Truncation strategy, defaults to DEFAULT_STRATEGY. Returns ------- @@ -252,5 +273,7 @@ def finite_differences_interpolation(ψmps, space, **kwargs): """ space = copy.deepcopy(space) for i, q in enumerate(space.qubits_per_dimension): - ψmps, space = finite_differences_interpolation_1D(ψmps, space, dim=i, **kwargs) + ψmps, space = finite_differences_interpolation_1D( + ψmps, space, dim=i, strategy=strategy + ) return ψmps From 55a32cd5ebe33bf88a4aff3f4baf464d859d3cd9 Mon Sep 17 00:00:00 2001 From: PaulaGarciaMolina Date: Thu, 25 Apr 2024 09:00:18 +0200 Subject: [PATCH 3/4] Import List from typing --- src/seemps/analysis/interpolation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/seemps/analysis/interpolation.py b/src/seemps/analysis/interpolation.py index 3acb3560..4424f61d 100644 --- a/src/seemps/analysis/interpolation.py +++ b/src/seemps/analysis/interpolation.py @@ -2,6 +2,7 @@ import copy from math import sqrt +from typing import List import numpy as np From 61c02bd1f0be947801c5011fd580b646e9f38046 Mon Sep 17 00:00:00 2001 From: PaulaGarciaMolina Date: Thu, 25 Apr 2024 09:08:10 +0200 Subject: [PATCH 4/4] Add normalize_inplace() to Fourier state --- src/seemps/analysis/interpolation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/seemps/analysis/interpolation.py b/src/seemps/analysis/interpolation.py index 4424f61d..c0ad5a5c 100644 --- a/src/seemps/analysis/interpolation.py +++ b/src/seemps/analysis/interpolation.py @@ -86,7 +86,7 @@ def fourier_interpolation_1D( ψfmps = iQFT_op @ (U2c @ Fψfmps) ψfmps = (Mf / M0) * ψfmps if strategy.get_normalize_flag(): - ψfmps = ψfmps * (1 / ψfmps.norm()) + ψfmps = ψfmps.normalize_inplace() return ψfmps, new_space