Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for V2 primitives MVP2 #1088

Merged
merged 70 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
f0a29e5
sampler supports twirling
beckykd Mar 25, 2024
3df2e39
fix link
beckykd Mar 25, 2024
461bcc4
link
beckykd Mar 25, 2024
c44fd62
Updates round 1
beckykd Mar 26, 2024
3b73093
fix link
beckykd Mar 26, 2024
a81964e
add note type
beckykd Mar 26, 2024
fe250eb
add some tabs
beckykd Mar 26, 2024
9078d7d
reformat
beckykd Mar 27, 2024
da63759
fix links
beckykd Mar 27, 2024
763741a
update link
beckykd Mar 27, 2024
4574dbb
more edits
beckykd Mar 28, 2024
cbd06b6
edits
beckykd Mar 28, 2024
ec90616
commit
beckykd Mar 28, 2024
ada2001
update code
beckykd Mar 29, 2024
94be339
dynamic circuits
beckykd Mar 29, 2024
d7d7a7d
commit
beckykd Mar 29, 2024
883aea1
update examples
beckykd Mar 29, 2024
90fd63d
spelling
beckykd Mar 29, 2024
3acf8c8
Update docs/api/migration-guides/qiskit-runtime-setup.mdx
beckykd Mar 29, 2024
a919109
edits
beckykd Mar 29, 2024
4455b49
rendering issue
beckykd Mar 29, 2024
ba8cebe
edits
beckykd Mar 29, 2024
2255b0b
delete unneeded examples
beckykd Mar 29, 2024
915acd4
link
beckykd Mar 29, 2024
70ff73c
title
beckykd Mar 29, 2024
85fa8cb
missing tag
beckykd Mar 29, 2024
3f7341b
Jessie comments
beckykd Mar 29, 2024
a40be74
remove unused file
beckykd Mar 29, 2024
f2b66c2
edits
beckykd Mar 29, 2024
ed28672
more edits
beckykd Mar 29, 2024
ff32933
commit
beckykd Mar 29, 2024
3f65b28
Apply suggestions from code review
beckykd Apr 2, 2024
64053c0
comments - check table
beckykd Apr 2, 2024
6d59807
need to close br tags
beckykd Apr 2, 2024
57fd9cb
fix table
beckykd Apr 2, 2024
ae070c7
fix table
beckykd Apr 2, 2024
8e687b0
Edits
beckykd Apr 3, 2024
0055ffb
Apply suggestions from code review
beckykd Apr 3, 2024
0b531d6
Pull out the Local section
beckykd Apr 3, 2024
4547ea8
combine examples
beckykd Apr 3, 2024
859fd65
update examples
beckykd Apr 3, 2024
9414dd4
edits
beckykd Apr 3, 2024
ee68272
Because tables are the worst and you can't put anything special in them.
beckykd Apr 3, 2024
baf0ce4
try the table this way
beckykd Apr 3, 2024
71d1a87
bullets?
beckykd Apr 3, 2024
03f0b6e
fancy bullets
beckykd Apr 3, 2024
bbb886e
edits
beckykd Apr 3, 2024
b877b2c
Apply suggestions from code review
beckykd Apr 4, 2024
835210f
Apply suggestions from code review
beckykd Apr 4, 2024
00b6fe1
Apply suggestions from code review
beckykd Apr 4, 2024
9104db6
Apply suggestions from code review
beckykd Apr 4, 2024
88780e1
Comments from review
beckykd Apr 4, 2024
f654cb5
add isa-circuits
beckykd Apr 4, 2024
6a15ced
extra space
beckykd Apr 4, 2024
04f0e50
meas-level-1
beckykd Apr 4, 2024
8761acd
Apply suggestions from code review
beckykd Apr 4, 2024
bbe49d9
comments
beckykd Apr 4, 2024
0a47344
Merge branch 'primitives-MVP2' of https://github.com/Qiskit/documenta…
beckykd Apr 4, 2024
f56fc88
Apply suggestions from code review
beckykd Apr 5, 2024
2462aa0
Elena comments
beckykd Apr 5, 2024
7a747a0
Explain how to find the classical register name
beckykd Apr 5, 2024
997d68e
not in estimator
beckykd Apr 5, 2024
84e9e96
Jessie comment
beckykd Apr 8, 2024
d2b205b
Apply suggestions from code review
beckykd Apr 8, 2024
98860e9
Apply suggestions from code review
beckykd Apr 8, 2024
fb24173
add brackets
beckykd Apr 8, 2024
4ec8d29
message
beckykd Apr 8, 2024
c7e2ed2
No DD with dynamic circuits
beckykd Apr 9, 2024
5208434
Primitives accept dynamic circuits
beckykd Apr 10, 2024
8ec8f7f
Add more notes about needing the classical register name
beckykd Apr 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions docs/api/migration-guides/_toc.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,28 @@
"title": "Migrate to Qiskit Runtime",
"children": [
{
"title": "How to migrate",
"title": "Migrate from backend.run to primitives",
beckykd marked this conversation as resolved.
Show resolved Hide resolved
"url": "/api/migration-guides/qiskit-runtime"
},
{
"title": "Examples",
"url": "/api/migration-guides/qiskit-runtime-examples"
"title": "Migrate from qiskit-ibmq-provider",
"url": "/api/migration-guides/qiskit-from-ibmq-provider"
},
{
"title": "Migrate from qiskit-ibm-provider",
"url": "/api/migration-guides/qiskit-runtime-from-ibm-provider"
},
{
"title": "Migrate backend.run options to primitive options",
beckykd marked this conversation as resolved.
Show resolved Hide resolved
"url": "/api/migration-guides/qiskit-runtime-options"
},
{
"title": "Common use cases",
"url": "/api/migration-guides/qiskit-runtime-use-case"
},
{
"title": "qiskit_ibm_provider to qiskit_ibm_runtime",
"url": "/api/migration-guides/qiskit-runtime-from-provider"
beckykd marked this conversation as resolved.
Show resolved Hide resolved
"title": "End-to-end examples",
"url": "/api/migration-guides/qiskit-runtime-examples"
}
]
},
Expand Down
5 changes: 4 additions & 1 deletion docs/api/migration-guides/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ These migration guides help you more effectively use Qiskit and Qiskit Runtime:
* [Migrate to the Qiskit Runtime V2 primitives](/api/migration-guides/v2-primitives)
* Migrate to Qiskit Runtime
* [How to migrate](./qiskit-runtime)
* [Migrate from `qiskit-ibmq-provider`](./qiskit-runtime-from-ibmq-provider)
* [Migrate from `qiskit_ibm_provider`](./qiskit-runtime-from-ibm-provider)
* [Migrate `backend.run` options to primitives](./qiskit-runtime-options)
* [Common use cases](./qiskit-runtime-use-case)
* [Examples](./qiskit-runtime-examples)
* [Migrate `backend.run()` from `qiskit_ibm_provider` to `qiskit_ibm_runtime`](./qiskit-runtime-from-provider)
* [Migrate from cloud simulators to local simulators](/api/migration-guides/local-simulators)
* Qiskit 0.44 changes
* [`qiskit.algorithms` new interface](./qiskit-algorithms-module)
Expand Down
214 changes: 107 additions & 107 deletions docs/api/migration-guides/qiskit-runtime-examples.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
title: Qiskit Runtime migration examples
title: End-to-end examples
description: Examples of migrating from using backend.run to using Qiskit Runtime primitives

