From 00b609186dda51c7b6cca24ba6185498624b241d Mon Sep 17 00:00:00 2001 From: Manoel Marques Date: Wed, 19 May 2021 11:32:08 -0400 Subject: [PATCH] Fix #192 (#193) (#195) * Add regression test against #192 * Fix #192 * Add reno * Run black Co-authored-by: Max Rossmannek --- .../electronic_structure_problem.py | 7 +++-- ...-core-and-z2symmetry-1b5caa71986abaee.yaml | 6 +++++ .../test_groundstate_eigensolver.py | 26 +++++++++++++++++-- 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 releasenotes/notes/fix-incompatibility-of-freeze-core-and-z2symmetry-1b5caa71986abaee.yaml diff --git a/qiskit_nature/problems/second_quantization/electronic/electronic_structure_problem.py b/qiskit_nature/problems/second_quantization/electronic/electronic_structure_problem.py index fee94c7253..462694fc5d 100644 --- a/qiskit_nature/problems/second_quantization/electronic/electronic_structure_problem.py +++ b/qiskit_nature/problems/second_quantization/electronic/electronic_structure_problem.py @@ -148,11 +148,10 @@ def symmetry_sector_locator(self, z2_symmetries: Z2Symmetries) -> Optional[List[ Returns: The sector of the tapered operators with the problem solution. """ - q_molecule = cast(QMolecule, self._molecule_data_transformed) - hf_bitstr = hartree_fock_bitstring( - num_spin_orbitals=2 * q_molecule.num_molecular_orbitals, - num_particles=self.num_particles) + num_spin_orbitals=z2_symmetries.symmetries[0].num_qubits, + num_particles=self.num_particles, + ) sector_locator = ElectronicStructureProblem._pick_sector(z2_symmetries, hf_bitstr) return sector_locator diff --git a/releasenotes/notes/fix-incompatibility-of-freeze-core-and-z2symmetry-1b5caa71986abaee.yaml b/releasenotes/notes/fix-incompatibility-of-freeze-core-and-z2symmetry-1b5caa71986abaee.yaml new file mode 100644 index 0000000000..cbba53d903 --- /dev/null +++ b/releasenotes/notes/fix-incompatibility-of-freeze-core-and-z2symmetry-1b5caa71986abaee.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + The `FreezeCoreTransformer` (and `ActiveSpaceTransformer`) were incompatible with the automatic + `Z2Symmetry` reduction. This issue was fixed by correcting the + `ElectronicStructureProblem.symmetry_sector_locator` method. diff --git a/test/algorithms/ground_state_solvers/test_groundstate_eigensolver.py b/test/algorithms/ground_state_solvers/test_groundstate_eigensolver.py index a4e5895d34..65487c3116 100644 --- a/test/algorithms/ground_state_solvers/test_groundstate_eigensolver.py +++ b/test/algorithms/ground_state_solvers/test_groundstate_eigensolver.py @@ -33,8 +33,10 @@ from qiskit_nature.mappers.second_quantization import JordanWignerMapper, ParityMapper from qiskit_nature.converters.second_quantization import QubitConverter from qiskit_nature.problems.second_quantization import ElectronicStructureProblem -from qiskit_nature.problems.second_quantization.electronic.builders.fermionic_op_builder import \ - build_ferm_op_from_ints +from qiskit_nature.problems.second_quantization.electronic.builders.fermionic_op_builder import ( + build_ferm_op_from_ints, +) +from qiskit_nature.transformers import FreezeCoreTransformer class TestGroundStateEigensolver(QiskitNatureTestCase): @@ -376,6 +378,26 @@ def test_uccsd_hf_aer_qasm_snapshot(self): result = gsc.solve(self.electronic_structure_problem) self.assertAlmostEqual(result.total_energies[0], self.reference_energy, places=3) + def test_freeze_core_z2_symmetry_compatibility(self): + """Regression test against #192. + + An issue arose when the FreezeCoreTransformer was combined with the automatic Z2Symmetry + reduction. This regression test ensures that this behavior remains fixed. + """ + driver = HDF5Driver(hdf5_input=self.get_resource_path("LiH_sto3g.hdf5", "transformers")) + problem = ElectronicStructureProblem(driver, [FreezeCoreTransformer()]) + qubit_converter = QubitConverter( + ParityMapper(), + two_qubit_reduction=True, + z2symmetry_reduction="auto", + ) + + solver = NumPyMinimumEigensolverFactory() + gsc = GroundStateEigensolver(qubit_converter, solver) + + result = gsc.solve(problem) + self.assertAlmostEqual(result.total_energies[0], -7.882, places=2) + if __name__ == '__main__': unittest.main()