# Sampling from the D-Wave QPU¶

Sampling from energy-based distributions is a computationally intensive task that is an excellent match for the way that the D-Wave system solves problems; that is, by seeking low-energy states. Samples from the D-Wave QPU can be obtained quickly and provide an advantage over sampling from classical distributions.

When training a probabilistic model, we need a well-characterized distribution; otherwise, it is difficult to calculate gradients and we have no guarantee of convergence. While both classical Boltzmann and quantum Boltzmann distributions are well characterized, all but the smallest problems solved by the QPU should undergo postprocessing to bring them closer to a Boltzmann distribution.

The D-Wave QPU follows a predetermined
quantum annealing schedule, described in detail in *Technical Description of the D-Wave Quantum Processing Unit*. You obtain samples
by sending *problems* to the D-Wave QPU via the Solver API (SAPI) for solution.

## QPU Architecture: Chimera¶

Understanding the logical layout of the D-Wave QPU is critical to translating a
QUBO or Ising objective function into a format that a D-Wave system can solve.
Before we can send a graph that represents a problem to the QPU for solution, we
must perform *embedding* to translate the problem graph into a structure that fits
on the physical architecture.

The D-Wave QPU is a lattice of interconnected qubits. While some qubits connect to
others via couplers, the D-Wave QPU is not a fully connected graph. Instead, the qubits
interconnect in a bipartite architecture known as *Chimera*; see Figure 98.

The Chimera architecture comprises sets of connected unit cells, each with four horizontal qubits connected to four vertical qubits via couplers. Unit cells are tiled vertically and horizontally with adjacent qubits connected, creating a lattice of sparsely connected qubits.

Within a given system, certain qubits or couplers may not attain
the specifications to function as desired. In such cases, the devices
are eliminated from the programmable fabric that users access when
using the system. The subset of the graph with which users
interface is referred to as the *working graph*. The yield of the
working graph corresponds to the percentage of working qubits and couplers.

When formulating a problem for programming, the nodes and edges on the graph that represents an objective function translate to the qubits and couplers in Chimera. Each logical variable, in the graph of the objective function, may be represented by one or more physical qubits. The process of mapping the logical variables to physical qubits is what we call embedding.

For more details on Chimera and a basic lesson on how to embed a problem graph
on the Chimera working graph, see *Getting Started with the D-Wave System*.

## Interfacing with the QPU: Solver API¶

When sampling from the D-Wave QPU, make calls to the *Solver API (SAPI)*.

SAPI is the programmatic interface between the client libraries that programmers use to interact with the system and the D-Wave QPU itself. SAPI connects to backend servers, including the quantum server that sends problems to and returns results from the QPU via a fiber interface, and postprocessing servers that (optionally) run postprocessing algorithms on the raw results returned from the QPU.

The client libraries allow you to go through SAPI to:

- Query available solvers and solver properties
- Submit problems
- Cancel previously submitted problems
- Retrieve problem status
- Fetch results if successful
- Retrieve errors if unsuccessful

The client libraries use a traditional client/server request/response paradigm, where the application code runs on a client system, and the client commands are translated to REST/HTTP calls and then transmitted from the client to the server. All requests are initiated by the client.

## Factors Affecting Results¶

This section briefly discusses several factors that may affect results.

### Postprocessing¶

Sampling from the D-Wave QPU yields results that are useful for training small
PML models. However, it may
be necessary to bring the raw results closer to a Boltzmann distribution by running a
low-treewidth postprocessing algorithm. D-Wave provides such an algorithm that you
(optionally) enable when you submit the problem to the system. See *Postprocessing Methods on D-Wave Systems* for more
information on the available algorithms.

Use the `postprocess`

SAPI parameter with the `sampling`

option to run this algorithm.

### Temperature Effects¶

The D-Wave QPU operates at cryogenic temperatures, nominally \(15\) mK, which can be translated to a scale parameter \(\beta\). The effective value of \(\beta\) varies from QPU to QPU and in fact from problem to problem since the D-Wave QPU samples are not Boltzmann and time-varying phenomena may affect samples. Therefore, to attain Boltzmann samples, run the Gibbs chain for a number of iterations starting from quantum computer samples. The objective is to further anneal the samples to the correct temperature of interest \(T = 1/{\beta}\), where \(\beta = 1.0\).

You can control these factors using the `beta`

and `postprocess`

SAPI parameters.

Note

As in statistical mechanics, \(\beta\) represents inverse temperature: \(1/(k_B T)\), where \(T\)
is the thermodynamic temperature in kelvin and \(k_B\) is Boltzmann’s constant. In the D-Wave software,
postprocessing refines the returned solutions to target a Boltzmann distribution characterized by \(\beta\), which
is represented by a floating point number without units. When choosing a value for \(\beta\), be aware that lower
values result in samples less constrained to the lowest energy states. For more information on \(\beta\) and how it
is used in the sampling postprocessing algorithm, see *Postprocessing Methods on D-Wave Systems*.

### Problem Scaling¶

In training a model, the parameter \(\theta\) is not regularized, which means it can either grow or shrink in magnitude. However, when submitting problems to the QPU, recall that there are hard thresholds for the biases and weights.[1] Unless we explicitly disable scaling, the D-Wave software automatically adjusts the biases and weights of a problem to use the entire range available before sending it to the QPU. When the energies and objective values are reported at the end of the runs, we see the prescaling values.

[1] | Check the h_range and j_range solver properties for your system. |

The `auto_scale`

SAPI parameter controls this behavior; it is enabled by default.

### Spin-Reversal Transforms¶

To compensate for integrated control errors (ICE) that may affect the QPU,[2] we can
transform the problems by randomly flipping the qubits before we send them to the QPU.
To maintain the total energy unchanged, we can flip the biases and couplers corresponding to the
qubits. This technique is called a *spin-reversal transform*. Note that when samples are
gathered from the QPU, the qubits that have been flipped need to be flipped again to
undo the spin-reversal transform.

The `num_spin_reversal_transforms`

SAPI parameter allows you to apply one or more transforms when submitting a problem.

[2] | For more on ICE effects, see Technical Description of the D-Wave Quantum Processing Unit. |