# 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 Topology¶

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 fully connected. Instead, the qubits of D-Wave 2000Q and earlier generations
of QPUs interconnect in a topology known as *Chimera* while Advantage QPUs incorporate the *Pegasus* topology.

In a D-Wave QPU, the set of qubits and couplers that are available for computation is
known as the *working graph*. The *yield* of a working graph is typically less than the
total number of qubits and couplers that are fabricated and physically present in the QPU.

A given *logical problem* defined on a general graph can be mapped to a *physical problem*
defined on the working graph using *chains*. A chain is a collection of qubits bound together
to represent a single logical node. The association between the logical problem and the
physical problem is carried out by minor *embedding*. For more details on
minor embedding, see *D-Wave Problem-Solving Handbook*.

The qubits, denoted \(q_i\), implement the Ising spins. Their physical connectivity determines which couplings, \(J_{i,j}\), can be set to nonzero values. The allowed connectivity is described with a Chimera or Pegasus graph..

For further details, see the *Getting Started with the D-Wave System* guide.

## 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. |