Example #1
0
    def test_mesh(self):
        """ prints out some meshes for debugging MZI mesh"""

        # triangular mesh
        print('')
        N = 10
        M = Mesh(N, mesh_type='triangular', initialization='random', M=None)
        print('Triangular, N = {}, M = None:'.format(N))
        print(M)

        # full clements mesh
        M = Mesh(N, mesh_type='clements', initialization='random', M=None)
        print('Clements, N = {}, M = None:'.format(N))
        print(M)

        # clements mesh with custom number of layers
        M_temp = 50
        M = Mesh(N - 1,
                 mesh_type='clements',
                 initialization='random',
                 M=M_temp)
        print('Clements, N = {}, M = {}:'.format(N - 1, M_temp))

        # check if unitary with random initialization
        self.is_unitary(M.full_matrix)

        N = 4
        # check if identity matrix with zero initialization
        M = Mesh(N, mesh_type='clements', initialization='zeros', M=1)
        np.testing.assert_array_almost_equal(M.full_matrix,
                                             np.eye(N, dtype=np.complex64))
Example #2
0
    def test_real(self):

        N = 10
        mesh = Mesh(N, mesh_type='clements', initialization='real', M=None)
        print(mesh)
        R = (np.random.random(
            (N, 1)) - 0.5)  #+ 1j*(np.random.random((N, 1)) - 0.5)

        mesh.input_couple(R)
        output_values = mesh.output_values
        print(output_values)
    def test_updown_1_1(self):
        # uniform to uniform
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.one
        output_target = self.one

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)
    def test_spread_rand_rand(self):
        # random input to rand
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = npr.random((N, 1))
        output_target = normalize_vec(npr.random((N, 1)))

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)
    def test_spread_top_top(self):
        # top input to top
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.top
        output_target = self.top

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)
    def test_spread_1_mid(self):
        # uniform to middle
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.one
        output_target = self.mid

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)
    def test_updown_rand_top(self):
        # random input to top
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = npr.random((N, 1))
        output_target = self.top

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)
    def test_updown_top_mid(self):
        # top input to middle
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.top
        output_target = self.mid

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)
    def setUp(self):

        self.N = 10
        self.mesh_t = Mesh(self.N,
                           mesh_type='triangular',
                           initialization='random',
                           M=None)
        self.mesh_c_r = Mesh(self.N,
                             mesh_type='clements',
                             initialization='random',
                             M=None)
        self.mesh_c_z = Mesh(self.N,
                             mesh_type='clements',
                             initialization='zeros',
                             M=None)

        self.one = normalize_vec(np.ones((self.N, 1)))
        self.top = np.zeros((self.N, 1))
        self.top[0] = 1
        self.mid = np.zeros((self.N, 1))
        self.mid[self.N // 2] = 1
        self.bot = np.zeros((self.N, 1))
        self.bot[-1] = 1
class TestCoupling(unittest.TestCase):
    """ Code for testing the MZI controllers"""
    def setUp(self):

        self.N = 10
        self.mesh_t = Mesh(self.N,
                           mesh_type='triangular',
                           initialization='random',
                           M=None)
        self.mesh_c_r = Mesh(self.N,
                             mesh_type='clements',
                             initialization='random',
                             M=None)
        self.mesh_c_z = Mesh(self.N,
                             mesh_type='clements',
                             initialization='zeros',
                             M=None)

        self.one = normalize_vec(np.ones((self.N, 1)))
        self.top = np.zeros((self.N, 1))
        self.top[0] = 1
        self.mid = np.zeros((self.N, 1))
        self.mid[self.N // 2] = 1
        self.bot = np.zeros((self.N, 1))
        self.bot[-1] = 1

    def test_IO(self):
        """ Tests basic coupling"""

        # first on the zeros initialized clements mesh

        input_values = normalize_vec(npr.random((self.N, 1)))
        self.mesh_c_z.input_couple(input_values)
        output_values = self.mesh_c_z.output_values

        # because the matrix was initialized with zeros, output should be identical to input
        np.testing.assert_array_almost_equal(input_values, output_values)

        # same goes for the partial values within MZI
        for partial_value in self.mesh_c_z.partial_values:
            np.testing.assert_array_almost_equal(input_values, partial_value)

        # then on the random initialized clements mesh

        input_values = npr.random((self.N, ))
        self.mesh_c_r.input_couple(input_values)
        output_values = self.mesh_c_r.output_values

        # should not be equal for random initialization
        assert_array_compare(operator.__ne__, input_values, output_values)

        # same goes for the partial values within MZI
        for partial_value in self.mesh_c_r.partial_values:
            assert_array_compare(operator.__ne__, input_values, output_values)

        # finally on the random initialized triangular mesh

        input_values = normalize_vec(npr.random((self.N, 1)))
        self.mesh_t.input_couple(input_values)
        output_values = self.mesh_t.output_values

        # should not be equal for random initialization
        assert_array_compare(operator.__ne__, input_values, output_values)

        # same goes for the partial values within MZI
        for partial_value in self.mesh_t.partial_values:
            assert_array_compare(operator.__ne__, input_values, output_values)

    def check_power(self, mesh, output_target):
        # checks if output equals target
        P_out = power_vec(mesh.output_values)
        P_target = power_vec(output_target)
        error = norm(P_out - P_target) / self.N
        self.assertLess(error, EPSILON)

#### VARIOUS INPUTS TO UNIFORM OUTPUTS ####

    def test_updown_1_1(self):
        # uniform to uniform
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.one
        output_target = self.one

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_bot_1(self):
        # bottom input to uniform
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.bot
        output_target = self.one

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_mid_1(self):
        # middle input to uniform
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.mid
        output_target = self.one

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_top_1(self):
        # top input to uniform
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.top
        output_target = self.one

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_rand_1(self):
        # random input to uniform
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = npr.random((N, 1))
        output_target = self.one

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

#### VARIOUS INPUTS TO BOTTOM OUTPUTS ####

    def test_updown_1_bot(self):
        # uniform to bottom
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.one
        output_target = self.bot

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_bot_bot(self):
        # bottom input to bottom
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.bot
        output_target = self.bot

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_mid_bot(self):
        # middle input to bottom
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.mid
        output_target = self.bot

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_top_bot(self):
        # top input to bottom
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.top
        output_target = self.bot

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_rand_bot(self):
        # random input to bottom
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = npr.random((N, 1))
        output_target = self.bot

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

#### VARIOUS INPUTS TO MIDDLE OUTPUTS ####

    def test_updown_1_mid(self):
        # uniform to middle
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.one
        output_target = self.mid

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_bot_mid(self):
        # bottom input to middle
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.bot
        output_target = self.mid

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_mid_mid(self):
        # middle input to middle
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.mid
        output_target = self.mid

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_top_mid(self):
        # top input to middle
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.top
        output_target = self.mid

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_rand_mid(self):
        # random input to middle
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = npr.random((N, 1))
        output_target = self.mid

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

#### VARIOUS INPUTS TO TOP OUTPUTS ####

    def test_updown_1_top(self):
        # uniform to top
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.one
        output_target = self.top

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_bot_top(self):
        # bottom input to top
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.bot
        output_target = self.top

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_mid_top(self):
        # middle input to top
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.mid
        output_target = self.top

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_top_top(self):
        # top input to top
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.top
        output_target = self.top

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_rand_top(self):
        # random input to top
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = npr.random((N, 1))
        output_target = self.top

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

#### VARIOUS INPUTS TO RANDOM OUTPUTS ####

    def test_updown_1_rand(self):
        # uniform to rand
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.one
        output_target = normalize_vec(npr.random((N, 1)))

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_bot_rand(self):
        # bottom input to rand
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.bot
        output_target = normalize_vec(npr.random((N, 1)))

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_mid_rand(self):
        # middle input to rand
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.mid
        output_target = normalize_vec(npr.random((N, 1)))

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_top_rand(self):
        # top input to rand
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.top
        output_target = normalize_vec(npr.random((N, 1)))

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_updown_rand_rand(self):
        # random input to rand
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = npr.random((N, 1))
        output_target = normalize_vec(npr.random((N, 1)))

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='up_down')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

#### VARIOUS INPUTS TO UNIFORM OUTPUTS ####

    def test_spread_1_1(self):
        # uniform to uniform
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.one
        output_target = self.one

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_bot_1(self):
        # bottom input to uniform
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.bot
        output_target = self.one

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_mid_1(self):
        # middle input to uniform
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.mid
        output_target = self.one

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_top_1(self):
        # top input to uniform
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.top
        output_target = self.one

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_rand_1(self):
        # random input to uniform
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = npr.random((N, 1))
        output_target = self.one

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

#### VARIOUS INPUTS TO BOTTOM OUTPUTS ####

    def test_spread_1_bot(self):
        # uniform to bottom
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.one
        output_target = self.bot

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_bot_bot(self):
        # bottom input to bottom
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.bot
        output_target = self.bot

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_mid_bot(self):
        # middle input to bottom
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.mid
        output_target = self.bot

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_top_bot(self):
        # top input to bottom
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.top
        output_target = self.bot

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_rand_bot(self):
        # random input to bottom
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = npr.random((N, 1))
        output_target = self.bot

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

#### VARIOUS INPUTS TO MIDDLE OUTPUTS ####

    def test_spread_1_mid(self):
        # uniform to middle
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.one
        output_target = self.mid

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_bot_mid(self):
        # bottom input to middle
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.bot
        output_target = self.mid

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_mid_mid(self):
        # middle input to middle
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.mid
        output_target = self.mid

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_top_mid(self):
        # top input to middle
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.top
        output_target = self.mid

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_rand_mid(self):
        # random input to middle
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = npr.random((N, 1))
        output_target = self.mid

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

#### VARIOUS INPUTS TO TOP OUTPUTS ####

    def test_spread_1_top(self):
        # uniform to top
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.one
        output_target = self.top

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_bot_top(self):
        # bottom input to top
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.bot
        output_target = self.top

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_mid_top(self):
        # middle input to top
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.mid
        output_target = self.top

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_top_top(self):
        # top input to top
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.top
        output_target = self.top

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_rand_top(self):
        # random input to top
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = npr.random((N, 1))
        output_target = self.top

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)