---

# Migration examples
# End-to-end examples

Follow these examples to design a Qiskit Runtime algorithm.

Expand All @@ -23,80 +23,71 @@ you don't have to manually construct the final expectation circuit.
This results in a considerable reduction of the code complexity and a
more compact algorithm design.

<Admonition type="note">
**Backend.run() model:** In this model, you accessed real systems and remote simulators using the `qiskit-ibmq-provider` module (now migrated to `qiskit-ibm-provider`). To run **local** simulations, you could import a specific simulator from `qiskit-aer`. All of them followed the `backend.run()` interface.

<details>
<summary>Code example for `qiskit-ibmq-provider` & `backend.run()`</summary>

``` python
from qiskit import IBMQ

# Select provider
provider = IBMQ.get_provider(hub="ibm-q", group="open", project="main")

# Get backend
backend = provider.get_backend("ibmq_qasm_simulator") # cloud simulator

# Run
result = backend.run(expectation_circuits)
```
</details>

<details>
<summary>Code example for `qiskit-aer` & `backend.run()`</summary>
**Backend.run() model:** In this model, you accessed real systems and remote simulators using the `qiskit-ibmq-provider` module (now migrated to `qiskit-ibm-provider`). To run **local** simulations, you could import a specific simulator from `qiskit-aer`. All of them followed the `backend.run()` interface.

``` python
from qiskit_aer import AerSimulator # former import: from qiskit import Aer

# Get local simulator backend
backend = AerSimulator()
<Tabs>
<TabItem value="provider" label="qiskit-ibmq-provider">
beckykd marked this conversation as resolved.
Show resolved Hide resolved
``` python
from qiskit import IBMQ
beckykd marked this conversation as resolved.
Show resolved Hide resolved

