<commit_before>import numpy as np
import floq.parametric_system as ps


class SpinEnsemble(object):
    """
    A system of n non-interacting spins, where each
    spin is described by the Hamiltonian
    H(t) = w/2 s_z + 1/2 sum_k (a_k s_x + b_k s_y) sin(k omega t).

    Commonly, the values for w will be set slightly different
    for each spin, and a_k and b_k will be multiplied by some
    attenuation factor for each spin. Fidelities etc. will then be computed as ensemble averages.
    """

    def __init__(self, n, nc, omega, freqs, amps):
        """
        Initialise a SpinEnsemble instance with
        - n: number of spins
        - nc: number of components in the control pulse
        - omega: base frequency of control pulse
        - freqs: vector of n frequencies
        - amps: vector of n amplitudes
        """

        self.n = n
        self.nc = nc
        self.omega = omega
        self.freqs = freqs
        self.amps = amps


    def get_single_system(self, i, controls, t):
        """
        Build an instance of FixedSystem for the ith spin,
        with the given controls -- these are expected to be
        2*nc values ordered as follows:
        [a1, b1, a2, b2, ... a_nc, b_nc]
        """
        hf = self._build_single_hf(self.freq[i], self.amps[i], controls)
        dhf = self._build_single_dhf(self.freq[i], self.amps[i], controls)


    def _build_single_hf(self, nc, freq, amp, controls):
        # assemble hf for one spin

        hf = np.zeros([2*nc+1, 2, 2], dtype='complex128')
        for k in xrange(0, nc):
            # the controls are ordered in reverse
            # compared to how they are placed in hf
            a = controls[-2*k-2]*amp
            b = controls[-2*k-1]*amp

            hf[k, :, :] = np.array([[0.0, 0.25*(1j*a+b)],
                                    [0.25*(1j*a-b), 0.0]])

            hf[-k-1, :, :] = np.array([[0.0, -0.25*(1j*a+b)],
                                       [0.25*(-1j*a+b), 0.0]])

        hf[nc] = np.array([[freq/2.0, 0.0],
                           [0.0, -freq/2.0]])

        return hf
<commit_msg>add some comments<commit_after>import numpy as np
import floq.parametric_system as ps


class SpinEnsemble(object):
    """
    A system of n non-interacting spins, where each
    spin is described by the Hamiltonian
    H(t) = w/2 s_z + 1/2 sum_k (a_k s_x + b_k s_y) sin(k omega t).

    Commonly, the values for w will be set slightly different
    for each spin, and a_k and b_k will be multiplied by some
    attenuation factor for each spin. Fidelities etc. will then be computed as ensemble averages.
    """

    def __init__(self, n, nc, omega, freqs, amps):
        """
        Initialise a SpinEnsemble instance with
        - n: number of spins
        - nc: number of components in the control pulse
        - omega: base frequency of control pulse
        - freqs: vector of n frequencies
        - amps: vector of n amplitudes
        """

        self.n = n
        self.nc = nc
        self.omega = omega
        self.freqs = freqs
        self.amps = amps


    def get_single_system(self, i, controls, t):
        """
        Build an instance of FixedSystem for the ith spin,
        with the given controls -- these are expected to be
        2*nc values ordered as follows:
        [a1, b1, a2, b2, ... a_nc, b_nc]
        """
        hf = self._build_single_hf(self.freq[i], self.amps[i], controls)
        dhf = self._build_single_dhf(self.freq[i], self.amps[i], controls)


    def _build_single_hf(self, nc, freq, amp, controls):
        # assemble hf for one spin

        hf = np.zeros([2*nc+1, 2, 2], dtype='complex128')
        for k in xrange(0, nc):
            # the controls are ordered in reverse
            # compared to how they are placed in hf
            a = controls[-2*k-2]*amp
            b = controls[-2*k-1]*amp

            # The controls are placed symmetrically around
            # the centre of hf, so we can place them at the 
            # same time to save us some work!
            hf[k, :, :] = np.array([[0.0, 0.25*(1j*a+b)],
                                    [0.25*(1j*a-b), 0.0]])

            hf[-k-1, :, :] = np.array([[0.0, -0.25*(1j*a+b)],
                                       [0.25*(-1j*a+b), 0.0]])

        # This is the centre (with Fourier index 0)
        hf[nc] = np.array([[freq/2.0, 0.0],
                           [0.0, -freq/2.0]])

        return hf

{'fraction_non_alphanumeric': 0.11496844003606853, 'fraction_numerical': 0.02412082957619477, 'mean_word_length': 1.790566037735849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '620269', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1417, 'n_words': 474}