def test_two_dim_matmul(self):
    max_seq_length = 3
    hidden_dim = 8

    # We test the 2D matmul function on the DFT calculation (primary use-case).
    dft_mat_seq = jnp.asarray(linalg.dft(max_seq_length))
    dft_mat_hidden = jnp.asarray(linalg.dft(hidden_dim))
    two_dim_matmul = functools.partial(
        fourier.two_dim_matmul,
        matrix_dim_one=dft_mat_seq,
        matrix_dim_two=dft_mat_hidden)

    inputs = jnp.array([[1, 0, 0, 11, 9, 2, 0.4, 2], [1, 1, 0, 1, 0, 2, 8, 1],
                        [1, 4, 0, 5, 5, 0, -3, 1]],
                       dtype=jnp.float32)

    expected_output = np.fft.fftn(inputs)

    for precision, delta in zip(
        [lax.Precision.DEFAULT, lax.Precision.HIGH, lax.Precision.HIGHEST],
        [1e-4, 1e-5, 1e-5]):
      actual_output = two_dim_matmul(inputs, precision=precision)
      # Compare results row by row.
      for i in range(max_seq_length):
        self.assertSequenceAlmostEqual(
            actual_output[i], expected_output[i], delta=delta)
Example #2
0
  def _init_fourier_transform(self):
    """Initializes Fourier Transform.

    On GPUs/CPUs: The native FFT implementation is optimal for all sequence
    lengths.

    On TPUs: For relatively shorter sequences, it is faster to pre-compute the
    DFT matrix and then compute Fourier Transform using matrix multiplications.
    For longer sequences, the FFT is faster, provided the MAX_SEQ_LENGTH is a
    power of 2.
    """
    if self.config.use_fft:
      if (self.config.max_seq_length > 4096 and
          not math.log2(self.config.max_seq_length).is_integer()):
        raise ValueError(
            "For large input sequence lengths (>4096), the maximum input "
            "sequence length must be a power of 2 to take advantage of FFT "
            "optimizations. We encourage the same for the model hidden "
            "dimension. config.max_seq_length: %d. config.d_model: $d" %
            self.config.max_seq_length, self.config.d_model)

      self.fourier_transform = jnp.fft.fftn
    else:
      dft_mat_hidden = linalg.dft(self.config.d_model)
      dft_mat_seq = linalg.dft(self.config.max_seq_length)

      self.fourier_transform = functools.partial(
          fourier.two_dim_matmul,
          matrix_dim_one=jnp.asarray(dft_mat_seq),
          matrix_dim_two=jnp.asarray(dft_mat_hidden),
          precision=lax.Precision.DEFAULT)
def fftImageBothWays(im, raw=True):
    if raw:
        im = np.array(imRaw).astype(float)
    else:
        im = imRaw

    dftLeft = dft(im.shape[0])
    dftRight = dft(im.shape[1])

    return multiplyMatrixRGBArrayMatrixTranspose(dftLeft, im, dftRight)
def test_dft():
    m = dft(2)
    expected = array([[1.0, 1.0], [1.0, -1.0]])
    yield (assert_array_almost_equal, m, expected)
    m = dft(2, scale='n')
    yield (assert_array_almost_equal, m, expected / 2.0)
    m = dft(2, scale='sqrtn')
    yield (assert_array_almost_equal, m, expected / sqrt(2.0))

    x = array([0, 1, 2, 3, 4, 5, 0, 1])
    m = dft(8)
    mx = m.dot(x)
    fx = fft(x)
    yield (assert_array_almost_equal, mx, fx)
Example #5
0
def test_dft():
    m = dft(2)
    expected = array([[1.0, 1.0], [1.0, -1.0]])
    yield (assert_array_almost_equal, m, expected)
    m = dft(2, scale='n')
    yield (assert_array_almost_equal, m, expected/2.0)
    m = dft(2, scale='sqrtn')
    yield (assert_array_almost_equal, m, expected/sqrt(2.0))

    x = array([0, 1, 2, 3, 4, 5, 0, 1])
    m = dft(8)
    mx = m.dot(x)
    fx = fft(x)
    yield (assert_array_almost_equal, mx, fx)
