When to use sampling
Usesample(...) when you want to perform measurements. This method is typically used to
understand the probability distribution over measurement outcomes.
Sampling is a good choice when:
- you want to inspect measured bitstrings
- you want counts and probabilities
- you want to compare outputs across different parameter values
- you want behavior that resembles repeated measurements on a quantum device
sample function accepts these arguments:
Basic examples
Understanding the result
The returned object is a DataFrame, which makes it easy to analyze using familiar tools. Common columns are:- quantum variables - the quantum number values, quantum arrays, and qubits values measured
- bitstring — the measured computational basis state
- counts — how many times this result was observed
- probability — normalized frequency
Choosing execution settings
Sampling accepts the same common execution settings used by other high-level execution APIs. For example:backend— the backend on which the program will runconfig— provider-specific configurationnum_shots— how many times to execute the circuitrandom_seed— useful for reproducibilityparameters— values for parameterized quantum programs
Parameterized Execution
Many quantum programs are parameterized. Instead of hardcoding values directly into the circuit, you define them as inputs to your quantum function and provide their values at execution time. This is useful for:- variational algorithms
- parameter sweeps
- repeated evaluation of the same circuit structure with different values
Defining a parameterized quantum program
angle_rxandangle_ryare symbolic parameters- the structure of the quantum program is fixed
- the actual numerical values are supplied during execution
Supplying parameter values
To execute the parameterized program, pass a dictionary toparameters:
This means the parameter values are first bound to the quantum program, and the resulting instantiated circuit is then sampled.
Batch sampling
Often, you want to evaluate the same circuit for multiple parameter values. Instead of callingsample(...) repeatedly in a loop, you can pass a list of parameter dictionaries.
In that case, the function performs one execution per parameter set and returns a list of DataFrames.
Example
Understanding batch results
When parameters is a list:- the output is a list of DataFrames
- each DataFrame corresponds to one execution
- the order of the outputs matches the order of the parameter dictionaries you passed in
Sampling OpenQASM
You can pass OpenQASM 2.0 or 3.0 text as the first argument tosample instead of a QuantumProgram. The backend runs the circuit the same way as for a synthesized program; the returned DataFrame still includes bitstring, counts, and related columns.
OpenQASM from anytool is supported as long as you pass a string, for example:
- Do not pass
parametersfor OpenQASM strings; use aQuantumProgramif you need Qmodmainparameters, or express parameters inside the QASM (e.g. QiskitParameterandassign_parametersbefore dumping). - Batch sampling with a list of parameter dicts is only defined for
QuantumProgramexecution.
Summary
Use sampling when your goal is to understand measurement outcomes rather than the full state or a single expectation value. Sampling:- returns a DataFrame
- is well suited for measured distributions
- supports both single and batch parameterized execution