def test_uccsd_hf_qpUCCD(self): """paired uccd test""" optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( self.num_spin_orbitals, self.num_particles, self.qubit_converter ) ansatz = PUCCD( self.qubit_converter, self.num_particles, self.num_spin_orbitals, initial_state=initial_state, ) solver = VQE( ansatz=ansatz, optimizer=optimizer, quantum_instance=QuantumInstance(backend=BasicAer.get_backend("statevector_simulator")), ) gsc = GroundStateEigensolver(self.qubit_converter, solver) result = gsc.solve(self.electronic_structure_problem) self.assertAlmostEqual(result.total_energies[0], self.reference_energy_pUCCD, places=6)
def test_qubits_2_py_h2(self): """qubits 2 py h2 test""" num_particles = (1, 1) converter = QubitConverter(ParityMapper(), two_qubit_reduction=True) converter.force_match(num_particles=num_particles) state = HartreeFock(4, num_particles, converter) ref = QuantumCircuit(2) ref.x(0) self.assertEqual(state, ref)
def _prepare_uccsd_hf(self, qubit_converter): initial_state = HartreeFock(self.num_spin_orbitals, self.num_particles, qubit_converter) ansatz = UCCSD( qubit_converter, self.num_particles, self.num_spin_orbitals, initial_state=initial_state, ) return ansatz
def test_mp2_initial_point_with_real_molecules( self, atom, ): """Test MP2InitialPoint with real molecules.""" from pyscf import gto # pylint: disable=import-error # Compute the PySCF result pyscf_mol = gto.M(atom=atom, basis="sto3g", verbose=0) pyscf_mp = pyscf_mol.MP2().run(verbose=0) driver = PySCFDriver(atom=atom, basis="sto3g") problem = ElectronicStructureProblem(driver) problem.second_q_ops() grouped_property = problem.grouped_property_transformed particle_number = grouped_property.get_property(ParticleNumber) num_particles = (particle_number.num_alpha, particle_number.num_beta) num_spin_orbitals = particle_number.num_spin_orbitals qubit_converter = QubitConverter(mapper=JordanWignerMapper()) initial_state = HartreeFock( num_spin_orbitals=num_spin_orbitals, num_particles=num_particles, qubit_converter=qubit_converter, ) ansatz = UCC( num_spin_orbitals=num_spin_orbitals, num_particles=num_particles, excitations="sd", qubit_converter=qubit_converter, initial_state=initial_state, ) mp2_initial_point = MP2InitialPoint() mp2_initial_point.grouped_property = grouped_property mp2_initial_point.ansatz = ansatz with self.subTest("Test the MP2 energy correction."): np.testing.assert_almost_equal(mp2_initial_point.energy_correction, pyscf_mp.e_corr, decimal=4) with self.subTest("Test the total MP2 energy."): np.testing.assert_almost_equal(mp2_initial_point.total_energy, pyscf_mp.e_tot, decimal=4) with self.subTest("Test the T2 amplitudes."): mp2_initial_point.compute() np.testing.assert_array_almost_equal( mp2_initial_point.t2_amplitudes, pyscf_mp.t2, decimal=4)
def test_qubits_6_py_lih(self): """qubits 6 py lih test""" num_particles = (1, 1) converter = QubitConverter(ParityMapper(), two_qubit_reduction=True) z2symmetries = Z2Symmetries( symmetries=[Pauli("ZIZIZIZI"), Pauli("ZZIIZZII")], sq_paulis=[Pauli("IIIIIIXI"), Pauli("IIIIIXII")], sq_list=[2, 3], tapering_values=[1, 1], ) converter.force_match(num_particles=num_particles, z2symmetries=z2symmetries) state = HartreeFock(10, num_particles, converter) ref = QuantumCircuit(6) ref.x([0, 1]) self.assertEqual(state, ref)
def get_solver( # type: ignore[override] self, problem: ElectronicStructureProblem, qubit_converter: QubitConverter, ) -> MinimumEigensolver: """Returns a VQE with a UCCSD wavefunction ansatz, based on ``qubit_converter``. Args: problem: a class encoding a problem to be solved. qubit_converter: a class that converts second quantized operator to qubit operator according to a mapper it is initialized with. Returns: A VQE suitable to compute the ground state of the molecule. """ driver_result = problem.grouped_property_transformed particle_number = cast(ParticleNumber, driver_result.get_property(ParticleNumber)) num_spin_orbitals = particle_number.num_spin_orbitals num_particles = particle_number.num_alpha, particle_number.num_beta initial_state = self.initial_state if initial_state is None: initial_state = HartreeFock(num_spin_orbitals, num_particles, qubit_converter) ansatz = self.ansatz if ansatz is None: ansatz = UCCSD() ansatz.qubit_converter = qubit_converter ansatz.num_particles = num_particles ansatz.num_spin_orbitals = num_spin_orbitals ansatz.initial_state = initial_state self.minimum_eigensolver.ansatz = ansatz if isinstance(self.initial_point, InitialPoint): self.initial_point.ansatz = ansatz self.initial_point.grouped_property = driver_result initial_point = self.initial_point.to_numpy_array() else: initial_point = self.initial_point self.minimum_eigensolver.initial_point = initial_point return self.minimum_eigensolver
def test_excitation_preserving(self): """Test the excitation preserving wavefunction on a chemistry example.""" driver = HDF5Driver( self.get_resource_path("test_driver_hdf5.hdf5", "second_q/drivers/hdf5d")) converter = QubitConverter(ParityMapper()) problem = ElectronicStructureProblem(driver) _ = problem.second_q_ops() particle_number = cast( ParticleNumber, problem.grouped_property_transformed.get_property(ParticleNumber)) num_particles = (particle_number.num_alpha, particle_number.num_beta) num_spin_orbitals = particle_number.num_spin_orbitals optimizer = SLSQP(maxiter=100) initial_state = HartreeFock(num_spin_orbitals, num_particles, converter) wavefunction = ExcitationPreserving(num_spin_orbitals) wavefunction.compose(initial_state, front=True, inplace=True) solver = VQE( ansatz=wavefunction, optimizer=optimizer, quantum_instance=QuantumInstance( BasicAer.get_backend("statevector_simulator"), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ), ) gsc = GroundStateEigensolver(converter, solver) result = gsc.solve(problem) self.assertAlmostEqual(result.total_energies[0], self.reference_energy, places=4)
def test_setters_getters(self): """Test Getter/Setter""" with self.subTest("Initial Point"): self.assertTrue( isinstance(self._vqe_uvcc_factory.initial_point, VSCFInitialPoint)) initial_point = [1, 2, 3] self._vqe_uvcc_factory.initial_point = initial_point self.assertEqual(self._vqe_uvcc_factory.initial_point, initial_point) with self.subTest("Ansatz"): self.assertEqual(self._vqe_uvcc_factory.ansatz, None) ansatz = UVCCSD() self._vqe_uvcc_factory.ansatz = ansatz self.assertTrue(isinstance(self._vqe_uvcc_factory.ansatz, UVCCSD)) with self.subTest("Initial State"): self.assertEqual(self._vqe_uvcc_factory.initial_state, None) initial_state = HartreeFock(4, (1, 1), self.converter) self._vqe_uvcc_factory.initial_state = initial_state self.assertEqual(self._vqe_uvcc_factory.initial_state, initial_state)
def test_qubits_4_bk_h2(self): """qubits 4 bk h2 test""" state = HartreeFock(4, (1, 1), QubitConverter(BravyiKitaevMapper())) ref = QuantumCircuit(4) ref.x([0, 1, 2]) self.assertEqual(state, ref)
def test_qubits_4_py_h2(self): """qubits 4 py h2 test""" state = HartreeFock(4, (1, 1), QubitConverter(ParityMapper())) ref = QuantumCircuit(4) ref.x([0, 1]) self.assertEqual(state, ref)
def test_qubits_4_jw_h2(self): """qubits 4 jw h2 test""" state = HartreeFock(4, (1, 1), QubitConverter(JordanWignerMapper())) ref = QuantumCircuit(4) ref.x([0, 2]) self.assertEqual(state, ref)