Esempio n. 1
0
    def test_kronecker_prod(self):
        matrix = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]],
                              dtype=torch.double)

        expect = torch.tensor(
            [
                [
                    [-24, -28, -28, -32],
                    [-32, -36, -36, -40],
                    [-32, -36, -36, -40],
                    [-40, -44, -44, -48],
                ],
                [
                    [10, 16, 16, 24],
                    [22, 28, 32, 40],
                    [22, 32, 28, 40],
                    [42, 52, 52, 64],
                ],
            ],
            dtype=torch.double,
        )

        self.assertTensorsEqual(cplx.kronecker_prod(matrix, matrix),
                                expect,
                                msg="Kronecker product failed!")
Esempio n. 2
0
    def rotate_rho(self, basis, space, Z, unitaries, rho=None):
        r"""Computes the density matrix rotated into some basis

        :param basis: The basis into which to rotate the density matrix
        :type basis: numpy.ndarray
        :param space: The Hilbert space of the system
        :type space: torch.Tensor
        :param unitaries: A dictionary of unitary matrices associated with
                          rotation into each basis
        :type unitaries: dict[str, torch.Tensor]
        :returns: The rotated density matrix
        :rtype: torch.Tensor
        """
        rho = self.rhoRBM(space, space) if rho is None else rho

        unitaries = {k: v for k, v in unitaries.items()}
        us = [unitaries[b] for b in basis]
        if len(us) == 0:
            return rho

        # After ensuring there is more than one measurement, compute the
        # composite unitary by repeated Kronecker products
        U = us[0]
        for index in range(len(us) - 1):
            U = cplx.kronecker_prod(U, us[index + 1])

        U = U.to(rho)
        U_dag = cplx.conjugate(U)

        rot_rho = cplx.matmul(rho, U_dag)
        rot_rho_ = cplx.matmul(U, rot_rho)

        return rot_rho_
Esempio n. 3
0
 def test_kronecker_prod_error_large(self):
     # take KronProd of 2 rank 4 tensors, instead of rank 3
     tensor = torch.arange(16, dtype=torch.double).reshape(2, 2, 2, 2)
     with self.assertRaises(ValueError):
         cplx.kronecker_prod(tensor, tensor)
Esempio n. 4
0
 def test_kronecker_prod_error_small(self):
     # take KronProd of 2 rank 2 tensors, instead of rank 3
     tensor = torch.tensor([[1, 2], [3, 4]], dtype=torch.double)
     with self.assertRaises(ValueError):
         cplx.kronecker_prod(tensor, tensor)