Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Quantum-enhanced Markov chain Monte Carlo

Tags: algorithm sample-based

This tutorial demonstrates how to implement Quantum-enhanced Markov chain Monte Carlo (QeMCMC) Layden et al. (2023) using Qamomile.


Background

Sampling the Boltzmann Distribution

In many physics and engineering problems, drawing samples x\bm{x} from a probability distribution μ(x)\mu(\bm{x}) is a central computational task. A classic example is the Boltzmann distribution from statistical mechanics:

μ(x)=1Zexp⁡(−βE(x)),Z=∑xexp⁡(−βE(x)).\mu(\bm{x}) = \frac{1}{Z} \exp\bigl(-\beta E(\bm{x})\bigr), \quad Z = \sum_{\bm{x}} \exp\bigl(-\beta E(\bm{x})\bigr).

Here, E(x)E(\bm{x}) is the energy of state x\bm{x}, β=1/T\beta = 1/T is the inverse temperature, and ZZ is the normalization constant known as the partition function. Beyond giving the probability distribution of state x\bm{x} at thermal equilibrium, sampling that targets the Boltzmann distribution is also widely used as an approach for combinatorial optimization problems.

As a concrete example of an energy function for the Boltzmann distribution, consider the Ising model. The Ising model is a system with a spin variable xi∈{−1,+1}x_i \in \{-1, +1\} at each site ii, whose energy is given by:

E(x)=−∑⟨i,j⟩Jij xixj−∑ihi xi.E(\bm{x}) = -\sum_{\langle i, j \rangle} J_{ij} \, x_i x_j - \sum_i h_i \, x_i.

Here, JijJ_{ij} is the interaction between spins and hih_i is the external magnetic field at site ii. Since the total number of states grows exponentially as 2n2^n, computing the partition function ZZ exactly becomes intractable for large nn. MCMC, introduced below, is therefore used to draw samples directly from μ(x)\mu(\bm{x}).

First, let us actually visualize the Boltzmann distribution for a small Ising model. We take a 1D ferromagnetic Ising chain (Ji,i+1=1J_{i,i+1} = 1, hi=0h_i = 0) and plot histograms of the probability aggregated by energy E(x)E(\bm{x}) for several values of the inverse temperature β\beta.

<Figure size 1200x350 with 3 Axes>

Markov chain Monte Carlo (MCMC)

Markov chain Monte Carlo (MCMC) is a general-purpose method for sampling from probability distributions. It achieves sampling from a target distribution μ(x)\mu(\bm{x}) by exploiting a stochastic process called a Markov chain. Here we introduce the Metropolis-Hastings (MH) algorithm Hastings (1970) , a common implementation of MCMC.

The MH algorithm generates a new transition x→y\bm{x} \rightarrow \bm{y} of the Markov chain according to a proposal probability Q(y∣x)Q(\bm{y}|\bm{x}), and then accepts or rejects this transition according to the acceptance probability

A(y∣x)=min⁡(1,μ(y)μ(x)⋅Q(x∣y)Q(y∣x)).A(\bm{y} | \bm{x}) = \min \left(1, \frac{\mu(\bm{y})}{\mu(\bm{x})} \cdot \frac{Q(\bm{x} | \bm{y})}{Q(\bm{y} | \bm{x})} \right).

These two steps generate the state at time t+1t+1 from the state at time tt. After sufficient time, the states of this Markov chain follow the target distribution μ(x)\mu(\bm{x}). Thus, by running enough transitions, the resulting chain states {x(t)}\{\bm{x}^{(t)}\} can be taken as the desired samples.

Let us verify this in practice. We will sample from the Boltzmann distribution we just prepared using the MH method. Many choices of proposal distribution are possible; here we use the simplest one, which picks a single random spin and flips it.

Next, we implement the step that stochastically processes the proposed transition according to the acceptance probability. Since this proposal satisfies Q(x∣y)=Q(y∣x)Q(\bm{x} \mid \bm{y}) = Q(\bm{y} \mid \bm{x}), the ratio of QQ cancels out in the acceptance probability. Therefore, when the target distribution is the Boltzmann distribution, the acceptance probability takes the following simple form

A(y∣x)=min⁡(1,μ(y)μ(x))=min⁡(1,exp⁡(−β(E(y)−E(x)))).A(\bm{y} | \bm{x}) = \min \left(1, \frac{\mu(\bm{y})}{\mu(\bm{x})}\right) = \min \left(1, \exp(-\beta (E(\bm{y}) - E(\bm{x})))\right).

Now we have a working MCMC implementation. Let us run it to draw samples.

Let us confirm that the sampled sequence follows the Boltzmann distribution. Using the samples, we estimate a physical quantity associated with the Boltzmann distribution μ(x)\mu(\bm{x}). Here we estimate the spin’s average magnetization:

⟨m⟩=∑xμ(x)m(x)\langle m \rangle = \sum_{\bm{x}} \mu(\bm{x}) m(\bm{x})

where the magnetization is

m(x)=1n∑i=1nxi.m(\bm{x}) = \frac{1}{n} \sum_{i=1}^n x_i.

Since the average magnetization is the expectation of the magnetization with respect to the Boltzmann distribution, the estimator should become more accurate as the number of samples grows and as the sample distribution gets closer to the Boltzmann distribution. Let us plot the estimator mˉt\bar{m}_t obtained from the first tt MCMC samples.

