Skip to content

Commit

Permalink
feat: added support for power elevation + removed outdated test
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoLiegiBastonLiegi committed Oct 16, 2024
1 parent 63b1ce7 commit 2b34b5c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 15 deletions.
5 changes: 1 addition & 4 deletions src/qibo/hamiltonians/hamiltonians.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion tests/test_hamiltonians.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
10 changes: 0 additions & 10 deletions tests/test_hamiltonians_symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 2b34b5c

Please sign in to comment.