Skip to content

Commit

Permalink
Describe how to convert V2 output back to V1 (Qiskit#2384)
Browse files Browse the repository at this point in the history
Closes Qiskit#2128

---------

Co-authored-by: Jessie Yu <jessieyu@us.ibm.com>
  • Loading branch information
beckykd and jyu00 authored Nov 27, 2024
1 parent f34ae98 commit 6195846
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/guides/primitive-input-output.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"### Sampler PUB\n",
"For the Sampler primitive, the format of the PUB tuple contains at most three values:\n",
"- A single `QuantumCircuit`, which may contain one or more [`Parameter`](../api/qiskit/qiskit.circuit.Parameter) objects\n",
" - *Note: these circuits should also include measurement instructions for each of the qubits to be sampled.*\n",
" *Note: These circuits should also include measurement instructions for each of the qubits to be sampled.*\n",
"- A collection of parameter values to bind the circuit against $\\theta_k$ (only needed if any `Parameter` objects are used that must be bound at runtime)\n",
"- (Optionally) a number of shots to measure the circuit with"
]
Expand Down
1 change: 1 addition & 0 deletions docs/guides/specify-runtime-options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Precision scales inversely with usage. That is, the lower the precision, the mo

There are many available options, but the following are the most commonly used:

<span id="shots"></span>
### Shots
For some algorithms, setting a specific number of shots is a core part of their routines. Shots (or precision) can be specified in multiple places. They are prioritized as follows:

Expand Down
21 changes: 21 additions & 0 deletions docs/migration-guides/v2-primitives.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ The output is now in the [`PubResult`](/api/qiskit/qiskit.primitives.PubResult)
</TabItem>
</Tabs>

<span id="sampler-examples"></span>
#### Sampler examples (input and output)

{/*Verified by Elena.*/}
Expand Down Expand Up @@ -210,6 +211,18 @@ The output is now in the [`PubResult`](/api/qiskit/qiskit.primitives.PubResult)
job = sampler_v2.run([(circuit1, vals1), (circuit2, vals1)])
counts1 = job.result()[0].data.meas.get_counts() # result for pub 1 (circuit 1)
counts2 = job.result()[1].data.meas.get_counts() # result for pub 2 (circuit 2)
```
</TabItem>

<TabItem value="SamplerOut" label="Convert V2 output to V1 format">
The V1 output format was a dictionary of bitstrings (as an int) as the key and quasi-probabilities as the value for each circuit. The V2 format has the same key (but as a string) and counts as the value. To convert the V2 format to V1, divide the counts by the number of shots, where the number of shots selected is described in the [Specify options](/guides/specify-runtime-options#shots) guide.

```python
v2_result = sampler_v2_job.result()
v1_format = []
for pub_result in v2_result:
counts = pub_result.data.meas.get_counts()
v1_format.append( {int(key, 2): val/shots for key, val in counts.items()} )
```
</TabItem>
</Tabs>
Expand Down Expand Up @@ -1177,3 +1190,11 @@ print(f" > Quasi-probability distribution job 2: {another_result.quasi_dists}")
```
</TabItem>
</Tabs>

## Next steps

<Admonition type="tip" title="Recommendations">
- Learn more about setting options in the [Specify options](/guides/specify-runtime-options) guide.
- Learn more details about [Primitive inputs and outputs.](/guides/primitive-input-output)
- Experiment with the [CHSH Inequality](https://learning.quantum.ibm.com/tutorial/chsh-inequality) tutorial.
</Admonition>

0 comments on commit 6195846

Please sign in to comment.