129 lines
5.4 KiB
Plaintext
129 lines
5.4 KiB
Plaintext
---
|
||
title: StatevectorSampler (v1.2)
|
||
description: API reference for qiskit.primitives.StatevectorSampler in qiskit v1.2
|
||
in_page_toc_min_heading_level: 1
|
||
python_api_type: class
|
||
python_api_name: qiskit.primitives.StatevectorSampler
|
||
---
|
||
|
||
# StatevectorSampler
|
||
|
||
<Class id="qiskit.primitives.StatevectorSampler" isDedicatedPage={true} github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/primitives/statevector_sampler.py#L52-L203" signature="qiskit.primitives.StatevectorSampler(*, default_shots=1024, seed=None)" modifiers="class">
|
||
Bases: [`BaseSamplerV2`](qiskit.primitives.BaseSamplerV2 "qiskit.primitives.base.base_sampler.BaseSamplerV2")
|
||
|
||
Simple implementation of [`BaseSamplerV2`](qiskit.primitives.BaseSamplerV2 "qiskit.primitives.BaseSamplerV2") using full state vector simulation.
|
||
|
||
This class is implemented via [`Statevector`](qiskit.quantum_info.Statevector "qiskit.quantum_info.Statevector") which turns provided circuits into pure state vectors, and is therefore incompatible with mid-circuit measurements (although other implementations may be).
|
||
|
||
As seen in the example below, this sampler supports providing arrays of parameter value sets to bind against a single circuit.
|
||
|
||
Each tuple of `(circuit, <optional> parameter values, <optional> shots)`, called a sampler primitive unified bloc (PUB), produces its own array-valued result. The [`run()`](#qiskit.primitives.StatevectorSampler.run "qiskit.primitives.StatevectorSampler.run") method can be given many pubs at once.
|
||
|
||
```python
|
||
from qiskit.circuit import (
|
||
Parameter, QuantumCircuit, ClassicalRegister, QuantumRegister
|
||
)
|
||
from qiskit.primitives import StatevectorSampler
|
||
|
||
import matplotlib.pyplot as plt
|
||
import numpy as np
|
||
|
||
# Define our circuit registers, including classical registers
|
||
# called 'alpha' and 'beta'.
|
||
qreg = QuantumRegister(3)
|
||
alpha = ClassicalRegister(2, "alpha")
|
||
beta = ClassicalRegister(1, "beta")
|
||
|
||
# Define a quantum circuit with two parameters.
|
||
circuit = QuantumCircuit(qreg, alpha, beta)
|
||
circuit.h(0)
|
||
circuit.cx(0, 1)
|
||
circuit.cx(1, 2)
|
||
circuit.ry(Parameter("a"), 0)
|
||
circuit.rz(Parameter("b"), 0)
|
||
circuit.cx(1, 2)
|
||
circuit.cx(0, 1)
|
||
circuit.h(0)
|
||
circuit.measure([0, 1], alpha)
|
||
circuit.measure([2], beta)
|
||
|
||
# Define a sweep over parameter values, where the second axis is over.
|
||
# the two parameters in the circuit.
|
||
params = np.vstack([
|
||
np.linspace(-np.pi, np.pi, 100),
|
||
np.linspace(-4 * np.pi, 4 * np.pi, 100)
|
||
]).T
|
||
|
||
# Instantiate a new statevector simulation based sampler object.
|
||
sampler = StatevectorSampler()
|
||
|
||
# Start a job that will return shots for all 100 parameter value sets.
|
||
pub = (circuit, params)
|
||
job = sampler.run([pub], shots=256)
|
||
|
||
# Extract the result for the 0th pub (this example only has one pub).
|
||
result = job.result()[0]
|
||
|
||
# There is one BitArray object for each ClassicalRegister in the
|
||
# circuit. Here, we can see that the BitArray for alpha contains data
|
||
# for all 100 sweep points, and that it is indeed storing data for 2
|
||
# bits over 256 shots.
|
||
assert result.data.alpha.shape == (100,)
|
||
assert result.data.alpha.num_bits == 2
|
||
assert result.data.alpha.num_shots == 256
|
||
|
||
# We can work directly with a binary array in performant applications.
|
||
raw = result.data.alpha.array
|
||
|
||
# For small registers where it is anticipated to have many counts
|
||
# associated with the same bitstrings, we can turn the data from,
|
||
# for example, the 22nd sweep index into a dictionary of counts.
|
||
counts = result.data.alpha.get_counts(22)
|
||
|
||
# Or, convert into a list of bitstrings that preserve shot order.
|
||
bitstrings = result.data.alpha.get_bitstrings(22)
|
||
print(bitstrings)
|
||
```
|
||
|
||
**Parameters**
|
||
|
||
* **default\_shots** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")) – The default shots for the sampler if not specified during run.
|
||
* **seed** (*np.random.Generator |* [*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)") *| None*) – The seed or Generator object for random number generation. If None, a random seeded default RNG will be used.
|
||
|
||
## Attributes
|
||
|
||
### default\_shots
|
||
|
||
<Attribute id="qiskit.primitives.StatevectorSampler.default_shots">
|
||
Return the default shots
|
||
</Attribute>
|
||
|
||
### seed
|
||
|
||
<Attribute id="qiskit.primitives.StatevectorSampler.seed">
|
||
Return the seed or Generator object for random number generation.
|
||
</Attribute>
|
||
|
||
## Methods
|
||
|
||
### run
|
||
|
||
<Function id="qiskit.primitives.StatevectorSampler.run" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/primitives/statevector_sampler.py#L155-L170" signature="run(pubs, *, shots=None)">
|
||
Run and collect samples from each pub.
|
||
|
||
**Parameters**
|
||
|
||
* **pubs** (*Iterable\[SamplerPubLike]*) – An iterable of pub-like objects. For example, a list of circuits or tuples `(circuit, parameter_values)`.
|
||
* **shots** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)") *| None*) – The total number of shots to sample for each sampler pub that does not specify its own shots. If `None`, the primitive’s default shots value will be used, which can vary by implementation.
|
||
|
||
**Returns**
|
||
|
||
The job object of Sampler’s result.
|
||
|
||
**Return type**
|
||
|
||
[PrimitiveJob](qiskit.primitives.PrimitiveJob "qiskit.primitives.PrimitiveJob")\[[PrimitiveResult](qiskit.primitives.PrimitiveResult "qiskit.primitives.PrimitiveResult")\[[SamplerPubResult](qiskit.primitives.SamplerPubResult "qiskit.primitives.SamplerPubResult")]]
|
||
</Function>
|
||
</Class>
|
||
|