Skip to main content

IBM Quantum client for Qiskit Runtime.

Project description

Qiskit Runtime IBM Client

License CI Code style: black Coverage Status

Qiskit is an open-source SDK for working with quantum computers at the level of circuits, algorithms, and application modules.

Qiskit Runtime is a new architecture offered by IBM Quantum that streamlines quantum computations. It is designed to use classical compute resources to execute quantum circuits with more efficiency on quantum processors.

Using Qiskit Runtime, for example, a research team at IBM Quantum was able to achieve 120x speed up in their lithium hydride simulation. For more information, see the IBM Research blog.

This module provides the interface to access Qiskit Runtime.

Installation

You can install this package using pip:

pip install qiskit-ibm-runtime

Account Setup

Qiskit Runtime on IBM Cloud

Qiskit Runtime is now part of the IBM Quantum Services on IBM Cloud. To use this service, you'll need to create an IBM Cloud account and a quantum service instance. This guide contains step-by-step instructions on setting this up, including directions to find your IBM Cloud API key and Cloud Resource Name (CRN), which you will need for authentication.

Qiskit Runtime on IBM Quantum

Prior to becoming an IBM Cloud service, Qiskit Runtime was offered on IBM Quantum. If you have an existing IBM Quantum account, you can continue using Qiskit Runtime on IBM Quantum.

You will need your IBM Quantum API token to authenticate with the Qiskit Runtime service:

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

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

Saving Account on Disk

Once you have the account credentials, you can save them on disk, so you won't have to input them each time. The credentials are saved in the $HOME/.qiskit/qiskit-ibm.json file, where $HOME is your home directory.

:warning: Account credentials are saved in plain text, so only do so if you are using a trusted device.
from qiskit_ibm_runtime import QiskitRuntimeService

# Save an IBM Cloud account.
QiskitRuntimeService.save_account(channel="ibm_cloud", token="MY_IBM_CLOUD_API_KEY", instance="MY_IBM_CLOUD_CRN")

# Save an IBM Quantum account.
QiskitRuntimeService.save_account(channel="ibm_quantum", token="MY_IBM_QUANTUM_TOKEN")

Once the account is saved on disk, you can instantiate the service without any arguments:

from qiskit_ibm_runtime import QiskitRuntimeService
service = QiskitRuntimeService()

Loading Account from Environment Variables

Alternatively, the service can discover credentials from environment variables:

export QISKIT_IBM_TOKEN="MY_IBM_CLOUD_API_KEY"
export QISKIT_IBM_INSTANCE="MY_IBM_CLOUD_CRN"
export QISKIT_IBM_CHANNEL="ibm_cloud"

Then instantiate the service without any arguments:

from qiskit_ibm_runtime import QiskitRuntimeService
service = QiskitRuntimeService()

Enabling Account for Current Python Session

As another alternative, you can also enable an account just for the current session by instantiating the service with your credentials.

from qiskit_ibm_runtime import QiskitRuntimeService

# For an IBM Cloud account.
ibm_cloud_service = QiskitRuntimeService(channel="ibm_cloud", token="MY_IBM_CLOUD_API_KEY", instance="MY_IBM_CLOUD_CRN")

# For an IBM Quantum account.
ibm_quantum_service = QiskitRuntimeService(channel="ibm_quantum", token="MY_IBM_QUANTUM_TOKEN")

Qiskit Runtime Session

A Qiskit Runtime session allows you to group a collection of iterative calls to the quantum computer. A session is started when the first job within the session is started. Subsequent jobs within the session are prioritized by the scheduler to minimize artificial delay within an iterative algorithm. Data used within a session, such as transpiled circuits, is also cached to avoid unnecessary overhead.

You can use the qiskit_ibm_runtime.Session class to start a session. You are encouraged to start a session as a context manager, to ensure the session is automatically closed upon exit. There are some examples in the sections below.

Primitives

Primitives are a simplified interface for defining near-time quantum-classical workloads <https://research.ibm.com/blog/near-real-time-quantum-compute>_ required to efficiently build and customize applications. The initial release of Qiskit Runtime includes two primitives: Estimator and Sampler. They perform foundational quantum computing tasks and act as an entry point to the Qiskit Runtime service.

There are several different options you can specify when calling the primitives. See qiskit_ibm_runtime.Options class for more information.

Sampler

This is a primitive that takes a list of user circuits as an input and generates an error-mitigated readout of quasi-probabilities. This provides users a way to better evaluate shot results using error mitigation and enables them to more efficiently evaluate the possibility of multiple relevant data points in the context of destructive interference.

To invoke the Sampler primitive within a session:

from qiskit_ibm_runtime import QiskitRuntimeService, Session, Options, Sampler
from qiskit import QuantumCircuit

service = QiskitRuntimeService()
options = Options(optimization_level=1)
options.execution.shots = 1024  # Options can be set using auto-complete.

bell = QuantumCircuit(2)
bell.h(0)
bell.cx(0, 1)
bell.measure_all()

