Skip to content

Commit

Permalink
Merge branch 'main' into gt/ml-flow
Browse files Browse the repository at this point in the history
  • Loading branch information
smitchaudhary committed Jul 19, 2024
2 parents eb9a98c + 921e659 commit a0706bb
Show file tree
Hide file tree
Showing 3 changed files with 21 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
2 changes: 2 additions & 0 deletions qadence/ml_tools/train_grad.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit a0706bb

Please sign in to comment.