Exemplo n.º 1
0
 def test_set_optimizer_to_none(self):
     """Tests that setting the optimizer to None results in the default behavior"""
     vqd = VQD(
         k=1,
         ansatz=self.ryrz_wavefunction,
         optimizer=L_BFGS_B(),
         quantum_instance=self.statevector_simulator,
     )
     vqd.optimizer = None
     self.assertIsInstance(vqd.optimizer, SLSQP)
Exemplo n.º 2
0
    def test_vqd_optimizer(self):
        """Test running same VQD twice to re-use optimizer, then switch optimizer"""
        vqd = VQD(
            k=2,
            optimizer=SLSQP(),
            quantum_instance=QuantumInstance(
                BasicAer.get_backend("statevector_simulator")),
        )

        def run_check():
            result = vqd.compute_eigenvalues(operator=self.h2_op)
            np.testing.assert_array_almost_equal(result.eigenvalues.real,
                                                 self.h2_energy_excited,
                                                 decimal=3)

        run_check()

        with self.subTest("Optimizer re-use"):
            run_check()

        with self.subTest("Optimizer replace"):
            vqd.optimizer = L_BFGS_B()
            run_check()