def test_vqe_uvccsd_with_callback(self): """Test VQE UVCCSD with callback.""" def cb_callback(nfev, parameters, energy, stddev): print(f"iterations {nfev}: energy: {energy}") optimizer = COBYLA(maxiter=5000) quantum_instance = QuantumInstance( backend=qiskit.BasicAer.get_backend("statevector_simulator"), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ) solver = VQEUVCCFactory(quantum_instance=quantum_instance, optimizer=optimizer, callback=cb_callback) gsc = GroundStateEigensolver(self.qubit_converter, solver) esc = QEOM(gsc, "sd") with contextlib.redirect_stdout(io.StringIO()) as out: results = esc.solve(self.vibrational_problem) for idx, energy in enumerate(self.reference_energies): self.assertAlmostEqual(results.computed_vibrational_energies[idx], energy, places=1) for idx, line in enumerate(out.getvalue().split("\n")): if line.strip(): self.assertTrue( line.startswith(f"iterations {idx+1}: energy: "))
def _solve_with_vqe_mes(self, converter: QubitConverter): solver = VQEUCCFactory(self.quantum_instance) gsc = GroundStateEigensolver(converter, solver) esc = QEOM(gsc, "sd") results = esc.solve(self.electronic_structure_problem) for idx, energy in enumerate(self.reference_energies): self.assertAlmostEqual(results.computed_energies[idx], energy, places=4)
def test_numpy_mes(self): """Test NumPyMinimumEigenSolver with QEOM""" solver = NumPyMinimumEigensolver() gsc = GroundStateEigensolver(self.qubit_converter, solver) esc = QEOM(gsc, "sd") results = esc.solve(self.electronic_structure_problem) for idx, energy in enumerate(self.reference_energies): self.assertAlmostEqual(results.computed_energies[idx], energy, places=4)
def test_vqe_mes(self): """Test VQEUCCSDFactory with QEOM""" solver = VQEUCCFactory(self.quantum_instance) gsc = GroundStateEigensolver(self.qubit_converter, solver) esc = QEOM(gsc, "sd") results = esc.solve(self.electronic_structure_problem) for idx in range(len(self.reference_energies)): self.assertAlmostEqual(results.computed_energies[idx], self.reference_energies[idx], places=4)
def test_numpy_mes(self): """ Test with NumPyMinimumEigensolver """ solver = NumPyMinimumEigensolverFactory( use_default_filter_criterion=True) gsc = GroundStateEigensolver(self.qubit_converter, solver) esc = QEOM(gsc, 'sd') results = esc.solve(self.vibrational_problem) for idx in range(len(self.reference_energies)): self.assertAlmostEqual(results.computed_vibrational_energies[idx], self.reference_energies[idx], places=4)
def test_vqe_uvccsd_factory(self): """ Test with VQE plus UVCCSD """ optimizer = COBYLA(maxiter=5000) solver = VQEUVCCFactory(QuantumInstance( BasicAer.get_backend('statevector_simulator')), optimizer=optimizer) gsc = GroundStateEigensolver(self.qubit_converter, solver) esc = QEOM(gsc, 'sd') results = esc.solve(self.vibrational_problem) for idx in range(len(self.reference_energies)): self.assertAlmostEqual(results.computed_vibrational_energies[idx], self.reference_energies[idx], places=1)
def test_vqe_uvccsd_factory(self): """Test with VQE plus UVCCSD""" optimizer = COBYLA(maxiter=5000) solver = VQEUVCCFactory( QuantumInstance( qiskit.Aer.get_backend("aer_simulator_statevector")), optimizer=optimizer, ) gsc = GroundStateEigensolver(self.qubit_converter, solver) esc = QEOM(gsc, "sd") results = esc.solve(self.vibrational_problem) for idx, energy in enumerate(self.reference_energies): self.assertAlmostEqual(results.computed_vibrational_energies[idx], energy, places=1)
def test_vqe_uvccsd_factory(self): """Test with VQE plus UVCCSD""" optimizer = COBYLA(maxiter=5000) quantum_instance = QuantumInstance( backend=qiskit.BasicAer.get_backend("statevector_simulator"), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ) solver = VQEUVCCFactory(quantum_instance=quantum_instance, optimizer=optimizer) gsc = GroundStateEigensolver(self.qubit_converter, solver) esc = QEOM(gsc, "sd") results = esc.solve(self.vibrational_problem) for idx, energy in enumerate(self.reference_energies): self.assertAlmostEqual(results.computed_vibrational_energies[idx], energy, places=1)
def test_vqe_uvcc_factory_with_user_initial_point(self): """Test VQEUVCCFactory when using it with a user defined initial point.""" initial_point = np.asarray( [-7.35250290e-05, -9.73079292e-02, -5.43346282e-05]) optimizer = COBYLA(maxiter=1) quantum_instance = QuantumInstance( backend=qiskit.BasicAer.get_backend("statevector_simulator"), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ) solver = VQEUVCCFactory(quantum_instance=quantum_instance, optimizer=optimizer, initial_point=initial_point) gsc = GroundStateEigensolver(self.qubit_converter, solver) esc = QEOM(gsc, "sd") results = esc.solve(self.vibrational_problem) np.testing.assert_array_almost_equal( results.raw_result.ground_state_raw_result.optimal_point, initial_point)