From 2b34b5cd0ec2bbdda3e3840a79def3a3eddec822 Mon Sep 17 00:00:00 2001 From: BrunoLiegiBastonLiegi Date: Wed, 16 Oct 2024 17:32:30 +0200 Subject: [PATCH] feat: added support for power elevation + removed outdated test --- src/qibo/hamiltonians/hamiltonians.py | 5 +---- tests/test_hamiltonians.py | 5 ++++- tests/test_hamiltonians_symbolic.py | 10 ---------- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/qibo/hamiltonians/hamiltonians.py b/src/qibo/hamiltonians/hamiltonians.py index a7328041b2..df1a85b4d6 100644 --- a/src/qibo/hamiltonians/hamiltonians.py +++ b/src/qibo/hamiltonians/hamiltonians.py @@ -585,7 +585,7 @@ def _exp_from_circuit(self, circuit: dict, qubit_map: dict, nshots: int = None): ) measurements = [ gates.M(factor.target_qubit, basis=factor.gate.__class__) - for factor in term.factors + for factor in set(term.factors) ] circ_copy = circuit.copy(True) [circ_copy.add(m) for m in measurements] @@ -672,9 +672,6 @@ def expectation_from_samples( Returns: (float) the computed expectation value. """ - for term in self.terms: - if len(term.factors) != len(set(term.factors)): - raise_error(NotImplementedError, "Z^k is not implemented since Z^2=I.") if isinstance(data, dict): return self._exp_from_freq(data, qubit_map) diff --git a/tests/test_hamiltonians.py b/tests/test_hamiltonians.py index 1f02f642e7..4bdf6cbd03 100644 --- a/tests/test_hamiltonians.py +++ b/tests/test_hamiltonians.py @@ -264,7 +264,10 @@ def test_hamiltonian_expectation_errors(backend): @pytest.mark.parametrize( "observable", - [2 * Z(0) * Z(1) + Z(0) * Z(2), X(0) * Z(1) + Y(0) * X(2) / 2 - Z(0) * Y(1)], + [ + 2 * Z(0) * Z(1) ** 2 + Z(0) * Z(2), + X(0) * Z(1) + Y(0) * X(2) / 2 - Z(0) * Y(1) ** 3, + ], ) def test_hamiltonian_expectation_from_samples(backend, observable): """Test Hamiltonian expectation value calculation.""" diff --git a/tests/test_hamiltonians_symbolic.py b/tests/test_hamiltonians_symbolic.py index 54d8dac2e3..d1d8485703 100644 --- a/tests/test_hamiltonians_symbolic.py +++ b/tests/test_hamiltonians_symbolic.py @@ -330,16 +330,6 @@ def test_hamiltonian_expectation_from_samples(backend): backend.assert_allclose(ev0, ev1, atol=20 / np.sqrt(nshots)) -def test_hamiltonian_expectation_from_samples_errors(backend): - obs = [Z(0) * Y(1), Z(0) * Z(1) ** 3] - h1 = hamiltonians.SymbolicHamiltonian(obs[0], backend=backend) - h2 = hamiltonians.SymbolicHamiltonian(obs[1], backend=backend) - with pytest.raises(NotImplementedError): - h1.expectation_from_samples(None, qubit_map=None) - with pytest.raises(NotImplementedError): - h2.expectation_from_samples(None, qubit_map=None) - - @pytest.mark.parametrize("density_matrix", [False, True]) @pytest.mark.parametrize("calcterms", [False, True]) def test_symbolic_hamiltonian_abstract_symbol_ev(backend, density_matrix, calcterms):