2
$\begingroup$

I have an IBM Quantum account. How can I save the IBM Quantum API token to local environment so that I can access services including real quantum systems, simulators and Qiskit Runtime programs on my local machine using Qiskit?

$\endgroup$

4 Answers 4

2
$\begingroup$
  1. Create an IBM Quantum account or log in to your existing account by visiting the IBM Quantum login page.

  2. Copy (and/or optionally regenerate) your API token from your IBM Quantum account page.

  3. Take your token from step 2, here called MY_API_TOKEN, and run:

    from qiskit import IBMQ
    IBMQ.save_account('MY_API_TOKEN')
    

    The command above stores your credentials locally in a configuration file called qiskitrc. By default, this file is located in $HOME/.qiskit, where $HOME is your home directory. If you are still using Qconfig.py, please delete that file and run the command above.

See more info here: https://github.com/Qiskit/qiskit-ibmq-provider#setting-up-the-ibm-quantum-provider

$\endgroup$
1
$\begingroup$

You can use python Keyring package for storing and retrieving the secrets on local OS. It leverages the OS provided security and supports mac, windows and linux. This avoids token leak into plain text and notebooks.

If you are on windows os, you can store the credentials into windows credential manager and retrieve it using the keyring.

Storing token: (One time)
import keyring
keyring.set_password("IBMQ", "ibmq-token", "the-token-goes-here")
Retrieve token:
retrieved_token = keyring.get_password("IBMQ", "ibmq-token")

Then perform login:

IBMQ.enable_account(retrieved_token)
$\endgroup$
1
$\begingroup$

from @yet-another-random-guy,

i think the syntax changed from set_as_default = True to overwrite= True

->

 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>", overwrite= True)
 
# Load saved credentials
service = QiskitRuntimeService() 

This works for me, thanks @yet-another-random-guy for your timely update!

$\endgroup$
1
$\begingroup$

Since the package qiskit.providers.ibmq is being deprecated. The latest way to load it is via using service.

Authenticate to the service by calling QiskitRuntimeService with your IBM Cloud API key

# do pip install qiskit-ibm-runtime first
from qiskit_ibm_runtime import QiskitRuntimeService
 
service = QiskitRuntimeService(channel="ibm_quantum", token="<MY_IBM_QUANTUM_TOKEN>")

or, optionally use the save_account() method to save your credentials for easy access later on, before initializing the service.

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>", overwrite=True)
 
# Load saved credentials
service = QiskitRuntimeService()

and now you can also test it:

 from qiskit import QuantumCircuit
 from qiskit_ibm_runtime import QiskitRuntimeService, Sampler
 
 # Create empty circuit
 example_circuit = QuantumCircuit(2)
 example_circuit.measure_all()
 
 # You'll need to specify the credentials when initializing QiskitRuntimeService, if they were not previously saved.
 service = QiskitRuntimeService()
 backend = service.backend("ibmq_qasm_simulator")
 job = Sampler(backend).run(example_circuit)
 print(f"job id: {job.job_id()}")
 result = job.result()
 print(result)

or you can check which backends you have access to:

service.backends()

** Updated from the recent comments.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.