From 5e4a1a7bdfce5415d87dacfa0779510bd8c569b7 Mon Sep 17 00:00:00 2001 From: tsura-crisaldo <54267053+tsura-crisaldo@users.noreply.github.com> Date: Sat, 15 Aug 2020 23:31:00 +1000 Subject: [PATCH] correct the UCCSD.compute_excitation_lists() (#1201) * correct the UCCSD.compute_excitation_lists * Add unit test for excitations and active space Co-authored-by: Manoel Marques Co-authored-by: woodsp --- .../components/variational_forms/uccsd.py | 4 +- ...CCSD-excitation-list-0fbb1af60f47192f.yaml | 6 ++ test/chemistry/test_uccsd_hartree_fock.py | 55 +++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/UCCSD-excitation-list-0fbb1af60f47192f.yaml diff --git a/qiskit/chemistry/components/variational_forms/uccsd.py b/qiskit/chemistry/components/variational_forms/uccsd.py index 9728fa0f74..987135dc21 100644 --- a/qiskit/chemistry/components/variational_forms/uccsd.py +++ b/qiskit/chemistry/components/variational_forms/uccsd.py @@ -527,7 +527,7 @@ def compute_excitation_lists(num_particles, num_orbitals, active_occ_list=None, raise ValueError( 'Invalid index {} in active active_occ_list {}'.format(i, active_occ_list)) if i < num_beta: - active_occ_list_beta.append(i) + active_occ_list_beta.append(i + beta_idx) else: raise ValueError( 'Invalid index {} in active active_occ_list {}'.format(i, active_occ_list)) @@ -545,7 +545,7 @@ def compute_excitation_lists(num_particles, num_orbitals, active_occ_list=None, raise ValueError('Invalid index {} in active active_unocc_list {}' .format(i, active_unocc_list)) if i >= num_beta: - active_unocc_list_beta.append(i) + active_unocc_list_beta.append(i + beta_idx) else: raise ValueError('Invalid index {} in active active_unocc_list {}' .format(i, active_unocc_list)) diff --git a/releasenotes/notes/UCCSD-excitation-list-0fbb1af60f47192f.yaml b/releasenotes/notes/UCCSD-excitation-list-0fbb1af60f47192f.yaml new file mode 100644 index 0000000000..4f80f4b2a2 --- /dev/null +++ b/releasenotes/notes/UCCSD-excitation-list-0fbb1af60f47192f.yaml @@ -0,0 +1,6 @@ +--- +ixes: + - | + The UCCSD excitation list, comprising single and double excitations, was not being + generated correctly when an active space was explicitly provided to UCSSD via the + active_(un)occupied parameters. diff --git a/test/chemistry/test_uccsd_hartree_fock.py b/test/chemistry/test_uccsd_hartree_fock.py index f6521633a2..3fe7cf6f27 100644 --- a/test/chemistry/test_uccsd_hartree_fock.py +++ b/test/chemistry/test_uccsd_hartree_fock.py @@ -15,6 +15,9 @@ """ Test of UCCSD and HartreeFock Aqua extensions """ from test.chemistry import QiskitChemistryTestCase + +from ddt import ddt, idata, unpack + from qiskit import BasicAer from qiskit.aqua import QuantumInstance from qiskit.aqua.algorithms import VQE @@ -25,6 +28,7 @@ from qiskit.chemistry.core import Hamiltonian, QubitMappingType +@ddt class TestUCCSDHartreeFock(QiskitChemistryTestCase): """Test for these aqua extensions.""" @@ -55,3 +59,54 @@ def test_uccsd_hf(self): result = algo.run(QuantumInstance(BasicAer.get_backend('statevector_simulator'))) result = core.process_algorithm_result(result) self.assertAlmostEqual(result.energy, self.reference_energy, places=6) + + EXCITATION_RESULTS = \ + [[[[0, 1], [0, 2], [3, 4], [3, 5]], + [[0, 1, 3, 4], [0, 1, 3, 5], [0, 2, 3, 4], [0, 2, 3, 5]]], # 0 full: 6 orbs, 2 particles + [[[0, 2], [3, 5]], [[0, 2, 3, 5]]], # 1 limited active space + [[[0, 1], [0, 2], [3, 4], [3, 5]], []], # 2 singles only + [[], [[0, 1, 3, 4], [0, 1, 3, 5], [0, 2, 3, 4], [0, 2, 3, 5]]], # 3 doubles only + [[[0, 1], [3, 4]], []], # 4 singles only limited active space + [[[0, 2], [1, 2], [3, 5], [4, 5]], + [[0, 2, 3, 5], [0, 2, 4, 5], [1, 2, 3, 5], [1, 2, 4, 5]]], # 5 full: 6 orbs, 4 particles + [[[1, 2], [4, 5]], [[1, 2, 4, 5]]], # 6 limited active space + [[[0, 2], [0, 3], [1, 2], [1, 3], [4, 6], [4, 7], [5, 6], [5, 7]], # 7 + [[0, 2, 4, 6], [0, 2, 4, 7], [0, 2, 5, 6], [0, 2, 5, 7], [0, 3, 4, 6], [0, 3, 4, 7], + [0, 3, 5, 6], [0, 3, 5, 7], [1, 2, 4, 6], [1, 2, 4, 7], [1, 2, 5, 6], [1, 2, 5, 7], + [1, 3, 4, 6], [1, 3, 4, 7], [1, 3, 5, 6], [1, 3, 5, 7], [0, 2, 1, 3], [4, 6, 5, 7]]], + [[[0, 2], [0, 3], [1, 2], [1, 3], [4, 6], [4, 7], [5, 6], [5, 7]], # 8 No same spins + [[0, 2, 4, 6], [0, 2, 4, 7], [0, 2, 5, 6], [0, 2, 5, 7], [0, 3, 4, 6], [0, 3, 4, 7], + [0, 3, 5, 6], [0, 3, 5, 7], [1, 2, 4, 6], [1, 2, 4, 7], [1, 2, 5, 6], [1, 2, 5, 7], + [1, 3, 4, 6], [1, 3, 4, 7], [1, 3, 5, 6], [1, 3, 5, 7]]], + ] + + @idata([[0, 6, 2], + [0, 6, 2, [0], [0, 1]], # Full active space + [1, 6, 2, [0], [1]], # Restrict active space + [0, 6, 2, [0], [0, 1], False], + [2, 6, 2, None, None, True, 'both', 'ucc', 's'], + [3, 6, 2, None, [0, 1], True, 'both', 'ucc', 'd'], + [4, 6, 2, [0], [0], False, 'both', 'ucc', 's'], + [5, 6, 4], + [5, 6, 4, [0, 1], [0]], # Full active space + [6, 6, 4, [1], [0]], # Restrict active space + [7, 8, 4], + [8, 8, 4, None, None, False], + ]) + @unpack + def test_uccsd_excitations(self, expected_result_idx, num_orbitals, num_particles, + active_occupied=None, active_unoccupied=None, + same_spin_doubles=True, + method_singles='both', method_doubles='ucc', + excitation_type='sd' + ): + """ Test generated excitation lists in conjunction with active space """ + + excitations = UCCSD.compute_excitation_lists( + num_orbitals=num_orbitals, num_particles=num_particles, + active_occ_list=active_occupied, active_unocc_list=active_unoccupied, + same_spin_doubles=same_spin_doubles, + method_singles=method_singles, method_doubles=method_doubles, + excitation_type=excitation_type) + + self.assertListEqual(list(excitations), self.EXCITATION_RESULTS[expected_result_idx])