From 4768357ac3d31f0dadf43cc172af2dd65145aee5 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 7 Oct 2025 14:23:10 +0200 Subject: [PATCH 01/39] Update version.py --- src/tequila/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tequila/version.py b/src/tequila/version.py index efefaf49..49325def 100644 --- a/src/tequila/version.py +++ b/src/tequila/version.py @@ -1,2 +1,2 @@ -__version__ = "1.9.10.dev" +__version__ = "1.9.10" __author__ = "Tequila Developers " From 6848e6bde634879b81bea77702f2f2bcea66a98d Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Thu, 31 Jul 2025 14:50:26 +0200 Subject: [PATCH 02/39] Update README.md (#424) --- README.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 233e282b..934a7bc4 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@ Tequila can execute the underlying quantum expectation values on state of the ar # Getting Started Get started with our collection of -- *[Tutorials](https://tequilahub.github.io/tequila-tutorials/)* +- *[Tutorials](https://tequilahub.github.io/tequila-tutorials/)* +- *[Documentation](https://tequilahub.github.io/tequila-tutorials/docs/sphinx/)* Further sources: - [overview article](https://arxiv.org/abs/2011.03057) @@ -295,12 +296,7 @@ pip install pyscf Works similar as Psi4. Classical methods are also integrated in the madness interface allowing to use them in a basis-set-free representation. # Documentation -You can build the documentation by navigating to `docs` and entering `make html`. -Open the documentation with a browser over like `firefox docs/build/html/index.html` -Note that you will need some additional python packages like `sphinx` and `mr2` that are not explicitly listed in the requirements.txt - -You can also visit our prebuild online [documentation](https://tequilahub.github.io/tequila/) -that will correspond to the github master branch +see [here](https://tequilahub.github.io/tequila-tutorials/docs/sphinx/) # How to contribute If you find any bugs or inconveniences in `tequila` please don't be shy and let us know. From 309d127b1ef02fa781db2b7bf48ea4bd22aaf3f3 Mon Sep 17 00:00:00 2001 From: S-Erik Date: Sun, 26 Oct 2025 09:55:49 +0100 Subject: [PATCH 03/39] Adding excitationsolve optimizer --- requirements.txt | 1 + src/tequila/optimizers/__init__.py | 10 +- src/tequila/optimizers/optimizer_excsolve.py | 139 +++++++++++++++++++ 3 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 src/tequila/optimizers/optimizer_excsolve.py diff --git a/requirements.txt b/requirements.txt index 68c0382b..e0a81911 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,6 +10,7 @@ pytest openfermion ~= 1.0 # can not be smaller than 1.0 #cmake # needed by qulacs, can be removed otherwise, now in qulacs requirements qulacs # default simulator (best integration), remove if the installation gives you trouble and just install one of the other supported backend. Version restriction only for noise models, otherwise the new version is fine +excitationsolve #optional quantum backends #cirq >= 0.9.2 # diff --git a/src/tequila/optimizers/__init__.py b/src/tequila/optimizers/__init__.py index 206c6d65..e3423aa6 100644 --- a/src/tequila/optimizers/__init__.py +++ b/src/tequila/optimizers/__init__.py @@ -1,8 +1,10 @@ from tequila.optimizers.optimizer_base import OptimizerHistory, Optimizer, TequilaOptimizerException, OptimizerResults from tequila.optimizers.optimizer_scipy import OptimizerSciPy from tequila.optimizers.optimizer_gd import OptimizerGD +from tequila.optimizers.optimizer_excsolve import OptimizerExcitationSolve from tequila.optimizers.optimizer_scipy import minimize as minimize_scipy from tequila.optimizers.optimizer_gd import minimize as minimize_gd +from tequila.optimizers.optimizer_excsolve import minimize as minimize_excsolve from tequila.simulators.simulator_api import simulate from dataclasses import dataclass @@ -18,12 +20,16 @@ class _Optimizers: methods: list = None -SUPPORTED_OPTIMIZERS = ["scipy", "gpyopt", "gd"] +SUPPORTED_OPTIMIZERS = ["scipy", "gpyopt", "gd", "excitationsolve"] INSTALLED_OPTIMIZERS = {} INSTALLED_OPTIMIZERS["scipy"] = _Optimizers( cls=OptimizerSciPy, minimize=minimize_scipy, methods=OptimizerSciPy.available_methods() ) INSTALLED_OPTIMIZERS["gd"] = _Optimizers(cls=OptimizerGD, minimize=minimize_gd, methods=OptimizerGD.available_methods()) +INSTALLED_OPTIMIZERS["excitationsolve"] = _Optimizers( + cls=OptimizerExcitationSolve, minimize=minimize_excsolve, methods=OptimizerExcitationSolve.available_methods() +) + has_gpyopt = False try: @@ -138,5 +144,5 @@ def minimize( ) raise TequilaOptimizerException( - "Could not find optimization method {} in tequila optimizers. You might miss dependencies" + f"Could not find optimization method {method} in tequila optimizers. You might miss dependencies" ) diff --git a/src/tequila/optimizers/optimizer_excsolve.py b/src/tequila/optimizers/optimizer_excsolve.py new file mode 100644 index 00000000..e7494c0b --- /dev/null +++ b/src/tequila/optimizers/optimizer_excsolve.py @@ -0,0 +1,139 @@ +import scipy +import numpy +import typing +import numbers +from excitationsolve import ExcitationSolveScipy +from tequila.objective import Objective +from tequila.objective.objective import assign_variable, Variable, format_variable_dictionary, format_variable_list +from .optimizer_base import Optimizer, OptimizerResults +from ._containers import _EvalContainer, _GradContainer, _HessContainer, _QngContainer +from .optimizer_scipy import SciPyResults +from tequila.utils.exceptions import TequilaException +from tequila.circuit.noise import NoiseModel +from tequila.tools.qng import get_qng_combos + +from dataclasses import dataclass + + +class OptimizerExcitationSolve(Optimizer): + r"""The ExcitationSolve optimizer as a SciPy optimizer that can be given to the scipy.optimize.minimize function. + + Usage: + ```python + excsolve_obj = ExcitationSolveScipy(maxiter=100, tol=1e-10, save_parameters=True) + optimizer = excsolve_obj.minimize + res = scipy.optimize.minimize(cost, params, method=optimizer) + energies = excsolve_obj.energies + counts = excsolve_obj.nfevs + ``` + + Note that this optimizer never needs to evaluate the ansatz circuit + at the (current) optimal parameters, unless the optimal parameters fall onto + the sample points used to reconstruct the energy function. + Therefore, when used with a qiskit VQE object, the energies transmitted + to a VQE callback function, do not seem to improve or converge. Nevertheless, + the determined optimal energy and parameters are still returned. + + Args: + maxiter (int): Maximum number of VQE iterations (maximum number of times to optimize all parameters) + tol: Threshold of energy difference after subsequent VQE iterations defining convergence + num_samples (int, optional): Number of different parameter values at which to sample + the energy to reconstruct the energy function in one parameter. + Must be greater or equal to 5. Defaults to 5. + hf_energy (float | None, optional): The Hartree-Fock energy, i.e. the energy of the + system where all parameters in the circuit are zero. If none, this will be + calculated by evaluating the energy of the ansatz with all parameters set to zero. + If this energy is known from a prior classical calculation, e.g. a Hartree-Fock + calculation, one energy evaluation is saved. Defaults to None. + save_parameters (bool, optional): If True, params member variable contains + all optimal parameter values after each optimization step, + i.e. after optimizing each single parameter. Defaults to False. + param_scaling (float, optional): Factor used for rescaling the parameters. This ExcitationSolve optimizer + expects the parameters to be 2\pi periodic. For example, in Qiskit + the excitation parameters result in excitation operators being \pi periodic. + Therefore, we use a factor of 0.5 for qiskit, resulting in a Period of 2\pi. + """ + + @classmethod + def available_methods(cls): + """:return: All tested available methods""" + return ["excitationsolve"] + + def __init__( + self, maxiter, tol=1e-12, num_samples=5, hf_energy=None, save_parameters=False, param_scaling=0.5, **kwargs + ): + if maxiter is None: + maxiter = 10 + + super().__init__(**kwargs) + + self.opt = ExcitationSolveScipy( + maxiter=maxiter, + tol=tol, + num_samples=num_samples, + hf_energy=hf_energy, + save_parameters=save_parameters, + param_scaling=param_scaling, + ) + + def __call__( + self, + objective: Objective, + variables: typing.List[Variable], + initial_values: typing.Dict[Variable, numbers.Real] = None, + *args, + **kwargs, + ) -> scipy.optimize.OptimizeResult: + objective = objective.contract() + infostring = "{:15} : {}\n".format("Method", "ExcitationSolve") + infostring += "{:15} : {} expectationvalues\n".format("Objective", objective.count_expectationvalues()) + + # if self.save_history and reset_history: + # self.reset_history() + + active_angles, passive_angles, variables = self.initialize_variables(objective, initial_values, variables) + + # Transform the initial value directory into (ordered) arrays + param_keys, param_values = zip(*active_angles.items()) + param_values = numpy.array(param_values) + + # do the compilation here to avoid costly recompilation during the optimization + compiled_objective = self.compile_objective(objective=objective, *args, **kwargs) + E = _EvalContainer( + objective=compiled_objective, + param_keys=param_keys, + samples=self.samples, + passive_angles=passive_angles, + save_history=self.save_history, + print_level=self.print_level, + ) + + res = self.opt.minimize(E, param_values, *args, **kwargs) + + if self.save_history: + self.history.energy_calls = self.opt.energies + + return SciPyResults(energy=res.fun, history=self.history, variables=res.x, scipy_result=res) + + +def minimize( + objective: Objective, + variables: typing.List[Variable], + initial_values: typing.Dict[Variable, numbers.Real] = None, + method: str = "excitationsolve", + maxiter: int = 10, + *args, + **kwargs, +): + optimize = OptimizerExcitationSolve( + maxiter=maxiter, + *args, + **kwargs, + ) + return optimize( + objective=objective, + variables=variables, + initial_values=initial_values, + *args, + **kwargs, + ) From 0753cab6b08b6be136340045b6c17b305398e06f Mon Sep 17 00:00:00 2001 From: S-Erik Date: Sun, 26 Oct 2025 10:07:25 +0100 Subject: [PATCH 04/39] Fixing return value type hint in __call__ --- src/tequila/optimizers/optimizer_excsolve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tequila/optimizers/optimizer_excsolve.py b/src/tequila/optimizers/optimizer_excsolve.py index e7494c0b..7a2cbad0 100644 --- a/src/tequila/optimizers/optimizer_excsolve.py +++ b/src/tequila/optimizers/optimizer_excsolve.py @@ -83,7 +83,7 @@ def __call__( initial_values: typing.Dict[Variable, numbers.Real] = None, *args, **kwargs, - ) -> scipy.optimize.OptimizeResult: + ) -> SciPyResults: objective = objective.contract() infostring = "{:15} : {}\n".format("Method", "ExcitationSolve") infostring += "{:15} : {} expectationvalues\n".format("Objective", objective.count_expectationvalues()) From 405922b7b6844a2463e69aa9a13ea9887bc7f038 Mon Sep 17 00:00:00 2001 From: S-Erik Date: Sun, 2 Nov 2025 16:52:11 +0100 Subject: [PATCH 05/39] Adding pip install excitationsolve to ci_basic. Put ExcSolve behind try import block --- .github/workflows/ci_basic.yml | 1 + src/tequila/optimizers/__init__.py | 15 ++++++++++++--- src/tequila/optimizers/optimizer_excsolve.py | 5 ++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci_basic.yml b/.github/workflows/ci_basic.yml index b6bd5930..afa22035 100644 --- a/.github/workflows/ci_basic.yml +++ b/.github/workflows/ci_basic.yml @@ -30,6 +30,7 @@ jobs: pip install -r requirements.txt # test default simulator against in-house simulator pip install git+https://github.com/Mikel-Ma/spex@devel + pip install excitationsolve - name: Lint with flake8 run: | pip install flake8 diff --git a/src/tequila/optimizers/__init__.py b/src/tequila/optimizers/__init__.py index e3423aa6..14f47d44 100644 --- a/src/tequila/optimizers/__init__.py +++ b/src/tequila/optimizers/__init__.py @@ -26,9 +26,6 @@ class _Optimizers: cls=OptimizerSciPy, minimize=minimize_scipy, methods=OptimizerSciPy.available_methods() ) INSTALLED_OPTIMIZERS["gd"] = _Optimizers(cls=OptimizerGD, minimize=minimize_gd, methods=OptimizerGD.available_methods()) -INSTALLED_OPTIMIZERS["excitationsolve"] = _Optimizers( - cls=OptimizerExcitationSolve, minimize=minimize_excsolve, methods=OptimizerExcitationSolve.available_methods() -) has_gpyopt = False @@ -43,6 +40,18 @@ class _Optimizers: except ImportError: has_gpyopt = False +has_excsolve = False +try: + from tequila.optimizers.optimizer_excsolve import OptimizerExcitationSolve + from tequila.optimizers.optimizer_excsolve import minimize as minimize_excsolve + + INSTALLED_OPTIMIZERS["excitationsolve"] = _Optimizers( + cls=OptimizerExcitationSolve, minimize=minimize_excsolve, methods=OptimizerExcitationSolve.available_methods() + ) + has_excsolve = True +except ImportError: + has_excsolve = False + def show_available_optimizers(module=None): """ diff --git a/src/tequila/optimizers/optimizer_excsolve.py b/src/tequila/optimizers/optimizer_excsolve.py index 7a2cbad0..ae51e4f7 100644 --- a/src/tequila/optimizers/optimizer_excsolve.py +++ b/src/tequila/optimizers/optimizer_excsolve.py @@ -111,7 +111,9 @@ def __call__( res = self.opt.minimize(E, param_values, *args, **kwargs) if self.save_history: - self.history.energy_calls = self.opt.energies + self.history.energies = self.opt.energies + self.history.angles = self.opt.params + # self.history.gradients = self.opt.energies_shiftste return SciPyResults(energy=res.fun, history=self.history, variables=res.x, scipy_result=res) @@ -127,6 +129,7 @@ def minimize( ): optimize = OptimizerExcitationSolve( maxiter=maxiter, + save_parameters=True, *args, **kwargs, ) From 23f2ad8bc942da41e5b23b5b0aaa4567da1de091 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 11 Nov 2025 11:34:44 +0100 Subject: [PATCH 06/39] Update Python versions in CI workflow --- .github/workflows/ci_basic.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_basic.yml b/.github/workflows/ci_basic.yml index afa22035..ae797b0a 100644 --- a/.github/workflows/ci_basic.yml +++ b/.github/workflows/ci_basic.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.9', '3.10'] + python-version: ['3.10', '3.11', '3.12'] steps: - uses: actions/checkout@v2 From 85f094446e857a017a6b33d0b192859fe5fbe1e5 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 11 Nov 2025 11:34:58 +0100 Subject: [PATCH 07/39] Update ci_basic_autograd.yml --- .github/workflows/ci_basic_autograd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_basic_autograd.yml b/.github/workflows/ci_basic_autograd.yml index 3ca8463d..a71d09d0 100644 --- a/.github/workflows/ci_basic_autograd.yml +++ b/.github/workflows/ci_basic_autograd.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.9', '3.10'] + python-version: ['3.11'] steps: - uses: actions/checkout@v2 From 7fcf35e5b65d1301179f276b6b01dad201c937ed Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 11 Nov 2025 11:35:17 +0100 Subject: [PATCH 08/39] Update Python version in CI workflow to 3.11 --- .github/workflows/ci_chemistry_psi4.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_chemistry_psi4.yml b/.github/workflows/ci_chemistry_psi4.yml index 3334474b..f43bacd9 100644 --- a/.github/workflows/ci_chemistry_psi4.yml +++ b/.github/workflows/ci_chemistry_psi4.yml @@ -22,7 +22,7 @@ jobs: with: miniconda-version: 'latest' auto-activate-base: false - python-version: '3.10' + python-version: '3.11' - name: Initialize Conda for the shell run: | From a78533d06ec827444b83e98521c565df16c33816 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 11 Nov 2025 11:35:31 +0100 Subject: [PATCH 09/39] Update Python version in CI workflow to 3.11 --- .github/workflows/ci_chemistry_pyscf.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_chemistry_pyscf.yml b/.github/workflows/ci_chemistry_pyscf.yml index f68e6c64..5328c8cf 100644 --- a/.github/workflows/ci_chemistry_pyscf.yml +++ b/.github/workflows/ci_chemistry_pyscf.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.9] + python-version: [3.11] steps: - uses: actions/checkout@v2 From 5d4527f319cdad4d8f42f52bcc107febaabcdadb Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 11 Nov 2025 11:35:44 +0100 Subject: [PATCH 10/39] Update ci_conda_madness.yml --- .github/workflows/ci_conda_madness.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_conda_madness.yml b/.github/workflows/ci_conda_madness.yml index ed042d4a..abc289b7 100644 --- a/.github/workflows/ci_conda_madness.yml +++ b/.github/workflows/ci_conda_madness.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.9] + python-version: [3.11] steps: - uses: actions/checkout@v2 From 556570f39e823bef9cd3cdc70415b8401a399451 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 11 Nov 2025 11:37:31 +0100 Subject: [PATCH 11/39] Update ci_ml.yml --- .github/workflows/ci_ml.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_ml.yml b/.github/workflows/ci_ml.yml index e649b377..77574d92 100644 --- a/.github/workflows/ci_ml.yml +++ b/.github/workflows/ci_ml.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.9] + python-version: [3.11] steps: - uses: actions/checkout@v2 From 381bf426cfcd0574f059338ed707d858998e81b1 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 11 Nov 2025 11:37:46 +0100 Subject: [PATCH 12/39] Update Python version in CI workflow to 3.11 --- .github/workflows/ci_pyquil.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_pyquil.yml b/.github/workflows/ci_pyquil.yml index 926ec8dc..47728338 100644 --- a/.github/workflows/ci_pyquil.yml +++ b/.github/workflows/ci_pyquil.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.9] + python-version: [3.11] steps: - uses: actions/checkout@v2 From baf343af0e1c7e6e18cb61d4d593fb7906e9821d Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 11 Nov 2025 11:38:28 +0100 Subject: [PATCH 13/39] Revise Python version recommendation in README Update recommended Python version in installation instructions. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 934a7bc4..701a4eb7 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Further sources: - [talks and slides](https://kottmanj.github.io/talks_and_material/) # Installation -Recommended Python version is 3.10 (3.11). +Recommended Python version is 3.11 (3.10 or 3.12 shouldn't be a problem either). Tequila supports linux, osx and windows. However, not all optional dependencies (especially chemistry) are supported on windows. ## Install from PyPi From 045b8aec76d45062fd67d8d584e9a47f50e7e6de Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 11 Nov 2025 14:30:42 +0100 Subject: [PATCH 14/39] Change Python version from 3.11 to 3.10 --- .github/workflows/ci_chemistry_pyscf.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_chemistry_pyscf.yml b/.github/workflows/ci_chemistry_pyscf.yml index 5328c8cf..079d3760 100644 --- a/.github/workflows/ci_chemistry_pyscf.yml +++ b/.github/workflows/ci_chemistry_pyscf.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.11] + python-version: [3.10] steps: - uses: actions/checkout@v2 From 8c889b367325dc72af2ebf35272c5b83531b15df Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 11 Nov 2025 14:33:21 +0100 Subject: [PATCH 15/39] Fix Python version format in CI workflow --- .github/workflows/ci_chemistry_pyscf.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_chemistry_pyscf.yml b/.github/workflows/ci_chemistry_pyscf.yml index 079d3760..46ea93e0 100644 --- a/.github/workflows/ci_chemistry_pyscf.yml +++ b/.github/workflows/ci_chemistry_pyscf.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.10] + python-version: ['3.10'] steps: - uses: actions/checkout@v2 From 437b2774ed236f12213476ad77bfa0f3533e390e Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 11 Nov 2025 14:52:04 +0100 Subject: [PATCH 16/39] Comment out h5py installation in CI workflow Comment out h5py installation to prevent conflicts. --- .github/workflows/ci_chemistry_pyscf.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_chemistry_pyscf.yml b/.github/workflows/ci_chemistry_pyscf.yml index 46ea93e0..97a4ea8b 100644 --- a/.github/workflows/ci_chemistry_pyscf.yml +++ b/.github/workflows/ci_chemistry_pyscf.yml @@ -39,7 +39,7 @@ jobs: python -m pip install -r requirements.txt python -m pip install -e . python -m pip install pyscf - python -m pip install 'h5py <= 3.1' + #python -m pip install 'h5py <= 3.1' cd tests ls pytest test_chemistry.py test_TrotErr.py -m "not dependencies" From cbc2427d086acfbd3d03e13a933f988228cab533 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Fri, 14 Nov 2025 10:00:33 +0100 Subject: [PATCH 17/39] Disable tests for TaperedBinary and REORDEREDTAPEREDBINARY Comment out problematic test cases related to openfermion. --- tests/test_chemistry.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_chemistry.py b/tests/test_chemistry.py index b4b68aeb..eec315c2 100644 --- a/tests/test_chemistry.py +++ b/tests/test_chemistry.py @@ -948,9 +948,9 @@ def test_orbital_optimization_hcb(geometry): "JordanWigner", "ReorderedJordanWigner", "BravyiKitaev", - "BravyiKitaevTree", - "TaperedBinary", - "REORDEREDTAPEREDBINARY", + "BravyiKitaevTree"#, + #"TaperedBinary", + #"REORDEREDTAPEREDBINARY", # currently there is an issue with openfermion (see PR https://github.com/quantumlib/OpenFermion/pull/1171) ], ) @pytest.mark.parametrize("size", [2, 8]) From cfdb2cabe18753c0abfe6be6d4c29e5968f92577 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Fri, 14 Nov 2025 10:07:19 +0100 Subject: [PATCH 18/39] Update test_chemistry.py --- tests/test_chemistry.py | 49 +++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/tests/test_chemistry.py b/tests/test_chemistry.py index eec315c2..0bb7fb49 100644 --- a/tests/test_chemistry.py +++ b/tests/test_chemistry.py @@ -16,11 +16,24 @@ # Get QC backends for parametrized testing import select_backends - HAS_PYSCF = "pyscf" in qc.INSTALLED_QCHEMISTRY_BACKENDS HAS_PSI4 = "psi4" in qc.INSTALLED_QCHEMISTRY_BACKENDS backends = select_backends.get() +trafos = [ + "JordanWigner", + "ReorderedJordanWigner", + "BravyiKitaev", + "BravyiKitaevTree" # , + # "TaperedBinary", + # "REORDEREDTAPEREDBINARY", # currently there is an issue with openfermion (see PR https://github.com/quantumlib/OpenFermion/pull/1171) + ] + +standard_trafos = [ + "JordanWigner", + "ReorderedJordanWigner", + "BravyiKitaev" + ] def teardown_function(function): @@ -79,18 +92,13 @@ def test_base(trafo): H = molecule.make_hamiltonian() eigvals = numpy.linalg.eigvalsh(H.to_matrix()) assert numpy.isclose(eigvals[0], -2.87016214e00) - if "trafo" in [ - "JordanWigner", - "BravyiKitaev", - "bravyi_kitaev_fast", - "BravyiKitaevTree", - ]: # others change spectrum outside of the groundstate + if "trafo" in standard_trafos: # others change spectrum outside of the groundstate assert numpy.isclose(eigvals[-1], 7.10921141e-01) assert len(eigvals) == 16 @pytest.mark.skipif(condition=not HAS_PSI4 and not HAS_PYSCF, reason="you don't have psi4 or pyscf") -@pytest.mark.parametrize("trafo", ["JordanWigner", "BravyiKitaev", "BravyiKitaevTree", "TaperedBinary"]) +@pytest.mark.parametrize("trafo", trafos) def test_prepare_reference(trafo): geometry = "Li 0.0 0.0 0.0\nH 0.0 0.0 1.5" basis_set = "sto-3g" @@ -191,8 +199,8 @@ def do_test_h2_hamiltonian(qc_interface): @pytest.mark.skipif(condition=not HAS_PSI4, reason="you don't have psi4") @pytest.mark.parametrize( - "trafo", ["JordanWigner", "BravyiKitaev", "BravyiKitaevTree", "TaperedBinary"] -) # bravyi_kitaev_fast not yet supported for ucc + "trafo", trafos +) @pytest.mark.parametrize("backend", backends) def test_ucc_psi4(trafo, backend): if backend == "symbolic": @@ -491,16 +499,7 @@ def test_fermionic_gates(assume_real, trafo): @pytest.mark.skipif(condition=not HAS_PSI4 and not HAS_PYSCF, reason="psi4/pyscf not found") @pytest.mark.parametrize( - "trafo", - [ - "JordanWigner", - "BravyiKitaev", - "BravyiKitaevTree", - "ReorderedJordanWigner", - "ReorderedBravyiKitaev", - "TaperedBinary", - "REORDEREDTAPEREDBINARY", - ], + "trafo", trafos ) def test_hcb(trafo): geomstring = "Be 0.0 0.0 0.0\n H 0.0 0.0 1.6\n H 0.0 0.0 -1.6" @@ -943,15 +942,7 @@ def test_orbital_optimization_hcb(geometry): @pytest.mark.parametrize( - "transformation", - [ - "JordanWigner", - "ReorderedJordanWigner", - "BravyiKitaev", - "BravyiKitaevTree"#, - #"TaperedBinary", - #"REORDEREDTAPEREDBINARY", # currently there is an issue with openfermion (see PR https://github.com/quantumlib/OpenFermion/pull/1171) - ], + "transformation", trafos ) @pytest.mark.parametrize("size", [2, 8]) def test_givens_on_molecule(size, transformation): From 582ff48876aa686fc2cd0f5bbb7a7c5907cf2540 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Fri, 14 Nov 2025 12:11:05 +0100 Subject: [PATCH 19/39] Refactor transformation parameterization in tests --- tests/test_chemistry.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/tests/test_chemistry.py b/tests/test_chemistry.py index 0bb7fb49..7a96321c 100644 --- a/tests/test_chemistry.py +++ b/tests/test_chemistry.py @@ -20,21 +20,9 @@ HAS_PSI4 = "psi4" in qc.INSTALLED_QCHEMISTRY_BACKENDS backends = select_backends.get() -trafos = [ - "JordanWigner", - "ReorderedJordanWigner", - "BravyiKitaev", - "BravyiKitaevTree" # , - # "TaperedBinary", - # "REORDEREDTAPEREDBINARY", # currently there is an issue with openfermion (see PR https://github.com/quantumlib/OpenFermion/pull/1171) - ] - -standard_trafos = [ - "JordanWigner", - "ReorderedJordanWigner", - "BravyiKitaev" - ] - +standard_trafos = ["JordanWigner", "ReorderedJordanWigner", "BravyiKitaev"] +trafos = standard_trafos +# trafos = list(known_encodings().keys() # currently issues with openfermion def teardown_function(function): [os.remove(x) for x in glob.glob("data/*.pickle")] @@ -61,7 +49,7 @@ def test_UR_and_UC(): assert numpy.isclose(result.energy, fci) -@pytest.mark.parametrize("trafo", list(known_encodings().keys())) +@pytest.mark.parametrize("trafo", trafos) def test_base(trafo): obt = numpy.asarray([[-1.94102524, -0.31651552], [-0.31651552, -0.0887454]]) tbt = numpy.asarray( @@ -386,7 +374,7 @@ def test_rdms_psi4(): @pytest.mark.skipif(condition=not HAS_PSI4 and not HAS_PYSCF, reason="psi4/pyscf not found") @pytest.mark.parametrize("geometry", ["H 0.0 0.0 0.0\nH 0.0 0.0 0.7"]) -@pytest.mark.parametrize("trafo", tq.quantumchemistry.encodings.known_encodings()) +@pytest.mark.parametrize("trafo", trafos) def test_upccgsd(geometry, trafo): molecule = tq.chemistry.Molecule(geometry=geometry, units="angstrom", basis_set="sto-3g", transformation=trafo) if not molecule.supports_ucc(): @@ -785,7 +773,7 @@ def test_spa_ansatz_be(): "geometry", ["H 0.0 0.0 0.0\nH 0.0 0.0 4.5", "Li 0.0 0.0 0.0\nH 0.0 0.0 3.0", "Be 0.0 0.0 0.0\nH 0.0 0.0 3.0\nH 0.0 0.0 -3.0"], ) -@pytest.mark.parametrize("transformation", tq.quantumchemistry.encodings.known_encodings()) +@pytest.mark.parametrize("transformation", trafos) @pytest.mark.skipif(condition=not HAS_PSI4 and not HAS_PYSCF, reason="psi4/pyscf not found") def test_spa_consistency(geometry, name, optimize, transformation): mol = tq.Molecule( From 9154136b0d871f9ba7297e71a27126ec14a3bf6e Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Sat, 15 Nov 2025 11:17:14 +0100 Subject: [PATCH 20/39] Change Python version in CI workflow to 3.9 --- .github/workflows/ci_pyquil.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_pyquil.yml b/.github/workflows/ci_pyquil.yml index 47728338..27281556 100644 --- a/.github/workflows/ci_pyquil.yml +++ b/.github/workflows/ci_pyquil.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.11] + python-version: ["3.9"] steps: - uses: actions/checkout@v2 From 7cd254676e1ad5f16d37fff2ebf9f23f0a31aa19 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Sat, 15 Nov 2025 11:19:18 +0100 Subject: [PATCH 21/39] Refactor parametrize decorators for tests --- tests/test_chemistry.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/tests/test_chemistry.py b/tests/test_chemistry.py index 7a96321c..4ea42f66 100644 --- a/tests/test_chemistry.py +++ b/tests/test_chemistry.py @@ -24,6 +24,7 @@ trafos = standard_trafos # trafos = list(known_encodings().keys() # currently issues with openfermion + def teardown_function(function): [os.remove(x) for x in glob.glob("data/*.pickle")] [os.remove(x) for x in glob.glob("data/*.out")] @@ -186,9 +187,7 @@ def do_test_h2_hamiltonian(qc_interface): @pytest.mark.skipif(condition=not HAS_PSI4, reason="you don't have psi4") -@pytest.mark.parametrize( - "trafo", trafos -) +@pytest.mark.parametrize("trafo", trafos) @pytest.mark.parametrize("backend", backends) def test_ucc_psi4(trafo, backend): if backend == "symbolic": @@ -486,9 +485,7 @@ def test_fermionic_gates(assume_real, trafo): @pytest.mark.skipif(condition=not HAS_PSI4 and not HAS_PYSCF, reason="psi4/pyscf not found") -@pytest.mark.parametrize( - "trafo", trafos -) +@pytest.mark.parametrize("trafo", trafos) def test_hcb(trafo): geomstring = "Be 0.0 0.0 0.0\n H 0.0 0.0 1.6\n H 0.0 0.0 -1.6" mol1 = tq.Molecule( @@ -929,9 +926,7 @@ def test_orbital_optimization_hcb(geometry): assert (numpy.isclose(opt1.mo_coeff, opt2.mo_coeff, atol=1.0e-5)).all() -@pytest.mark.parametrize( - "transformation", trafos -) +@pytest.mark.parametrize("transformation", trafos) @pytest.mark.parametrize("size", [2, 8]) def test_givens_on_molecule(size, transformation): # dummy one-electron integrals From 9004af1e02b05816c6132234721e2ede9277a488 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Sat, 15 Nov 2025 11:22:02 +0100 Subject: [PATCH 22/39] Fix parameterization in test_ucc_psi4 --- tests/test_chemistry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_chemistry.py b/tests/test_chemistry.py index 4ea42f66..966785b2 100644 --- a/tests/test_chemistry.py +++ b/tests/test_chemistry.py @@ -187,7 +187,7 @@ def do_test_h2_hamiltonian(qc_interface): @pytest.mark.skipif(condition=not HAS_PSI4, reason="you don't have psi4") -@pytest.mark.parametrize("trafo", trafos) +@pytest.mark.parametrize("trafo", trafos) @pytest.mark.parametrize("backend", backends) def test_ucc_psi4(trafo, backend): if backend == "symbolic": From a5b1a6bedd77cc584b7631b5330314fe9c8287ac Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Mon, 17 Nov 2025 15:25:09 +0100 Subject: [PATCH 23/39] Update ci_pyquil.yml --- .github/workflows/ci_pyquil.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_pyquil.yml b/.github/workflows/ci_pyquil.yml index 27281556..6c810d01 100644 --- a/.github/workflows/ci_pyquil.yml +++ b/.github/workflows/ci_pyquil.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.9"] + python-version: ["3.10"] steps: - uses: actions/checkout@v2 From 1af2122fc10c0660815e2438b78a146d24da2e81 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Mon, 17 Nov 2025 15:25:44 +0100 Subject: [PATCH 24/39] Update test_chemistry.py --- tests/test_chemistry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_chemistry.py b/tests/test_chemistry.py index 966785b2..b8be2318 100644 --- a/tests/test_chemistry.py +++ b/tests/test_chemistry.py @@ -98,7 +98,7 @@ def test_prepare_reference(trafo): energy = tq.simulate(E) hf_energy = mol.compute_energy("hf") assert numpy.isclose(energy, hf_energy, atol=1.0e-4) - mol = tq.Molecule(geometry=geometry, units="angstrom", basis_set=basis_set, transformation="reordered" + trafo) + mol = tq.Molecule(geometry=geometry, units="angstrom", basis_set=basis_set, transformation=trafo) H = mol.make_hamiltonian() U = mol.prepare_reference() E = tq.ExpectationValue(H=H, U=U) From ea7fa2c8eef09450c2bbad66b97fbc589296e388 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Mon, 17 Nov 2025 15:33:42 +0100 Subject: [PATCH 25/39] Delete .github/workflows/ci_pyquil.yml too much work to maintain --- .github/workflows/ci_pyquil.yml | 45 --------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 .github/workflows/ci_pyquil.yml diff --git a/.github/workflows/ci_pyquil.yml b/.github/workflows/ci_pyquil.yml deleted file mode 100644 index 6c810d01..00000000 --- a/.github/workflows/ci_pyquil.yml +++ /dev/null @@ -1,45 +0,0 @@ -# This workflow will install Python dependencies, run tests and lint with a variety of Python versions -# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions - -name: Tequila-Test-Pyquil - -on: - push: - branches: [ master, devel ] - pull_request: - branches: [] - -jobs: - - build: - - runs-on: ubuntu-latest - strategy: - matrix: - python-version: ["3.10"] - - steps: - - uses: actions/checkout@v2 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v1 - with: - python-version: ${{ matrix.python-version }} - - name: Install and run - run: | - python -m pip install --upgrade pip - pip install --upgrade pytest - pip install -r requirements.txt - pip install "pyquil<3.0" # needs updates - pip install -e . - pip install --upgrade pip 'urllib3<2' # issues with old pyquil version otherwise - docker pull rigetti/qvm:edge - docker pull rigetti/quilc - docker run --rm -itd -p 5555:5555 rigetti/quilc -R - docker run --rm -itd -p 5000:5000 rigetti/qvm -S - pytest -m "not dependencies" tests/test_simulator_backends.py --slow - pytest tests/test_recompilation_routines.py --slow - pytest -m "not dependencies" tests/test_noise.py --slow - pytest tests/test_gradient.py --slow - pytest tests/test_scipy.py --slow - pytest tests/test_mappings.py --slow - From 4740567cf06e24ee88851a9eb4243bc20598b0fd Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Mon, 17 Nov 2025 15:34:49 +0100 Subject: [PATCH 26/39] Change Python version in CI workflow to 3.09 (cuda issue) --- .github/workflows/ci_backends.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_backends.yml b/.github/workflows/ci_backends.yml index 7d156e2a..e4f41699 100644 --- a/.github/workflows/ci_backends.yml +++ b/.github/workflows/ci_backends.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.11'] + python-version: ['3.09'] include: - os: ubuntu-latest cxx: /usr/bin/g++-14 From a024b8f93ab32550a617fdb728afea7b312bdfb7 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Mon, 17 Nov 2025 15:35:02 +0100 Subject: [PATCH 27/39] Fix Python version in CI workflow --- .github/workflows/ci_backends.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_backends.yml b/.github/workflows/ci_backends.yml index e4f41699..51c9c5c3 100644 --- a/.github/workflows/ci_backends.yml +++ b/.github/workflows/ci_backends.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.09'] + python-version: ['3.9'] include: - os: ubuntu-latest cxx: /usr/bin/g++-14 From 0ae7a18b9bad5959c53de990b50c881f138b6fc1 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Mon, 17 Nov 2025 15:37:27 +0100 Subject: [PATCH 28/39] Update ci_backends.yml --- .github/workflows/ci_backends.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci_backends.yml b/.github/workflows/ci_backends.yml index 51c9c5c3..435bd8b4 100644 --- a/.github/workflows/ci_backends.yml +++ b/.github/workflows/ci_backends.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.9'] + python-version: ['3.11'] include: - os: ubuntu-latest cxx: /usr/bin/g++-14 @@ -33,7 +33,7 @@ jobs: - name: Install CUDA-Q run: | - cuda_version=12.4.0 + cuda_version=12.6.0 conda create -y -n cudaq-env python=3.11 pip conda install -y -n cudaq-env -c "nvidia/label/cuda-${cuda_version}" cuda conda install -y -n cudaq-env -c conda-forge mpi4py "openmpi>=5.0.3" cxx-compiler From 3004c4056f9c589a50b48633fdce994de567a991 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Mon, 17 Nov 2025 21:18:01 +0100 Subject: [PATCH 29/39] Update main_trot.py --- src/tequila/trotter_err/main_trot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tequila/trotter_err/main_trot.py b/src/tequila/trotter_err/main_trot.py index f8743381..8025aff8 100644 --- a/src/tequila/trotter_err/main_trot.py +++ b/src/tequila/trotter_err/main_trot.py @@ -23,7 +23,7 @@ def SpecNormComm(Op1, Op2, nqubs, Projector=None): if Projector is not None: Comm = Projector * Comm - spNorm = sparse.linalg.eigs(Comm, k=1, which="LM", return_eigenvectors=False) + spNorm = sparse.linalg.eigsh(Comm, k=4, which="LM", return_eigenvectors=False) return np.abs(spNorm[0]) From cfcc544e45bb6b28a35b97cff253ff497b8d2d7c Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Tue, 18 Nov 2025 14:16:32 +0100 Subject: [PATCH 30/39] Update main_trot.py --- src/tequila/trotter_err/main_trot.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/tequila/trotter_err/main_trot.py b/src/tequila/trotter_err/main_trot.py index 8025aff8..9943e3ff 100644 --- a/src/tequila/trotter_err/main_trot.py +++ b/src/tequila/trotter_err/main_trot.py @@ -23,8 +23,11 @@ def SpecNormComm(Op1, Op2, nqubs, Projector=None): if Projector is not None: Comm = Projector * Comm - spNorm = sparse.linalg.eigsh(Comm, k=4, which="LM", return_eigenvectors=False) - + try: + spNorm = sparse.linalg.eigsh(Comm, k=4, which="LM", return_eigenvectors=False) + except Exception as E: + print("comm=", Comm) + raise E return np.abs(spNorm[0]) From 652bfba04e9c81170a1202e2e3122229e0f7a4d7 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Wed, 19 Nov 2025 12:12:06 +0100 Subject: [PATCH 31/39] Add zero norm check for Comm before eigsh call Added a check for zero norm before eigenvalue computation. --- src/tequila/trotter_err/main_trot.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/tequila/trotter_err/main_trot.py b/src/tequila/trotter_err/main_trot.py index 9943e3ff..de35f116 100644 --- a/src/tequila/trotter_err/main_trot.py +++ b/src/tequila/trotter_err/main_trot.py @@ -23,6 +23,10 @@ def SpecNormComm(Op1, Op2, nqubs, Projector=None): if Projector is not None: Comm = Projector * Comm + # some issues with new scipy ... keep this for now + if np.isclose(sparse.linalg.norm(Comm),0.0): + return 0.0 + try: spNorm = sparse.linalg.eigsh(Comm, k=4, which="LM", return_eigenvectors=False) except Exception as E: From 8875272befd9aa46995e055aaaa10b0f2efd215d Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Thu, 20 Nov 2025 14:53:43 +0100 Subject: [PATCH 32/39] Add debug prints for Comm and operator calculations Added debug print statements for troubleshooting. --- src/tequila/trotter_err/main_trot.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/tequila/trotter_err/main_trot.py b/src/tequila/trotter_err/main_trot.py index de35f116..ec1f6c8b 100644 --- a/src/tequila/trotter_err/main_trot.py +++ b/src/tequila/trotter_err/main_trot.py @@ -25,6 +25,12 @@ def SpecNormComm(Op1, Op2, nqubs, Projector=None): # some issues with new scipy ... keep this for now if np.isclose(sparse.linalg.norm(Comm),0.0): + print("XXX") + print(Comm) + print(Op1) + print(Op1) + print(1j*(Op1*Op2 - Op2*Op1)) + print(Projector) return 0.0 try: From 239f616a28dbe22385a8e1fd930b91fe7837822c Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Thu, 20 Nov 2025 15:37:00 +0100 Subject: [PATCH 33/39] Update main_trot.py --- src/tequila/trotter_err/main_trot.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/tequila/trotter_err/main_trot.py b/src/tequila/trotter_err/main_trot.py index ec1f6c8b..0d12b589 100644 --- a/src/tequila/trotter_err/main_trot.py +++ b/src/tequila/trotter_err/main_trot.py @@ -19,19 +19,14 @@ def SpecNormComm(Op1, Op2, nqubs, Projector=None): SpOp2 = openfermion.get_sparse_operator(Op2, n_qubits=nqubs) Comm = 1j * (SpOp1 * SpOp2 - SpOp2 * SpOp1) + # some issues with openfermion ... keep this for now + if np.isclose(sparse.linalg.norm(Comm),0.0): + Comm = 1j * (Op1 * Op2 - Op2 * Op1) + if Projector is not None: + Comm = openfermion.get_sparse_operator(Comm, n_qubits=nqubs) if Projector is not None: Comm = Projector * Comm - - # some issues with new scipy ... keep this for now - if np.isclose(sparse.linalg.norm(Comm),0.0): - print("XXX") - print(Comm) - print(Op1) - print(Op1) - print(1j*(Op1*Op2 - Op2*Op1)) - print(Projector) - return 0.0 try: spNorm = sparse.linalg.eigsh(Comm, k=4, which="LM", return_eigenvectors=False) From be2cfbf6502110534c787b2d412c193a02822c97 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Thu, 20 Nov 2025 20:54:18 +0100 Subject: [PATCH 34/39] Update main_trot.py --- src/tequila/trotter_err/main_trot.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/tequila/trotter_err/main_trot.py b/src/tequila/trotter_err/main_trot.py index 0d12b589..41eee606 100644 --- a/src/tequila/trotter_err/main_trot.py +++ b/src/tequila/trotter_err/main_trot.py @@ -20,8 +20,9 @@ def SpecNormComm(Op1, Op2, nqubs, Projector=None): Comm = 1j * (SpOp1 * SpOp2 - SpOp2 * SpOp1) # some issues with openfermion ... keep this for now - if np.isclose(sparse.linalg.norm(Comm),0.0): + if np.isclose(sparse.linalg.norm(Comm), 0.0): Comm = 1j * (Op1 * Op2 - Op2 * Op1) + Comm = Comm.split()[0] if Projector is not None: Comm = openfermion.get_sparse_operator(Comm, n_qubits=nqubs) @@ -29,9 +30,13 @@ def SpecNormComm(Op1, Op2, nqubs, Projector=None): Comm = Projector * Comm try: - spNorm = sparse.linalg.eigsh(Comm, k=4, which="LM", return_eigenvectors=False) + spNorm = sparse.linalg.eigs(Comm, k=4, which="LM", return_eigenvectors=False) except Exception as E: print("comm=", Comm) + print("Op1=", Op1) + print("Op2=", Op2) + print("SpOp1=", Op1) + print("Projector=", Projector) raise E return np.abs(spNorm[0]) From 566b2745f132e7a6f6d5ae6894206505f9382e41 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Thu, 20 Nov 2025 21:08:57 +0100 Subject: [PATCH 35/39] Update main_trot.py --- src/tequila/trotter_err/main_trot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tequila/trotter_err/main_trot.py b/src/tequila/trotter_err/main_trot.py index 41eee606..68cdb34c 100644 --- a/src/tequila/trotter_err/main_trot.py +++ b/src/tequila/trotter_err/main_trot.py @@ -22,7 +22,6 @@ def SpecNormComm(Op1, Op2, nqubs, Projector=None): # some issues with openfermion ... keep this for now if np.isclose(sparse.linalg.norm(Comm), 0.0): Comm = 1j * (Op1 * Op2 - Op2 * Op1) - Comm = Comm.split()[0] if Projector is not None: Comm = openfermion.get_sparse_operator(Comm, n_qubits=nqubs) From b058699756da5800a62b56f8f9be0639e8f65875 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Mon, 2 Feb 2026 11:12:54 +0100 Subject: [PATCH 36/39] Update main_trot.py From f6792da90f2d74c4c9194e786e8084d585d90db5 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Mon, 2 Feb 2026 11:26:54 +0100 Subject: [PATCH 37/39] Update main_trot.py --- src/tequila/trotter_err/main_trot.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/tequila/trotter_err/main_trot.py b/src/tequila/trotter_err/main_trot.py index 68cdb34c..f8743381 100644 --- a/src/tequila/trotter_err/main_trot.py +++ b/src/tequila/trotter_err/main_trot.py @@ -19,24 +19,12 @@ def SpecNormComm(Op1, Op2, nqubs, Projector=None): SpOp2 = openfermion.get_sparse_operator(Op2, n_qubits=nqubs) Comm = 1j * (SpOp1 * SpOp2 - SpOp2 * SpOp1) - # some issues with openfermion ... keep this for now - if np.isclose(sparse.linalg.norm(Comm), 0.0): - Comm = 1j * (Op1 * Op2 - Op2 * Op1) - if Projector is not None: - Comm = openfermion.get_sparse_operator(Comm, n_qubits=nqubs) if Projector is not None: Comm = Projector * Comm - - try: - spNorm = sparse.linalg.eigs(Comm, k=4, which="LM", return_eigenvectors=False) - except Exception as E: - print("comm=", Comm) - print("Op1=", Op1) - print("Op2=", Op2) - print("SpOp1=", Op1) - print("Projector=", Projector) - raise E + + spNorm = sparse.linalg.eigs(Comm, k=1, which="LM", return_eigenvectors=False) + return np.abs(spNorm[0]) From 2ec1c83c169a3c6b2f144f5503f81c0885e525f7 Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Mon, 2 Feb 2026 11:32:39 +0100 Subject: [PATCH 38/39] Implement error handling in basis state index calculation Added error handling for basis state conversion. --- src/tequila/grouping/fermionic_functions.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/tequila/grouping/fermionic_functions.py b/src/tequila/grouping/fermionic_functions.py index 4a7bc5d4..e7608754 100644 --- a/src/tequila/grouping/fermionic_functions.py +++ b/src/tequila/grouping/fermionic_functions.py @@ -589,8 +589,12 @@ def find_index(basis_state): index = 0 n_qubits = len(basis_state) for j in range(n_qubits): - index += int(basis_state[j]) * 2 ** (n_qubits - j - 1) - + try: + index += int(basis_state[j]) * 2 ** (n_qubits - j - 1) + except E as Exception: + print("basis_state=", basis_state) + print(f"j={j}") + print(f"basis_state[j]={basis_state[j]}") return index From d964af4daea4f5cbdacbd3a7521ffaeed78ec85c Mon Sep 17 00:00:00 2001 From: "J. S. Kottmann" Date: Mon, 2 Feb 2026 11:35:19 +0100 Subject: [PATCH 39/39] Fix exception handling syntax in fermionic_functions.py --- src/tequila/grouping/fermionic_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tequila/grouping/fermionic_functions.py b/src/tequila/grouping/fermionic_functions.py index e7608754..1454bdb9 100644 --- a/src/tequila/grouping/fermionic_functions.py +++ b/src/tequila/grouping/fermionic_functions.py @@ -591,7 +591,7 @@ def find_index(basis_state): for j in range(n_qubits): try: index += int(basis_state[j]) * 2 ** (n_qubits - j - 1) - except E as Exception: + except Exception as E: print("basis_state=", basis_state) print(f"j={j}") print(f"basis_state[j]={basis_state[j]}")