<Figure size 640x480 with 1 Axes>

Algorithm

The Quantum-enhanced MCMC algorithm is an MCMC that uses sampling from a quantum circuit as its proposal distribution Layden et al. (2023). Starting from the current state x\bm{x}, we apply a quantum circuit UU and measure in the computational basis to obtain a new state y\bm{y}. The resulting proposal distribution Q(y∣x)Q(\bm{y}|\bm{x}) is:

Q(y∣x)=∥⟨y∣U∣x⟩∥2Q(\bm{y}|\bm{x}) = \| \langle \bm{y} | U | \bm{x} \rangle \|^2

Computing this probability directly is difficult, but when the quantum circuit satisfies U=U⊤U = U^\top, the proposal distribution satisfies Q(x∣y)=Q(y∣x)Q(\bm{x} \mid \bm{y}) = Q(\bm{y} \mid \bm{x}), so the QQ terms cancel out in the acceptance probability, eliminating the need to explicitly compute QQ. For example, to sample from the Boltzmann distribution of the Ising model, we can use a trotterized time evolution under a time-independent Hamiltonian:

U(γ,t)=exp⁡(−iHt),H=(1−γ)αHM+γHC.U(\gamma, t) = \exp(-i H t), \quad \quad H = (1-\gamma) \alpha H_M + \gamma H_C.

Here, HMH_M is called the mixer Hamiltonian and generates quantum transitions between states, while HCH_C is the Ising Hamiltonian. γ∈[0,1]\gamma \in [0,1] is a parameter that controls the relative weights of the two terms. α\alpha is a normalization factor used to ensure that the eigenvalues of the mixer and cost Hamiltonians are on the same scale. (γ,t)(\gamma, t) are tunable parameters that determine the efficiency of the MCMC process.


Implementation

1. Preparing the Hamiltonians

Now let us implement the algorithm. First, we prepare the Ising Hamiltonian HCH_C for the model we wish to sample and the mixer Hamiltonian HMH_M for the proposal circuit UU.

2. Building the Quantum Circuit

Next, let us implement the quantum circuit. First, we prepare the quantum state ∣x⟩\ket{\bm{x}} using computational_basis_state in order to encode the current state x\bm{x} as the input state. The proposal transition uses time evolution simulation based on Trotter decomposition. We use trotterized_time_evolution to build the circuit for the Hamiltonians we just prepared.

3. Transpiling

We transpile the kernel. Running the quantum circuit requires fixed values for the Hamiltonian mixing coefficient γ\gamma and the simulation time tt. Following Christmann et al. (2025) , we set γ=0.45\gamma=0.45, t=12t=12, and Δt=0.8\Delta t = 0.8. At transpile time we bind n, order, time, and step, while keeping input_bits as a runtime parameter.

4. Incorporating the Quantum Circuit into MCMC

The quantum circuit simulation is now ready. Finally, let us plug the quantum component into the MCMC. Since the circuit’s input and output are bit strings x∈{0,1}n\bm{x} \in \{0,1\}^n, we also prepare a conversion between bit strings and spin variables s∈{1,−1}n\bm{s} \in \{1, -1\}^n.


Example Run

Let us run the QeMCMC algorithm we just implemented. We switch to a lower temperature β=1.0\beta = 1.0 where classical local updates mix slowly, so that we can observe the behavior of the quantum proposal distribution under conditions that are harder for the classical baseline. For a fair comparison, we also run a classical MCMC at the same β=1.0\beta = 1.0 alongside the quantum run.

We compute the estimator of the average magnetization and compare it with the classical MCMC result obtained at the same β=1.0\beta = 1.0.

<Figure size 640x480 with 1 Axes>

Summary

In this tutorial, we began with a review of classical Metropolis-Hastings MCMC and then implemented Quantum-enhanced MCMC (QeMCMC) on top of Qamomile, using the quantum circuit U(γ,t)=exp⁡(−itH)U(\gamma, t) = \exp(-i t H) with H=(1−γ)HM+γHCH = (1-\gamma) H_M + \gamma H_C as the proposal distribution. Specifically, after preparing the mixer and cost Hamiltonians with qamomile.observable, we built the proposal circuit via Suzuki-Trotter time evolution using trotterized_time_evolution inside an @qkernel. Finally, we plugged the quantum proposal into the existing MH loop through the transpiled executor and compared the convergence of the average magnetization for classical MCMC and QeMCMC on the same Ising chain, confirming that the end-to-end quantum-classical hybrid loop behaves as intended.

References
  1. Layden, D., Mazzola, G., Mishmash, R. V., Motta, M., Wocjan, P., Kim, J.-S., & Sheldon, S. (2023). Quantum-enhanced Markov chain Monte Carlo. Nature, 619(7969), 282–287. 10.1038/s41586-023-06095-4
  2. Hastings, W. K. (1970). Monte Carlo sampling methods using Markov chains and their applications. Biometrika, 57(1), 97–109. 10.1093/biomet/57.1.97
  3. Christmann, J., Ivashkov, P., Chiurco, M., & Mazzola, G. (2025). From quantum-enhanced to quantum-inspired Monte Carlo. Physical Review A, 111(4). 10.1103/physreva.111.042615