Beispiel #1
0
def test_shape_sanity_check(in_dim_base, dim1, dim2, num_nodes, bond_dim):
    model = Sequential([
        Input(in_dim_base**num_nodes),
        DenseMPO(dim1**num_nodes, num_nodes=num_nodes, bond_dim=bond_dim),
        DenseMPO(dim2**num_nodes, num_nodes=num_nodes, bond_dim=bond_dim),
    ])
    # Hard code batch size.
    result = model.predict(np.ones((32, in_dim_base**num_nodes)))
    assert result.shape == (32, dim2**num_nodes)
Beispiel #2
0
def test_config(make_model):
    # Disable the redefined-outer-name violation in this function
    # pylint: disable=redefined-outer-name
    model = make_model

    expected_num_parameters = model.layers[0].count_params()

    # Serialize model and use config to create new layer
    model_config = model.get_config()
    layer_config = model_config['layers'][1]['config']
    if 'mpo' in model.layers[0].name:
        new_model = DenseMPO.from_config(layer_config)
    elif 'decomp' in model.layers[0].name:
        new_model = DenseDecomp.from_config(layer_config)
    elif 'condenser' in model.layers[0].name:
        new_model = DenseCondenser.from_config(layer_config)
    elif 'expander' in model.layers[0].name:
        new_model = DenseExpander.from_config(layer_config)
    elif 'entangler' in model.layers[0].name:
        new_model = DenseEntangler.from_config(layer_config)

    # Build the layer so we can count params below
    new_model.build(layer_config['batch_input_shape'])

    # Check that original layer had same num params as layer built from config
    np.testing.assert_equal(expected_num_parameters, new_model.count_params())
Beispiel #3
0
def test_mpo_num_parameters(dummy_data):
    # Disable the redefined-outer-name violation in this function
    # pylint: disable=redefined-outer-name
    data, _ = dummy_data
    output_dim = data.shape[1]
    num_nodes = int(math.log(data.shape[1], 8))
    bond_dim = 8

    model = Sequential()
    model.add(
        DenseMPO(output_dim,
                 num_nodes=num_nodes,
                 bond_dim=bond_dim,
                 use_bias=True,
                 activation='relu',
                 input_shape=(data.shape[1], )))

    in_leg_dim = math.ceil(data.shape[1]**(1. / num_nodes))
    out_leg_dim = math.ceil(output_dim**(1. / num_nodes))

    # num_params = num_edge_node_params + num_middle_node_params + bias_params
    expected_num_parameters = (2 * in_leg_dim * bond_dim * out_leg_dim) + (
        (num_nodes - 2) * in_leg_dim * bond_dim * bond_dim *
        out_leg_dim) + output_dim

    np.testing.assert_equal(expected_num_parameters, model.count_params())
Beispiel #4
0
def make_high_dim_model(high_dim_data, request):
    # Disable the redefined-outer-name violation in this function
    # pylint: disable=redefined-outer-name
    data = high_dim_data

    if request.param == 'DenseMPO':
        model = Sequential()
        model.add(
            DenseMPO(data.shape[-1],
                     num_nodes=int(math.log(int(data.shape[-1]), 8)),
                     bond_dim=8,
                     use_bias=True,
                     activation='relu',
                     input_shape=(data.shape[-1], )))
    elif request.param == 'DenseDecomp':
        model = Sequential()
        model.add(
            DenseDecomp(512,
                        decomp_size=128,
                        use_bias=True,
                        activation='relu',
                        input_shape=(data.shape[-1], )))
    elif request.param == 'DenseCondenser':
        model = Sequential()
        model.add(
            DenseCondenser(exp_base=2,
                           num_nodes=3,
                           use_bias=True,
                           activation='relu',
                           input_shape=(data.shape[-1], )))
    elif request.param == 'DenseExpander':
        model = Sequential()
        model.add(
            DenseExpander(exp_base=2,
                          num_nodes=3,
                          use_bias=True,
                          activation='relu',
                          input_shape=(data.shape[-1], )))
    elif request.param == 'DenseEntangler':
        num_legs = 3
        leg_dim = round(data.shape[-1]**(1. / num_legs))
        assert leg_dim**num_legs == data.shape[-1]

        model = Sequential()
        model.add(
            DenseEntangler(leg_dim**num_legs,
                           num_legs=num_legs,
                           num_levels=3,
                           use_bias=True,
                           activation='relu',
                           input_shape=(data.shape[-1], )))

    return data, model