# Run
result = backend.run(expectation_circuits)
```
</details>
# Select provider
provider = IBMQ.get_provider(hub="ibm-q", group="open", project="main")
beckykd marked this conversation as resolved.
Show resolved Hide resolved

**Primitives model:** Access real systems and remote simulators through the `qiskit-ibm-runtime` **primitives** (`Sampler` and `Estimator`). To run **local** simulations, you can import specific local primitives from `qiskit_aer.primitives` and `qiskit.primitives`. All of them follow the `BaseSampler` and `BaseEstimator` interfaces, but **only the Runtime primitives offer access to the Runtime service, sessions, and built-in error mitigation**.
# Get backend
backend = provider.get_backend("ibmq_qasm_simulator") # cloud simulator

<details>
<summary>Code example for Runtime Estimator</summary>
# Run
result = backend.run(expectation_circuits)
```
</TabItem>

``` python
from qiskit_ibm_runtime import QiskitRuntimeService, Estimator
<TabItem value="aer" label="qiskit-aer">
``` python
from qiskit_aer import AerSimulator # former import: from qiskit import Aer

# Define service
service = QiskitRuntimeService()
# Get local simulator backend
backend = AerSimulator()

# Get backend
backend = service.backend("ibmq_qasm_simulator") # cloud simulator
# Run
result = backend.run(expectation_circuits)
```
</TabItem>
</Tabs>

# Define Estimator
# (see tutorials for more information about sessions)
estimator = Estimator(session=backend)
**Primitives model:** Access real systems and remote simulators through the `qiskit-ibm-runtime` **primitives** (`Sampler` and `Estimator`). To run **local** simulations, you can import specific local primitives from `qiskit_aer.primitives` and `qiskit.primitives`. All of them follow the `BaseSampler` and `BaseEstimator` interfaces, but **only the Runtime primitives offer access to the Runtime service, sessions, and built-in error mitigation**.

# Run Expectation value calculation
result = estimator.run(circuits, observables).result()
```
</details>
<Tabs>
<TabItem value="provider" label="Runtime Estimator">
``` python
from qiskit_ibm_runtime import QiskitRuntimeService, Estimator

<details>
<summary>Code example for Aer Estimator</summary>
# Define service
service = QiskitRuntimeService()

``` python
from qiskit_aer import Estimator
# Get backend
backend = service.backend("ibmq_qasm_simulator") # cloud simulator

# Get local simulator Estimator
estimator = Estimator()
# Define Estimator
# (see tutorials for more information about sessions)
estimator = Estimator(session=backend)

# Run expectation value calculation
result = estimator.run(circuits, observables).result()
```
# Run Expectation value calculation
result = estimator.run(circuits, observables).result()
```
</TabItem>

</details>
<TabItem value="aer" label="qiskit-aer">
``` python
from qiskit_aer import Estimator

</Admonition>
# Get local simulator Estimator
estimator = Estimator()

# Run expectation value calculation
result = estimator.run(circuits, observables).result()
```
</TabItem>
</Tabs>

If your code previously calculated expectation values using
`backend.run()`, you most likely used the `qiskit.opflow` module to
Expand Down Expand Up @@ -153,43 +144,9 @@ This step is no longer necessary when using the primitives.

#### 2. Calculate expectation values on real device or cloud simulator

##### 2.a. Legacy: Use opflow & backend.run()

The legacy workflow required many steps to compute an expectation value:

<Admonition type="note">
Replace `ibmq_qasm_simulator` with your device name to see the complete workflow for a real device.
</Admonition>

``` python
from qiskit.opflow import StateFn, PauliExpectation, CircuitSampler
from qiskit import IBMQ

# Define the state to sample
measurable_expression = StateFn(opflow_op, is_measurement=True).compose(opflow_state)

# Convert to expectation value calculation object
expectation = PauliExpectation().convert(measurable_expression)

# Define provider and backend
provider = IBMQ.get_provider(hub="ibm-q", group="open", project="main")
backend = provider.get_backend("ibmq_qasm_simulator")

# Inject backend into circuit sampler
sampler = CircuitSampler(backend).convert(expectation)

# Evaluate
expectation_value = sampler.eval().real
```

``` python
>>> print("expectation: ", expectation_value)
expectation: -1.065734058826613
```

##### 2.b. Updated: Use the Estimator Runtime primitive

The `Estimator` simplifies the user-side syntax, making it a more
<Tabs>
<TabItem value="updated" label="Runtime Estimator">
Estimator simplifies the user-side syntax, making it a more
beckykd marked this conversation as resolved.
Show resolved Hide resolved
convenient tool for algorithm design.

