qiskit-documentation/docs/api/qiskit-ibm-runtime/0.16/qiskit_ibm_runtime.Sampler.mdx

152 lines
5.2 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Sampler (v0.16)
description: API reference for qiskit_ibm_runtime.Sampler in qiskit-ibm-runtime v0.16
in_page_toc_min_heading_level: 1
python_api_type: class
python_api_name: qiskit_ibm_runtime.Sampler
---
# Sampler
<Class id="qiskit_ibm_runtime.Sampler" isDedicatedPage={true} github="https://github.com/qiskit/qiskit-ibm-runtime/tree/stable/0.16/qiskit_ibm_runtime/sampler.py" signature="Sampler(backend=None, session=None, options=None)" modifiers="class">
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()`](#qiskit_ibm_runtime.Sampler.run "qiskit_ibm_runtime.Sampler.run") method can be used to submit circuits and parameters to the Sampler primitive.
You are encouraged to use [`Session`](qiskit_ibm_runtime.Session "qiskit_ibm_runtime.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:
```python
from qiskit.test.reference_circuits import ReferenceCircuits
from qiskit_ibm_runtime import QiskitRuntimeService, Session, Sampler
service = QiskitRuntimeService(channel="ibm_cloud")
bell = ReferenceCircuits.bell()
with Session(service, backend="ibmq_qasm_simulator") as session:
sampler = Sampler(session=session)
job = sampler.run(bell, 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`](qiskit_ibm_runtime.IBMBackend "qiskit_ibm_runtime.ibm_backend.IBMBackend"), `None`]) Backend to run the primitive. This can be a backend name or an [`IBMBackend`](qiskit_ibm_runtime.IBMBackend "qiskit_ibm_runtime.IBMBackend") instance. If a name is specified, the default account (e.g. `QiskitRuntimeService()`) is used.
* **session** (`Union`\[[`Session`](qiskit_ibm_runtime.Session "qiskit_ibm_runtime.session.Session"), `str`, [`IBMBackend`](qiskit_ibm_runtime.IBMBackend "qiskit_ibm_runtime.ibm_backend.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`](qiskit_ibm_runtime.Session "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`](qiskit_ibm_runtime.options.Options "qiskit_ibm_runtime.options.options.Options"), `None`]) Primitive options, see `Options` for detailed description. The `backend` keyword is still supported but is deprecated.
## Attributes
### circuits
<Attribute id="qiskit_ibm_runtime.Sampler.circuits">
Quantum circuits to be sampled.
**Return type**
`tuple`\[`QuantumCircuit`, `...`]
**Returns**
The quantum circuits to be sampled.
</Attribute>
### options
<Attribute id="qiskit_ibm_runtime.Sampler.options">
Return options values for the sampler.
**Return type**
`Options`
**Returns**
options
</Attribute>
### parameters
<Attribute id="qiskit_ibm_runtime.Sampler.parameters">
Parameters of quantum circuits.
**Return type**
`tuple`\[`ParameterView`, `...`]
**Returns**
List of the parameters in each quantum circuit.
</Attribute>
### session
<Attribute id="qiskit_ibm_runtime.Sampler.session">
Return session used by this primitive.
**Return type**
`Optional`\[[`Session`](qiskit_ibm_runtime.Session "qiskit_ibm_runtime.session.Session")]
**Returns**
Session used by this primitive, or `None` if session is not used.
</Attribute>
## Methods
### run
<Function id="qiskit_ibm_runtime.Sampler.run" signature="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`](qiskit_ibm_runtime.RuntimeOptions "qiskit_ibm_runtime.RuntimeOptions").
**Return type**
[RuntimeJob](qiskit_ibm_runtime.RuntimeJob "qiskit_ibm_runtime.RuntimeJob")
**Returns**
Submitted job. The result of the job is an instance of `qiskit.primitives.SamplerResult`.
**Raises**
**ValueError** Invalid arguments are given.
</Function>
### set\_options
<Function id="qiskit_ibm_runtime.Sampler.set_options" signature="set_options(**fields)">
Set options values for the sampler.
**Parameters**
**\*\*fields** The fields to update the options
**Return type**
`None`
</Function>
</Class>