#### VARIOUS INPUTS TO RANDOM OUTPUTS ####

    def test_spread_1_rand(self):
        # uniform to rand
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.one
        output_target = normalize_vec(npr.random((N, 1)))

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_bot_rand(self):
        # bottom input to rand
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.bot
        output_target = normalize_vec(npr.random((N, 1)))

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_mid_rand(self):
        # middle input to rand
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.mid
        output_target = normalize_vec(npr.random((N, 1)))

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_top_rand(self):
        # top input to rand
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = self.top
        output_target = normalize_vec(npr.random((N, 1)))

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)

    def test_spread_rand_rand(self):
        # random input to rand
        N = self.N
        mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)

        input_values = npr.random((N, 1))
        output_target = normalize_vec(npr.random((N, 1)))

        TO = TriangleOptimizer(mesh,
                               input_values=input_values,
                               output_target=output_target)
        TO.optimize(algorithm='spread')

        # mesh.plot_powers(); plt.show()
        self.check_power(mesh, output_target)
Example #11
0
import numpy as np
import numpy.random as npr
import matplotlib.pylab as plt

from DLA_Control import Mesh
from DLA_Control import TriangleOptimizer, ClementsOptimizer
from DLA_Control.plots import plot_bar_3d, colorbar, plot_powers
""" FIRST OPTIMIZE A TRIANGULAR MESH """

