qemcmc
qemcmc ¶
BaseCircuitMaker ¶
Constructs and simulates quantum circuits used to generate QeMCMC proposals.
This class builds the Hamiltonian corresponding to a given energy model and simulates its time evolution using PennyLane. Starting from a classical bitstring configuration, the circuit performs Trotterised quantum evolution and samples a new configuration from the resulting quantum state.
The generated sample serves as the proposal state in the quantum-enhanced MCMC algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
EnergyModel
|
Energy model defining the problem Hamiltonian. |
required |
Source code in src/qemcmc/circuits/basecircuitmaker.py
get_problem_hamiltonian ¶
Construct the problem Hamiltonian from symmetric coupling tensors.
This method supports both 'ising' (-1/+1) and 'binary' (0/1) models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
couplings
|
List[BinaryPolynomial]
|
A list of coupling polynomials. |
required |
sign
|
int
|
A sign to apply to the Hamiltonian. Default is |
1
|
Returns:
| Type | Description |
|---|---|
Hamiltonian
|
The problem Hamiltonian. |
Source code in src/qemcmc/circuits/basecircuitmaker.py
get_mixer_hamiltonian ¶
Constructs the Mixer Hamiltonian: Σ X_i.
This can be for the full system or a subgroup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_wires
|
int
|
The number of wires (qubits) for the mixer. If None, uses the total number of qubits. |
None
|
Returns:
| Type | Description |
|---|---|
Hamiltonian
|
The mixer Hamiltonian. |
Source code in src/qemcmc/circuits/basecircuitmaker.py
check_Hamiltonian ¶
Utility function to print the Energy of the Hamiltonian to be simulated. Naturally, for a classical problem, the energy of the Hamiltonian wrt to some basis state b H|b> = E|b> should equal the energy of the corresponding state in the classical model. This is a useful sanity check to ensure that the Hamiltonian is being constructed correctly from the coupling tensors.
Source code in src/qemcmc/circuits/basecircuitmaker.py
QuenchCircuitMaker ¶
Bases: BaseCircuitMaker
The total Hamiltonian simulated by the circuit is
``H = γ H_mixer + (1 - γ) α H_problem``
where H_problem encodes the classical energy model and H_mixer
corresponds to a transverse-field term. The evolution time t and
number of Trotter steps r are passed per call, giving an effective
Trotter step size of δt = t / r. The evolution is approximated
using Trotterisation via qml.ApproxTimeEvolution.
Source code in src/qemcmc/circuits/quenchcircuitmaker.py
get_state_vector ¶
Evolve the initial state and return the final state vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
Input bitstring representing the initial state. |
required |
weights
|
list of float
|
Coefficients for the problem Hamiltonian terms. |
required |
time
|
float
|
Total evolution time. |
required |
r
|
int
|
Number of Trotter steps for the approximate time evolution. |
required |
mix_weight
|
float
|
Coefficient for the mixer Hamiltonian. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The final state vector. |
Notes
The total Hamiltonian simulated by the circuit is a weighted sum of the problem Hamiltonian terms and the mixer Hamiltonian:
In Ferguson et al. (2025) [arXiv:2506.19538], we use gammas to weight the entire problem Hamiltonian vs the mixer vs the constraint Hamiltonian, such that the total Hamiltonian is:
H = g_p * H_p + g_m * H_m + g_c * H_c
but here we allow for separate weights for each coupling tensor term, as well as a separate gamma for the mixer. The total Hamiltonian is then:
H = (w_b1*H_b1 + w_b2*H_b2 + ... + w_b2*H_bm) + g_m * H_m
In other words, the constraint hamiltonian is absorbed in the coupling list, and weighted by the corresponding gamma in the gammas list. This allows for more flexible weighting of different terms, and also allows us to use the same code for both constrained and unconstrained problems (by simply including or excluding the constraint Hamiltonian in the coupling list and adjusting the gammas accordingly).
Note that it is assumed that each term is already normalised appropriately, so the gammas can be interpreted as the relative weights of each term in the total Hamiltonian.
Source code in src/qemcmc/circuits/quenchcircuitmaker.py
get_sample ¶
Generate a single sample by evolving the system and measuring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s_cg
|
str
|
Input bitstring for the subgroup. |
required |
time
|
float
|
Total evolution time. |
required |
r
|
int
|
Number of Trotter steps for the approximate time evolution. |
required |
mix_weight
|
float
|
Coefficient for the mixer Hamiltonian. |
required |
local_couplings
|
list
|
Coupling tensors for the subgroup. |
required |
weights
|
list of float
|
Coefficients for the problem Hamiltonian terms. Defaults to ones. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A single bitstring sample. |
Source code in src/qemcmc/circuits/quenchcircuitmaker.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
update ¶
Update a bitstring by evolving a subgroup.
This performs a time evolution on a coarse-grained Hamiltonian to get s' from s.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The initial bitstring. |
required |
subgroup_choice
|
list of int
|
Indices of the subgroup to evolve. |
required |
local_couplings
|
list
|
Coupling tensors for the subgroup. |
required |
gamma
|
float
|
Mixing parameter. |
required |
time
|
float
|
Evolution time. |
required |
r
|
int
|
Number of Trotter steps for the approximate time evolution. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The updated bitstring s'. |
Source code in src/qemcmc/circuits/quenchcircuitmaker.py
AdiabaticCircuitMaker ¶
Bases: BaseCircuitMaker
Constructs and simulates adiabatic quantum circuits for QeMCMC proposals.
The total time-dependent Hamiltonian simulated by the circuit is:
``H(t) = h(t) * mix_weight * H_mixer + H_problem``
where H_problem encodes the classical energy model and H_mixer
corresponds to a transverse-field term. The dimensionless schedule function
h(t) ramps linearly from 0 to 1 over ramp duration alpha, remains at 1
for plateau duration kappa, and ramps down from 1 to 0 over duration alpha
(total duration 2 * alpha + kappa).
The time-dependent evolution is approximated using second-order Trotterisation
via qml.ApproxTimeEvolution over r = max(1, round((2 * alpha + kappa) / dt)) steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
EnergyModel
|
Energy model defining the target Boltzmann distribution. |
required |
Source code in src/qemcmc/circuits/adiabaticcircuitmaker.py
get_sample ¶
Generate a single sample by evolving the system under a time-dependent Hamiltonian and measuring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s_cg
|
str
|
Input bitstring for the subgroup. |
required |
alpha
|
float
|
Duration of the adiabatic ramp. |
required |
kappa
|
float
|
Duration of the plateau. |
required |
mix_weight
|
float
|
Coefficient for the mixer Hamiltonian. |
required |
local_couplings
|
list
|
Coupling tensors for the subgroup. |
required |
weights
|
list of float
|
Coefficients for the problem Hamiltonian terms. Defaults to ones. |
None
|
dt
|
float
|
Timestep size for second order Trotter evolution. Defaults to 0.3. |
0.3
|
Returns:
| Type | Description |
|---|---|
str
|
A single bitstring sample. |
Source code in src/qemcmc/circuits/adiabaticcircuitmaker.py
linear_schedule ¶
Calculates the linear ramp factor h(t) with a plateau of duration kappa and ramp duration alpha.
The schedule satisfies h(t): 0 -> 1 -> 1 -> 0 as t: 0 -> alpha -> alpha + kappa -> 2 * alpha + kappa.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
float
|
Current time during the evolution. |
required |
alpha
|
float
|
Duration of the linear ramp. |
required |
kappa
|
float
|
Duration of the constant plateau. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The instantaneous mixer weight factor in [0, 1]. |
Source code in src/qemcmc/circuits/adiabaticcircuitmaker.py
get_state_vector ¶
Evolve the initial state under the adiabatic Hamiltonian and return the final state vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s_cg
|
str
|
Input bitstring for the subgroup. |
required |
alpha
|
float
|
Duration of the adiabatic ramp. |
required |
kappa
|
float
|
Duration of the plateau. |
required |
mix_weight
|
float
|
Coefficient for the mixer Hamiltonian. |
required |
weights
|
list of float
|
Coefficients for the problem Hamiltonian terms. Defaults to ones. |
None
|
dt
|
float
|
Timestep size for second order Trotter evolution. Defaults to 0.3. |
0.3
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The final quantum state vector. |
Source code in src/qemcmc/circuits/adiabaticcircuitmaker.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
update ¶
Update a bitstring by evolving a subgroup.
This performs a time evolution on a coarse-grained Hamiltonian to get s' from s.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The initial bitstring. |
required |
subgroup_choice
|
list of int
|
Indices of the subgroup to evolve. |
required |
local_couplings
|
list
|
Coupling tensors for the subgroup. |
required |
gamma
|
float
|
Mixing parameter. |
required |
alpha
|
float
|
Adiabatic ramp time. |
required |
kappa
|
float
|
Plateau time. |
required |
dt
|
float
|
Trotter step size for the approximate time evolution. Defaults to 0.3. |
0.3
|
Returns:
| Type | Description |
|---|---|
str
|
The updated bitstring s'. |
Source code in src/qemcmc/circuits/adiabaticcircuitmaker.py
CoarseGraining ¶
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
|
m
|
int
|
The number of partitions to generate. Ignored if user-specified subgroups are provided. |
1
|
Source code in src/qemcmc/coarse_grain.py
sample ¶
Randomly samples a subgroup according to the specified probability distribution.
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
ConstraintModel ¶
Bases: EnergyModel
A subclass of EnergyModel that incorporates a constraint function to define valid configurations.
The constraint function takes a state as input and returns True if the state is valid (satisfies the constraint) and False otherwise. The energy of invalid states is set to infinity, effectively excluding them from the Boltzmann distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of spins in the model. |
required |
constraint_couplings
|
list[BinaryPolynomial]
|
List of coupling tensors (dimod.BinaryPolynomial) defining the constraint. |
required |
constraint_signs
|
list[float]
|
Sign convention(s) for the constraint couplings. |
required |
couplings
|
list[BinaryPolynomial]
|
List of coupling tensors (dimod.BinaryPolynomial) defining the energy function. |
required |
constraint_func
|
Callable[[str], bool]
|
A function that takes a state (string representation of spin configuration) and returns True if the state satisfies the constraint, and False otherwise. |
required |
name
|
str
|
Optional label for the model (used in plotting / logging). |
required |
cost_function_signs
|
list[float]
|
Sign convention(s) used by downstream components (e.g. proposal/acceptance conventions). |
required |
model_type
|
str
|
Type of model, either 'ising' or 'binary'. This determines how the binary states are interpreted and how the energy is calculated. 'ising' models use spin values {-1, +1}, while 'binary' models use binary values {0, 1}. |
required |
Notes
- The energy of any state that does not satisfy the constraint is set to infinity, which means such states will have zero probability in the Boltzmann distribution.
- This class can be used to model systems with hard constraints on the configurations, such as certain combinatorial optimization problems or physical systems with forbidden states.
Source code in src/qemcmc/model/constraint_model.py
get_initial_states_constraint ¶
Generates a list of random initial states that satisfy the constraint function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_initial_states
|
int
|
The number of initial states to generate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
A list of random initial states that are valid according to the constraint. |
Source code in src/qemcmc/model/constraint_model.py
get_constraint_energy ¶
Calculate the energy contribution from the constraint couplings for a given state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
The state for which to calculate the constraint energy. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The energy contribution from the constraint couplings for the given state. |
Source code in src/qemcmc/model/constraint_model.py
get_total_energy ¶
Calculate the total energy of a given state, including both the regular energy and the constraint energy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
The state for which to calculate the total energy. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The total energy of the given state, including contributions from both the regular couplings and the constraint couplings. |
Source code in src/qemcmc/model/constraint_model.py
EnergyModel ¶
EnergyModel(
n,
couplings=[],
name=None,
cost_function_signs=[-1, -1],
model_type="ising",
manual_get_energy=None,
)
A base class for energy models for a classical energy function over n spins, defined by dimod.BinaryPolynomial coupling tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of spins. |
required |
couplings
|
list of dimod.BinaryPolynomial or list of np.ndarray
|
List of dimod.BinaryPolynomial representing interaction tensors. |
[]
|
name
|
str
|
Optional label for the model (used in plotting / logging). |
None
|
cost_function_signs
|
list of float
|
Sign convention(s) used by downstream components (e.g. proposal/acceptance conventions).
For example, |
[-1, -1]
|
model_type
|
str
|
Type of model, either 'ising' or 'binary'. This determines how the binary states are interpreted and how the energy is calculated. 'ising' models use spin values {-1, +1}, while 'binary' models use binary values {0, 1}. |
'ising'
|
manual_get_energy
|
callable
|
Optional custom energy function. If provided, this function will be used to calculate energies instead of the default methods. Must agree on energy values with the default method for a sample of states, otherwise a ValueError is raised. |
None
|
Notes
- Energies are computed by mapping binary states
{0,1}to spin values{-1,+1}internally. - Brute-force methods such as
get_all_energiesscale as O(2^n) and are intended only for small systems.
Source code in src/qemcmc/model/energy_model.py
get_energy ¶
Returns the energy of a given state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
Binary or spin state string. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Energy of the state. |
Source code in src/qemcmc/model/energy_model.py
get_initial_states ¶
Generates a list of random initial states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_initial_states
|
int
|
The number of initial states to generate. |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
A list of random initial states. |
Source code in src/qemcmc/model/energy_model.py
calculate_energy ¶
Calculate the energy of a given state for an arbitrary-order Ising/binary model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str, list, tuple, or np.ndarray
|
State configuration. Can be: - Binary: "011", [0, 1, 1], (0, 1, 1), etc. - Spin: [-1, 1, 1], (-1, 1, 1), etc. |
required |
couplings
|
list of dimod.BinaryPolynomial
|
List of coupling polynomials defining the interaction terms. |
required |
cost_function_signs
|
list of float
|
Sign convention applied to each coupling term. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Total energy of the state. |
Source code in src/qemcmc/model/energy_model.py
get_subgroup_couplings ¶
Calculates local couplings for a subgroup of spins.
Spins outside the specified subgroup are treated as frozen constants, and their values are absorbed into the couplings of the subgroup. This is useful for calculating local effective Hamiltonians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subgroup
|
list of int
|
A list of indices of the spins in the subgroup. |
required |
current_state
|
str
|
The current state of the full system, as a binary string. |
required |
coupling_weights
|
list of float
|
Optional weights for the couplings. This can be used to effectively remove certain couplings from the proposal (e.g., constraints) by setting their weight to 0. It is important not to normalize the couplings after applying these weights. |
None
|
Returns:
| Type | Description |
|---|---|
list of dimod.BinaryPolynomial
|
A new list of coupling polynomials for the subgroup. |
Notes
It might seem off to do the reweighting at this point, but here are reasons why I [SF] did it! Basically, when you get the subgroup couplings, the terms jumble up, so the returned local coupling list necessarily does not respect the order etc. of the different terms in the original.
Source code in src/qemcmc/model/energy_model.py
calculate_alpha ¶
Compute alpha = sqrt(n) / sqrt(sum of squares of UNIQUE coupling coefficients), assuming coupling tensors are symmetric representations.
Any non-symmetric 2-body input raises ValueError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of spins. |
required |
couplings
|
list of dimod.BinaryPolynomial
|
Couplings to use. Defaults to self.couplings if not provided. |
None
|
eps
|
float
|
Small threshold to avoid division by zero. |
1e-15
|
Returns:
| Type | Description |
|---|---|
float
|
Normalisation factor for the couplings. |
Source code in src/qemcmc/model/energy_model.py
get_all_energies ¶
Calculate the energies for all possible spin states.
This method generates all possible spin states for the system and calculates the energy for each state.
Returns:
| Type | Description |
|---|---|
ndarray
|
An array containing the energies of all possible spin states. |
Source code in src/qemcmc/model/energy_model.py
get_lowest_energies ¶
Retrieve the lowest energy states and their degeneracies.
This method computes all possible energies and then finds the specified number of lowest energy states along with their degeneracies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_states
|
int
|
The number of lowest energy states to retrieve. |
required |
return_configurations
|
bool
|
Whether to also return the corresponding configurations of the lowest energy states. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
lowest_energies |
ndarray
|
The lowest energy values. |
lowest_energy_degeneracy |
ndarray
|
The degeneracies of the corresponding energy values. |
lowest_configs |
(list, optional)
|
List of configurations corresponding to each energy (only returned if return_configurations=True). |
Notes
This method is intended for small instances due to its brute-force nature, which is extremely memory intensive and slow.
Source code in src/qemcmc/model/energy_model.py
find_lowest_values ¶
Find the lowest unique values in an array and their degeneracies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
ndarray
|
The input array from which to find the lowest values. |
required |
num_values
|
int
|
The number of lowest unique values to find. Defaults to 5. |
5
|
Returns:
| Name | Type | Description |
|---|---|---|
lowest_values |
ndarray
|
The lowest unique values in the array. |
degeneracy |
ndarray
|
The counts of each of the lowest unique values. |
Source code in src/qemcmc/model/energy_model.py
get_lowest_energy ¶
Calculate and return the lowest energy from all possible energies.
This method uses a brute force approach to find the lowest energy, making it extremely memory intensive and slow. It is recommended to use this method only for small instances.
Returns:
| Type | Description |
|---|---|
float
|
The lowest energy value. |
Notes
If the lowest energy has already been calculated and stored
in self.lowest_energy, it will return that value directly
to save computation time.
Source code in src/qemcmc/model/energy_model.py
get_boltzmann_factor ¶
Get un-normalised boltzmann probability of a given state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
Configuration of spins for which probability is to be calculated. |
required |
beta
|
float
|
Inverse temperature (1/T) at which the probability is to be calculated. Defaults to 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Un-normalised Boltzmann probability of the given state. |
Source code in src/qemcmc/model/energy_model.py
get_boltzmann_factor_from_energy ¶
Get un-normalized Boltzmann probability for a given energy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
E
|
float
|
Energy for which the Boltzmann factor is to be calculated. |
required |
beta
|
float
|
Inverse temperature (1/T) at which the probability is to be calculated. Defaults to 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
The un-normalized Boltzmann probability for the given energy. |
Source code in src/qemcmc/model/energy_model.py
ModelMaker ¶
Utility class for constructing standard Ising energy models used in simulations and experiments.
Source code in src/qemcmc/model/model_maker.py
make_fully_connected_binary ¶
Transforms the existing Ising couplings into an mathematically equivalent QUBO model via s = 2x - 1.
Source code in src/qemcmc/model/model_maker.py
ClassicalProposal ¶
Bases: Proposal
Classical Markov Chain Monte Carlo proposer.
This class implements purely classical proposal mechanisms for MCMC. New candidate states are generated either by sampling a completely random (uniform) configuration, or by performing a local single-spin or two-spin flip.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
EnergyModel
|
Energy model defining the target Boltzmann distribution. |
required |
method
|
str
|
Proposal mechanism used to generate candidate states.
Default is |
'uniform'
|
Source code in src/qemcmc/sampler/classical_proposal.py
update_uniform ¶
Proposes a new state by generating a random bitstring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_state_bitstring
|
str
|
The current state represented as a bitstring (not used, but required by the API). |
required |
Returns:
| Type | Description |
|---|---|
str
|
A new random state bitstring of the same length. |
Source code in src/qemcmc/sampler/classical_proposal.py
update_local ¶
Proposes a new state by flipping a single randomly chosen spin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_state_bitstring
|
str
|
The current state represented as a bitstring. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The new state bitstring after flipping one spin. |
Source code in src/qemcmc/sampler/classical_proposal.py
update_2local ¶
Proposes a new state by flipping two randomly chosen spins.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_state_bitstring
|
str
|
The current state represented as a bitstring. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The new state bitstring after flipping two spins. |
Source code in src/qemcmc/sampler/classical_proposal.py
Proposal ¶
Bases: ABC
Abstract base class for producing proposals for Markov Chain Monte Carlo algorithms.
Subclasses implement the proposal mechanism by defining an
update(state) method that generates a candidate state from the current one
(e.g. single-spin flips, block updates, or quantum proposals).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
EnergyModel
|
Energy model defining the target distribution over spin configurations. |
required |
Source code in src/qemcmc/sampler/proposal.py
update
abstractmethod
¶
Generate a candidate state from the current state using the proposal mechanism.
This method should be implemented by subclasses to define the specific proposal strategy (e.g., single-spin flips, block updates, or quantum proposals).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
MCMCState
|
The current state of the Markov chain. |
required |
Returns:
| Type | Description |
|---|---|
MCMCState
|
A new candidate state. |
Source code in src/qemcmc/sampler/proposal.py
QuenchProposal ¶
QuenchProposal(
model,
gamma,
time,
r=None,
delta_t=None,
coarse_graining=None,
coupling_weights=None,
)
Bases: Proposal
Quantum-enhanced Markov Chain Monte Carlo proposer.
This class implements the proposal mechanism of the quantum-enhanced MCMC by Layden et al.. Candidate states are generated by simulating the quantum time evolution of a transverse-field Hamiltonian.
The quantum proposal circuit is constructed using :class:QuenchCircuitMaker
and can optionally operate on coarse-grained subgroups of spins to
improve scalability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
EnergyModel
|
Energy model defining the target Boltzmann distribution. |
required |
gamma
|
float | tuple[float, float]
|
Transverse field strength (Γ). If a tuple is provided, a value is sampled uniformly from the range [min, max] at each step. |
required |
time
|
float | tuple[float, float]
|
Total evolution time. If a tuple is provided, a value is sampled uniformly from the range [min, max] at each step. |
required |
r
|
int | None
|
Number of Trotter steps. Mutually exclusive with |
None
|
delta_t
|
float | None
|
Trotter step size. Mutually exclusive with |
None
|
coarse_graining
|
CoarseGraining | None
|
A coarse-graining scheme to define spin subgroups. If None, no coarse-graining
is used. By default |
None
|
coupling_weights
|
list[float | tuple[float, float]] | None
|
Weights for the coupling tensors. If a tuple is provided, a weight is
sampled uniformly from the range. This allows for dynamic adjustment
of the influence of different terms in the Hamiltonian during the proposal
step. By default |
None
|
Notes
The proposal step simulates the time evolution under a transverse-field Hamiltonian defined by the energy model and measures the resulting state to produce a candidate configuration. This proposal is then accepted or rejected using the Metropolis criterion to ensure convergence to the target Boltzmann distribution.
Exactly one of r or delta_t should be passed in as an argument. If neither is
given, a default Δt of 0.8 is used.
Source code in src/qemcmc/sampler/quenchproposal.py
update ¶
Perform sequential quantum updates across non-overlapping subgroups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_state
|
str
|
Current state of the system as a bitstring. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Updated state of the system as a bitstring. |
Source code in src/qemcmc/sampler/quenchproposal.py
sample_hyperparams ¶
Sample hyperparameters (coupling weights, gamma, time, r) for the current proposal step.
Returns:
| Type | Description |
|---|---|
tuple[list[float], float, float, int]
|
(final_coupling_weights, gamma, time, r) for this proposal step. |
Source code in src/qemcmc/sampler/quenchproposal.py
AdiabaticProposal ¶
AdiabaticProposal(
model,
gamma,
alpha,
kappa,
delta_t=None,
coarse_graining=None,
coupling_weights=None,
)
Bases: Proposal
Quantum-enhanced Markov Chain Monte Carlo proposer with adiabatic dressing.
This class implements the adiabatic dressing proposal mechanism for quantum-enhanced MCMC (Ferguson & Wallden / Hsieh et al., arXiv:2603.28076). Candidate states are generated by simulating the quantum time evolution of a time-dependent transverse-field Hamiltonian:
``H(t) = h(t) * gamma * H_mixer + H_problem``
The schedule function h(t) ramps the transverse field up from 0 to 1 over ramp
duration alpha, holds it at 1 for plateau duration kappa, and ramps it down
from 1 to 0 over duration alpha (total duration 2 * alpha + kappa).
For long evolution times, adiabaticity preserves the ground/eigenstates, whereas for intermediate
times diabatic transitions induce proposals concentrated near the target energy levels.
The quantum proposal circuit is constructed using :class:AdiabaticCircuitMaker
and can optionally operate on coarse-grained subgroups of spins to
improve scalability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
EnergyModel
|
Energy model defining the target Boltzmann distribution. |
required |
gamma
|
float | tuple[float, float]
|
Transverse field strength. If a tuple is provided, a value is sampled uniformly from the range [min, max] at each step. |
required |
alpha
|
float | tuple[float, float]
|
Duration of the linear adiabatic ramp. If a tuple is provided, a value is sampled uniformly from the range [min, max] at each step. |
required |
kappa
|
float | tuple[float, float]
|
Duration of the constant plateau. If a tuple is provided, a value is sampled uniformly from the range [min, max] at each step. |
required |
delta_t
|
float | None
|
Trotter step size for the approximate time evolution. The number of Trotter
steps is derived as |
None
|
coarse_graining
|
CoarseGraining | None
|
A coarse-graining scheme to define spin subgroups. If None, no coarse-graining is used. Defaults to None. |
None
|
coupling_weights
|
list[float | tuple[float, float]] | None
|
Weights for the coupling tensors. If a tuple is provided, a weight is sampled uniformly from the range. Defaults to ones. |
None
|
Notes
The proposal step simulates time evolution under a time-dependent transverse-field Hamiltonian and measures the resulting state to produce a candidate configuration. This proposal is then accepted or rejected using the Metropolis criterion to ensure convergence to the target Boltzmann distribution.
Source code in src/qemcmc/sampler/adiabaticproposal.py
update ¶
Perform sequential quantum updates across non-overlapping subgroups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_state
|
str
|
Current state of the system as a bitstring. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Updated state of the system as a bitstring. |
Source code in src/qemcmc/sampler/adiabaticproposal.py
sample_hyperparams ¶
Sample hyperparameters (coupling weights, gamma, alpha, kappa) for the current proposal step.
Returns:
| Type | Description |
|---|---|
tuple[list[float], float, float, float]
|
(final_coupling_weights, gamma, alpha, kappa) for this proposal step. |
Source code in src/qemcmc/sampler/adiabaticproposal.py
SpectralGap ¶
Bases: Runner
Class that finds the spectral gap, and the acceptance and proposal matrices for a given mcmc.
Source code in src/qemcmc/spectralgap.py
find_acceptance_matrix ¶
Function to find the acceptance matrix for a given model instance.
Returns: A (np.ndarray): The acceptance matrix for the mcmc
Source code in src/qemcmc/spectralgap.py
find_proposal_matrix_local ¶
Function to find the proposal matrix for a given local chain.
Returns: Q (np.ndarray): The Q matrix for local proposal
Source code in src/qemcmc/spectralgap.py
find_proposal_matrix_uniform ¶
Function to find the proposal matrix for a given uniform chain.
Returns: Q (np.ndarray): The Q matrix for uniform proposal
Source code in src/qemcmc/spectralgap.py
find_proposal_matrix_quantum ¶
Function to find the proposal matrix for a given QeMCMCChain object.
Returns: Q (np.ndarray): The Q matrix for quantum proposal
Source code in src/qemcmc/spectralgap.py
find_proposal_matrix_adiabatic ¶
Function to find the proposal matrix for a given QeMCMCChain object.
Returns: Q (np.ndarray): The Q matrix for quantum proposal
Source code in src/qemcmc/spectralgap.py
find_proposal_matrix ¶
Function to find the proposal matrix for a given mcmc. This is not done by brute force
Source code in src/qemcmc/spectralgap.py
find_spectral_gap ¶
Function to find the spectral gap of a given mcmc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
The acceptance matrix for the mcmc (optional, if not given, will be calculated) |
None
|
|
Q
|
The proposal matrix for the mcmc (optional, if not given, will be calculated) |
None
|
Source code in src/qemcmc/spectralgap.py
MCMCChain
dataclass
¶
Container for the sequence of states produced during an MCMC run.
This class records all proposed states, tracks accepted configurations, and provides helper methods for extracting trajectories, energies, and empirical distributions from the Markov chain.
Source code in src/qemcmc/utils/helpers.py
MCMCState
dataclass
¶
Represents a single step in an MCMC trajectory.
Stores the proposed configuration, whether it was accepted by the Metropolis rule, its energy, and the position of the step in the chain.
get_all_possible_states ¶
Returns all possible binary strings of length n=num_spins
Paremeters: num_spins: n length of the bitstring Returns: list: A list of all possible binary strings of length num_spins.
Source code in src/qemcmc/utils/helpers.py
get_random_state ¶
Generate a random state for a given number of spins.
Parameters: num_spins (int): The number of spins in the system. Returns: str: A bitstring representing the random state.