def test_call(): A = larray(np.array([1, 2, 3]), shape=(3, )) - 1 B = 0.5 * larray(lambda i: 2 * i, shape=(3, )) C = B(A) assert_array_equal(C.evaluate(), np.array([0, 1, 2])) assert_array_equal(A.evaluate(), np.array([0, 1, 2])) # A should be unchanged
def build_genn_neuron(self, native_params, init_vals): # Take a copy of the native parameters amended_native_params = deepcopy(native_params) # Create empty numpy arrays to hold start and end step indices start_index = np.empty(shape=amended_native_params.shape, dtype=np.uint32) end_index = np.empty(shape=amended_native_params.shape, dtype=np.uint32) # Calculate indices for each sequence cum_size = 0 for i, seq in enumerate(native_params["tbins"]): start_index[i] = cum_size cum_size += len(seq.value) end_index[i] = cum_size # Wrap indices in lazy arrays and add to amended native parameters amended_native_params["startIndex"] = la.larray(start_index) amended_native_params["endIndex"] = la.larray(end_index) # Call superclass method to build # neuron model from amended native parameters return super(SpikeSourceInhGamma, self).build_genn_neuron(amended_native_params, init_vals)
def test__issue3(): a = np.arange(12).reshape((4, 3)) b = larray(a) c = larray(lambda i, j: 3 * i + j, shape=(4, 3)) assert_array_equal(a[(1, 3), :][:, (0, 2)], b[(1, 3), :][:, (0, 2)]) assert_array_equal(b[(1, 3), :][:, (0, 2)], c[(1, 3), :][:, (0, 2)]) assert_array_equal(a[(1, 3), (0, 2)], b[(1, 3), (0, 2)]) assert_array_equal(b[(1, 3), (0, 2)], c[(1, 3), (0, 2)])
def test_evaluate_twice_with_vectorized_iterable(): input = MockRNG(0, 1) m1 = larray(input, shape=(7, 3)) + 3 m2 = larray(input, shape=(7, 3)) + 17 assert_array_equal(m1.evaluate(), numpy.arange(3, 24).reshape((7, 3))) assert_array_equal(m2.evaluate(), numpy.arange(38, 59).reshape((7, 3)))
def test_getitem_from_array_with_operations(): a1 = np.array([[1, 3, 5], [7, 9, 11]]) m1 = larray(a1) f = lambda i, j: np.sqrt(i * i + j * j) a2 = np.fromfunction(f, shape=(2, 3)) m2 = larray(f, shape=(2, 3)) a3 = 3 * a1 + a2 m3 = 3 * m1 + m2 assert_array_equal(a3[:, (0, 2)], m3[:, (0, 2)])
def test_add_two_constant_arrays(): m0 = larray(5, shape=(4, 3)) m1 = larray(7, shape=(4, 3)) m2 = m0 + m1 assert_equal(m2.evaluate(simplify=True), 12) # the following tests the internals, not the behaviour # it is just to check I understand what's going on assert_equal(m2.base_value, m0.base_value) assert_equal(m2.operations, [(operator.add, m1)])
def test_equality_with_number(): m1 = larray(42.0, shape=(4, 5)) m2 = larray([42, 42, 42]) m3 = larray([42, 42, 43]) m4 = larray(42.0, shape=(4, 5)) + 2 assert_equal(m1, 42.0) assert_equal(m2, 42) assert_not_equal(m3, 42) assert_raises(Exception, m4.__eq__, 44.0)
def test_is_homogeneous(): m0 = larray(10, shape=(5, )) m1 = larray(np.arange(1, 6)) m2 = m0 + m1 m3 = 9 + m0 / m1 assert m0.is_homogeneous assert not m1.is_homogeneous assert not m2.is_homogeneous assert not m3.is_homogeneous
def test_partially_evaluate_constant_array_size_one_with_boolean_index_false(): m = larray(3, shape=(1, )) m1 = larray(3, shape=(1, 1)) a = np.array([3]) a1 = np.array([[3]], ndmin=2) addr_bool = np.array([False]) addr_bool1 = np.array([[False]], ndmin=2) addr_bool2 = np.array([False]) assert_equal(m[addr_bool].shape, a[addr_bool].shape) assert_equal(m1[addr_bool1].shape, a1[addr_bool1].shape)
def __init__(self, phase, n=1): """Initialisation method. Phase input defines size of phase shift, n defines number of qubits to act on. """ LMatrix.__init__(self, "Gate") self.phase = phase ph = larray([[1, 0], [0, np.exp(1j * phase)]]) phn = ph for i in range(n - 1): ph = tensor_lazy(ph, phn) self.array = larray(ph)
def __init__(self, n=1): """Initialisation method. n defines number of qubits to act on. Alternatively single qubit Hadamards can be tensored together """ LMatrix.__init__(self, "Gate") h = larray([[1, 1], [1, -1]]) hn = h for i in range(n - 1): hn = tensor_lazy(h, hn) hn = hn * (2**(-0.5 * n)) self.array = larray(hn)
def test_partially_evaluate_constant_array_size_one_with_boolean_index_true(): m = larray(3, shape=(1, )) a = np.array([3]) addr_bool = np.array([True]) m1 = larray(3, shape=(1, 1)) a1 = 3 * np.ones((1, 1)) addr_bool1 = np.array([[True]], ndmin=2) assert_equal(m[addr_bool][0], a[0]) assert_equal(m[addr_bool], a[addr_bool]) assert_equal(m[addr_bool].shape, a[addr_bool].shape) assert_equal(m1[addr_bool1][0], a1[addr_bool1][0]) assert_equal(m1[addr_bool1].shape, a1[addr_bool1].shape)
def test_size_related_properties(): m1 = larray(1, shape=(9, 7)) m2 = larray(1, shape=(13, )) m3 = larray(1) assert_equal(m1.nrows, 9) assert_equal(m1.ncols, 7) assert_equal(m1.size, 63) assert_equal(m2.nrows, 13) assert_equal(m2.ncols, 1) assert_equal(m2.size, 13) assert_raises(ValueError, lambda: m3.nrows) assert_raises(ValueError, lambda: m3.ncols) assert_raises(ValueError, lambda: m3.size)
def test_getitem_from_vectorized_iterable(): input = MockRNG(0, 1) m = larray(input, shape=(7, )) m3 = m[3] assert isinstance(m3, (int, np.integer)) assert_equal(m3, 0) assert_equal(m[0], 1)
def test_apply_function_to_structured_array(): f = lambda m: 2 * m + 3 input = np.arange(12).reshape((4, 3)) m0 = larray(input, shape=(4, 3)) m1 = f(m0) assert isinstance(m1, larray) assert_array_equal(m1.evaluate(simplify=True), input * 2 + 3)
def set(self, **parameters): # Loop through all parameters parent_params = self.parent._parameters for n, v in iteritems(parameters): # Expand parent parameters param_vals = parent_params[n].evaluate(simplify=False) # If parameter is a sequence and value has a length # **NOTE** following logic is copied from # pyNN.parameters.ParameterSpace if parent_params[n].dtype is Sequence and isinstance(v, Sized): # If it's empty, replace v with empty sequence if len(v) == 0: v = Sequence([]) # Otherwise, if v isn't a sequence of sequences elif not isinstance(v[0], Sequence): # If v is a sequence of some other things with length, if isinstance(v[0], Sized): v = type(v)([Sequence(x) for x in v]) # Otherwise, convert v into a Sequence else: v = Sequence(v) # Replace masked section of values param_vals[self.mask] = v # Convert result back into lazy array parent_params[n] = larray(param_vals, dtype=parent_params[n].dtype, shape=parent_params[n].shape)
def normalise(self): """Renormalise qubit register such that probabilities sum to 1 """ div = np.sqrt(np.sum(np.square(self.array))) a = np.empty(len(self.array)) a.fill(div) self.array = larray(np.divide(self.array, a))
def test_multiple_operations_with_functional_array(): m = larray(lambda i: i, shape=(5,)) m0 = m / 100.0 m1 = 0.2 + m0 assert_array_almost_equal(m0.evaluate(), numpy.array([0.0, 0.01, 0.02, 0.03, 0.04]), decimal=12) assert_array_almost_equal(m1.evaluate(), numpy.array([0.20, 0.21, 0.22, 0.23, 0.24]), decimal=12) assert_equal(m1[0], 0.2)
def test_getitem_with_mask_from_2D_functional_array(): a = numpy.arange(30).reshape((6, 5)) m = larray(lambda i, j: 5 * i + j, shape=(6, 5)) assert_array_equal(a[[2, 3], [3, 4]], numpy.array([13, 19])) assert_array_equal(m[[2, 3], [3, 4]], numpy.array([13, 19]))
def test_multiple_operations_with_structured_array(): input = np.arange(12).reshape((4, 3)) m0 = larray(input, shape=(4, 3)) m1 = (m0 + 2) < 5 m2 = (m0 < 5) + 2 assert_array_equal(m1.evaluate(simplify=True), (input + 2) < 5) assert_array_equal(m2.evaluate(simplify=True), (input < 5) + 2) assert_array_equal(m0.evaluate(simplify=True), input)
def test__issue4(): # In order to avoid the errors associated with version changes of numpy, mask1 and mask2 no longer contain boolean values but integer values a = np.arange(12).reshape((4, 3)) b = larray(np.arange(12).reshape((4, 3))) mask1 = (slice(None), int(True)) mask2 = (slice(None), np.array([int(True)])) assert_equal(b[mask1].shape, partial_shape(mask1, b.shape), a[mask1].shape) assert_equal(b[mask2].shape, partial_shape(mask2, b.shape), a[mask2].shape)
def test_deepcopy(): m1 = 3 * larray(lambda i, j: 5 * i + j, shape=(4, 5)) + 2 m2 = deepcopy(m1) m1.shape = (3, 4) m3 = deepcopy(m1) assert_equal(m1.shape, m3.shape, (3, 4)) assert_equal(m2.shape, (4, 5)) assert_array_equal(m1.evaluate(), m3.evaluate())
def test_apply_function_to_functional_array(): input = lambda i, j: 2 * i + j m0 = larray(input, shape=(4, 3)) f = lambda m: 2 * m + 3 m1 = f(m0) assert_array_equal( m1.evaluate(), np.array([[3, 5, 7], [7, 9, 11], [11, 13, 15], [15, 17, 19]]))
def test_partially_evaluate_functional_array_with_boolean_index(): m = larray(lambda i, j: 5 * i + j, shape=(4, 5)) a = np.arange(20.0).reshape((4, 5)) addr_bool = np.array([True, True, False, False, True]) addr_int = np.array([0, 1, 4]) assert_equal(a[::2, addr_bool].shape, a[::2, addr_int].shape) assert_equal(a[::2, addr_int].shape, m[::2, addr_int].shape) assert_equal(a[::2, addr_bool].shape, m[::2, addr_bool].shape)
def test_getitem_with_slice_from_2D_functional_array_2(): def test_function(i, j): return i * i + 2 * i * j + 3 m = larray(test_function, shape=(3, 15)) assert_array_equal( m[:, 3:14:3], np.fromfunction(test_function, shape=(3, 15))[:, 3:14:3])
def test_partially_evaluate_constant_array_with_boolean_index(): m = larray(3, shape=(4, 5)) a = 3 * np.ones((4, 5)) addr_bool = np.array([True, True, False, False, True]) addr_int = np.array([0, 1, 4]) assert_equal(a[::2, addr_bool].shape, a[::2, addr_int].shape) assert_equal(a[::2, addr_int].shape, m[::2, addr_int].shape) assert_equal(a[::2, addr_bool].shape, m[::2, addr_bool].shape)
def test_evaluate_with_functional_array(): input = lambda i, j: 2 * i + j m = larray(input, shape=(4, 3)) assert_array_equal(m.evaluate(), numpy.array([[0, 1, 2], [2, 3, 4], [4, 5, 6], [6, 7, 8]]))
def __init__(self, data): """Initialise generic gate. data is a matrix. Although no checks are performed, matrices must be: Unitary, Hermitian, and size 2^n * 2^n. If an input matrix does not satisfy these constraints, unpredictable and wrong things may happen. """ LMatrix.__init__(self, "Gate") self.array = larray(data)
def test_create_with_generator(): def plusone(): i = 0 while True: yield i i += 1 A = larray(plusone(), shape=(5, 11)) assert_array_equal(A.evaluate(), np.arange(55).reshape((5, 11)))
def ln_lut(input_shift, float_to_fixed): # Calculate the size of the LUT size = (1 << float_to_fixed.n_frac) >> input_shift # Build a lazy array of x values to calculate log for x = la.larray(np.arange(1.0, 2.0, 1.0 / float(size))) # Take log and convert to fixed point return float_to_fixed(la.log(x))
def __init__(self, phase, n=2): """Initialisation method. phase defines phase shift on phase gate, defines the number of total qubits for the gate (n-1 control qubits + 1 target qubit) """ LMatrix.__init__(self, "Gate") self.array = np.identity(2**n, dtype=complex) self.array[2**n - 1, 2**n - 1] = np.exp(1j * phase) self.array = larray(self.array)