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

Add instructions to securely manage token #2322

Merged
merged 68 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
cab2eea
test twisty
abbycross Nov 14, 2024
f92740f
add info on env variable
abbycross Nov 14, 2024
8cf4f0f
add to functions intro page
abbycross Nov 14, 2024
880a541
squeaky
abbycross Nov 14, 2024
8e7b1bd
add separate section
abbycross Nov 15, 2024
929a1a0
tweak
abbycross Nov 15, 2024
6004777
restore IBM Cloud note
abbycross Nov 15, 2024
ca16e69
Update setup-channel.mdx
abbycross Nov 15, 2024
08377eb
Update docs/guides/setup-channel.mdx
abbycross Nov 18, 2024
5747749
Add tabs w/Runtime option
abbycross Nov 18, 2024
eb1b3a1
Apply suggestions from code review
abbycross Nov 18, 2024
602289c
Update docs/guides/functions.ipynb
abbycross Nov 18, 2024
6f5ed59
Update setup-channel.mdx
abbycross Nov 19, 2024
04285f9
rework page
abbycross Nov 19, 2024
05ec983
rmv ref to removed section
abbycross Nov 20, 2024
2973262
tweaks
abbycross Nov 20, 2024
a150476
rmv extra comments
abbycross Nov 20, 2024
d1f89a0
clarify default
abbycross Nov 20, 2024
215ab29
updates to the Platform section of the page
abbycross Nov 20, 2024
2b88216
redundant
abbycross Nov 21, 2024
ff129c2
bold
abbycross Nov 21, 2024
8b20e08
instructions for untrusted machine
abbycross Nov 21, 2024
d2451be
input from Vignesh
abbycross Nov 22, 2024
4aa4333
tweaks to iqp section
abbycross Nov 22, 2024
185939a
no need to italicize twice in a row
abbycross Nov 22, 2024
8e334db
first pass at REST API section
abbycross Nov 22, 2024
8d26470
test out subsection for REST API
abbycross Nov 22, 2024
c800067
Update docs/guides/setup-channel.mdx
abbycross Nov 26, 2024
b8df8c2
Update docs/guides/setup-channel.mdx
abbycross Nov 26, 2024
1f3d40d
move adm higher, rmv "or random", use original lang
abbycross Nov 26, 2024
0e18cd3
rmv "in plain text"
abbycross Nov 26, 2024
2bb59bf
add save_account() API link
abbycross Nov 26, 2024
f20da5a
copy paste code from account pg
abbycross Nov 26, 2024
b311aa2
fix indent
abbycross Nov 26, 2024
84de35b
rmv overwrite=True
abbycross Nov 26, 2024
e7acc27
separate into 2 blocks
abbycross Nov 26, 2024
8aeac62
tweaks
abbycross Nov 26, 2024
acc2bbd
updating wording on Functions pg
abbycross Nov 26, 2024
54a6c47
tox
abbycross Nov 26, 2024
7925905
Merge branch 'main' into ajc/update-install-instructions
abbycross Nov 26, 2024
c84195d
start fresh w serverless-manage-resources
abbycross Nov 26, 2024
d201682
rmv placeholder API text
abbycross Nov 26, 2024
9f34a79
comment in qunasys code
abbycross Nov 26, 2024
d3d602e
Update serverless-manage-resources.ipynb
abbycross Nov 26, 2024
1e59e09
add comments, remove placeholder text for api code
abbycross Nov 26, 2024
8374f06
add catalog back in
abbycross Nov 26, 2024
97b4f1d
more fix
abbycross Nov 26, 2024
5f31633
underscore
abbycross Nov 26, 2024
beb9434
tox
abbycross Nov 26, 2024
cb6b6fc
Apply suggestions from code review
abbycross Nov 27, 2024
b003b40
Update docs/guides/setup-channel.mdx
abbycross Nov 27, 2024
e0a952d
Update docs/guides/setup-channel.mdx
abbycross Nov 27, 2024
dbf79a9
Merge branch 'main' into ajc/update-install-instructions
abbycross Nov 27, 2024
6f5da2e
format a la iqp section
abbycross Nov 27, 2024
0be84fc
bring warning higher up in section
abbycross Nov 27, 2024
89ad0c0
break up line for readability
abbycross Nov 27, 2024
8c89b3e
Python to `python`
abbycross Nov 27, 2024
ee626fe
update untrusted language
abbycross Nov 27, 2024
57dbf4f
clarify refresh token language
abbycross Nov 27, 2024
a055c25
expire rather than rotate
abbycross Nov 27, 2024
c5a9851
what to do if you accidentally share token
abbycross Nov 27, 2024
dc974c2
tox
abbycross Nov 27, 2024
fe3ad1f
Update docs/guides/setup-channel.mdx
abbycross Nov 27, 2024
9eec6b9
Update docs/guides/setup-channel.mdx
abbycross Nov 27, 2024
cb4f46f
Update docs/guides/setup-channel.mdx
abbycross Nov 27, 2024
58a0205
pythonic
abbycross Nov 27, 2024
065dd39
Merge branch 'ajc/update-install-instructions' of https://github.com/…
abbycross Nov 27, 2024
9a14488
fix up token name
abbycross Nov 27, 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
2 changes: 1 addition & 1 deletion docs/guides/functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"id": "42843fcd-1779-4a74-8d6a-7d00c22e374e",
"metadata": {},
"source": [
"Optionally, you can use the `save_account()` method to save your credentials for easy access later on, before initializing the service. This saves your credentials in the same place as `QiskitRuntimeService.save_account()`, so you can skip this step if you had previously used `QiskitRuntimeService` to save your account. *Note that account credentials are saved in plain text, so only do so if you are using a trusted device.*"
"Optionally, you can use the `save_account()` method to save your credentials for easy access later on, before initializing the service. This saves your credentials in the same place as `QiskitRuntimeService.save_account()`, so you can skip this step if you had previously used `QiskitRuntimeService` to save your account."
]
},
{
Expand Down
180 changes: 106 additions & 74 deletions docs/guides/setup-channel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Installation and setup instructions for IBM Quantum Platform or IBM

# Set up an IBM Quantum channel

You can access IBM® quantum processing units (QPUs) by using the IBM Quantum Platform or IBM Cloud® _channel_. _Channel_ is the term used to describe the method you use to access IBM Quantum services.
You can access IBM® quantum processing units (QPUs) by using the IBM Quantum Platform or IBM Cloud® channel. _Channel_ is the term used to describe the method you use to access IBM Quantum services.

## Select an IBM Quantum channel

Expand All @@ -23,7 +23,6 @@ Available plans:

* **Premium Plan** - Run quantum circuits on the world's best QPUs using an enterprise quantum time subscription.


### IBM Cloud

IBM Cloud offers pay-as-you-go access plans. See [IBM Quantum access plans](https://www.ibm.com/quantum/access-plans) for details.
Expand All @@ -49,55 +48,47 @@ Available plans:
The Instances section in your [IBM Quantum account page](https://quantum.ibm.com/account) lists the instances that you can access.
</Admonition>

1. Retrieve your IBM Quantum token from the [IBM Quantum account page](https://quantum.ibm.com/account).

1. <span id="save-account"></span>Authenticate to the service by calling `QiskitRuntimeService` with your IBM Quantum API key and CRN:

```python
from qiskit_ibm_runtime import QiskitRuntimeService

service = QiskitRuntimeService(channel="ibm_quantum", token="<MY_IBM_QUANTUM_TOKEN>")
1. Retrieve your API token from the [IBM Quantum account page](https://quantum.ibm.com/account), and activate your Python virtual environment. See the [installation instructions](/guides/install-qiskit#local) if you do not already have a virtual environment set up.

```

Or, optionally use the `save_account()` method to save your credentials for easy access later on, before initializing the service.
<span id="save-account"></span>**If you are working in a trusted Python environment (such as on a personal laptop or workstation),** use the `save_account()` method to save your credentials locally. ([Skip to the next step](#untrusted-machine) if you are not using a trusted environment, such as a shared or public computer, to authenticate to IBM Quantum Platform.) To use `save_account()`, run Python in your virtual environment to open a read-eval-print loop (REPL), then enter the following:
abbycross marked this conversation as resolved.
Show resolved Hide resolved

```python
from qiskit_ibm_runtime import QiskitRuntimeService

# Save an IBM Quantum account and set it as your default account.
QiskitRuntimeService.save_account(
channel="ibm_quantum",
token="<MY_IBM_QUANTUM_TOKEN>",
set_as_default=True,
# Use `overwrite=True` if you're updating your token.
overwrite=True,
token="<your-token">,
channel="ibm_quantum", # `channel` distinguishes between different account types
overwrite=True # Use `overwrite=True` if you're updating your token
)
abbycross marked this conversation as resolved.
Show resolved Hide resolved
abbycross marked this conversation as resolved.
Show resolved Hide resolved
```

Close out of the REPL with `exit()`. From now on, whenever you need to authenticate to the service, you can load your credentials with `QiskitRuntimeService()`.

```python
# Load saved credentials
service = QiskitRuntimeService()
```

* If you save your credentials to disk, you can use `QiskitRuntimeService()` in the future to initialize your account. The `channel` parameter distinguishes between different account types.
* If you are saving multiple accounts per channel, consider using the `name` parameter to differentiate them.
* Credentials are saved to `$HOME/.qiskit/qiskit-ibm.json`. Do not manually edit this file.
* If you don't save your credentials, you must specify them every time you start a new session.
* The `channel` parameter allows you to distinguish between different account types. When initializing the account, IBM Cloud is the default account used if have saved credentials for an IBM Quantum Platform and an IBM Cloud account.
* If you have saved credentials for both an IBM Quantum Platform account and an IBM Cloud account, IBM Cloud is the default account used when you initialize your account, unless you specify `set_as_default=True` in your IBM Quantum Platform account when you use the `save_account()` method.
* If you are saving multiple accounts per channel, consider using the `name` parameter to differentiate them.
* Credentials are saved to `$HOME/.qiskit/qiskit-ibm.json`. Do not manually edit this file.

<Admonition type="caution">
Account credentials are saved in plain text, so only do so if you are using a trusted device.
<Admonition type="warning">
**Protect your API token!** Never include your token in plain text in a notebook file. If you are working in a trusted Python environment (such as on a personal laptop or workstation), use the `save_account()` method described in the previous step to store your credentials, keeping in mind that your token will still be stored as plain text on your local drive. If you are executing code in a Python environment that is not trusted, see the [instructions in the next step](#untrusted-machine).
abbycross marked this conversation as resolved.
Show resolved Hide resolved
</Admonition>

<Admonition type="note">
Eric-Arellano marked this conversation as resolved.
Show resolved Hide resolved
IBM Cloud is the default account used if you don't specify a different channel or account name.
</Admonition>
1. <span id="untrusted-machine"></span>**Avoid executing code on an external or random cloud Python environment to minimize security risks.** If you must use an untrusted environment (on, for example, a public computer), change your API token after each use by rotating it on the [IBM Quantum Platform dashboard](https://quantum.ibm.com/) (click the refresh button in the API token field). To initialize the service in this situation, you can use code like the following:
abbycross marked this conversation as resolved.
Show resolved Hide resolved

<CodeAssistantAdmonition
abbycross marked this conversation as resolved.
Show resolved Hide resolved
tagLine="If you forget the setup code, try asking Qiskit Code Assistant."
prompts={[
"# Set up my IBM Runtime account using TOKEN for my token"
]}
/>
```python
from qiskit_ibm_runtime import QiskitRuntimeService

# Refresh your API token on the dashboard after using the code as follows:
service = QiskitRuntimeService(channel="ibm_quantum", token="<MY_IBM_QUANTUM_TOKEN>")
```

<Admonition type="warning">
When sharing code with others, ensure that your API token is not embedded directly within the Python script in plain text. Instead, share the script without the token and provide instructions for securely setting it up.
abbycross marked this conversation as resolved.
Show resolved Hide resolved
</Admonition>

1. Test your setup. Run a simple circuit using Sampler to ensure that your environment is set up properly:

Expand All @@ -121,20 +112,61 @@ print(result)
```

<span id="iqp-rest-api"></span>
## Set up to use IBM Quantum Platform with REST API
### Set up to use IBM Quantum Platform with REST API

Alternatively, you can also access quantum processors with REST APIs, enabling you to work with QPUs using any programming language or framework.

1. Retrieve your IBM Quantum token from the [IBM Quantum account page](https://quantum.ibm.com/account).

2. If you do not already have a user account, get one at the [IBM Quantum login page.](https://quantum.ibm.com/login) Your user account is associated with one or more [instances](./instances) (in the form hub / group / project) that give access to IBM Quantum services. Additionally, a unique token is assigned to each account, allowing for IBM Quantum access from Qiskit. The instructions in this section use our default instance. For instructions to choose a specific instance, see [Connect to an instance](./instances#connect-instance).
1. If you do not already have a user account, get one at the [IBM Quantum login page.](https://quantum.ibm.com/login) Your user account is associated with one or more [instances](./instances) (in the form hub / group / project) that give access to IBM Quantum services. Additionally, a unique token is assigned to each account, allowing for IBM Quantum access from Qiskit. The instructions in this section use our default instance. For instructions to choose a specific instance, see [Connect to an instance](./instances#connect-instance).

<Admonition type="note">
The Instances section in your [IBM Quantum account page](https://quantum.ibm.com/account) lists the instances that you can access.
</Admonition>

1. Retrieve your API token from the [IBM Quantum account page](https://quantum.ibm.com/account).

1. Because of the security risks posed by executing code that contains your API token in plain text, the recommended authentication method is to first create an environment variable for your API token, but only do so **if you are working in a trusted Python environment (such as on a personal laptop or workstation).** [Skip to the next step](#rest-untrusted) if you are not using a trusted environment, such as a shared or public computer.

The following demonstrates creating an environment variable:
abbycross marked this conversation as resolved.
Show resolved Hide resolved

```shell
export IQP_API_TOKEN=<your-token>
```
When you invoke the environment variable in your code, include `import os`, as in this example request:

```python
import os
import requests

reqUrl = "https://api.quantum-computing.ibm.com/runtime/jobs?limit=10&offset=0&exclude_params=true"

headersList = {
"Accept": "application/json",
"Authorization": "Bearer os.environ.pop(IQP_API_TOKEN)"
}
abbycross marked this conversation as resolved.
Show resolved Hide resolved

payload = ""

response = requests.request("GET", reqUrl, data=payload, headers=headersList)

print(response.json())
```

Note that when creating an environment variable, your API token is still stored locally in plain text, and should be safeguarded.

1. <span id="rest-untrusted"></span>**Avoid executing code on an external or random cloud Python environment to minimize security risks.** If you must use an untrusted environment (on, for example, a public computer), change your API token after each use by rotating it on the [IBM Quantum Platform dashboard](https://quantum.ibm.com/) (click the refresh button in the API token field). To initialize the service in this situation, use your API token directly:

```python
# Refresh your API token on the dashboard after using the code as follows:
"Authorization": "Bearer <your-token>"
```

<Admonition type="warning">
When sharing code with others, ensure that your API token is not embedded directly within the Python script in plain text. Instead, share the script without the token and provide instructions for securely setting it up.
abbycross marked this conversation as resolved.
Show resolved Hide resolved
</Admonition>

#### Use a temporary access token

3. Optionally obtain a temporary access token by supplying the API token obtained above. This is especially useful if you would like control over tokens, such as token invalidation. Alternatively, you can work directly with your IBM Quantum Platform API token.
1. Optionally obtain a temporary access token by supplying your API token. This is especially useful if you would like control over tokens, such as token invalidation.
abbycross marked this conversation as resolved.
Show resolved Hide resolved

```python
import requests
Expand All @@ -145,7 +177,7 @@ Alternatively, you can also access quantum processors with REST APIs, enabling y
auth_id=auth_response.json()['id']
```

4. View your available backends:
1. View your available backends:

```python
url_backends = 'https://api.quantum-computing.ibm.com/runtime/backends'
Expand All @@ -160,7 +192,7 @@ Alternatively, you can also access quantum processors with REST APIs, enabling y

You can then [transpile circuits using REST API](./transpile-rest-api) and run them using either the [Sampler or the Estimator primitives](./primitives-rest-api).

5. After your experiments are complete, you can proceed to invalidate your token and then test its invalidation.
1. After your experiments are complete, you can proceed to invalidate your token and then test its invalidation.

```python
logout_url = 'https://auth.quantum-computing.ibm.com/api/users/logout'
Expand Down Expand Up @@ -205,50 +237,50 @@ Alternatively, you can also access quantum processors with REST APIs, enabling y
1. Find your API key. From the [API keys page](https://cloud.ibm.com/iam/apikeys), view or create your API key, then copy it to a secure location so you can use it for authentication.
2. Find your Cloud Resource Name (CRN). Open the [Instances page](https://cloud.ibm.com/quantum/instances) and click your instance. In the page that opens, click the icon to copy your CRN. Save it in a secure location so you can use it for authentication.

<span id="cloud-save"></span>
1. Authenticate to the service by calling `QiskitRuntimeService` with your saved credentials or with your IBM Cloud API key and CRN:
1. <span id="cloud-save"></span>**If you are working in a trusted Python environment (such as on a personal laptop or workstation),** use the `save_account()` method to save your credentials locally. ([Skip to the next step](#cloud-untrusted) if you are not using a trusted environment, such as a shared or public computer, to authenticate to IBM Cloud.) To use `save_account()`, activate a Python virtual environment (see the [installation instructions](/guides/install-qiskit#local) if you do not already have a virtual environment set up). Then run Python in your virtual environment to open a read-eval-print loop (REPL), and enter the following:

```python
from qiskit_ibm_runtime import QiskitRuntimeService
```python
from qiskit_ibm_runtime import QiskitRuntimeService

service = QiskitRuntimeService(channel="ibm_cloud", token="<IBM Cloud API key>", instance="<IBM Cloud CRN>")
```
service = QiskitRuntimeService(
channel="ibm_cloud",
token="<IBM Cloud API key>", # Your token is confidential.
abbycross marked this conversation as resolved.
Show resolved Hide resolved
# Take care not to share your token in public code.
instance="<IBM Cloud CRN>",
name="<account-name>",
overwrite=True, # Use `overwrite=True` if you're updating your token.
)
```

You can optionally use the `save_account()` method to save your credentials for easy access later on, before initializing the service.
1. Close out of the REPL with `exit()`. From now on, whenever you need to authenticate to the service, you can load your credentials with `QiskitRuntimeService()`. Test your setup and ensure that you can connect to the service:

```python
```python
from qiskit_ibm_runtime import QiskitRuntimeService

# Save account to disk.
QiskitRuntimeService.save_account(
channel="ibm_cloud",
token="<IBM Cloud API key>",
instance="<IBM Cloud CRN>",
name="<account-name>",
# Use `overwrite=True` if you're updating your token.
overwrite=True,
)

# Load saved credentials
service = QiskitRuntimeService(name="<account-name>")
service = QiskitRuntimeService()
```

* If you save your credentials to disk, you can use `QiskitRuntimeService()` in the future to initialize your account. The `channel` parameter distinguishes between different account types. When initializing the account, IBM Cloud is the default account used if you have saved credentials for both an IBM Quantum Platform and an IBM Cloud account.
* If you are saving multiple accounts per channel, consider using the `name` parameter to differentiate them.
* Credentials are saved to `$HOME/.qiskit/qiskit-ibm.json`. Do not manually edit this file.
* If you don't save your credentials, you must specify them every time you start a new session.

<Admonition type="caution">
Account credentials are saved in plain text, so only do so if you are using a trusted device.
</Admonition>
* If you have saved credentials for both an IBM Quantum Platform account and an IBM Cloud account, IBM Cloud is the default account used when you initialize your account, unless you specify `set_as_default=True` in your IBM Quantum Platform account when you use the `save_account()` method.
* If you are saving multiple accounts per channel, consider using the `name` parameter to differentiate them.
* Credentials are saved to `$HOME/.qiskit/qiskit-ibm.json`. Do not manually edit this file.
* If you don't save your credentials, you must specify them every time you start a new session.

1. Test your setup. Ensure that you can connect to the service:
1. <span id="cloud-untrusted"></span>**Avoid executing code on an external or random cloud Python environment to minimize security risks.** If you must use an untrusted environment (on, for example, a public computer), change your API token after each use by deleting it on the [API keys page](https://cloud.ibm.com/iam/apikeys) and creating a new one. To initialize the service in this situation, you can use code like the following:

```python
```python
from qiskit_ibm_runtime import QiskitRuntimeService

service = QiskitRuntimeService()
```

service = QiskitRuntimeService(
channel="ibm_cloud",
# Delete your token on the API keys page after entering this code:
token="<IBM Cloud API key>",
instance="<IBM Cloud CRN>"
)
```
<Admonition type="warning">
When sharing code with others, ensure that your API token is not embedded directly within the Python script in plain text. Instead, share the script without the token and provide instructions for securely setting it up.
</Admonition>

## Next steps

Expand Down
Loading