Installing your API key and accessing your provider
Now that you have installed Qiskit®, you can immediately start creating your quantum programs and run these on local simulators. If, however, at some point, you want to run your quantum code on actual IBM Quantum® hardware, you must install your own unique API key locally.
API keys on IBM Quantum Experience®
If you are running your Qiskit® programs in the IBM Quantum Experience® notebook environment, your API key is automatically registered.
Getting ready
Before you can install your API key, you must first have an IBM Quantum Experience® account. If you have not yet created one, go back and do it (see the Creating your IBM Quantum Experience® account section).
How to do it...
Let's take a look at how to install the API key locally:
- Log in to IBM Quantum Experience® at https://quantum-computing.ibm.com/login.
- On the IBM Quantum Experience® dashboard, find your user icon in the upper-right corner, click it, and select My account.
- On the account page, find the Qiskit in local environment section, and click Copy token.
- You can now paste your token in a temporary location or keep it in the clipboard.
- On your local machine, access your Qiskit® environment. We have done this one, but here's a repeat of the process if you are using Anaconda.
- Activate your virtual environment:
$ conda activate environment_name
- Open Python:
$(environment_name) … $ python3
Verify that the Python header displays and that you are running the correct version of Python:
Python 3.7.6 (default, Jan 8 2020, 13:42:34) [Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
- Get the required
IBMQ
class:>>> from qiskit import IBMQ
- Install your API token locally:
>>> IBMQ.save_account('MY_API_TOKEN')
Here, instead of
MY_API_TOKEN
, paste in the API token that you just copied from IBM Quantum Experience®: Keep the single quotes as they are required for the command. - Load your account.
Now that the token is in place, let's verify that all is in order and that your account has the correct privileges:
>>> IBMQ.load_account()
This should display the following output:
<AccountProvider for IBMQ(hub='ibm-q', group='open', project='main')>
This is the provider information for your account, with
hub
,group
, andproject
.
How it works...
The main class that you import for this exercise is IBMQ
, which is a toolbox for working with the quantum hardware and software that is provided by IBM in the cloud.
In this chapter, we used save.account()
to store your account locally. As we go forward, in the recipes where we will access the IBM Quantum® machines, we will use the IBMQ.load_account()
and IBMQ.get_provider()
classes in your quantum programs to make sure that you have the correct access.
Updating your API key
If for some reason, you need to create a new API token on IBM Quantum Experience® and update your locally saved token, you can use the following command:
>>> IBMQ.save_account('NEW_API_TOKEN', overwrite=True)
There's more…
In the code that follows in the recipes in this cookbook, we will set a provider
variable to hold the provider information for your account by using the following command:
>>> provider = IBMQ.get_provider()
We can then use the provider
information when selecting the IBM Quantum® computer, or backend, to run your quantum programs on. In the following example, we select a quantum computer that is called IBM Q 5 Yorktown (ibmqx2
) as our backend. The internal reference for this machine is ibmqx2
:
>>> backend = provider.get_backend('ibmqx2')