Ejemplo n.º 1
0
 def test_parameterized_hamiltonian_frechet_derivative(self):
     """
     Test gradient-based optimization on parameterized Hamiltonian blocks
     """
     vqa = VQA(num_qubits=1)
     vqa.cost_observable = qutip.sigmaz()
     block = VQABlock(ParameterizedHamiltonian([qutip.sigmax()]))
     vqa.add_block(block)
     res = vqa.optimize_parameters(
         initial=[np.pi / 2 + 0.2], method="BFGS", use_jac=True
     )
     assert res.get_top_bitstring() == "|1>"
Ejemplo n.º 2
0
 def test_bfgs_optimization(self, use_jac):
     """
     test bfgs optimizer, starting from a close initial guess
     """
     block = VQABlock(qutip.sigmax())
     vqa = VQA(num_qubits=1, cost_method="OBSERVABLE")
     vqa.add_block(block)
     # try to reach the |1> state from the |0> state
     vqa.cost_observable = qutip.sigmaz()
     res = vqa.optimize_parameters(
         initial=[np.pi / 2 + 0.2], method="BFGS", use_jac=use_jac
     )
     assert res.get_top_bitstring() == "|1>"
     # check we actually found the function minimum
     assert round(res.res.x[0], 2) == round(np.pi / 2, 2)
Ejemplo n.º 3
0
 def test_optimization_errors(self):
     """
     Tests for value errors relating to optimization procedure
     """
     vqa = VQA(num_qubits=1)
     vqa.add_block(VQABlock(qutip.sigmax()))
     vqa.cost_observable = qutip.sigmax()
     with pytest.raises(ValueError):
         # Invalid initialization string
         res = vqa.optimize_parameters(initial="something else")
     with pytest.raises(ValueError):
         # Incorrect number of parameters
         res = vqa.optimize_parameters(initial=[1, 1])
     # Valid initialization string
     res = vqa.optimize_parameters(initial="ones")
     assert res is not None