def test(self):
     for units in DIMS:
         identity = np.eye(units)
         rdlayer = RMNumpy(units=units)
         fields = rdlayer.propagate(identity)
         inverse_fields = rdlayer.inverse_propagate(rdlayer.transform(identity))
         self.assertAllClose(fields, inverse_fields)
 def test_explicit(self):
     for units in DIMS:
         identity = random_gaussian_batch(batch_size=units, units=units)
         rdlayer = RMNumpy(units=units)
         fields = rdlayer.propagate(identity, explicit=True)
         inverse_fields = rdlayer.inverse_propagate(rdlayer.transform(identity), explicit=True)
         self.assertAllClose(fields, inverse_fields)
 def test(self):
     for units in DIMS:
         batch = random_gaussian_batch(batch_size=units, units=units)
         rdlayer = RMNumpy(units=units)
         fields = rdlayer.propagate(batch)
         inverse_fields = rdlayer.inverse_propagate(rdlayer.transform(batch))
         self.assertAllClose(fields, inverse_fields)
Example #4
0
 def test_rm_hadamard(self):
     for units in TEST_DIMENSIONS:
         for bs_error in (0, 0.1):
             rm_np = RMNumpy(units=units, hadamard=True, bs_error=bs_error)
             # set the testing to true and rebuild layer!
             rm_np._setup(None, testing=True)
             rm_tf = MeshLayer(rm_np.mesh.model)
             self.assertAllClose(rm_tf.matrix, rm_np.matrix)
             self.assertAllClose(rm_tf.matrix.conj().T,
                                 rm_np.inverse_matrix)
Example #5
0
 def test_rm(self):
     for units in TEST_DIMENSIONS:
         for bs_error in (0, 0.1):
             for hadamard in (True, False):
                 rm_np = RMNumpy(units=units,
                                 hadamard=hadamard,
                                 bs_error=bs_error)
                 test_correspondence(self, rm_np)
Example #6
0
 def test_rm(self):
     for units, bs_error, hadamard in itertools.product(TEST_DIMENSIONS, (0, 0.1), (True, False)):
         with self.subTest(units=units, bs_error=bs_error, hadamard=hadamard):
             rm_np = RMNumpy(
                 units=units,
                 hadamard=hadamard,
                 bs_error=bs_error
             )
             test_correspondence(self, rm_np)
    def test_mask_pcf(self):
        def random_mask_init(x, use_torch=False):
            x_mask = np.ones_like(x).flatten()
            x_mask[:x_mask.size // 2] = 0
            np.random.shuffle(x_mask)
            x_mask = np.reshape(x_mask, x.shape)
            fix_phase = fix_phase_torch if use_torch else fix_phase_tf
            return (x, fix_phase(x, x_mask)), x_mask

        for units, bs_error, hadamard in TEST_CASES:
            with self.subTest(units=units,
                              bs_error=bs_error,
                              hadamard=hadamard):
                np.random.seed(0)
                target = unitary_group.rvs(units, random_state=0)
                identity_matrix = tf.eye(units, dtype=TF_COMPLEX)
                rm_numpy = RMNumpy(units)
                theta_init, theta_mask = random_mask_init(rm_numpy.theta)
                phi_init, phi_mask = random_mask_init(rm_numpy.phi)
                gamma_init, gamma_mask = random_mask_init(rm_numpy.gamma)
                mesh_model = RectangularMeshModel(units=units,
                                                  hadamard=hadamard,
                                                  bs_error=bs_error,
                                                  theta_init=theta_init,
                                                  phi_init=phi_init,
                                                  gamma_init=gamma_init)
                rm = MeshLayer(mesh_model)
                with tf.GradientTape() as tape:
                    loss = complex_mse(rm(identity_matrix), target)
                grads = tape.gradient(loss, rm.trainable_variables)
                # t_loss = torch.nn.MSELoss(reduction='mean')
                # rm_torch = RMTorch(
                #     units=units,
                #     hadamard=hadamard,
                #     bs_error=bs_error,
                #     theta_init=random_mask_init(rm_numpy.theta, use_torch=True)[0],
                #     phi_init=random_mask_init(rm_numpy.phi, use_torch=True)[0],
                #     gamma_init=rm_numpy.gamma
                # )
                #
                # torch_loss = t_loss(torch.view_as_real(rm_torch(torch.eye(units, dtype=torch.cfloat))),
                #                     torch.view_as_real(torch.as_tensor(target, dtype=torch.cfloat)))
                # var = torch_loss.sum()
                # var.backward()
                # print(torch.autograd.grad(var, [rm_torch.theta]))
                theta_grad, phi_grad, gamma_grad = grads[0].numpy(
                ), grads[1].numpy(), grads[2].numpy()
                theta_grad_zeros = theta_grad[np.where(theta_mask == 0)]
                phi_grad_zeros = phi_grad[np.where(phi_mask == 0)]
                gamma_grad_zeros = gamma_grad[np.where(gamma_mask == 0)]
                self.assertAllClose(theta_grad_zeros,
                                    np.zeros_like(theta_grad_zeros))
                self.assertAllClose(phi_grad_zeros,
                                    np.zeros_like(phi_grad_zeros))
                self.assertAllClose(gamma_grad_zeros,
                                    np.zeros_like(gamma_grad_zeros))
    def test_tri_pcf(self):
        def random_mask_init(x, phase_range):
            return x, tri_phase_tf(phase_range)

        for units, bs_error, hadamard in TEST_CASES:
            with self.subTest(units=units,
                              bs_error=bs_error,
                              hadamard=hadamard):
                np.random.seed(0)
                rm_numpy = RMNumpy(units)
                theta_init = random_mask_init(rm_numpy.theta, np.pi)
                phi_init = random_mask_init(rm_numpy.phi, 2 * np.pi)
                mesh_model = RectangularMeshModel(units=units,
                                                  hadamard=hadamard,
                                                  bs_error=bs_error,
                                                  theta_init=theta_init,
                                                  phi_init=phi_init,
                                                  gamma_init=rm_numpy.gamma)
                rm = MeshLayer(mesh_model)
                phases, layers = rm.phases_and_layers
                theta_transformed = phases.theta.param
                phi_transformed = phases.phi.param
                self.assertAllLessEqual(theta_transformed, np.pi)
                self.assertAllLessEqual(phi_transformed, 2 * np.pi)
 def test(self):
     for units in TEST_DIMENSIONS:
         rm = RMNumpy(units=units)
         reconstructed_rm = clements_decomposition(rm.matrix)
         self.assertAllClose(rm.matrix, reconstructed_rm.matrix)
 def test_rm(self):
     for units in TEST_DIMENSIONS:
         rm = RMNumpy(units=units, basis='sm')
         reconstructed_rm = parallel_nullification(rm)
         self.assertAllClose(rm.matrix, reconstructed_rm.matrix)