---
title: Sampler quickstart
description: A quickstart guide on how to use the IBM Quantum Sampler primitive.
source: https://eu-de.quantum.cloud.ibm.com/docs/en/guides/get-started-with-sampler
---

# Sampler quickstart

The Sampler's core task is sampling the output register from the execution of one or more quantum circuits. [Dynamic circuits](/docs/guides/execute-dynamic-circuits) and parameterized circuits are accepted as input (if parametrized circuits are submitted, the parameter values must also be provided). Sampler also supports built-in dynamical decoupling and twirling for [error suppression](/docs/guides/error-mitigation-and-suppression-techniques).

The steps in this topic describe how to set up Sampler, explore the options you can use to configure it, and invoke it in a program.

### Package versions

The code on this page was developed using the following requirements.
We recommend using these versions or newer.

```
qiskit[all]~=2.5.1
qiskit-ibm-runtime~=0.47.0
```

## Steps to use the Sampler primitive

### 1. Initialize the account

You first need to initialize your account. You can then select the QPU you want to use to calculate the expectation value.

Follow the steps in the [Set up your IBM Cloud account](/docs/guides/cloud-setup) topic if you don't already have an account set up.

> **Fractional gates**
>
> To use the newly supported [fractional gates](/docs/guides/fractional-gates), set `use_fractional_gates=True` when requesting a backend from a `QiskitRuntimeService` instance. For example:
>
> ```python
> service = QiskitRuntimeService()
> fractional_gate_backend = service.least_busy(use_fractional_gates=True)
> ```
>
> This is an experimental feature and might change in the future.

```python
from qiskit_ibm_runtime import QiskitRuntimeService

service = QiskitRuntimeService()
backend = service.least_busy(
    operational=True, simulator=False, min_num_qubits=127
)
```

### 2. Create a circuit

You need at least one circuit as the input to the Sampler primitive.

```python
import numpy as np
from qiskit.circuit.library import efficient_su2

circuit = efficient_su2(127, entanglement="linear")
circuit.measure_all()
# The circuit is parametrized, so we will define the parameter values for execution
param_values = np.random.rand(circuit.num_parameters)
```

The circuit and observable need to be transformed to only use instructions supported by the QPU (referred to as *instruction set architecture (ISA)* circuits). Use the transpiler to do this.

```python
from qiskit.transpiler import generate_preset_pass_manager

pm = generate_preset_pass_manager(optimization_level=1, backend=backend)
isa_circuit = pm.run(circuit)
print(f">>> Circuit ops (ISA): {isa_circuit.count_ops()}")
```

Output:

```
>>> Circuit ops (ISA): OrderedDict([('rz', 3036), ('sx', 1769), ('cz', 378), ('measure', 127), ('barrier', 1)])
```

### 3. Initialize the IBM Quantum Sampler

When you initialize Sampler, use the `mode` parameter to specify the mode you want it to run in.  Possible values are `batch`, `session`, or `backend` objects for batch, session, and job execution mode, respectively. For more information, see [Introduction to IBM Quantum Compute Service execution modes](/docs/guides/execution-modes). Note that Open Plan users cannot submit session jobs.

```python
from qiskit_ibm_runtime import SamplerV2 as Sampler

sampler = Sampler(mode=backend)
```

### 4. Invoke Sampler and get results

Next, invoke the `run()` method to generate the output. The circuit and optional parameter value sets are input as *primitive unified bloc* (PUB) tuples.

```python
job = sampler.run([(isa_circuit, param_values)])
print(f">>> Job ID: {job.job_id()}")
print(f">>> Job Status: {job.status()}")
```

Output:

```
>>> Job ID: d9mq9og8csec73fagt7g
```

```
>>> Job Status: QUEUED
```

```python
result = job.result()

# Get results for the first (and only) PUB
pub_result = result[0]
print(
    f"First ten results for the 'meas' output register: "
    f"{pub_result.data.meas.get_bitstrings()[:10]}"
)
```

Output:

```
First ten results for the 'meas' output register: ['1010011011000000000000000000000000001111111111001010101010001110011000011101100100011100111111111010010110000011010100110111100', '0111100001100000011011101000000111111001011010110000101100011000000011010001000100000111001110011001111010100001100010100000000', '0111010101001110000110100000000110011000011011001111000110101101010100010010111111101011000000010000101111000001010110001000000', '1100111011000010011001100100001101111000101111110110011001011001100110000000000110111100110011000001100000100100001000000000000', '0011111010101101001111111110011000111000000010100011011111111011100110101010111111111101111110100011101101011100110010011010100', '1011111111011010000000000111000000010101001001000110011101100010010001100110100101111111110010001000001000101111100100011000000', '1110101101101011111111000010101010111100110011001010101111110110101010101111110000001110001111101101010100111011110001000000000', '1001010011111010000001010100011000101110111110111101100001010100011110000001000001000110000000000001101010110010110000011001000', '0000101010100000111000100001010101100101011101010110000001100110011001111011111110111010101000011001000011001001101101001010110', '1001001011000000010011101100010011101100010001001100100110011101100100000000111101010010000101011011011110110110001000111101100']
```

## Next steps

> **Recommendations**
>
> - Learn how to [test locally](/docs/guides/local-testing-mode) before running on quantum computers.
> - Review detailed [examples](/docs/guides/sampler-examples).
> - Practice with primitives by working through the [Cost function lesson](/learning/courses/variational-algorithm-design/cost-functions) in IBM Quantum Learning.
> - Learn how to transpile locally in the [Transpile](/docs/guides/transpile/) section.
> - Try the [Compare transpiler settings](/docs/guides/circuit-transpilation-settings#compare-transpiler-settings) guide.
> - Learn how to [use the primitive options](/docs/guides/runtime-options-overview).
> - View the API for [Sampler](/docs/api/qiskit-ibm-runtime/options-sampler-options) options.
