diff --git a/src/qforte/ucc/adaptvqe.py b/src/qforte/ucc/adaptvqe.py index 2eeec1ce..ed674fc2 100644 --- a/src/qforte/ucc/adaptvqe.py +++ b/src/qforte/ucc/adaptvqe.py @@ -174,13 +174,13 @@ def run( ) if self._is_multi_state: - if self._state_prep_type != "computer": + if self._state_prep_type != "computer": E, A, ops = qforte.excited_state_algorithms.ritz_eigh( - self._nqb, self._qb_ham, self.build_Uvqc() + self._nqb, self._qb_ham, self.build_Uvqc() ) else: H_eff = qforte.build_effective_array( - self._qb_ham, self.build_Uvqc()[0], self.get_initial_computer() + self._qb_ham, self.build_Uvqc()[0], self.get_initial_computer() ).real E, A = np.linalg.eigh(H_eff) self._diag_energies.append(E) @@ -192,7 +192,6 @@ def run( cur_string += f" {e}" print(cur_string) print(diag_string) - while not self._converged: print("\n\n -----> ADAPT-VQE iteration ", avqe_iter, " <-----\n") @@ -212,13 +211,13 @@ def run( self.compute_moment_energies() if self._is_multi_state: - if self._state_prep_type != "computer": + if self._state_prep_type != "computer": E, A, ops = qforte.excited_state_algorithms.ritz_eigh( self._nqb, self._qb_ham, self.build_Uvqc() ) else: H_eff = qforte.build_effective_array( - self._qb_ham, self.build_Uvqc()[0], self.get_initial_computer() + self._qb_ham, self.build_Uvqc()[0], self.get_initial_computer() ).real E, A = np.linalg.eigh(H_eff) diff --git a/tests/test_sa_adapt_vqe.py b/tests/test_sa_adapt_vqe.py index 3df42444..f113a95f 100644 --- a/tests/test_sa_adapt_vqe.py +++ b/tests/test_sa_adapt_vqe.py @@ -235,12 +235,9 @@ def test_LiH_more_adapt_vqe(self): for j in range(len(E_more)): assert dip_dir[i, j] - total_dip[i, j] == approx(0.0, abs=1e-7) - - - THIS_DIR = os.path.dirname(os.path.abspath(__file__)) data_path = os.path.join(THIS_DIR, "lih_cas_dump.json") - spaces = [[1,0,0,0],[2,0,0,0],[1,0,1,1]] + spaces = [[1, 0, 0, 0], [2, 0, 0, 0], [1, 0, 1, 1]] mol = system_factory( system_type="molecule", mol_geometry=geom, @@ -253,43 +250,43 @@ def test_LiH_more_adapt_vqe(self): no_reorient=True, json_dump=data_path, ) - - mol = system_factory( - build_type = "external", - filename = data_path - ) - occ_refs = [[1,1,1,1,0,0] + [0]*6, - [1,1,0,0,1,1] + [0]*6, - [1,1,0,1,1,0] + [0]*6, - [1,1,1,0,0,1] + [0]*6] + mol = system_factory(build_type="external", filename=data_path) + + occ_refs = [ + [1, 1, 1, 1, 0, 0] + [0] * 6, + [1, 1, 0, 0, 1, 1] + [0] * 6, + [1, 1, 0, 1, 1, 0] + [0] * 6, + [1, 1, 1, 0, 0, 1] + [0] * 6, + ] alg = ADAPTVQE( mol, print_summary_file=False, is_multi_state=True, reference=occ_refs, - weights=[.25]*4, + weights=[0.25] * 4, compact_excitations=True, ) - H_eff = build_effective_symmetric_operator(12, mol.hamiltonian, alg._refprep).real + H_eff = build_effective_symmetric_operator( + 12, mol.hamiltonian, alg._refprep + ).real E_casscf = np.linalg.eigh(H_eff)[0][0] assert E_casscf == approx(-7.873605319132174, 1e-8) - alg.run(pool_type = "GSD", adapt_maxiter = 3) + alg.run(pool_type="GSD", adapt_maxiter=3) correct_Es = [ -7.8593451680521662, -7.7158474059591828, -7.6836465355578119, - -7.2065322341909379 + -7.2065322341909379, ] - - + for i in range(4): assert correct_Es[i] == approx(alg._diag_energies[-1][i]) - + comp_refs = [Computer(12) for i in range(4)] coeff_vec = np.zeros(2**12) @@ -299,15 +296,15 @@ def test_LiH_more_adapt_vqe(self): coeff_vec = np.zeros(2**12) coeff_vec[int("110011", 2)] = 1 comp_refs[1].set_coeff_vec(copy.deepcopy(coeff_vec)) - + coeff_vec = np.zeros(2**12) coeff_vec[int("100111", 2)] = 1 comp_refs[2].set_coeff_vec(copy.deepcopy(coeff_vec)) - + coeff_vec = np.zeros(2**12) coeff_vec[int("011011", 2)] = 1 comp_refs[3].set_coeff_vec(copy.deepcopy(coeff_vec)) - + alg = ADAPTVQE( mol, print_summary_file=False, @@ -318,17 +315,17 @@ def test_LiH_more_adapt_vqe(self): state_prep_type="computer", ) - alg.run(pool_type = "GSD", adapt_maxiter = 3) + alg.run(pool_type="GSD", adapt_maxiter=3) for i in range(4): assert correct_Es[i] == approx(alg._diag_energies[-1][i]) - + circ_refs = [] circ_refs.append(build_refprep([1] * 2 + [1, 1, 0, 0] + [0] * 6)) circ_refs.append(build_refprep([1] * 2 + [0, 0, 1, 1] + [0] * 6)) circ_refs.append(build_refprep([1] * 2 + [0, 1, 1, 0] + [0] * 6)) circ_refs.append(build_refprep([1] * 2 + [1, 0, 0, 1] + [0] * 6)) - + alg = ADAPTVQE( mol, print_summary_file=False, @@ -339,7 +336,6 @@ def test_LiH_more_adapt_vqe(self): state_prep_type="unitary_circ", ) - alg.run(pool_type = "GSD", adapt_maxiter = 3) + alg.run(pool_type="GSD", adapt_maxiter=3) for i in range(4): assert correct_Es[i] == approx(alg._diag_energies[-1][i]) - \ No newline at end of file