def test_self_loop_raises_error(self): """Test graphs with self loop raises ValueError""" g = nx.complete_graph(3).to_directed() edge_weight_data = {edge: (i + 1) * 0.5 for i, edge in enumerate(g.edges)} for k, v in edge_weight_data.items(): g[k[0]][k[1]]["weight"] = v g.add_edge(1, 1) # add self loop with pytest.raises(ValueError, match="Graph contains self-loops"): loss_hamiltonian(g)
def test_loss_hamiltonian_complete(self): """Test if the loss_hamiltonian function returns the expected result on a manually-calculated example of a 3-node complete digraph""" g = nx.complete_graph(3).to_directed() edge_weight_data = { edge: (i + 1) * 0.5 for i, edge in enumerate(g.edges) } for k, v in edge_weight_data.items(): g[k[0]][k[1]]["weight"] = v h = loss_hamiltonian(g) expected_ops = [ qml.PauliZ(0), qml.PauliZ(1), qml.PauliZ(2), qml.PauliZ(3), qml.PauliZ(4), qml.PauliZ(5), ] expected_coeffs = [ np.log(0.5), np.log(1), np.log(1.5), np.log(2), np.log(2.5), np.log(3) ] assert expected_coeffs == h.coeffs assert all( [op.wires == exp.wires for op, exp in zip(h.ops, expected_ops)]) assert all( [type(op) is type(exp) for op, exp in zip(h.ops, expected_ops)])
def test_missing_edge_weight_data_raises_error(self): """Test graphs with no edge weight data raises `KeyError`""" g = nx.complete_graph(3).to_directed() with pytest.raises(KeyError, match="does not contain weight data"): loss_hamiltonian(g)