def test_bitstring_cost(self): "Check the bitstring sampling function" vqa = VQA(num_qubits=1, cost_method="BITSTRING") vqa.add_block(VQABlock(qutip.sigmax())) # target the |1> state by giving the "1" string a cost of 0 vqa.cost_func = lambda s: 1 - int(s) res = vqa.optimize_parameters(initial=[np.pi / 2 + 1e-3]) assert res.get_top_bitstring() == "|1>"
def test_layer_by_layer(self): """ tests trivial optimization going layer-by-layer """ vqa = VQA(num_qubits=1, cost_method="STATE", num_layers=4) block = VQABlock(qutip.sigmax()) vqa.add_block(block) vqa.cost_func = lambda s: 1 - s.overlap(qutip.basis(2, 1)).real res = vqa.optimize_parameters( initial=[np.pi / 2, 0, 0, 0], layer_by_layer=True ) assert res.get_top_bitstring() == "|1>"
def test_trivial_optimization(self): """ tests trivial optimization case where the initial conditinos are already optimal for the problem """ block = VQABlock(qutip.sigmax()) # test the STATE type of cost function vqa = VQA(num_qubits=1, cost_method="STATE") vqa.add_block(block) # try to reach the |1> state from the |0> state vqa.cost_func = lambda s: 1 - s.overlap(qutip.basis(2, 1)).real res = vqa.optimize_parameters(initial=[np.pi / 2]) assert res.get_top_bitstring() == "|1>"
def test_plot(self, todo): """ Check plotting function returns without error """ # Only test on environments that have the matplotlib dependency try: import matplotlib.pyplot as plt except Exception: return True vqa = VQA(num_qubits=4, num_layers=1, cost_method="STATE") for i in range(4): vqa.add_block(VQABlock("X", targets=[i])) vqa.cost_func = lambda s: 0 res = vqa.optimize_parameters() res.plot(top_ten=todo, display=False)