def test_backend_change(self, user_expectation): """Test that VQE works when backend changes.""" vqe = VQE( ansatz=TwoLocal(rotation_blocks=["ry", "rz"], entanglement_blocks="cz"), optimizer=SLSQP(maxiter=2), expectation=user_expectation, quantum_instance=BasicAer.get_backend("statevector_simulator"), ) result0 = vqe.compute_minimum_eigenvalue(operator=self.h2_op) if user_expectation is not None: with self.subTest("User expectation kept."): self.assertEqual(vqe.expectation, user_expectation) vqe.quantum_instance = BasicAer.get_backend("qasm_simulator") # works also if no expectation is set, since it will be determined automatically result1 = vqe.compute_minimum_eigenvalue(operator=self.h2_op) if user_expectation is not None: with self.subTest( "Change backend with user expectation, it is kept."): self.assertEqual(vqe.expectation, user_expectation) with self.subTest("Check results."): self.assertEqual(len(result0.optimal_point), len(result1.optimal_point))
def test_reuse(self): """Test re-using a VQE algorithm instance.""" vqe = VQE() with self.subTest(msg='assert running empty raises AlgorithmError'): with self.assertRaises(AlgorithmError): _ = vqe.run() var_form = TwoLocal(rotation_blocks=['ry', 'rz'], entanglement_blocks='cz') vqe.var_form = var_form with self.subTest(msg='assert missing operator raises AlgorithmError'): with self.assertRaises(AlgorithmError): _ = vqe.run() vqe.operator = self.h2_op with self.subTest(msg='assert missing backend raises AlgorithmError'): with self.assertRaises(AlgorithmError): _ = vqe.run() vqe.quantum_instance = self.statevector_simulator with self.subTest(msg='assert VQE works once all info is available'): result = vqe.run() self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=5) 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 = vqe.compute_minimum_eigenvalue(operator) self.assertAlmostEqual(result.eigenvalue.real, -1.0, places=5)
def test_reuse(self): """Test re-using a VQE algorithm instance.""" vqe = VQE() with self.subTest(msg="assert running empty raises AlgorithmError"): with self.assertRaises(AlgorithmError): _ = vqe.compute_minimum_eigenvalue(operator=self.h2_op) ansatz = TwoLocal(rotation_blocks=["ry", "rz"], entanglement_blocks="cz") vqe.ansatz = ansatz with self.subTest(msg="assert missing operator raises AlgorithmError"): with self.assertRaises(AlgorithmError): _ = vqe.compute_minimum_eigenvalue(operator=self.h2_op) vqe.expectation = MatrixExpectation() vqe.quantum_instance = self.statevector_simulator with self.subTest(msg="assert VQE works once all info is available"): result = vqe.compute_minimum_eigenvalue(operator=self.h2_op) self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=5) 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 = vqe.compute_minimum_eigenvalue(operator=operator) self.assertAlmostEqual(result.eigenvalue.real, -1.0, places=5)
def test_backend_change(self, user_expectation): """Test that VQE works when backend changes.""" vqe = VQE( var_form=TwoLocal(rotation_blocks=['ry', 'rz'], entanglement_blocks='cz'), optimizer=SLSQP(maxiter=2), expectation=user_expectation, quantum_instance=BasicAer.get_backend('statevector_simulator')) result0 = vqe.compute_minimum_eigenvalue(operator=self.h2_op) if user_expectation is not None: with self.subTest('User expectation kept.'): self.assertEqual(vqe.expectation, user_expectation) else: with self.subTest('Expectation created.'): self.assertIsInstance(vqe.expectation, ExpectationBase) try: vqe.quantum_instance = BasicAer.get_backend('qasm_simulator') except Exception as ex: # pylint: disable=broad-except self.fail("Failed to change backend. Error: '{}'".format(str(ex))) return result1 = vqe.compute_minimum_eigenvalue(operator=self.h2_op) if user_expectation is not None: with self.subTest( 'Change backend with user expectation, it is kept.'): self.assertEqual(vqe.expectation, user_expectation) else: with self.subTest( 'Change backend without user expectation, one created.'): self.assertIsInstance(vqe.expectation, ExpectationBase) with self.subTest('Check results.'): self.assertEqual(len(result0.optimal_point), len(result1.optimal_point))