<Admonition type="note">
Expand Down Expand Up @@ -226,15 +183,57 @@ to the following:
- [How to run a session
topic](../../run/run-jobs-in-session)

#### 3. Other execution alternatives (non-Runtime)
</TabItem>

This section describes how to use non-Runtime primitives to test an
algorithm using local simulation. Let's assume that we want to solve
<TabItem value="legacy" label="opflow & backend.run()">
The legacy workflow required many steps to compute an expectation value:
beckykd marked this conversation as resolved.
Show resolved Hide resolved

<Admonition type="note">
Replace `ibmq_qasm_simulator` with your device name to see the complete workflow for a real device.
</Admonition>

``` python
from qiskit.opflow import StateFn, PauliExpectation, CircuitSampler
from qiskit import IBMQ
beckykd marked this conversation as resolved.
Show resolved Hide resolved

# Define the state to sample
measurable_expression = StateFn(opflow_op, is_measurement=True).compose(opflow_state)

# Convert to expectation value calculation object
expectation = PauliExpectation().convert(measurable_expression)

# Define provider and backend
provider = IBMQ.get_provider(hub="ibm-q", group="open", project="main")
backend = provider.get_backend("ibmq_qasm_simulator")

# Inject backend into circuit sampler
beckykd marked this conversation as resolved.
Show resolved Hide resolved
sampler = CircuitSampler(backend).convert(expectation)
beckykd marked this conversation as resolved.
Show resolved Hide resolved

# Evaluate
expectation_value = sampler.eval().real
```

``` python
>>> print("expectation: ", expectation_value)
expectation: -1.065734058826613
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elena, do you know why this outputs only 1 expectation value, when 5 observables were passed in? The estimator example shows 5 values.

Copy link
Collaborator

@ElePT ElePT Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the original example used a single SparsePauliOp with multiple paulis (representing the H2 molecule), so it was counted as a single operator (1 expectation value). The new example for V2 primitives "unwraps" this pauli into its 5 components when doing: isa_observables = [operator.apply_layout(isa_state.layout) for operator in op], so it now returns 5 expectation values. The old workflow doesn't easily allow to use multiple operators so I'd suggest not unwrapping it in the new workflow and returning one value in both. Trying to show the output of the 5 values would make the code unnecessarily complicated, and in this example we would "care" about the aggregated value.

```
</TabItem>
</Tabs>


#### 3. Local execution
beckykd marked this conversation as resolved.
Show resolved Hide resolved

This section describes how to use Qiskit Runtime to test an
algorithm locally. Let's assume that we want to solve
the problem defined above with a local state vector simulation.

<span id="3a"></span>
##### 3.a. Legacy: Using the Qiskit Aer simulator
<Tabs>
<TabItem value="updated" label="Local testing mode">
Qiskit Runtime has a local testing mode that gives you access to


</TabItem>
<TabItem value="legacy-aer" label="Qiskit Aer simulator">
``` python
from qiskit.opflow import StateFn, PauliExpectation, CircuitSampler
from qiskit_aer import AerSimulator
Expand All @@ -258,10 +257,10 @@ expectation_value = circuit_sampler.eval().real
``` python
>>> print("expectation: ", expectation_value)
expectation: -1.0636533500290943
```

##### 3.b. Updated: Use the Reference Estimator or Aer Estimator primitive
```
</TabItem>

<TabItem value="legacy-primitive" label="Reference Estimator or Aer Estimator primitive">
beckykd marked this conversation as resolved.
Show resolved Hide resolved
The reference `Estimator` lets you perform either an exact or a
shot-based noisy simulation based on the `Statevector` class in the
`qiskit.quantum_info` module.
Expand All @@ -285,7 +284,7 @@ expectation: [-1.03134297]
You can still access the Aer Simulator through its dedicated
`Estimator`. This can be handy for performing simulations with noise
models. In this example, the simulation method has been updated to match
the result from [3.a](#3a).
the Qiskit Aer simulator example result.

``` python
from qiskit_aer.primitives import Estimator # import change
Expand All @@ -299,8 +298,9 @@ expectation_value = estimator.run(state, op, shots=100).result().values
>>> print("expectation: ", expectation_value)
expectation: [-1.06365335]
```
</TabItem>
</Tabs>

For more information on using the Aer primitives, see the [VQE tutorial](https://github.com/Qiskit/qiskit-tutorials/blob/master/tutorials/algorithms/03_vqe_simulation_with_noise.ipynb).

For more information about running noisy simulations with the **Runtime primitives**, see the [Noisy simulators in Qiskit Runtime](../../verify/using-ibm-quantum-simulators) topic.

Expand Down
Loading
Loading