Result¶
The Result class holds the output of a quantum circuit execution —
measurement counts, probabilities, and methods for analysis and visualization.
Overview¶
import quantsdk as qs
circuit = qs.Circuit(2).h(0).cx(0, 1).measure_all()
result = qs.run(circuit, shots=1000)
# Access data
result.counts # {'00': 512, '11': 488}
result.probabilities # {'00': 0.512, '11': 0.488}
result.most_likely # '00'
result.top_k(1) # [('00', 512)]
# Visualization
result.plot_histogram() # matplotlib bar chart
df = result.to_pandas() # pandas DataFrame
print(result.summary()) # formatted summary string
# Serialization
result.to_dict() # dict for JSON export
API Reference¶
Result
dataclass
¶
Result(
counts: dict[str, int],
shots: int,
backend: str = "local_simulator",
job_id: str = "",
metadata: dict[str, Any] = dict(),
)
Result of a quantum circuit execution.
Contains measurement counts, backend metadata, and convenience methods for analysis and visualization.
| ATTRIBUTE | DESCRIPTION |
|---|---|
counts |
Dictionary mapping bitstring outcomes to their counts.
TYPE:
|
shots |
Number of shots (measurement repetitions).
TYPE:
|
backend |
Name of the backend that executed the circuit.
TYPE:
|
job_id |
Unique identifier for the execution job.
TYPE:
|
metadata |
Additional backend-specific metadata.
TYPE:
|
Attributes¶
probabilities
property
¶
Measurement probabilities (counts normalized to sum = 1.0).
| RETURNS | DESCRIPTION |
|---|---|
dict[str, float]
|
Dictionary mapping bitstrings to their probability. |
most_likely
property
¶
The most frequently measured bitstring.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The bitstring with the highest count. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If counts are empty. |
Functions¶
get_probability
¶
Get the probability of a specific bitstring outcome.
| PARAMETER | DESCRIPTION |
|---|---|
bitstring
|
The measurement outcome to query.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Probability as a float (0.0 if bitstring was not observed). |
top_k
¶
Get the top-k most frequent measurement outcomes.
| PARAMETER | DESCRIPTION |
|---|---|
k
|
Number of top results to return.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[str, int, float]]
|
List of (bitstring, count, probability) tuples, sorted by count descending. |
expectation_value
¶
Compute the expectation value treating bitstrings as binary numbers.
| RETURNS | DESCRIPTION |
|---|---|
float
|
Weighted average of bitstring values (as integers). |
to_dict
¶
Export result as a plain dictionary.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
Dictionary containing all result data. |
to_pandas
¶
Export result as a pandas DataFrame.
Requires: pip install pandas
| RETURNS | DESCRIPTION |
|---|---|
Any
|
A |
Example::
result = qs.run(circuit, shots=1000)
df = result.to_pandas()
print(df.head())
plot_histogram
¶
plot_histogram(
*,
top_k: int | None = None,
title: str | None = None,
figsize: tuple[float, float] = (10, 5),
color: str = "#4C72B0",
show: bool = True,
) -> Any
Plot a histogram of measurement results.
Requires: pip install matplotlib
| PARAMETER | DESCRIPTION |
|---|---|
top_k
|
Show only the top-k most frequent outcomes. Default: all.
TYPE:
|
title
|
Plot title. Default: "Measurement Results ({shots} shots)".
TYPE:
|
figsize
|
Figure size as (width, height) in inches.
TYPE:
|
color
|
Bar color (any matplotlib color string).
TYPE:
|
show
|
Whether to call
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
A |
Example::
result = qs.run(circuit, shots=1000)
result.plot_histogram(top_k=10, title="Bell State")
summary
¶
Human-readable summary of the result.
| RETURNS | DESCRIPTION |
|---|---|
str
|
Formatted string with key result information. |