# create a triangular mesh
N = 10
mesh = Mesh(N, mesh_type='triangular', initialization='random', M=None)
print(mesh)

# comlex valued input coupling vector
input_values = np.zeros((N, 1))
input_values[-1] = 1
input_values = npr.random((N, 1))

f, (ax1, ax2) = plt.subplots(2, constrained_layout=True, figsize=(5, 5))

# couple light in and look at powers throughout mesh
mesh.input_couple(input_values)
im1 = plot_powers(mesh, ax=ax1)
colorbar(im1)
ax1.set_title('power distribution before optimizing')

# target output complex amplitude
output_target = np.ones((N, 1))

# define an optimizer over the triangular mesh
Example #12
0
from DLA_Control import TriangleOptimizer, ClementsOptimizer
from DLA_Control.plots import plot_bar_3d, colorbar, plot_powers, apply_sublabels

NR = 1
NC = 2

fig, (axes) = plt.subplots(nrows=NR, ncols=NC)#, figsize=(8.5, 5))

# # create a clements mesh
N = 30
M = 10

vmax = 3/N

uniform_vals = np.ones((N, 1))
mesh = Mesh(N, mesh_type='clements', initialization='random', M=M)
print(mesh)

for i, ax in enumerate(axes.flat):
    print("working on {}/{}".format(i+1, NR*NC))

    random_vals = npr.random((N, 1))

    mesh = Mesh(N, mesh_type='clements', initialization='random', M=M)

    CO = ClementsOptimizer(mesh, input_values=random_vals, output_target=uniform_vals)
    CO.optimize(algorithm='smart', verbose=False)

    im = plot_powers(mesh, ax=ax)
    im.set_clim(0,vmax)