Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Sessions are a key feature of hybrid quantum computing that allow you to group multiple quantum computing jobs together. A session is a logical grouping of one or more jobs that you submit to a single target. Each session has a unique ID attached to each job in that session. Sessions are useful when you want to run multiple quantum computing jobs in sequence, and run classical code between quantum jobs.
This article explains the architecture of sessions in hybrid quantum computing and how to create a new session in Azure Quantum.
Prerequisites
To create a session, you need the following prerequisites:
An Azure account with an active subscription. If you don’t have an Azure account, then register for free and sign up for a pay-as-you-go subscription.
An Azure Quantum workspace. For more information, see Create an Azure Quantum workspace.
A Python environment with Python and Pip installed.
The latest version of Visual Studio Code (VS Code), with the QDK extension, Python extension, and Jupyter extension installed.
The
qdkPython library. To submit Qiskit and Cirq programs, install theazure,qiskit, andcirqextras.pip install --upgrade "qdk[azure,qiskit,cirq]"
What is a session?
In sessions, you can move the client compute resource to the cloud for lower-latency and the ability to run your quantum program multiple times with different parameters. You can logically group jobs into one session, and you can prioritize jobs in that session over non-session jobs. Qubit states don't persist between jobs, but jobs in a session have shorter queue times. Shorter queue times allow you to run complex algorithms to better organize and track your individual quantum computing jobs.
Sessions are useful for parameterized quantum algorithms, where the output of one quantum computing job is used to define input parameters for the next quantum computing job. The most common examples of this type of algorithm are Variational Quantum Eigensolvers (VQE) and Quantum Approximate Optimization Algorithms (QAOA).
Supported hardware
Sessions are supported on all quantum computing hardware providers. In some cases, jobs that are submitted within a session are prioritized in the queue of that target. For more information, see Target behavior.
How to create a session
To create a session, follow these steps:
This example shows how to create a session with Q# inline code in a Jupyter notebook in VS Code.
Note
Sessions are managed with Python, even when running Q# inline code.
In VS Code, open the View menu and choose Command Palette.
Enter and select Create: New Jupyter Notebook.
In the top-right, VS Code will detect and display the version of Python and the virtual Python environment that was selected for the notebook. If you have multiple Python environments, you may need to select a kernel using the kernel picker in the top right. If no environment was detected, see Jupyter Notebooks in VS Code for setup information.
In the first cell of the notebook, run the following code:
from qdk.azure import Workspace workspace = Workspace(resource_id="") # add your resource IDAdd a new cell in the notebook and import the
qsharpPython package:from qdk import qsharpChoose your quantum target. In this example, the target is the IonQ simulator.
target = workspace.get_targets("ionq.simulator")Select the configurations of the target profile, either
Base,Adaptive_RI, orUnrestricted.qsharp.init(target_profile=qsharp.TargetProfile.Base)Note
Adaptive_RItarget profile jobs are currently supported on Quantinuum targets. For more information, see Integrated hybrid quantum computing.Write your Q# program. For example, the following Q# program generates a random bit. To illustrate the use of input arguments, this program takes an integer,
n, and an array of angles,angle, as input.%%qsharp import Std.Measurement.*; import Std.Arrays.*; operation GenerateRandomBits(n: Int, angle: Double[]) : Result[] { use qubits = Qubit[n]; // n parameter as the size of the qubit array for q in qubits { H(q); } R(PauliZ, angle[0], qubits[0]); // arrays as entry-points parameters R(PauliZ, angle[1], qubits[1]); let results = MeasureEachZ(qubits); ResetAll(qubits); return results; }Next, you create a session. Let's say you want to run
GenerateRandomBitoperation three times, so you usetarget.submitto submit the Q# operation with thetargetdata and you repeat the code three times - in a real world scenario, you may want to submit different programs instead of the same code.angle = [0.0, 0.0] with target.open_session(name="Q# session of three jobs") as session: target.submit(input_data=qsharp.compile(f"GenerateRandomBits(2, {angle})"), name="Job 1", shots=100) # First job submission angle[0] += 1 target.submit(input_data=qsharp.compile(f"GenerateRandomBits(2, {angle})"), name="Job 2", shots=100) # Second job submission angle[1] += 1 target.submit(input_data=qsharp.compile(f"GenerateRandomBits(2, {angle})"), name="Job 3", shots=100) # Third job submission session_jobs = session.list_jobs() [session_job.details.name for session_job in session_jobs]Important
When you pass arguments as parameters to the job, the arguments are formatted into the Q# expression when
qsharp.compileis called. This means that you need to format your arguments as Q# objects. In this example, because arrays in Python are already printed as[item0, item1, ...], the input arguments match the Q# formatting. For other Python data structures, you might need more handling to get the string values inserted into the Q# in a compatible way.Once you create a session, you can use
workspace.list_session_jobsto retrieve a list of all jobs in the session. For more information, see How to manage sessions.
Target behavior
Each quantum hardware provider defines their own heuristics to best manage the prioritization of jobs within a session.
Quantinuum
If you choose to submit jobs within a session to a Quantinuum target, then your session has exclusive access to the hardware as long as you queue jobs within one minute of each other. After that, your jobs are accepted and handled with the standard queueing and prioritization logic.