Quick reference

This quick reference links to the API of all quantum operations, measurements, templates and optimizers supported in PennyLane.

Quantum operations

PennyLane supports a wide variety of quantum operations - such as gates, state preparations and measurement observables. These operations can be used exclusively in quantum functions. Revisiting the first example from this section, we find the RZ, CNOT, RY gates and the PauliZ observable:

import pennylane as qml

def my_quantum_function(x, y):
    qml.RZ(x, wires=0)
    qml.CNOT(wires=[0,1])
    qml.RY(y, wires=1)
    return qml.expval(qml.PauliZ(1))

You find a list of all quantum operations here, as well as in the API.

Shared operations

The only operation shared by both qubit and continouous-variable architectures is the Identity.

Measurements

PennyLane can extract different types of measurement results: The expectation of an observable over multiple measurements, its variance, or a sample of a single measurement.

The quantum function from above, for example, used the expval measurement:

import pennylane as qml

def my_quantum_function(x, y):
    qml.RZ(x, wires=0)
    qml.CNOT(wires=[0,1])
    qml.RY(y, wires=1)
    return qml.expval(qml.PauliZ(1))

The three measurement functions can be found here:

Templates

PennyLane provides a growing library of pre-coded templates of common variational circuit architectures that can be used to easily build, evaluate, and train more complex models. In the literature, such architectures are commonly known as an ansatz.

Note

Templates are constructed out of structured combinations of the quantum operations provided by PennyLane. This means that template functions can only be used within a valid pennylane.qnode.QNode.

PennyLane conceptually distinguishes two types of templates, layer architectures and input embeddings. Most templates are complemented by functions that provide an array of random initial parameters .

An example of how to use templates is the following:

import pennylane as qml
from pennylane.templates.embeddings import AngleEmbedding
from pennylane.templates.layers import StronglyEntanglingLayers
from pennylane.init import strong_ent_layer_uniform

dev = qml.device('default.qubit', wires=2)

@qml.qnode(dev)
def circuit(weights, x=None):
    AngleEmbedding(x, [0,1])
    StronglyEntanglingLayers(weights=weights, wires=[0,1])
    return qml.expval(qml.PauliZ(0))

init_weights = strong_ent_layer_uniform(n_wires=2)
print(circuit(init_weights, x=[1., 2.]))

Here, we used the embedding template AngleEmbedding together with the layer template StronglyEntanglingLayers, and the uniform parameter initialization strategy strong_ent_layer_uniform.

Layer templates

Layer architectures, found in the templates.layers module, define sequences of gates that are repeated like the layers in a neural network. They usually contain only trainable parameters.

The following layer templates are available:

Embedding templates

Embeddings, found in the templates.embeddings module, encode input features into the quantum state of the circuit. Hence, they take a feature vector as an argument. These embeddings can also depend on trainable parameters, in which case the embedding is learnable.

The following embedding templates are available:

Parameter initializations

Each trainable template has a dedicated function in the init module, which generates a list of randomly initialized arrays for the trainable parameters.

Optimizers

When using the standard NumPy interface, PennyLane offers some custom-made optimizers. Some of these are specific to quantum optimization, such as the QNGOptimizer.

Warning

If using the PennyLane PyTorch or the PennyLane TensorFlow interfaces, PyTorch optimizers and TensorFlow optimizers (found in the module tf.train) should be used respectively.

Configuration

Some important default settings for a device, such as your user credentials for quantum hardware access, the number of shots, or the cutoff dimension for continuous-variable simulators, are defined in a configuration file called config.toml.

On first import, PennyLane attempts to load the configuration file by scanning the following three directories in order of preference:

  1. The current directory
  2. The path stored in the environment variable PENNYLANE_CONF
  3. The default user configuration directory:
    • On Linux: ~/.config/pennylane
    • On Windows: ~C:\Users\USERNAME\AppData\Local\Xanadu\pennylane
    • On MacOS: ~/Library/Preferences/pennylane

If no configuration file is found, a warning message will be displayed in the logs, and all device parameters will need to be passed as keyword arguments when loading the device.

The user can access the initialized configuration via pennylane.config, view the loaded configuration filepath, print the configurations options, access and modify them via keys (i.e., pennylane.config['main.shots']), and save/load new configuration files.

The configuration file config.toml uses the TOML standard, and has the following format:

[main]
# Global PennyLane options.
# Affects every loaded plugin if applicable.
shots = 1000
analytic = True

[strawberryfields.global]
# Options for the Strawberry Fields plugin
hbar = 1
shots = 100

  [strawberryfields.fock]
  # Options for the Strawberry Fields Fock plugin
  cutoff_dim = 10
  hbar = 0.5

  [strawberryfields.gaussian]
  # Indentation doesn't matter in TOML files,
  # but helps provide clarity.

[projectq.global]
# Options for the Project Q plugin

  [projectq.simulator]
  gate_fusion = true

  [projectq.ibm]
  user = "johnsmith"
  password = "secret123"
  use_hardware = true
  device = "ibmqx4"
  num_runs = 1024

Main PennyLane options, that are passed to all loaded devices, are provided under the [main] section. Alternatively, options can be specified on a per-plugin basis, by setting the options under [plugin.global].

For example, in the above configuration file, the Strawberry Fields devices will be loaded with a default of shots = 100, rather than shots = 1000. Finally, you can also specify settings on a device-by-device basis, by placing the options under the [plugin.device] settings.