def test_expand_pauli(self): """Test the expansion of a Pauli observable""" obs = qml.PauliX(0) @ qml.PauliY(1) m = MeasurementProcess(Expectation, obs=obs) tape = m.expand() assert len(tape.operations) == 4 assert tape.operations[0].name == "Hadamard" assert tape.operations[0].wires.tolist() == [0] assert tape.operations[1].name == "PauliZ" assert tape.operations[1].wires.tolist() == [1] assert tape.operations[2].name == "S" assert tape.operations[2].wires.tolist() == [1] assert tape.operations[3].name == "Hadamard" assert tape.operations[3].wires.tolist() == [1] assert len(tape.measurements) == 1 assert tape.measurements[0].return_type is Expectation assert tape.measurements[0].wires.tolist() == [0, 1] assert np.all(tape.measurements[0].eigvals == np.array([1, -1, -1, 1]))
def test_expand_hermitian(self, tol): """Test the expansion of an hermitian observable""" H = np.array([[1, 2], [2, 4]]) obs = qml.Hermitian(H, wires=["a"]) m = MeasurementProcess(Expectation, obs=obs) tape = m.expand() assert len(tape.operations) == 1 assert tape.operations[0].name == "QubitUnitary" assert tape.operations[0].wires.tolist() == ["a"] assert np.allclose( tape.operations[0].parameters[0], np.array([[-2, 1], [1, 2]]) / np.sqrt(5), atol=tol, rtol=0, ) assert len(tape.measurements) == 1 assert tape.measurements[0].return_type is Expectation assert tape.measurements[0].wires.tolist() == ["a"] assert np.all(tape.measurements[0].eigvals == np.array([0, 5]))