with Session(service=service, backend="ibmq_qasm_simulator") as session:
    sampler = Sampler(session=session, options=options)
    job = sampler.run(circuits=bell)
    print(f"Job ID is {job.job_id()}")
    print(f"Job result is {job.result()}")

    # You can make additional calls to Sampler and/or Estimator.

Estimator

This is a primitive that takes circuits and observables to evaluate expectation values and variances for a given parameter input. This primitive allows users to efficiently calculate and interpret expectation values of quantum operators required for many algorithms.

To invoke the Estimator primitive within a session:

from qiskit_ibm_runtime import QiskitRuntimeService, Session, Options, Estimator
from qiskit import QuantumCircuit
from qiskit.circuit.library import RealAmplitudes
from qiskit.quantum_info import SparsePauliOp

service = QiskitRuntimeService()
options = Options(optimization_level=1)
options.execution.shots = 1024  # Options can be set using auto-complete.

psi1 = RealAmplitudes(num_qubits=2, reps=2)
H1 = SparsePauliOp.from_list([("II", 1), ("IZ", 2), ("XI", 3)])
theta1 = [0, 1, 1, 2, 3, 5]

with Session(service=service, backend="ibmq_qasm_simulator") as session:
    estimator = Estimator(session=session, options=options)

    # calculate [ <psi1(theta1)|H1|psi1(theta1)> ]
    job = estimator.run(circuits=[psi1], observables=[H1], parameter_values=[theta1])
    print(f"Job ID is {job.job_id()}")
    print(f"Job result is {job.result()}")

    # You can make additional calls to Sampler and/or Estimator.

Accessing Qiskit Runtime Programs

In addition to the primitives, there are other Qiskit Runtime programs that you can call directly. These programs, however, don't have special class wrappers.

Finding available programs

To list all available programs:

from qiskit_ibm_runtime import QiskitRuntimeService
service = QiskitRuntimeService()
service.pprint_programs()

pprint_programs() prints the summary metadata of the first 20 programs visible to you. A program's metadata consists of its ID, name, description, input parameters, return values, interim results, and other information that helps you to know more about the program. pprint_programs(detailed=True, limit=None) will print all metadata for all programs visible to you.

Executing a Program

To run a program, specify the program ID, input parameters, as well as any execution options:

from qiskit_ibm_runtime import QiskitRuntimeService

service = QiskitRuntimeService()
program_inputs = {
    'iterations': 1
}
options = {'backend': 'ibmq_qasm_simulator'}
job = service.run(
    program_id="hello-world",
    options=options,
    inputs=program_inputs)
print(f"job ID: {job.job_id()}")
result = job.result()

Accessing your IBM Quantum backends

A backend is a quantum device or simulator capable of running quantum circuits or pulse schedules.

You can query for the backends you have access to. Attributes and methods of the returned instances provide information, such as qubit counts, error rates, and statuses, of the backends.

from qiskit_ibm_runtime import QiskitRuntimeService
service = QiskitRuntimeService()

# Display all backends you have access.
print(service.backends())

# Get a specific backend.
backend = service.backend('ibmq_qasm_simulator')

# Print backend coupling map.
print(backend.coupling_map)

Next Steps

Now you're set up and ready to check out some of the tutorials.

Contribution Guidelines

If you'd like to contribute to qiskit-ibm-runtime, please take a look at our contribution guidelines. This project adheres to Qiskit's code of conduct. By participating, you are expected to uphold to this code.

We use GitHub issues for tracking requests and bugs. Please use our slack for discussion and simple questions. To join our Slack community use the invite link at Qiskit.org. For questions that are more suited for a forum we use the Qiskit tag in Stack Exchange.

License

Apache License 2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qiskit-ibm-runtime-0.10.0.tar.gz (190.7 kB view details)

Uploaded Source

Built Distribution

qiskit_ibm_runtime-0.10.0-py3-none-any.whl (177.3 kB view details)

Uploaded Python 3

File details

Details for the file qiskit-ibm-runtime-0.10.0.tar.gz.

File metadata

  • Download URL: qiskit-ibm-runtime-0.10.0.tar.gz
  • Upload date:
  • Size: 190.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for qiskit-ibm-runtime-0.10.0.tar.gz
Algorithm Hash digest
SHA256 bcc4337c8e3c036d7e98b3d7ddcb31875831826d581b7fa1ccc0a050d9c12665
MD5 d5dafbfc1bdca0413111b8847f65194c
BLAKE2b-256 e71f7e8be63e834f1c753ffc5b325858e0b5bfb3199d603377d029ee4b49e932

See more details on using hashes here.

File details

Details for the file qiskit_ibm_runtime-0.10.0-py3-none-any.whl.

File metadata

File hashes

Hashes for qiskit_ibm_runtime-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c60983874d00f7beb297f6f8ace3a23b392869b021f0c672e0e91f7a154f20cf
MD5 efab21e9064bc85b666a9a93aee041bc
BLAKE2b-256 1c94084c5188e0c116a0787e71cd9d6fb36fe61bbf92a2e30597ee2fe787d2ca

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page