Skip to content

Commit

Permalink
Bug fix and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chmwzc committed Apr 11, 2024
1 parent 5a21b77 commit cb69ed1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/qibochem/measurement/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def pauli_term_measurement_expectation(pauli_term, frequencies):
pauli_z = [Z(int(factor.target_qubit)) for factor in pauli_term.factors if factor.name[0] != "I"]
z_only_ham = SymbolicHamiltonian(pauli_term.coefficient * reduce(lambda x, y: x * y, pauli_z, 1.0))
# Can now apply expectation_from_samples directly
return z_only_ham.expectation_from_samples(frequencies)
return z_only_ham.expectation_from_samples(
frequencies, qubit_map=[factor.target_qubit for factor in pauli_term.factors]
)


def expectation(
Expand Down
30 changes: 28 additions & 2 deletions tests/test_expectation_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from qibo import Circuit, gates
from qibo.hamiltonians import SymbolicHamiltonian
from qibo.symbols import X, Z
from qibo.symbols import X, Y, Z

from qibochem.driver import Molecule
from qibochem.measurement import expectation
Expand Down Expand Up @@ -100,11 +100,37 @@ def test_expectation_invalid_shot_allocation():
)


@pytest.mark.parametrize(
"hamiltonian",
[
SymbolicHamiltonian(Z(0) + X(0) * Y(1) + Z(0) * Y(2)),
SymbolicHamiltonian(Y(0) + Z(1) + X(0) * Z(2)),
],
)
def test_qwc_functionality(hamiltonian):
"""Small scale tests of QWC functionality"""
n_qubits = 3
circuit = Circuit(n_qubits)
circuit.add(gates.RX(_i, 0.1 * _i) for _i in range(n_qubits))
circuit.add(gates.CNOT(_i, _i + 1) for _i in range(n_qubits - 1))
circuit.add(gates.RZ(_i, 0.2 * _i) for _i in range(n_qubits))
expected = expectation(circuit, hamiltonian)
n_shots = 5000
test = expectation(
circuit,
hamiltonian,
from_samples=True,
n_shots=n_shots,
group_pauli_terms="qwc",
)
assert test == pytest.approx(expected, abs=0.05)


@pytest.mark.parametrize(
"n_shots_per_pauli_term,threshold",
[
(True, 0.005), # 5000 shots used for each term in Hamiltonian
(False, 0.015), # 5000 shots divided between each Pauli string in Hamiltonian
(False, 0.02), # 5000 shots divided between each Pauli string in Hamiltonian
],
)
def test_h2_hf_energy(n_shots_per_pauli_term, threshold):
Expand Down
6 changes: 0 additions & 6 deletions tests/test_measurement_optimisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@

import pytest

# from qibochem.driver import Molecule
# from qibochem.measurement import expectation
from qibochem.measurement.optimization import (
check_terms_commutativity,
group_commuting_terms,
)

# from qibo import Circuit, gates
# from qibo.hamiltonians import SymbolicHamiltonian
# from qibo.symbols import X, Z


@pytest.mark.parametrize(
"term1,term2,qwc_expected,gc_expected",
Expand Down

0 comments on commit cb69ed1

Please sign in to comment.