diff --git a/qadence/backends/pyqtorch/convert_ops.py b/qadence/backends/pyqtorch/convert_ops.py index e1fa40229..11c672914 100644 --- a/qadence/backends/pyqtorch/convert_ops.py +++ b/qadence/backends/pyqtorch/convert_ops.py @@ -201,7 +201,12 @@ def __init__( elif isinstance(block.generator, Tensor): m = block.generator.to(dtype=cdouble) hmat = block_to_tensor( - MatrixBlock(m, qubit_support=block.qubit_support), + MatrixBlock( + m, + qubit_support=block.qubit_support, + check_unitary=False, + check_hermitian=True, + ), qubit_support=self.qubit_support, use_full_support=False, ) diff --git a/qadence/blocks/matrix.py b/qadence/blocks/matrix.py index d8b91684a..a146ba5ce 100644 --- a/qadence/blocks/matrix.py +++ b/qadence/blocks/matrix.py @@ -60,7 +60,13 @@ class MatrixBlock(PrimitiveBlock): name = "MatrixBlock" matrix: torch.Tensor - def __init__(self, matrix: torch.Tensor | np.ndarray, qubit_support: tuple[int, ...]) -> None: + def __init__( + self, + matrix: torch.Tensor | np.ndarray, + qubit_support: tuple[int, ...], + check_unitary: bool = True, + check_hermitian: bool = False, + ) -> None: if isinstance(matrix, np.ndarray): matrix = torch.tensor(matrix) if matrix.ndim == 3 and matrix.size(0) == 1: @@ -69,10 +75,12 @@ def __init__(self, matrix: torch.Tensor | np.ndarray, qubit_support: tuple[int, raise TypeError("Please provide a 2D matrix.") if not self.is_square(matrix): raise TypeError("Please provide a square matrix.") - if not self.is_hermitian(matrix): - logger.warning("Provided matrix is not hermitian.") - if not self.is_unitary(matrix): - logger.warning("Provided matrix is not unitary.") + if check_hermitian: + if not self.is_hermitian(matrix): + logger.warning("Provided matrix is not hermitian.") + if check_unitary: + if not self.is_unitary(matrix): + logger.warning("Provided matrix is not unitary.") self.matrix = matrix.clone() super().__init__(qubit_support) diff --git a/qadence/ml_tools/train_grad.py b/qadence/ml_tools/train_grad.py index c607a8c1f..5f512386e 100644 --- a/qadence/ml_tools/train_grad.py +++ b/qadence/ml_tools/train_grad.py @@ -282,6 +282,8 @@ def loss_fn(model: torch.nn.Module, data: torch.Tensor) -> tuple[torch.Tensor, d xs = next(dl_iter) if dataloader is not None else None # type: ignore[arg-type] xs_to_device = data_to_device(xs, device=device, dtype=data_dtype) loss, metrics, *_ = loss_fn(model, xs_to_device) + if dataloader is None: + loss = loss.item() if iteration % config.print_every == 0 and config.verbose: print_metrics(loss, metrics, iteration)