Getting started with dynamic circuits

Note

Click here to view this tutorial as an interactive Jupyter notebook in IBM Quantum Lab (requires sign-in).

We will be using Qiskit throughout the tutorial. This notebook will guide you through the setup process.

Usage guide

The tutorial notebooks live in IBM Quantum Lab. You will need to have an IBM Quantum account to use Quantum Lab. If you do not already have one, sign up for an IBM Quantum account.

You can run these tutorials either in the cloud, or download them to run on your local machine.

Option 1 - Local Machine

Log on to IBM Quantum Lab with your IBM Quantum account credentials.

Download the Dynamic circuits notebooks, including the figs/ directory.

Next, install Qiskit by following these instructions.

  • Set up a Python virtual environment for the tutorial (good practice but not necessary).

  • Make sure you have have the latest Qiskit installed. (qiskit-terra>=0.22 and qiskit-ibm-provider>=0.1.0)

    • pip install jupyter matplotlib scipy

    • pip install qiskit --upgrade

    • To update qiskit-terra, run pip install qiskit-terra -U

    • pip install qiskit-ibm-provider

  • Open up this notebook (Getting-started-with-Dynamic-Circuits.ipynb), run to check your installation, and run your first OpenQASM 3 program on hardware.

Option 2 - Quantum Lab

Open the tutorial in Quantum Lab.

Please make sure to verify that your qiskit-terra version `>=0.22.0` and that you are using the latest qiskit-ibm-provider version.

import qiskit.tools.jupyter
%qiskit_version_table

Version Information

Qiskit SoftwareVersion
qiskit-terra0.22.0
qiskit-aer0.11.0
qiskit-ignis0.7.1
qiskit0.39.0
System information
Python version3.9.12
Python compilerClang 13.1.6 (clang-1316.0.21.2)
Python buildmain, Mar 26 2022 15:51:15
OSDarwin
CPUs6
Memory (Gb)16.0
Thu Oct 27 16:09:55 2022 EDT
# Running checks for the correct dependencies
import qiskit
from packaging import version

if version.parse(qiskit.__qiskit_version__["qiskit-terra"]) >= version.parse("0.22"):
    print(
        "Incorrect dependencies. Found qiskit-terra: "
        + qiskit.__qiskit_version__["qiskit-terra"]
        + "\nNeed qiskit-terra > 0.22"
    )

try:
    import qiskit_ibm_provider
except ModuleNotFoundError:
    print(
        "You must install the qiskit_ibm_provider with 'pip install qiskit_ibm_provider'"
    )
    raise
# Ignoring warnings
import sys
import warnings

if not sys.warnoptions:
    warnings.simplefilter("ignore")

The notebook is ready to run!

Testing OpenQASM 3 support

Let’s test out our installation by verifying we have access to the code and hardware necessary.

First, load our backend.

To view the simulators and systems supporting dynamic circuits that you can access, click here (requires the user to be logged in to IBM Quantum).

Note

This can be any hub/group/project that has access to the required device and Qiskit Runtime, and has OpenQASM 3 support.

# Note: This can be any hub/group/project that has access to the required device and Qiskit Runtime.
hub = "<hub>"
group = "<group>"
project = "<project>"
backend_name = "<your backend>"
hgp = f"{hub}/{group}/{project}"

Next, we load our IBM Quantum account credentials.

import qiskit
from qiskit_ibm_provider import IBMProvider

# Save token if necessary
# IBMProvider.save_account(token='MY_API_TOKEN')

# Get our backend
provider = IBMProvider()
backend = provider.get_backend(backend_name, instance=hgp)

Make sure to use any OpenQASM 3-enabled backend.

qasm3_backends = set(
    backend.name for backend in provider.backends(dynamic_circuits=True)
)

print(f"The following backends support dynamic circuits: {qasm3_backends}")

Now we prepare our circuit.

qubit = 0
shots = 1000
from qiskit import QuantumCircuit

h_qc = QuantumCircuit(1, 1, name="|+> Prep")
h_qc.h(0)
h_qc.measure(0, 0)
h_qc.draw(idle_wires=False, output="mpl")
../../../../_images/output_16_0.png

Next, transpile our quantum circuit for the hardware basis gates.

from qiskit import transpile

h_qc_transpiled = transpile(h_qc, backend, initial_layout=[1])
h_qc_transpiled.draw(idle_wires=False, output="mpl")
../../../../_images/output_18_0.png

Now we submit our circuit to the backend to verify it is working.

Note

Set dynamic=True to communicate to the backend that we want to run a dynamic circuit.

job = backend.run(h_qc_transpiled, shots=shots, dynamic=True)
print(f"Your job's id: {job.job_id()}")
result = job.result()
print(f"|+> preparation results: {result.get_counts(0)}")
Your job's id: cdg41ta1ev47esgjg860
|+> preparation results: {'0': 488, '1': 512}
job.error_message()
import qiskit.tools.jupyter
%qiskit_version_table

Version Information

Qiskit SoftwareVersion
qiskit-terra0.22.0
qiskit-aer0.11.0
qiskit-ignis0.7.1
qiskit-ibmq-provider0.19.2
qiskit0.39.0
System information
Python version3.9.12
Python compilerClang 13.1.6
Python buildmain, Mar 26 2022 15:51:15
OSDarwin
CPUs6
Memory (Gb)16.0
Mon Oct 31 11:31:14 2022 EDT