122 lines
6.9 KiB
Plaintext
122 lines
6.9 KiB
Plaintext
---
|
|
title: Use Qiskit Code Assistant in VS Code
|
|
description: How to install, use, and configure Qiskit Code Assistant in Visual Studio Code (VS Code).
|
|
---
|
|
|
|
# Use Qiskit Code Assistant in Visual Studio Code
|
|
|
|
Learn how to install, use, configure and uninstall the official Qiskit Code Assistant extension in Visual Studio Code (VS Code).
|
|
|
|
<Admonition type="note" title="Notes">
|
|
- This is an experimental feature available only to IBM Quantum™ Premium Plan users.
|
|
- Qiskit Code Assistant is in preview release status and is subject to change.
|
|
- If you have feedback or want to contact the developer team, use the [Qiskit Slack Workspace channel](https://qiskit.enterprise.slack.com/archives/C07LYA6PL83) or the related public GitHub repositories.
|
|
</Admonition>
|
|
|
|
|
|
<video title="A user uses Qiskit Code Assistant in Visual Studio Code. The user has previous imports and a function definition called get_random_linear_function, with a docstring defining what the function should do. After typing the Ctrl and . keys, Qiskit Code Assistant autocompletes the function with the code to generate a random linear function using Numpy and Qiskit. The video ends with the user executing the code generated and getting a matrix representing a random linear function in the terminal." className="max-w-auto h-auto" controls>
|
|
<source src="/videos/guides/qiskit-code-assistant/demo-Vscode-RandomLinearFunction_8b-qiskit.mp4" type="video/mp4"></source>
|
|
</video>
|
|
|
|
## Install the VS Code extension
|
|
|
|
To install the [VS Code extension](https://github.com/Qiskit/qiskit-code-assistant-vscode), follow these steps:
|
|
|
|
Install directly from VS Code:
|
|
|
|
1. Launch VS Code.
|
|
1. Click the Extensions icon in the left toolbar.
|
|
1. Search for `qiskit`
|
|
1. Find "Qiskit Code Assistant (Beta)" and click **Install**.
|
|
|
|
Alternatively, the extension is also available through the [VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=qiskit.qiskit-vscode).
|
|
|
|
The extension loads automatically and is listed on the bottom of the VS Code window, as shown. If it is not listed, reload the extension or application.
|
|
|
|
![Qiskit Code Assistant is shown on the bottom of the VS Code window.](/images/guides/qiskit-code-assistant/vscode-selected-model.png)
|
|
|
|
## Configure extension settings
|
|
|
|
The following settings can be configured:
|
|
|
|
- To change keyboard shortcuts, open the Keyboard Shortcuts settings (`Cmd/Ctrl`+`Shift`+`P` -> `Preferences: Open Keyboard Shortcuts (JSON)`) and search for `qiskit-vscode`.
|
|
|
|
- You can change the IBM Quantum API token to use in the VS Code command palette. To do that, type `Cmd/Ctrl`+`Shift`+`P`, search for `qiskit`, select the **Qiskit Code Assistant: Set IBM Quantum API token** command, and paste your IBM Quantum API token.
|
|
|
|
- [Advanced] To change the instance of the Qiskit Code Assistant Service that the extension should use, go to File -> Preferences -> Settings. On the User tab, search for Qiskit, and edit the `Qiskit Code Assistant: Url`.
|
|
|
|
## Get started using the Qiskit Code Assistant extension for VS Code
|
|
|
|
|
|
### Authentication and setup
|
|
|
|
After installing the extension, it tries to authenticate you. By default, the package tries to authenticate to IBM Quantum services with the defined Qiskit API token, and uses your token from the `QISKIT_IBM_TOKEN` environment variable or from the file `~/.qiskit/qiskit-ibm.json` (under the section `default-ibm-quantum`). If you need help configuring your account, follow the instructions in [Set up to use IBM Quantum Platform.](/guides/setup-channel#set-up-to-use-ibm-quantum-platform)
|
|
|
|
By default, the extension uses the `granite-8b-qiskit` model, which is listed in the Model Picker in the bottom status bar.
|
|
|
|
![The bottom status bar is shown, with `granite-8b-qiskit` listed.](/images/guides/qiskit-code-assistant/vscode-selected-model.png)
|
|
|
|
The first time you use the `granite-8b-qiskit` model, a modal opens listing some major restrictions that you should be aware of when using the model. Clicking `Accept` will accept the disclaimer and enable the model for code generation.
|
|
|
|
![The window that opens upon first use contains restrictions that must be accepted.](/images/guides/qiskit-code-assistant/eula.png)
|
|
|
|
### Generate code
|
|
|
|
While you develop your code using Qiskit, you can ask Qiskit Code Assistant to help you. In general, the Assistant suggests better code in response to Python comments or docstrings, but you can use the Assistant anywhere in your file.
|
|
|
|
To get a code suggestion, type a prompt, then `Ctrl`+`.`. There are two types of prompts you can use:
|
|
|
|
- Enter partial code and get a suggestion to finish the code. Example:
|
|
```python
|
|
from qiskit.circuit import QuantumCircuit
|
|
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
|
|
from qiskit_ibm_runtime import SamplerV2 as Sampler
|
|
from qiskit_ibm_runtime.fake_provider import FakeManilaV2
|
|
|
|
# Bell Circuit
|
|
qc = QuantumCircuit(2)
|
|
qc.h(0)
|
|
qc.cx(0, 1)
|
|
|
|
qc.mea # Type Ctrl + . for a code suggestion.
|
|
```
|
|
- Type a pound (#) sign, then type your prompt. Example: `# Give me a random circuit of 5 qubits and a depth of 4.`
|
|
|
|
![A code suggestion is shown, and is greyed out.](/images/guides/qiskit-code-assistant/vscode-inline-suggestion.png)
|
|
|
|
### Work with code suggestions
|
|
|
|
Use the following to accept, reject, and cycle through suggestions:
|
|
|
|
- Press `Tab` to accept the suggestion or press `ESC` to reject it.
|
|
- After getting a suggestion, type `Ctrl` + `.` again to generate additional suggestions.
|
|
- When a suggestion is shown, use `Option/Alt` + `]` and `Option/Alt` + `[` to cycle forward and backward, respectively, through the suggestions (if available).
|
|
- Hover the cursor over the suggestion to use the pop-up toolbar to cycle through the suggestions, as shown:
|
|
![The toolbar that pops up if you hover over suggested code is shown.](/images/guides/qiskit-code-assistant/vscode-inline-suggestion-toolbar.png)
|
|
|
|
## Uninstall the VS Code extension
|
|
|
|
To remove Qiskit Code Assistant from VS Code, follow these steps:
|
|
|
|
1. Launch Visual Studio Code.
|
|
1. Click the Extensions icon.
|
|
1. Search for `qiskit` and open Qiskit Code Assistant.
|
|
1. Click **Uninstall**.
|
|
|
|
|
|
## Troubleshooting
|
|
|
|
If you don't see the extension status bar in VS Code, check that the extension is installed and enabled under the extensions tab.
|
|
|
|
If the extension is installed and enabled, but it cannot select a model, verify that your current API token has been added and the `Qiskit Code Assistant: Url` is properly set.
|
|
|
|
## Contribute to the VS Code extension
|
|
|
|
The code for this official extension is publicly available and open source. Check it out in [GitHub.](https://github.com/Qiskit/qiskit-code-assistant-vscode)
|
|
|
|
## Next steps
|
|
|
|
<Admonition type="tip" title="Recommendations">
|
|
See examples to use Qiskit Code Assistant for [circuits](/guides/circuit-library), [configuring error suppression](/guides/configure-error-suppression), and [transpiling with pass managers.](/guides/transpile-with-pass-managers)
|
|
</Admonition>
|