Qiskit
Qiskit is an open-source quantum programming library developed in Python by IBM. Like PennyLane and Snowflurry, it allows for building, simulating, and executing quantum circuits.
Installation¶
-
Load Qiskit dependencies.
-
Create and activate a Python virtual environment.
-
Install a specific version of Qiskit.
whereX.Y.Zrepresents the version number, for example1.4.0. To install the latest version available on our clusters, do not specify a version. Here, we have only importedqiskitandqiskit_aer. You can add other Qiskit packages according to your needs by following the structureqiskit_package==X.Y.Zwhereqiskit_packagerepresents the desired package, for exampleqiskit-finance. Currently available wheels are listed on the Python Wheels page. -
Validate the Qiskit installation.
-
Freeze the environment and dependencies.
Run Qiskit on a Cluster¶
#!/bin/bash
#SBATCH --account=def-someuser #specify your account name
#SBATCH --time=00:15:00 #modify if necessary
#SBATCH --cpus-per-task=1 #modify if necessary
#SBATCH --mem-per-cpu=1G #modify if necessary
# Load module dependencies.
module load StdEnv/2023 gcc python/3.11 symengine/0.11.2
# Generate the 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
# Run the Qiskit program.
python qiskit_example.py
Use Qiskit with MonarQ¶
It is possible to use MonarQ directly with Qiskit via the qiskit-calculquebec plugin. This plugin allows for developing and executing Qiskit circuits on Calcul Québec's infrastructure.
Install Dependencies¶
- Step 1: Install dependencies
Note
qiskit-calculquebec automatically installs Qiskit.
Initialize MonarQ Backend¶
- Step 2: Configure your credentials and the backend
- Create a client with your credentials. Your token is available via the Thunderhead portal.
- The host is
https://monarq.calculquebec.ca. - Then initialize the MonarQ backend.
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"
# Client and backend creation
my_client = CalculQuebecClient(host, user, access_token, project_id=project_id)
backend = MonarQBackend("monarq", my_client)
# Simple circuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
Execute the Circuit¶
- Step 3: Transpile and execute the circuit
from qiskit_ibm_runtime import SamplerV2 as Sampler
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
# Backend-adapted transpilation
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¶
Note
Transpilation is necessary to adapt the circuit to MonarQ's connectivity and native gates.
Note
The number of shots can be adjusted as needed (maximum: 1024).
Note
The use of SamplerV2 is recommended for executing circuits with measurements.