Example #6
0
def DIF_FFT(N, s):
    W_N = np.exp(-1j * (2 * np.pi / N))

    p = [s[i] + s[i + N // 2] for i in range(N // 2)]
    q = [(s[i] - s[i + N // 2]) * (W_N ** i) for i in range(N // 2)]

    P = dft(N // 2) @ p
    Q = dft(N // 2) @ q

    result = []
    for i in range(N // 2):
        result.append(P[i])
        result.append(Q[i])

    return result
Example #7
0
def W2_init_Imag(shape,
                 dtype=None,
                 partition_info=None):  # both W1 and W2 need initialization
    # fk = np.arange(N)
    # fk = 2.0 * np.pi * fk / N
    # fk = np.square(fk)
    # Hk = -1.0 * 1j * fk * beta2 / 2.0 + alp / 2.0
    # Hk = Hk * delta / 2.0
    # Hk = np.exp(Hk)
    Hk = np.ones(N)
    Hk = Hk * 3.0
    dia_Hk = np.diag(Hk)

    w = dft(N)
    w_invert = linalg.inv(w)

    A = np.matmul(w_invert, dia_Hk)
    A = np.matmul(A, w)
    A_imag = np.imag(A)

    return K.variable(value=A_imag, dtype=dtype)


#
# def MF_init(shape, dtype=None, partition_info=None):
#     val = np.ones((N, M))
#     val[1:-1, 1:-1] = 0
#     return K.variable(value=val, dtype=dtype)
Example #8
0
    def __init__(self, perm=None, W=4, V=4, D=256, K=4, Modulation='QPSK'):

        self.Modulation = Modulation
        self.W = W
        self.V = V
        self.D = D
        self.K = K
        self.M = D // K

        if self.Modulation == 'QPSK':
            self.symbols = np.array([1 + 1j, 1 - 1j, -1 + 1j, -1 - 1j])
            self.Q = 4

        self.rate = self.V * self.K * (np.log2(self.Q) +
                                       np.log2(self.M)) / self.D

        if perm is None:
            self.perm = [np.random.permutation(self.D) for i in range(W * V)]
            self.perm_inv = [np.argsort(self.perm[i]) for i in range(W * V)]

        self.perm = np.vstack(self.perm)
        self.perm_inv = np.vstack(self.perm_inv)

        self.DFT = dft(self.D)
        self.dictionary = [self.DFT[self.perm[i]] for i in range(W * V)]
        self.dictionary = np.hstack(self.dictionary)
def get_matrix(n, tf='dct'):
    if tf == 'dft':
        F = la.dft(n, scale='sqrtn')
    elif tf == 'dct':
        I = np.identity(n)
        F = dct(I, norm='ortho')
    return F
def get_half_period_fourier_basis_as_rows(nb_points, nb_vectors):
    f = dft(2 * nb_points)
    f_real = np.real(f)
    f_imag = np.imag(f)
    return np.concatenate(
        [f_real[:nb_points, :nb_vectors], f_imag[:nb_points, :nb_vectors]],
        axis=1).astype(np.float32)
Example #11
0
def get_filters(R, filter_size, P=None, n_rings=None):
    """Perform single-frequency DFT on each ring of a polar-resampled patch"""
    k = filter_size
    filters = {}
    N = n_samples(k)
    from scipy.linalg import dft
    for m, r in R.iteritems():
        rsh = r.get_shape().as_list()
        # Get the basis matrices
        weights = get_interpolation_weights(k, m, n_rings=n_rings)
        DFT = dft(N)[m,:]
        LPF = np.dot(DFT, weights).T

        cosine = np.real(LPF).astype(np.float32)
        sine = np.imag(LPF).astype(np.float32)
        # Reshape for multiplication with radial profile
        cosine = tf.constant(cosine)
        sine = tf.constant(sine)
        # Project taps on to rotational basis
        r = tf.reshape(r, tf.stack([rsh[0],rsh[1]*rsh[2]]))
        ucos = tf.reshape(tf.matmul(cosine, r), tf.stack([k, k, rsh[1], rsh[2]]))
        usin = tf.reshape(tf.matmul(sine, r), tf.stack([k, k, rsh[1], rsh[2]]))
        if P is not None:
            # Rotate basis matrices
            ucos_ = tf.cos(P[m])*ucos + tf.sin(P[m])*usin
            usin = -tf.sin(P[m])*ucos + tf.cos(P[m])*usin
            ucos = ucos_
        filters[m] = (ucos, usin)
    return filters
Example #12
0
def dftMatrix(size):
    """Form an explicit DFT matrix."""
    M = 1
    for dim in size:
        m = dft(dim)
        M = np.kron(M, m)
    return (M)
Example #13
0
def generate_tmats(dim, rgen):
    assert SAMPLES >= 3
    mats = {'ID': np.eye(dim), 'SWAP': np.eye(dim)[::-1]}
    mats.update({'DFT': dft(dim, scale='sqrtn')})
    mats.update({'RAND_%i' % i: unitary_haar(dim, rgen=rgen)
                 for i in range(SAMPLES - 3)})
    return mats
Example #14
0
    def build(self, input_shape):
        """
        This method must be defined for any custom layer, here you define the training parameters.
        
        input_shape: a tensor that automatically captures the dimensions of the input by tensorflow. 
        """
        # get the size of the input
        self.N = input_shape.as_list()[1]

        # construct the DFT matrix
        self.F = dft(self.N) / self.fs

        # construct the trapezoidal rule matrix
        self.D = np.eye(self.N // 2)
        self.D[0, 0] = 0.5
        self.D[-1, -1] = 0.5

        # define the trainable parameters representing the double side band PSD of noise
        self.S = self.add_weight(name="S",
                                 shape=tf.TensorShape((self.N // 2, 1)),
                                 initializer=initializers.Ones(),
                                 constraint=constraints.NonNeg(),
                                 trainable=True)

        # this has to be called for any tensorflow custom layer
        super(coherence_Layer, self).build(input_shape)
Example #15
0
    def construct_circuit(self,
                          mode,
                          register=None,
                          circuit=None,
                          use_basis_gates=True):
        if mode == 'vector':
            return linalg.dft(2**self._num_qubits, scale='sqrtn')
        elif mode == 'circuit':
            if register is None:
                register = QuantumRegister(self._num_qubits, name='q')
            if circuit is None:
                circuit = QuantumCircuit(register)

            for j in reversed(range(self._num_qubits)):
                circuit.u2(0, np.pi,
                           register[j]) if use_basis_gates else circuit.h(
                               register[j])
                for k in reversed(range(j)):
                    lam = -1.0 * pi / float(2**(j - k))
                    if use_basis_gates:
                        circuit.u1(lam / 2, register[j])
                        circuit.cx(register[j], register[k])
                        circuit.u1(-lam / 2, register[k])
                        circuit.cx(register[j], register[k])
                        circuit.u1(lam / 2, register[k])
                    else:
                        circuit.cu1(lam, register[j], register[k])
            return circuit
        else:
            raise ValueError('Mode should be either "vector" or "circuit"')
Example #16
0
def dft_3D_norm(matrixin3D, size):
    m = dft(size)
    row_fft = zeros((size, size), dtype=complex)
    column_fft = zeros((size, size), dtype=complex)
    z_fft = []
    for i in range(0, size):
        z_fft.append([])
        for j in range(0, size):
            z_fft[i].append([])
            for k in range(0, size):
                z_fft[i][j].append(1)
    #Dft loop
    for k in range(0, size):
        test_matrix = matrixin3D[k]
        for i in range(0, size):
            row_fft_i = 1 / sqrt(size) * fft.fftshift(m @ test_matrix[i])
            row_fft[i] = row_fft_i
        for i in range(0, size):
            column_fft_i = 1 / sqrt(size) * fft.fftshift(m @ row_fft[:, i])
            column_fft[:, i] = (column_fft_i)
        z_fft[k] = column_fft
        z_fft = array(z_fft)
    z_fft = array(z_fft)
    #z_fft=abs(array(z_fft))
    for i in range(0, size):
        for j in range(0, size):
            z_fft_k = 1 / sqrt(size) * fft.fftshift(m @ z_fft[:, i, j])
            z_fft[:, i, j] = array(abs(z_fft_k))
    return z_fft
Example #17
0
def _cov_fourier(cov, dx, taper=None):
    """
    Determine the covariance of a Fourier space vector from the known covariance of a real-space vector.
    
    Parameters
    ----------
    cov : array
        Either square 2-dimensional covariance matrix in real space, or 3D, with first axis a dependent dimension.
        
    dx : float
        The physical interval between bins in the covariance matrix.

    Returns
    -------
    cov_fourier : Covariance of Fourier-space vector (same shape as cov)

    """
    N = cov.shape[-1]

    if taper is not None:
        cov *= np.outer(taper(N), taper(N))

    F = dft(N, "sqrtn")

    out = np.real(np.conjugate(F.T).dot(cov).dot(F).transpose(1, 0, 2) * dx**2)

    # Select only positive eta entries
    return out.T[1:N / 2, 1:N / 2].T
Example #18
0
def getQExp(beta, theta, n):
    assert n % 2 == 1

    diagHalfArray = [beta**(i/((n-1)/2)) for i in range(1, int((n-1)/2) + 1)]

    diagArray = [1] + diagHalfArray + diagHalfArray

    assert len(diagArray) == n

    DStar = np.diag(diagArray)

#    pl.matshow(DStar)
#    pl.show()

    normalizedDFT = dft(n)/sqrt(n)

#    print DStar

#    pl.matshow(DStar)
#    pl.show()

#    pl.matshow(abs(np.dot(np.dot(np.transpose(np.conj(normalizedDFT)), DStar),
#        normalizedDFT)))
#    pl.show()

    return np.dot(np.dot(np.transpose(np.conj(normalizedDFT)), DStar),
        normalizedDFT)
Example #19
0
def DIT_FFT(N, s):
    even_part = [s[i] for i in range(len(s)) if i % 2 == 0]
    odd_part = [s[i] for i in range(len(s)) if i % 2 != 0]

    F = dft(N // 2) @ even_part
    G = dft(N // 2) @ odd_part

    W_N = np.exp(-1j * (2 * np.pi / N))

    X = []
    for i in range(N // 2):
        X.append(F[i] + (W_N**i) * G[i])

    for i in range(N // 2):
        X.append(F[i] - (W_N**i) * G[i])

    return X
Example #20
0
 def set_perm(self, perm):
     self.perm = perm
     self.DFT = dft(self.D)
     self.dictionary = [
         self.DFT[self.perm[i]] for i in range(self.W * self.V)
     ]
     self.dictionary = np.hstack(self.dictionary)
     for i in range(self.W * self.V):
         self.perm_inv[i] = np.argsort(self.perm[i])
Example #21
0
def IDFT(fourier_signal):
    """
    A method for transforming back a fourier signal to it's original values
    :param fourier_signal: The transformed fourier to convert back
    :return: The input array in original values
    """
    N = fourier_signal.shape[0]
    fft_coef = np.linalg.inv(linalg.dft(N))
    out_vec = (1 / N) * fft_coef.dot(fourier_signal)
    return out_vec
Example #22
0
 def construct_circuit(self, mode, qubits=None, circuit=None):
     if mode == 'vector':
         # note the difference between QFT and DFT in the phase definition:
         # QFT: \omega = exp(2*pi*i/N) ; DFT: \omega = exp(-2*pi*i/N)
         # so linalg.inv(linalg.dft()) is correct for QFT
         return linalg.inv(linalg.dft(2 ** self._num_qubits, scale='sqrtn'))
     elif mode == 'circuit':
         ftc = FourierTransformCircuits(self._num_qubits, approximation_degree=0, inverse=False)
         return ftc.construct_circuit(qubits, circuit)
     else:
         raise ValueError('Mode should be either "vector" or "circuit"')
def multExpD(N, A, gamma, time, r):
    expDList = []
    qft = dft(N, scale='sqrtn')
    iqft = inv(qft)
    for t in time:
        B = -gamma * A * t / r
        lambdA = iqft @ B @ qft
        D = np.diag(lambdA)
        D = np.exp(-1j * D)
        expDList.append(list(D))
    return expDList
Example #24
0
def generate_sequence(N, signal_length):
    # generate random dictionary
    D = dft(signal_length).real

    # generate random outputs
    y = np.zeros((N,signal_length))
    for i in range(y.shape[0]):
        x = np.random.choice(D.shape[1], int(D.shape[1]/10))
        y[i,:] = np.sum(D[x,:], axis=0)
        y[i,:] = np.multiply(y[i,:], np.random.uniform(1, 2,size=y.shape[1]))
    return y
Example #25
0
 def _init_fourier_transform(self, config):
     if not config.use_tpu_fourier_optimizations:
         self.fourier_transform = partial(torch.fft.fftn, dim=(1, 2))
     elif config.max_position_embeddings <= 4096:
         if is_scipy_available():
             self.register_buffer(
                 "dft_mat_hidden", torch.tensor(linalg.dft(config.hidden_size), dtype=torch.complex64)
             )
             self.register_buffer(
                 "dft_mat_seq", torch.tensor(linalg.dft(config.tpu_short_seq_length), dtype=torch.complex64)
             )
             self.fourier_transform = partial(
                 two_dim_matmul, matrix_dim_one=self.dft_mat_seq, matrix_dim_two=self.dft_mat_hidden
             )
         else:
             logging.warning(
                 "SciPy is needed for DFT matrix calculation and is not found. Using TPU optimized fast fourier transform instead."
             )
             self.fourier_transform = fftn
     else:
         self.fourier_transform = fftn
Example #26
0
 def _init_fourier_transform(self, config):
     if not config.use_tpu_fourier_optimizations:
         self.fourier_transform = partial(torch.fft.fftn, dim=(1, 2))
     elif config.n_pos <= 4096:
         if is_scipy_available():
             self.register_buffer(
                 "dft_mat_hidden",
                 torch.tensor(linalg.dft(config.d_model), dtype=torch.complex64),
             )
             self.register_buffer(
                 "dft_mat_seq",
                 torch.tensor(linalg.dft(config.tpu_short_seq_length), dtype=torch.complex64),
             )
             self.fourier_transform = partial(
                 two_dim_matmul,
                 matrix_dim_one=self.dft_mat_seq,
                 matrix_dim_two=self.dft_mat_hidden,
             )
         else:
             self.fourier_transform = fftn
     else:
         self.fourier_transform = fftn
    def calc_coeffs_DFT(self, OAreg, HCreg, filt, ND = [None], target = [None], fluxTimesE = False):
        if not np.any(ND):
            ND = self.ND
        if not np.any(target):
            target = self.target
        if fluxTimesE:
            ND = ND*self.Ebins.reshape(ND.shape[0], 1)
            target = target*self.Ebins
        # if not np.any(filt):
        #     filt = np.ones_like(self.OAbins <= self.maxOA, dtype = float)
            
        # penalty matrix for coefficients
        nBinsOA = np.sum(self.OAbins <= self.maxOA)
        nBinsHC = self.ND_HC.shape[1]
        
        # # OA penalty term is a difference matrix
        # OA_penalty = np.diag(nBinsOA*[1]) - np.diag((nBinsOA - 1)*[1], k = 1)
        # # OA penalty term is the L1 norm
        # OA_penalty = np.eye(nBinsOA)
        # OA penalty is a frequency filter based on the DFT
        from scipy.linalg import dft
        # OA_penalty = np.diag(filt)*dft(nBinsOA) + OAreg*(np.diag(nBinsOA*[1]) - np.diag((nBinsOA - 1)*[1], k = 1))
        OA_penalty = np.diag(filt)*dft(nBinsOA) + OAreg*(np.diag(nBinsOA*[1]) - np.diag((nBinsOA - 1)*[1], k = 1))
        HC_penalty = np.eye(nBinsHC)
        self.A = block_diag(OA_penalty, HC_penalty)
        # Gamma = block_diag(OAreg*OA_penalty, HCreg*HC_penalty)
        Gamma = block_diag(OA_penalty, HCreg*HC_penalty)
        self.Gamma = Gamma

        # weighting matrix for target flux
        P = np.diag(np.where(self.Ebins > self.Ebounds[1],
                             self.OutOfRegionFactors[1],
                             np.where(self.Ebins < self.Ebounds[0],
                                      self.OutOfRegionFactors[0],
                                      1)))
        self.P = P

        # ND matrix
        NDmatr = np.matrix(ND)
        LHS = np.matmul(NDmatr.T, np.matmul(P, NDmatr)) + np.matmul(Gamma.T.conj(), Gamma)
        RHS = np.dot(np.matmul(NDmatr.T, P), target)

        self.c = np.array(np.dot(RHS, LHS.I)).squeeze()
        self.c = np.linalg.solve(LHS, RHS)
        self.cOA = self.c[:nBinsOA]
        self.cHC = self.c[nBinsOA:]

        self.fluxPred = np.dot(self.ND, self.c)
        self.ratePred = np.dot(self.ND_rate, self.c)
        self.ratePred_statErr = np.sqrt(np.dot(self.ND_rate, np.power(self.c, 2)))
Example #28
0
 def __init__(self, frame_size=512, frame_shift=256, loss_type='mae'):
     self.frame_size = frame_size
     self.frame_shift = frame_shift
     self.loss_type = loss_type
     #self.device = device
     self.frame = TorchSignalToFrames(frame_size=self.frame_size,
                                      frame_shift=self.frame_shift)
     D = linalg.dft(frame_size)
     W = np.hamming(self.frame_size)
     DR = np.real(D)
     DI = np.imag(D)
     self.DR = torch.from_numpy(DR).float().cuda()  # to(self.device)
     self.DR = self.DR.contiguous().transpose(0, 1)
     self.DI = torch.from_numpy(DI).float().cuda()  # to(self.device)
     self.DI = self.DI.contiguous().transpose(0, 1)
     self.W = torch.from_numpy(W).float().cuda()  # to(self.device)
Example #29
0
    def MMSE_FDE(self, receive_signal, channel, sigma):
        self.receive_signal = receive_signal
        self.channel = channel
        self.sigma = sigma

        # DFT matrix
        DFT = dft(self.Nc)
        IDFT = (np.conjugate(DFT).T) / self.Nc

        # Fourier transform
        Fourier_channel = np.dot(DFT, self.channel)
        Fourier_receive_signal = np.dot(DFT, self.receive_signal)

        # Noise Power
        Pn = 2 * self.sigma * self.sigma * self.user

        # Eye matrix for MMSE
        I = np.eye(self.BS, dtype=np.complex)
        I = Pn * I

        # Calculate MMSE Weight
        H = np.zeros((self.BS, self.user), dtype=np.complex)
        Fourier_FDE_signal = np.zeros((self.Nc, self.user), dtype=np.complex)
        R = np.zeros((self.BS, 1), dtype=np.complex)

        for r in range(self.BS):
            for s in range(self.user):
                H[r, s] = Fourier_channel[0, self.BS * r + s]

        MMSE_W = np.dot(H, np.conjugate(H.T)) + I
        MMSE_Weight = np.dot(np.linalg.inv(MMSE_W), H)

        for i in range(self.Nc):

            for r in range(self.BS):
                R[r, 0] = Fourier_receive_signal[i, r]

            tmp = np.dot(np.conjugate(MMSE_Weight.T), R)

            for s in range(self.user):
                Fourier_FDE_signal[i, s] = tmp[s, 0]

        # Inv Fourier transform
        FDE_signal = np.dot(IDFT, Fourier_FDE_signal)

        return FDE_signal
    def __init__(self,
                 block,
                 subcarrier,
                 CP,
                 sigma,
                 gamma=0.0,
                 phi=0.0,
                 PA_IBO_dB=5,
                 PA_rho=2,
                 LNA_alpha_1=5,
                 LNA_alpha_2=2,
                 h_si_list=None,
                 h_s_list=None,
                 h_si_len=1,
                 h_s_len=1,
                 receive_antenna=1,
                 tx_iqi=True,
                 pa=True,
                 lna=True,
                 rx_iqi=True,
                 equalizer='ZF'):
        self.block = block
        self.subcarrier = subcarrier
        self.CP = CP
        self.sigma = sigma
        self.gamma = gamma
        self.phi = phi
        self.PA_IBO_dB = PA_IBO_dB
        self.PA_rho = PA_rho
        self.LNA_alpha_1 = LNA_alpha_1
        self.LNA_alpha_2 = LNA_alpha_2
        self.h_si_list = h_si_list
        self.h_s_list = h_s_list
        self.h_si_len = h_si_len
        self.h_s_len = h_s_len
        self.receive_antenna = receive_antenna
        self.tx_iqi = tx_iqi
        self.pa = pa
        self.lna = lna
        self.rx_iqi = rx_iqi

        self.subcarrier_CP = subcarrier + CP
        self.dft_mat = dft(self.subcarrier, scale="sqrtn")
        self.idft_mat = self.dft_mat.conj().T
        self.cp_zero = np.hstack((np.zeros(
            (self.subcarrier, self.CP)), np.eye(self.subcarrier)))
Example #31
0
def DFT(size, cutoff):
    r"""Discrete Fourier transform in the Fock basis.

    Args:
        size (int): size of the DFT. The unitary will
            act on Fock basis elements |0> to |size-1>.
        cutoff (int): the Fock basis truncation of the returned unitary.
            Must be larger than size.

    Returns:
        array: a [cutoff, cutoff] complex array representing
            the action of the DFT in the truncated
            Fock basis.
    """
    U = np.identity(cutoff, dtype=np.complex128)
    U[:size, :size] = dft(size)/np.sqrt(size)
    return U
    def get_transform_matrix(self, size):
        """ Calculates the transform matrix based on self.matrix parameter, 
            which can be {'dct', 'dft'}."""

        if self.matrix == 'dct':
            '''Calculate the square DCT transform matrix. Results are 
                equivalent to Matlab dctmtx(n) with 64 bit precision.'''
            transform_matrix = np.array(range(size),
                                        np.float64).repeat(size).reshape(
                                            size, size)
            transform_matrixT = np.pi * (transform_matrix.transpose() +
                                         0.5) / size
            transform_matrixT = (1.0 / np.sqrt(size / 2.0)) * np.cos(
                transform_matrix * transform_matrixT)
            transform_matrixT[0] = transform_matrixT[0] * (np.sqrt(2.0) / 2.0)
        elif self.matrix == 'dft':
            transform_matrixT = dft(size, scale='sqrtn')

        return transform_matrixT
Example #33
0
    def construct_circuit(self, mode, register=None, circuit=None):
        if mode == 'vector':
            return linalg.dft(2**self._num_qubits, scale='sqrtn')
        elif mode == 'circuit':
            if register is None:
                register = QuantumRegister(self._num_qubits, name='q')
            if circuit is None:
                circuit = QuantumCircuit(register)

            for j in reversed(range(self._num_qubits)):
                circuit.h(register[j])
                # neighbor_range = range(np.max([0, j - self._degree + 1]), j)
                neighbor_range = range(
                    np.max([0, j - self._num_qubits + self._degree + 1]), j)
                for k in reversed(neighbor_range):
                    circuit.cu1(-1.0 * pi / float(2**(j - k)), register[j],
                                register[k])
            return circuit
        else:
            raise ValueError('Mode should be either "vector" or "circuit"')
Example #34
0
    def test_fourier_matrices_product(self):
        """
        Test Fourier basis transition

        This test validates that the transition of a matrix from Fourier basis to the standard basis is equivalent to
        multiplying my the DFT matrix from right and its inverse from the left.

        """
        rng = Generator(PCG64(1995))
        n: int = rng.integers(low=2, high=100)
        mat: Matrix = rng.standard_normal((n, n))
        tested_output = change_from_fourier_basis(mat)
        dft_mat: Matrix = dft(n, scale="sqrtn")
        expected_output: Matrix = np.conj(dft_mat.T).dot(mat).dot(dft_mat)
        self.assertTrue(np.allclose(tested_output, expected_output))

        mat: Matrix = rng.standard_normal((n, n))
        mat_fourier: Matrix = change_to_fourier_basis(mat)
        expected_output: Matrix = dft_mat.dot(mat).dot(np.conj(dft_mat.T))
        self.assertTrue(np.allclose(mat_fourier, expected_output))
    def __init__(self, idim, odim,
                 rng=None,
                 irange=0.1):

        super(ComplexLinear, self).__init__(rng=rng)

        self.idim = idim
        self.odim = odim
        # must be true for fourier layer
        assert idim == odim

        # odim  is output and the input dimensions of the DFT layer
        dft_mat = dft(odim).T
        # diag(M * M^{dagger}) gives me the normalization values for each row
        # setting M = M^T does the trick for collumns
        normalization_vals = numpy.diag((dft_mat.T).dot(numpy.conj(dft_mat.T)))
        # This is the renormalized weight matrix from my understanding
        self.W = dft_mat  # / normalization_vals.reshape(-1,1)
        # seperating in to two (outputing double number of neurons )
        self.Wr = numpy.real(self.W)
        self.Wi = numpy.imag(self.W)

        self.b = numpy.zeros((self.odim*2,), dtype=numpy.float32)
Example #36
0
    ax.set_ylabel("Mutual information (bits)")
    ax.set_xlabel("p")



    pl.show()

    sys.exit()

if covTest:
    n = 101
    scaling = 1

    cov = getChristosCovMat(n, scaling)

    print abs(np.dot(np.dot(dft(n), cov), np.transpose(dft(n))))/n

    pl.matshow(abs(np.dot(np.dot(dft(n), cov), np.transpose(dft(n)))))
    pl.show()



if uptTest:
    n = 80
#    upt = np.triu(np.ones((n,n)))
#    upt = np.identity(n)



#    upt = circ([0]*int(n/2) + [1]*int(n/2))
#    print sorted([abs(i) for i in np.linalg.eig(upt)[0]])
Example #37
0
	p.matshow(np.real(np.dot(meb, np.conj(np.transpose(meb)))), cmap="Greys_r")
	p.colorbar()
	p.show()

#	tm = circulant(seq)
#	meb = fourierEigenbasis(n)

	p.matshow(np.real(meb), cmap="Greys_r")
	p.show()

	diag = np.dot(np.dot(meb, tm), np.transpose(np.conj(meb)))

	p.matshow(np.real(diag), cmap="Greys_r")
	p.colorbar()
	p.show()

	meb = dft(n)
	tm = circulant(seq)

	diag = np.dot(np.dot(meb, tm), np.transpose(np.conj(meb)))

	p.matshow(np.real(meb), cmap="Greys_r")
	p.show()

	p.matshow(np.real(diag), cmap="Greys_r")
	p.colorbar()
	p.show()


Example #38
0
#    s = (np.random.binomial(1,p,n)*2 - np.ones(n))
    s = getCenteredVector(p, n)

    print mean(s)
    print variance(s)



#    print s
    c = circ(s)
    normalizedC = c/sqrt(n)


    print normalizedC

    dftMat = dft(n)/sqrt(n)
    diagC = np.dot(np.dot(np.conj(dftMat), normalizedC), dftMat)

    print diagC

    diagDiagC = np.diagonal(diagC)

    pl.plot(np.multiply(np.conjugate(diagDiagC), diagDiagC))
    pl.show()

    pl.matshow(np.real(diagC))
    pl.show()

#    sys.exit()
#    h = circhank(s)
#    normalizedH = h/sqrt(n)
Example #39
0
#    s = (np.random.binomial(1,p,n)*2 - np.ones(n))
    s = getCenteredVector(p, n)

    print mean(s)
    print variance(s)



#    print s
    c = circ(s)
    normalizedC = c/sqrt(n)


    print normalizedC

    dftMat = dft(n)/sqrt(n)
    diagC = np.dot(np.dot(np.conj(dftMat), normalizedC), dftMat)

    print diagC

    diagDiagC = np.diagonal(diagC)

    pl.plot(np.multiply(np.conjugate(diagDiagC), diagDiagC))
    pl.show()

    pl.matshow(np.real(diagC))
    pl.show()

#    sys.exit()
#    h = circhank(s)
#    normalizedH = h/sqrt(n)
Example #40
0
 def time_dft(self, size):
     sl.dft(size)