Skip to content

coarse_grain

qemcmc.coarse_grain

CoarseGraining

CoarseGraining(
    n,
    subgroups=None,
    subgroup_probs=None,
    repeated=True,
    m=1,
)

CoarseGraining class to generate partitions of spins for quantum proposals.

Parameters:

Name Type Description Default
n int

Number of spins in the system.

required
subgroups list[list[int]]

A list of subgroups, where each subgroup is a list of spin indices. If None, the entire set of spins is treated as one subgroup.

None
subgroup_probs list[float]

A list of probabilities corresponding to each subgroup, used for weighted random selection. Must sum to 1. If None, subgroups are selected uniformly at random.

None
repeated bool

If True, then multiple subgroups are run on the quantum computer in serial, if not then only one subgroup is selected at random and run on the quantum computer. Default is True.

True
m int

The number of partitions to generate. Ignored if user-specified subgroups are provided.

1
Source code in src/qemcmc/coarse_grain.py
def __init__(self, n, subgroups=None, subgroup_probs=None, repeated=True, m: int = 1):

    if not isinstance(m, int):
        raise TypeError("m must be an integer.")

    self._user_specified = subgroups is not None



    if subgroups is not None:
        if subgroup_probs is None:
            subgroup_probs = [1.0 / len(subgroups)] * len(subgroups)
        if m is not None:
            print("Cannot provide m and subgroups for course graining. Ignoring m")
    if self._user_specified:
        validate_subgroups(subgroups=subgroups, subgroup_probs=subgroup_probs, n_spins=n)

    self.m = m
    self.n = n
    self.subgroups = subgroups
    self.subgroup_probs = subgroup_probs
    self.repeated = repeated

    if self.repeated and self._user_specified:
        if not np.all([len(s) == len(subgroups[0]) for s in subgroups]):
            raise NotImplementedError("If providing own subgroups, repeated must be set to False as it is not obvious how one performs repeated coarse-graining without knowledge of the subgroups. If length, l, of each subrgoup is equal, we continue assuming l = n//m.")
        else:
            print("Provided subgroups are all of same length, so repeated coarse-graining will be performed accross a random choice of subgroups. This means that the same variables may be chosen multiple times within the same proposal.")

sample

sample()

Randomly samples a subgroup according to the specified probability distribution.

Source code in src/qemcmc/coarse_grain.py
def sample(self) -> list[int]:
    """
    Randomly samples a subgroup according to the specified probability distribution.
    """
    idx = np.random.choice(len(self.subgroups), p=self.subgroup_probs)
    return self.subgroups[idx]

get_partitions

get_partitions()

Returns partitions of spins for sequential quantum updates.

If the user provided explicit subgroups at initialisation, those are sampled accordingly. returned directly (all if repeated=True, otherwise just the first). If no subgroups were specified, random disjoint partitions of approximate size n/m are generated.

Source code in src/qemcmc/coarse_grain.py
def get_partitions(self) -> list[list[int]]:
    """
    Returns partitions of spins for sequential quantum updates.

    If the user provided explicit subgroups at initialisation, those are sampled accordingly.
    returned directly (all if ``repeated=True``, otherwise just the first).
    If no subgroups were specified, random disjoint partitions of
    approximate size n/m are generated.


    """

    if self._user_specified:

        if self.repeated:
            number_of_subgroups_that_fit = self.n//np.max([len(sg) for sg in self.subgroups])

            choices = np.random.choice(np.arange(0,len(self.subgroups)), size = number_of_subgroups_that_fit, p = self.subgroup_probs, replace=False)

            chosen_subgroups = []
            for choice in choices:
                chosen_subgroups.append(self.subgroups[choice])
            return chosen_subgroups
        else:
            chosen_subgroup = np.random.choice(self.subgroups, p=self.subgroup_probs)

            return [chosen_subgroup]
    else:
        spins = np.arange(self.n)
        np.random.shuffle(spins)
        # array_split handles uneven divisions gracefully
        chunks = [list(chunk) for chunk in np.array_split(spins,self.m)]
        if self.repeated:
            return chunks
        else:
            return [chunks[0]]