> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ionq.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PennyLane

> Learn how to use PennyLane to submit quantum circuits to IonQ's simulators and quantum computers.

## What is PennyLane?

[PennyLane](https://pennylane.ai/) is an open-source Python library designed for quantum algorithm development, quantum chemistry, and quantum machine learning (QML). It facilitates the creation, simulation, and optimization of quantum circuits, while enabling seamless integration with classical machine learning frameworks such as JAX, TensorFlow, and PyTorch. One of PennyLane’s key features is its ability to compute gradients of quantum circuits, a critical component for quantum machine learning models. PennyLane supports various quantum computing platforms, including IonQ, through its plugin system.

IonQ's [PennyLane plugin](https://pennylane-ionq.readthedocs.io) allows users to leverage IonQ's powerful trapped-ion quantum systems and high-performance cloud simulator directly within PennyLane, making it simpler to integrate quantum computing into your machine learning projects.

## Before you begin

Ensure you have an account on the [IonQ Quantum Cloud](https://cloud.ionq.com), and generate an API key for authentication. If you need assistance with this, there's a helpful guide on [setting up and managing your API keys](/guides/managing-api-keys).

You should also have Python 3.11 installed on your computer.

<Tip>
  To check your Python version, run `python --version` in your terminal.
</Tip>

***

## Setting up PennyLane and the IonQ plugin

Install PennyLane and the IonQ plugin using pip:

```bash theme={null}
pip install pennylane pennylane-ionq
```

<Note>
  **Note:** It's recommended to perform this installation within a virtual
  environment using tools like
  [virtualenv](https://virtualenv.pypa.io/en/latest/) or
  [conda](https://docs.conda.io/en/latest/) to manage your Python packages
  cleanly.
</Note>

## Configuring your environment

PennyLane will use the `IONQ_API_KEY` environment variable by default. Visit the [IonQ Cloud](https://cloud.ionq.com/settings/keys) to obtain an API key. Then set this variable in your shell or terminal:

```bash theme={null}
export IONQ_API_KEY="your_api_key_here"
```

Alternatively, you can specify your API key programmatically when creating a device in PennyLane.

## Writing a quantum circuit

Create a Python script or a Jupyter notebook and import PennyLane. Define a quantum device that targets IonQ's simulator (or a quantum processing unit, QPU, if you have access):

```python theme={null}
import pennylane as qml

# Setup the device
dev = qml.device(
    'ionq.simulator',
    api_key="your_api_key_here",
    wires=2
)
```

Define a simple quantum circuit as a function decorated with `@qml.qnode` that targets your device:

```python theme={null}
@qml.qnode(dev)
def bell_state():
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0, 1])
    return qml.probs(wires=[0, 1])
```

Execute the circuit and print the results:

```python theme={null}
print(bell_state())
```

This code snippet creates a Bell state and measures the probabilities of the quantum state being in each basis state.

***

## Running your circuit

Execute your script or Jupyter notebook cell. You should receive an output similar to:

```python theme={null}
[0.5 0.  0.  0.5]
```

This output represents the probabilities of measuring the quantum state in the `00`, `01`, `10`, and `11` states, showing that our Bell state preparation was successful.

***

## Viewing job results

You can view the results and status of your quantum jobs on the IonQ Quantum Cloud dashboard, under the "My Jobs" section.

***

## Expanding your knowledge

Congratulations on running your first quantum circuit with PennyLane and IonQ! To deepen your understanding:

* Explore [PennyLane's documentation](https://pennylane.readthedocs.io) for comprehensive guides and tutorials on quantum machine learning.
* Visit IonQ's [documentation](https://docs.ionq.com/) for more details on using their quantum systems.
* Check out advanced examples and use cases in the [PennyLane tutorials](https://pennylane.ai/qml/) and [IonQ's GitHub examples](https://github.com/ionq-samples).
