def get_grad(self, params: Sequence[float] = []) -> np.ndarray: """Returns the gradient for this gate, see Gate for more info.""" self.check_parameters(params) H = dot_product(params, self.sigmav) _, dU = dexpmv(H, self.sigmav) return dU
def get_unitary_and_grad( self, params: Sequence[float] = [], ) -> tuple[UnitaryMatrix, np.ndarray]: """Returns the unitary and gradient, see Gate for more info.""" self.check_parameters(params) H = dot_product(params, self.sigmav) U, dU = dexpmv(H, self.sigmav) return UnitaryMatrix(U, check_arguments=False), dU
def test_single(self, alpha: np.ndarray) -> None: paulis = PauliMatrices(2) H = paulis.dot_product(alpha) for p in paulis: F0, dF0 = dexpm_exact(H, p) F1, dF1 = dexpmv(H, p) assert np.allclose(F0, F1) assert np.allclose(dF0, dF1)
def test_vector(self, alpha: np.ndarray) -> None: paulis = PauliMatrices(2) H = paulis.dot_product(alpha) dFs0 = [] for p in paulis: _, dF = dexpm_exact(H, p) dFs0.append(dF) dFs0_np = np.array(dFs0) _, dFs1 = dexpmv(H, paulis.get_numpy()) assert np.allclose(dFs0_np, dFs1)
def test_invalid(self) -> None: with pytest.raises(Exception): dexpmv(0, 0) # type: ignore with pytest.raises(Exception): dexpmv(0, [1, 0]) # type: ignore with pytest.raises(Exception): dexpmv([1, 0], 0) # type: ignore with pytest.raises(Exception): dexpmv([1, 0], [1, 0]) # type: ignore I = np.identity(2) with pytest.raises(Exception): dexpmv(I, 0) # type: ignore with pytest.raises(Exception): dexpmv(0, I) # type: ignore with pytest.raises(Exception): dexpmv(I, [1, 0]) # type: ignore with pytest.raises(Exception): dexpmv([1, 0], I) # type: ignore