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.
PennyLane is an open-source Python framework for quantum programming. With the Microsoft Quantum Development Kit (QDK), you can write a quantum program in PennyLane, convert the program into QIR, and then submit the QIR to run on Azure Quantum targets. This article shows how to submit a PennyLane program to Azure Quantum from a Jupyter notebook in Visual Studio Code (VS Code).
For more information about PennyLane, see the PennyLane website.
Prerequisites
To submit a PennyLane program to Azure Quantum using the QDK Python library, you need to have an Azure Quantum workspace in your Azure subscription. To create a workspace, see Create an Azure Quantum workspace.
To follow the steps in this article, you need to have:
A Python environment with Python and Pip.
The latest version of the
qdkPython library with theazureextra.pip install --upgrade "qdk[azure]"The latest version of the PennyLane Python library.
pip install --upgrade "pennylane"
Submit a PennyLane program to Azure Quantum as QIR
The following steps explain how to write a PennyLane program, convert the program to QIR, and submit the program to Azure Quantum using the QDK Python library.
Import the required packages and objects
In VS Code, open the View menu and select Command Palette.
Enter Create: New Jupyter Notebook. An empty Jupyter notebook opens in a new tab.
In the first cell of the notebook, run the following imports:
import pennylane as qml from qdk.openqasm import compile from qdk.azure import Workspace from qdk import TargetProfile
Create a PennyLane circuit
Run the following code in a new cell to create a parameterized PennyLane circuit called
param_circ:device = qml.device('default.qubit', wires=2) @qml.qnode(device) def param_circ(theta): qml.H(0); qml.CNOT(wires=[0,1]) qml.RY(theta, wires=1) return qml.expval(qml.PauliZ(1))This circuit takes the parameter
thetato apply a rotation with theRYgate.Draw the circuit for a given value of
theta, for example0.3.print(qml.draw(param_circ)(0.3))
Convert the PennyLane program to QIR
To submit the PennyLane program to Azure Quantum, you need to convert the program to OpenQASM, and then convert the OpenQASM to QIR.
Convert the PennyLane program to OpenQASM.
theta = 0.3 qasm_str = qml.to_openqasm(param_circ)(theta)Set a QIR target profile and convert the OpenQASM to QIR.
target_profile = TargetProfile.Base qir = compile(qasm_str, target_profile)Note
The target profile must be compatible with the Azure Quantum target that you submit your job to. For more information about QIR target profiles, see Different types of target profiles in Azure Quantum.
Connect to your Azure Quantum workspace
To connect to your Azure Quantum workspace, you need the resource ID of the workspace. To find the resource ID, follow these steps:
- Sign in to the Azure portal.
- Go to your Azure Quantum workspace.
- Open the Overview panel.
- Copy the value in the Resource ID field.
To connect to your workspace, run the following code in your Jupyter notebook:
workspace = Workspace(resource_id="") # Add the resource ID of your workspace
Submit the program to Azure Quantum
Set your job parameters, including the Azure Quantum target name and the number of shots. For this example, the target is the free Rigetti simulator.
# Define parameters for the job submission target_name = 'rigetti.sim.qvm' job_name = 'pennylane-job' shots = 100 # Define parameter for the circuit theta = 0.3Note
You can only submit to Azure Quantum targets that are available in your workspace. To see your available targets, go to the Providers panel in your workspace in the Azure portal.
Set the target and run the job.
target = workspace.get_targets(target_name) job = target.submit(qir, job_name, shots)Display the job results.
results = job.get_results() print(results)