Sampler/Estimator Primitives with IBM Quantum Service & IBM Quantum System QRMI - Python Example#
Prerequisites#
Python 3.11 or 3.12
Installation of QRMI primitives Python package (qiskit-qrmi-primitives)
Install dependencies#
Assuming your Python virtual environment is located at
~/py311venv_qrmi_primitives/bin/activate:
source ~/py311venv_qrmi_primitives/bin/activate
pip install -r requirements.txt
Set environment variables#
Because QRMI is an environment variable driven software library, all
configuration parameters must be specified in environment variables. The
required environment variables are listed below. This example assumes
that a .env file is available under the current directory.
Common#
When run as a job in a Slurm cluster, these environment variables are set by the SPANK plugin.
Environment variables |
Descriptions |
|---|---|
|
Quantum resource names.
Comma-separated values,
e.g. |
|
Quantum resource types.
Comma-separated values
corresponding to each Quantum
resource name specified by
|
IBM Quantum System specific#
When run as a job in a Slurm cluster, these environment variables are set by users or administrator.
Environment variables |
Descriptions |
|---|---|
|
Quantum System endpoint URL |
|
IBM Cloud IAM endpoint
URL (e.g. |
|
IBM Cloud IAM API Key |
|
Cloud Resource Name (CRN) of the
provisioned Quantum System
instance, starting with
|
|
AWS Access Key ID to access S3 bucket |
|
AWS Secret Access Key to access S3 bucket |
|
S3 endpoint URL |
|
S3 bucket name |
|
S3 bucket region
name (e.g. |
|
Time (in seconds) after which job should time out and get cancelled. It is based on system execution time (not wall clock time). System execution time is the amount of time that the system is dedicated to processing your job. |
Example#
export QRMI_JOB_QPU_RESOURCES=test_eagle
export QRMI_JOB_QPU_TYPES=ibm-quantum-system
export test_eagle_QRMI_IBM_QS_ENDPOINT=http://localhost:8080
export test_eagle_QRMI_IBM_QS_IAM_ENDPOINT=https://iam.cloud.ibm.com
export test_eagle_QRMI_IBM_QS_IAM_APIKEY=your_apikey
export test_eagle_QRMI_IBM_QS_SERVICE_CRN=your_instance
export test_eagle_QRMI_IBM_QS_AWS_ACCESS_KEY_ID=your_aws_access_key_id
export test_eagle_QRMI_IBM_QS_AWS_SECRET_ACCESS_KEY=your_aws_secret_access_key
export test_eagle_QRMI_IBM_QS_S3_ENDPOINT=https://s3.us-east.cloud-object-storage.appdomain.cloud
export test_eagle_QRMI_IBM_QS_S3_BUCKET=test
export test_eagle_QRMI_IBM_QS_S3_REGION=us-east
export test_eagle_QRMI_IBM_QS_TIMEOUT_SECONDS=86400
IBM Quantum Compute Service specific#
When run as a job in a Slurm cluster, these environment variables are set by users or administrator.
Environment variables |
Descriptions |
|---|---|
|
IBM Quantum Compute Service endpoint
URL (e.g. |
|
IBM Cloud IAM endpoint
URL (e.g. |
|
IBM Cloud IAM API Key |
|
Cloud Resource Name (CRN) of the
provisioned Quantum System
instance, starting with
|
|
Time (in seconds) after which job should time out and get cancelled. It is based on system execution time (not wall clock time). System execution time is the amount of time that the system is dedicated to processing your job. |
|
Session mode, default=’dedicated’, batch or dedicated. |
|
Session ID, set by acquire function. Optional for acquire function, however, required other functions. |
Example#
export QRMI_JOB_QPU_RESOURCES=ibm_torino,ibm_marrakesh
export QRMI_JOB_QPU_TYPES=ibm-quantum-compute-service,ibm-quantum-compute-service
export ibm_torino_QRMI_IBM_QCS_ENDPOINT=https://quantum.cloud.ibm.com/api/v1
export ibm_torino_QRMI_IBM_QCS_IAM_ENDPOINT=https://iam.cloud.ibm.com
export ibm_torino_QRMI_IBM_QCS_IAM_APIKEY=your_apikey
export ibm_torino_QRMI_IBM_QCS_SERVICE_CRN=your_instance
export ibm_marrakesh_QRMI_IBM_QCS_ENDPOINT=https://quantum.cloud.ibm.com/api/v1
export ibm_marrakesh_QRMI_IBM_QCS_IAM_ENDPOINT=https://iam.cloud.ibm.com
export ibm_marrakesh_QRMI_IBM_QCS_IAM_APIKEY=your_apikey
export ibm_marrakesh_QRMI_IBM_QCS_SERVICE_CRN=your_instance
IBM Qiskit Runtime Service specific (deprecated)#
When run as a job in a Slurm cluster, these environment variables are set by users or administrator.
Environment variables |
Descriptions |
|---|---|
|
IBM Quantum Compute (formerly Qiskit Runtime) Service endpoint
URL (e.g. |
|
IBM Cloud IAM endpoint
URL (e.g. |
|
IBM Cloud IAM API Key |
|
Cloud Resource Name (CRN) of the
provisioned Quantum System
instance, starting with
|
|
Time (in seconds) after which job should time out and get cancelled. It is based on system execution time (not wall clock time). System execution time is the amount of time that the system is dedicated to processing your job. |
|
Session mode, default=’dedicated’, batch or dedicated. |
|
Session ID, set by acquire function. Optional for acquire function, however, required other functions. |
Example#
export QRMI_JOB_QPU_RESOURCES=ibm_torino,ibm_marrakesh
export QRMI_JOB_QPU_TYPES=qiskit-runtime-service,qiskit-runtime-service
export ibm_torino_QRMI_IBM_QRS_ENDPOINT=https://quantum.cloud.ibm.com/api/v1
export ibm_torino_QRMI_IBM_QRS_IAM_ENDPOINT=https://iam.cloud.ibm.com
export ibm_torino_QRMI_IBM_QRS_IAM_APIKEY=your_apikey
export ibm_torino_QRMI_IBM_QRS_SERVICE_CRN=your_instance
export ibm_marrakesh_QRMI_IBM_QRS_ENDPOINT=https://quantum.cloud.ibm.com/api/v1
export ibm_marrakesh_QRMI_IBM_QRS_IAM_ENDPOINT=https://iam.cloud.ibm.com
export ibm_marrakesh_QRMI_IBM_QRS_IAM_APIKEY=your_apikey
export ibm_marrakesh_QRMI_IBM_QRS_SERVICE_CRN=your_instance
How to run this example#
SamplerV2#
Code is based on the “Get started with Sampler” tutorial.
Run sampler.py:
python sampler.py
EstimatorV2#
Code is based on the “Get started with Estimator” tutorial.
Run estimator.py:
python estimator.py
SQD Tutorial#
01_chemistry_hamiltonian.ipynb is a QRMI primitive port of Improving energy estimation of a chemistry Hamiltonian with SQD.
Start Jupyter Notebook and run all cells from the beginning.