Skip to main contentIBM Quantum Documentation

Sampler

Sampler(backend=None, session=None, options=None)GitHub(opens in a new tab)

Class for interacting with Qiskit Runtime Sampler primitive service.

Qiskit Runtime Sampler primitive service calculates quasi-probability distribution of bitstrings from quantum circuits.

The run() method can be used to submit circuits and parameters to the Sampler primitive.

You are encouraged to use Session to open a session, during which you can invoke one or more primitives. Jobs submitted within a session are prioritized by the scheduler, and data is cached for efficiency.

Example:

from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit_ibm_runtime import QiskitRuntimeService, Session, Sampler
 
service = QiskitRuntimeService(channel="ibm_cloud")
 
# Bell Circuit
qr = QuantumRegister(2, name="qr")
cr = ClassicalRegister(2, name="cr")
qc = QuantumCircuit(qr, cr, name="bell")
qc.h(qr[0])
qc.cx(qr[0], qr[1])
qc.measure(qr, cr)
 
with Session(service, backend="ibmq_qasm_simulator") as session:
    sampler = Sampler(session=session)
 
    job = sampler.run(qc, shots=1024)
    print(f"Job ID: {job.job_id()}")
    print(f"Job result: {job.result()}")
 
    # You can run more jobs inside the session

Initializes the Sampler primitive.

Parameters

  • backend (Union[str, IBMBackend, None]) – Backend to run the primitive. This can be a backend name or an IBMBackend instance. If a name is specified, the default account (e.g. QiskitRuntimeService()) is used.

  • session (Union[Session, str, IBMBackend, None]) –

    Session in which to call the primitive.

    If both session and backend are specified, session takes precedence. If neither is specified, and the primitive is created inside a qiskit_ibm_runtime.Session context manager, then the session is used. Otherwise if IBM Cloud channel is used, a default backend is selected.

  • options (Union[Dict, Options, None]) – Primitive options, see Options for detailed description. The backend keyword is still supported but is deprecated.


Attributes

options

Return options values for the sampler.

Return type

Options

Returns

options

session

Return session used by this primitive.

Return type

Optional[Session]

Returns

Session used by this primitive, or None if session is not used.


Methods

run

run(circuits, parameter_values=None, **kwargs)

Submit a request to the sampler primitive.

Parameters

  • circuits (QuantumCircuit | Sequence[QuantumCircuit]) – A (parameterized) QuantumCircuit or a list of (parameterized) QuantumCircuit.
  • parameter_values (Sequence[float] | Sequence[Sequence[float]] | None) – Concrete parameters to be bound.
  • **kwargs – Individual options to overwrite the default primitive options. These include the runtime options in qiskit_ibm_runtime.RuntimeOptions.

Return type

RuntimeJob

Returns

Submitted job. The result of the job is an instance of qiskit.primitives.SamplerResult.

Raises

ValueError – Invalid arguments are given.

set_options

set_options(**fields)

Set options values for the sampler.

Parameters

**fields – The fields to update the options

Return type

None

Was this page helpful?