runners
qemcmc.sampler.runners ¶
Runner ¶
Base class for running MCMC routines. Subclasses implement specific MCMC based algorithms.
get_acceptance_probability ¶
Calculate the Metropolis acceptance probability.
This computes exp(-(E(s') - E(s)) / T), used to determine the acceptance probability of a new state s' given the current state s.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
energy_s
|
float
|
Energy of the current state |
required |
energy_sprime
|
float
|
Energy of the proposed state |
required |
temperature
|
float
|
Temperature T, default is |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
The acceptance probability. |
Source code in src/qemcmc/sampler/runners.py
is_accepted ¶
Decide whether to accept a proposed state.
Accepts state s' with probability A = min(1, exp(-(E(s')-E(s))/T)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
energy_s
|
float
|
Energy of the current state s. |
required |
energy_sprime
|
float
|
Energy of the proposed state s'. |
required |
temperature
|
float
|
Temperature T, default is |
1.0
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the new state is accepted, False otherwise. |
Source code in src/qemcmc/sampler/runners.py
MCMCRunner ¶
Bases: Runner
Orchestrates a standard MCMC run loop.
This runner uses a given proposal sampler and energy model to generate a Markov chain of states. It manages state updates, energy evaluations, and Metropolis acceptance tests. The sampler targets the Boltzmann distribution p(s) ∝ exp(-E(s) / T).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
EnergyModel
|
The energy model defining the system. |
required |
temp
|
float
|
The temperature for the Metropolis acceptance criterion. |
required |
Source code in src/qemcmc/sampler/runners.py
run ¶
Run the MCMC simulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
proposer
|
Proposal
|
The proposal engine for generating new states. |
required |
n_hops
|
int
|
The number of MCMC steps to perform. |
required |
initial_state
|
str
|
The starting bitstring for the chain. If None, a random state is generated. |
None
|
name
|
str
|
A name for the MCMC chain. |
None
|
verbose
|
bool
|
Enable progress bar and print statements. |
False
|
sample_frequency
|
int
|
The frequency at which to sample states for the chain. Default is |
1
|
Returns:
| Type | Description |
|---|---|
MCMCChain
|
The generated Markov chain of states. |
Source code in src/qemcmc/sampler/runners.py
ConstrainedMCMCRunner ¶
Bases: Runner
Orchestrates an MCMC run that enforces a hard constraint.
If a proposed state does not satisfy the constraint, it is immediately rejected without computing its energy or testing the Metropolis criterion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
ConstraintModel
|
A model that includes a constraint function. |
required |
temp
|
float
|
The temperature for the Metropolis acceptance test. |
required |
reject_invalid
|
bool
|
Proposed states that violate the constraint are rejected. Default is |
True
|
Source code in src/qemcmc/sampler/runners.py
run ¶
run(
proposer,
n_hops,
initial_state=None,
name=None,
verbose=False,
sample_frequency=1,
return_rejections=True,
)
Run the constrained MCMC simulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
proposer
|
Proposal
|
The proposal engine for generating new states. |
required |
n_hops
|
int
|
The number of MCMC steps to perform. |
required |
initial_state
|
str
|
The starting bitstring for the chain. Must satisfy the constraint.
If None, a valid random state is sought. Default is |
None
|
name
|
str
|
A name for the MCMC chain. Default is |
None
|
verbose
|
bool
|
Enables progress bar and print statements. Default is |
False
|
sample_frequency
|
int
|
The frequency at which to sample states for the chain. Default is |
1
|
return_rejections
|
bool
|
Return the number of rejections due to constraint violations. Default is |
True
|
Returns:
| Type | Description |
|---|---|
tuple[MCMCChain, int]
|
A tuple containing the generated Markov chain and the number of rejections due to constraints. If return_rejections is False, only the MCMCChain is returned. |
Source code in src/qemcmc/sampler/runners.py
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | |