Esempio n. 1
0
    def test_reuse(self):
        """Test re-using a VQD algorithm instance."""
        vqd = VQD(k=1)
        with self.subTest(msg="assert running empty raises AlgorithmError"):
            with self.assertRaises(AlgorithmError):
                _ = vqd.compute_eigenvalues(operator=self.h2_op)

        ansatz = TwoLocal(rotation_blocks=["ry", "rz"],
                          entanglement_blocks="cz")
        vqd.ansatz = ansatz
        with self.subTest(msg="assert missing operator raises AlgorithmError"):
            with self.assertRaises(AlgorithmError):
                _ = vqd.compute_eigenvalues(operator=self.h2_op)

        vqd.expectation = MatrixExpectation()
        vqd.quantum_instance = self.statevector_simulator
        with self.subTest(msg="assert VQE works once all info is available"):
            result = vqd.compute_eigenvalues(operator=self.h2_op)
            np.testing.assert_array_almost_equal(result.eigenvalues.real,
                                                 self.h2_energy,
                                                 decimal=2)

        operator = PrimitiveOp(
            np.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 2, 0], [0, 0, 0,
                                                                  3]]))

        with self.subTest(msg="assert minimum eigensolver interface works"):
            result = vqd.compute_eigenvalues(operator=operator)
            self.assertAlmostEqual(result.eigenvalues.real[0], -1.0, places=5)
Esempio n. 2
0
 def test_set_ansatz_to_none(self):
     """Tests that setting the ansatz 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.ansatz = None
     self.assertIsInstance(vqd.ansatz, RealAmplitudes)