Skip to content

Commit

Permalink
[Feature] Toggle MatrixBlock warnings (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmoutinho authored Jul 18, 2024
1 parent 7d96d1c commit 921e659
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
7 changes: 6 additions & 1 deletion qadence/backends/pyqtorch/convert_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
18 changes: 13 additions & 5 deletions qadence/blocks/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand Down

0 comments on commit 921e659

Please sign in to comment.