Edit

How to submit a PennyLane circuit to Azure Quantum

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.

  • VS Code with the Python and Jupyter extensions installed.

  • The latest version of the qdk Python library with the azure extra.

    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

  1. In VS Code, open the View menu and select Command Palette.

  2. Enter Create: New Jupyter Notebook. An empty Jupyter notebook opens in a new tab.

  3. 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

  1. 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 theta to apply a rotation with the RY gate.

  2. Draw the circuit for a given value of theta, for example 0.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.

  1. Convert the PennyLane program to OpenQASM.

    theta = 0.3
    qasm_str = qml.to_openqasm(param_circ)(theta)
    
  2. 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:

  1. Sign in to the Azure portal.
  2. Go to your Azure Quantum workspace.
  3. Open the Overview panel.
  4. 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

  1. 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.3
    

    Note

    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.

  2. Set the target and run the job.

    target = workspace.get_targets(target_name)
    job = target.submit(qir, job_name, shots)
    
  3. Display the job results.

    results = job.get_results()
    print(results)