Qiskit/en
Developed in Python by IBM, Qiskit is an open-source quantum computing library. Like PennyLane and Snowflurry, it allows you to build, simulate, and run quantum circuits.
Installation¶
-
Load the Qiskit dependencies.
-
Create and activate a Python virtual environment.
-
Install a version of Qiskit.
whereX.Y.Zis the version number, for example1.4.0. To install the most recent version available on our clusters, do not specify a number. Here, we only importedqiskitandqiskit_aer. You can add other Qiskit software with the syntaxqiskit_package==X.Y.Zwhereqiskit_packageis the software name, for exampleqiskit-finance. To see the wheels that are currently available, see Available Python wheels. -
Validate the installation.
-
Freeze the environment and its dependencies.
Running Qiskit on a cluster¶
script.sh
You can then submit your job to the scheduler.
#!/bin/bash
#SBATCH --account=def-someuser #Modify with your account name
#SBATCH --time=00:15:00 #Modify as needed
#SBATCH --cpus-per-task=1 #Modify as needed
#SBATCH --mem-per-cpu=1G #Modify as needed
# Load module dependencies.
module load StdEnv/2023 gcc python/3.11 symengine/0.11.2
# Generate your virtual environment in $SLURM_TMPDIR.
virtualenv --no-download ${SLURM_TMPDIR}/env
source ${SLURM_TMPDIR}/env/bin/activate
# Install Qiskit and its dependencies.
pip install --no-index --upgrade pip
pip install --no-index --requirement ~/qiskit_requirements.txt
# Modify your Qiskit program.
python qiskit_example.py
Using Qiskit with MonarQ¶
You can use MonarQ directly with Qiskit via the qiskit-calculquebec plugin. This plugin allows you to develop and run Qiskit circuits on the Calcul Québec infrastructure.
Install the dependencies¶
- Step 1: Install the dependencies
Note
qiskit-calculquebec installs Qiskit automatically.
MonarQ backend initialisation¶
- Step 2: Set up your credentials and the backend
- Create a client using your credentials. Your token is available via the Thunderhead portal.
- The host is
https://monarq.calculquebec.ca. - Then initialize the MonarQ backend.
qiskit_example.py
import qiskit
from qiskit import QuantumCircuit
from qiskit_calculquebec.API.client import CalculQuebecClient
from qiskit_calculquebec.backends import MonarQBackend
# Credentials
user = "username"
access_token = "token"
host = "https://monarq.calculquebec.ca"
project_id = "project_id"
# Creating the client and the backend
my_client = CalculQuebecClient(host, user, access_token, project_id=project_id)
backend = MonarQBackend("monarq", my_client)
# Simple circuit example
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
Running the circuit¶
- Step 3: Transpile and run the circuit
qiskit_example.py
from qiskit_ibm_runtime import SamplerV2 as Sampler
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
# Transpilation tailored to the backend
pm = generate_preset_pass_manager(optimization_level=3, backend=backend)
transpiled_qc = pm.run(qc)
# Execution
sampler = Sampler(mode=backend)
job = sampler.run([transpiled_qc], shots=1000)
result = job.result()
print(result[0].data.meas.get_counts())
Notes¶
- Transpilation is required to adapt the circuit to MonarQ's native connectivity and ports.
- The number of shots can be adjusted as needed (maximum: 1024).
- The use of
SamplerV2is recommended for running circuits with measures.