-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from qiboteam/refactor-tests
Refactor tests to use pytest.parametrize
- Loading branch information
Showing
4 changed files
with
320 additions
and
468 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,37 @@ | ||
""" | ||
Test HF reference circuit ansatz | ||
""" | ||
|
||
# import numpy as np | ||
import pytest | ||
|
||
from qibochem.ansatz.hf_reference import hf_circuit | ||
from qibochem.driver.molecule import Molecule | ||
from qibochem.measurement.expectation import expectation | ||
|
||
|
||
def test_jw_circuit(): | ||
"""Tests the HF circuit with the Jordan-Wigner mapping""" | ||
# Hardcoded benchmark results | ||
h2_ref_energy = -1.117349035 | ||
|
||
h2 = Molecule([("H", (0.0, 0.0, 0.0)), ("H", (0.0, 0.0, 0.7))]) | ||
h2.run_pyscf() | ||
|
||
# JW-HF circuit | ||
circuit = hf_circuit(h2.nso, h2.nelec, ferm_qubit_map=None) | ||
|
||
# Molecular Hamiltonian and the HF expectation value | ||
hamiltonian = h2.hamiltonian() | ||
hf_energy = expectation(circuit, hamiltonian) | ||
|
||
# assert h2.e_hf == pytest.approx(hf_energy) | ||
assert h2_ref_energy == pytest.approx(hf_energy) | ||
|
||
|
||
def test_bk_circuit_1(): | ||
"""Tests the HF circuit with the Brayvi-Kitaev mapping for H2""" | ||
# Hardcoded benchmark results | ||
h2_ref_energy = -1.117349035 | ||
|
||
h2 = Molecule([("H", (0.0, 0.0, 0.0)), ("H", (0.0, 0.0, 0.7))]) | ||
h2.run_pyscf() | ||
|
||
# JW-HF circuit | ||
circuit = hf_circuit(h2.nso, h2.nelec, ferm_qubit_map="bk") | ||
|
||
# Molecular Hamiltonian and the HF expectation value | ||
hamiltonian = h2.hamiltonian(ferm_qubit_map="bk") | ||
hf_energy = expectation(circuit, hamiltonian) | ||
|
||
# assert h2.e_hf == pytest.approx(hf_energy) | ||
assert h2_ref_energy == pytest.approx(hf_energy) | ||
|
||
|
||
def test_bk_circuit_2(): | ||
"""Tests the HF circuit with the Brayvi-Kitaev mapping for LiH""" | ||
# Hardcoded benchmark results | ||
lih = Molecule([("Li", (0.0, 0.0, 0.0)), ("H", (0.0, 0.0, 1.3))]) | ||
lih.run_pyscf() | ||
|
||
# JW-HF circuit | ||
circuit = hf_circuit(lih.nso, lih.nelec, ferm_qubit_map="bk") | ||
|
||
# Molecular Hamiltonian and the HF expectation value | ||
hamiltonian = lih.hamiltonian(ferm_qubit_map="bk") | ||
hf_energy = expectation(circuit, hamiltonian) | ||
|
||
assert lih.e_hf == pytest.approx(hf_energy) | ||
|
||
|
||
def test_mapping_error(): | ||
"""Tests the HF circuit with an incorrect mapping""" | ||
with pytest.raises(KeyError): | ||
hf_circuit(4, 2, ferm_qubit_map="incorrect") | ||
""" | ||
Test HF reference circuit ansatz | ||
""" | ||
|
||
import pytest | ||
|
||
from qibochem.ansatz.hf_reference import hf_circuit | ||
from qibochem.driver.molecule import Molecule | ||
from qibochem.measurement.expectation import expectation | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"mapping,", | ||
[ | ||
None, # JW mapping | ||
"bk", # BK mapping | ||
], | ||
) | ||
def test_h2(mapping): | ||
"""Tests the HF circuit for H2""" | ||
# Hardcoded benchmark results | ||
h2_ref_energy = -1.117349035 | ||
|
||
h2 = Molecule([("H", (0.0, 0.0, 0.0)), ("H", (0.0, 0.0, 0.7))]) | ||
h2.run_pyscf() | ||
hamiltonian = h2.hamiltonian(ferm_qubit_map=mapping) | ||
circuit = hf_circuit(h2.nso, h2.nelec, ferm_qubit_map=mapping) | ||
hf_energy = expectation(circuit, hamiltonian) | ||
|
||
# assert h2.e_hf == pytest.approx(hf_energy) | ||
assert pytest.approx(hf_energy) == h2_ref_energy | ||
|
||
|
||
def test_mapping_error(): | ||
"""Tests the HF circuit with an incorrect mapping""" | ||
with pytest.raises(KeyError): | ||
hf_circuit(4, 2, ferm_qubit_map="incorrect") |
Oops, something went wrong.