def test_xy_mixer_type_error(self): """Tests that the XY mixer throws the correct error""" graph = [(0, 1), (1, 2)] with pytest.raises(ValueError, match=r"Input graph must be a nx.Graph object, got list"): qaoa.xy_mixer(graph)
def test_xy_mixer_output(self, graph, target_hamiltonian): """Tests that the output of the XY mixer is correct""" mixer_hamiltonian = qaoa.xy_mixer(graph) mixer_coeffs = mixer_hamiltonian.coeffs mixer_ops = [i.name for i in mixer_hamiltonian.ops] mixer_wires = [i.wires for i in mixer_hamiltonian.ops] target_coeffs = target_hamiltonian.coeffs target_ops = [i.name for i in target_hamiltonian.ops] target_wires = [i.wires for i in target_hamiltonian.ops] assert mixer_coeffs == target_coeffs assert mixer_ops == target_ops assert mixer_wires == target_wires
class TestLayers: """Tests that the cost and mixer layers are being constructed properly""" def test_mixer_layer_errors(self): """Tests that the mixer layer is throwing the correct errors""" hamiltonian = [[1, 1], [1, 1]] with pytest.raises( ValueError, match=r"hamiltonian must be of type pennylane.Hamiltonian"): qaoa.mixer_layer(0.1, hamiltonian) def test_cost_layer_errors(self): """Tests that the cost layer is throwing the correct errors""" hamiltonian = [[1, 1], [1, 1]] with pytest.raises( ValueError, match=r"hamiltonian must be of type pennylane.Hamiltonian"): qaoa.cost_layer(0.1, hamiltonian) hamiltonian = qml.Hamiltonian([1, 1], [qml.PauliZ(0), qml.PauliX(1)]) with pytest.raises( ValueError, match= r"hamiltonian must be written only in terms of PauliZ and Identity gates" ): qaoa.cost_layer(0.1, hamiltonian) @pytest.mark.parametrize( ("mixer", "gates"), [[ qml.Hamiltonian([1, 1], [qml.PauliX(0), qml.PauliX(1)]), [qml.PauliRot(2, "X", wires=[0]), qml.PauliRot(2, "X", wires=[1])] ], [ qaoa.xy_mixer(Graph([(0, 1), (1, 2), (2, 0)])), [ qml.PauliRot(1, "XX", wires=[0, 1]), qml.PauliRot(1, "YY", wires=[0, 1]), qml.PauliRot(1, "XX", wires=[0, 2]), qml.PauliRot(1, "YY", wires=[0, 2]), qml.PauliRot(1, "XX", wires=[1, 2]), qml.PauliRot(1, "YY", wires=[1, 2]) ] ]]) def test_mixer_layer_output(self, mixer, gates): """Tests that the gates of the mixer layer are correct""" alpha = 1 with qml._queuing.OperationRecorder() as rec: qaoa.mixer_layer(alpha, mixer) for i, j in zip(rec.operations, gates): prep = [i.name, i.parameters, i.wires] target = [j.name, j.parameters, j.wires] assert prep == target @pytest.mark.parametrize( ("cost", "gates"), [[ qml.Hamiltonian([1, 1], [qml.PauliZ(0), qml.PauliZ(1)]), [qml.PauliRot(2, "Z", wires=[0]), qml.PauliRot(2, "Z", wires=[1])] ], [ qaoa.maxcut(Graph([(0, 1), (1, 2), (2, 0)]))[0], [ qml.PauliRot(1, "ZZ", wires=[0, 1]), qml.PauliRot(1, "ZZ", wires=[0, 2]), qml.PauliRot(1, "ZZ", wires=[1, 2]) ] ]]) def test_cost_layer_output(self, cost, gates): """Tests that the gates of the cost layer is correct""" gamma = 1 with qml._queuing.OperationRecorder() as rec: qaoa.cost_layer(gamma, cost) for i, j in zip(rec.operations, gates): prep = [i.name, i.parameters, i.wires] target = [j.name, j.parameters, j.wires] assert prep == target