From e2fce4f7306bf0bb39326132588f1e7f9c3b8ab6 Mon Sep 17 00:00:00 2001 From: mhucka <1450019+mhucka@users.noreply.github.com> Date: Fri, 10 Apr 2026 21:30:24 +0000 Subject: [PATCH] Add tests for ValueError in double_commutator Added test cases to DoubleCommutatorTest in commutators_test.py to cover the cases where the intersection of indices for hopping operators in double_commutator results in an empty set or a set with more than one element, which triggers a ValueError. --- src/openfermion/utils/commutators_test.py | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/openfermion/utils/commutators_test.py b/src/openfermion/utils/commutators_test.py index 414d17d9f..485e638a0 100644 --- a/src/openfermion/utils/commutators_test.py +++ b/src/openfermion/utils/commutators_test.py @@ -217,6 +217,38 @@ def test_double_commtator_more_info_both_hopping(self): com, (FermionOperator('4^ 3^ 4 2', 2.73) + FermionOperator('4^ 2^ 4 3', 2.73)) ) + def test_double_commutator_hopping_no_intersection(self): + # Case where intersection is empty + op1 = FermionOperator('0^ 0') + op2 = FermionOperator('1^ 2') + FermionOperator('2^ 1') + op3 = FermionOperator('3^ 4') + FermionOperator('4^ 3') + res = double_commutator( + op1, + op2, + op3, + indices2={1, 2}, + indices3={3, 4}, + is_hopping_operator2=True, + is_hopping_operator3=True, + ) + self.assertEqual(res, FermionOperator.zero()) + + def test_double_commutator_hopping_multi_intersection(self): + # Case where intersection has more than one element + op1 = FermionOperator('0^ 0') + op2 = FermionOperator('1^ 2') + FermionOperator('2^ 1') + op3 = FermionOperator('1^ 2') + FermionOperator('2^ 1') + res = double_commutator( + op1, + op2, + op3, + indices2={1, 2}, + indices3={1, 2}, + is_hopping_operator2=True, + is_hopping_operator3=True, + ) + self.assertEqual(res, FermionOperator.zero()) + class TriviallyDoubleCommutesDualBasisUsingTermInfoTest(unittest.TestCase): def test_number_operators_trivially_commute(self):