qml.devices

Warning

Unless you are a PennyLane or plugin developer, you likely do not need to use these classes directly.

See the main plugins page for more details on available plugins.

Modules

This subpackage provides default devices for PennyLane, which do not need external plugins to be installed. The default devices provide basic built-in qubit and CV circuit simulators that can be used with PennyLane without the need for additional dependencies. They may also be used in the PennyLane test suite in order to verify and test quantum gradient computations.

default_qubit

The default.qubit device is PennyLane’s standard qubit-based device.

default_qubit_legacy

This module contains the legacy implementation of default.qubit.

default_qubit_jax

This module contains a jax implementation of the DefaultQubitLegacy reference plugin.

default_qubit_torch

This module contains a PyTorch implementation of the DefaultQubitLegacy reference plugin.

default_qubit_tf

This module contains a TensorFlow implementation of the DefaultQubitLegacy reference plugin.

default_qubit_autograd

This module contains an autograd implementation of the DefaultQubitLegacy reference plugin.

default_gaussian

The default.gaussian device is a simulator for Gaussian continuous-variable quantum computations, and can be used as a template for writing PennyLane devices for new CV backends.

default_mixed

The default.mixed device is PennyLane’s standard qubit simulator for mixed-state computations.

default_qutrit

The default.qutrit device is PennyLane’s standard qutrit-based device.

default_qutrit_mixed

The default.qutrit.mixed device is PennyLane’s standard qutrit simulator for mixed-state computations.

default_clifford

This module contains the Clifford simulator using stim.

default_tensor

This module contains the default.tensor device to perform tensor network simulations of quantum circuits using quimb.

_legacy_device

This module contains the Device abstract base class.

_qubit_device

This module contains the QubitDevice abstract base class.

_qutrit_device

This module contains the QutritDevice abstract base class.

null_qubit

The null.qubit device is a no-op device, useful for resource estimation, and for benchmarking PennyLane’s auxiliary functionality outside direct circuit evaluations.

tests

This subpackage provides integration tests for the devices with PennyLane’s core functionalities.

Next generation devices

pennylane.devices.Device is the latest interface for the next generation of devices that replaces pennylane.Device and pennylane.QubitDevice.

While the previous interface pennylane.Device is imported top level, the new pennylane.devices.Device is accessible from the pennylane.devices submodule.

ExecutionConfig([grad_on_execution, …])

A class to configure the execution of a quantum circuit on a device.

MCMConfig([mcm_method, postselect_mode])

A class to store mid-circuit measurement configurations.

Device([wires, shots])

A device driver that can control one or more backends.

DefaultQubit([wires, shots, seed, max_workers])

A PennyLane device written in Python and capable of backpropagation derivatives.

DefaultTensor([wires, method, c_dtype])

A PennyLane device to perform tensor network simulations of quantum circuits using quimb.

NullQubit([wires, shots])

Null qubit device for PennyLane.

DefaultQutritMixed([wires, shots, seed, …])

A PennyLane Python-based device for mixed-state qutrit simulation.

LegacyDeviceFacade(device)

A Facade that converts a device from the old qml.Device interface into the new interface.

Preprocessing Transforms

The preprocess module offers several transforms that can be used in constructing the preprocess() method for devices.

decompose(tape, stopping_condition[, …])

Decompose operations until the stopping condition is met.

validate_observables(tape, stopping_condition)

Validates the observables and measurements for a circuit.

validate_measurements(tape[, …])

Validates the supported state and sample based measurement processes.

validate_device_wires(tape[, wires, name])

Validates that all wires present in the tape are in the set of provided wires.

validate_multiprocessing_workers(tape, …)

Validates the number of workers for multiprocessing.

validate_adjoint_trainable_params(tape)

Raises a warning if any of the observables is trainable, and raises an error if any trainable parameters belong to state-prep operations.

no_sampling(tape[, name])

Raises an error if the tape has finite shots.

Other transforms that may be relevant to device preprocessing include:

defer_measurements(tape[, reduce_postselected])

Quantum function transform that substitutes operations conditioned on measurement outcomes to controlled operations.

transforms.broadcast_expand(tape)

Expand a broadcasted tape into multiple tapes and a function that stacks and squeezes the results.

transforms.sum_expand(tape[, group])

Splits a quantum tape measuring a Sum expectation into multiple tapes of summand expectations, and provides a function to recombine the results.

transforms.split_non_commuting(tape[, …])

Splits a circuit into tapes measuring groups of commuting observables.

transforms.hamiltonian_expand(tape[, group])

Splits a tape measuring a Hamiltonian expectation into mutliple tapes of Pauli expectations, and provides a function to recombine the results.

Modifiers

The modifiers allow for the easy addition of default behaviour to a device.

single_tape_support(cls)

Modifies all functions to accept single tapes in addition to batches.

simulator_tracking(cls)

Modifies all methods to add default simulator style tracking.

For example with a custom device we can add simulator-style tracking and the ability to handle a single circuit. See the documentation for each modifier for more details.

@simulator_tracking
@single_tape_support
class MyDevice(qml.devices.Device):

    def execute(self, circuits, execution_config = qml.devices.DefaultExecutionConfig):
        return tuple(0.0 for _ in circuits)
>>> dev = MyDevice()
>>> tape = qml.tape.QuantumTape([qml.S(0)], [qml.expval(qml.X(0))])
>>> with dev.tracker:
...     out = dev.execute(tape)
>>> out
0.0
>>> dev.tracker.history
{'batches': [1],
 'simulations': [1],
 'executions': [1],
 'results': [0.0],
 'resources': [Resources(num_wires=1, num_gates=1,
 gate_types=defaultdict(<class 'int'>, {'S': 1}),
 gate_sizes=defaultdict(<class 'int'>, {1: 1}), depth=1,
 shots=Shots(total_shots=None, shot_vector=()))]}

Qubit Simulation Tools

Submodule for performing qubit-based simulations of quantum circuits.

This submodule is internal and subject to change without a deprecation cycle. Use at your own discretion.

create_initial_state(wires[, …])

Returns an initial state, defaulting to \(\ket{0}\) if no state-prep operator is provided.

apply_operation()

Apply and operator to a given state.

measure(measurementprocess, state[, …])

Apply a measurement process to a state.

measure_with_samples(measurements, state, shots)

Returns the samples of the measurement process performed on the given state.

sample_state(state, shots[, …])

Returns a series of samples of a state.

simulate(circuit[, debugger, state_cache])

Simulate a single quantum script.

adjoint_jacobian(tape[, state])

Implements the adjoint method outlined in Jones and Gacon to differentiate an input tape.

adjoint_jvp(tape, tangents[, state])

The jacobian vector product used in forward mode calculation of derivatives.

adjoint_vjp(tape, cotangents[, state])

The vector jacobian product used in reverse-mode differentiation.

Qutrit Mixed-State Simulation Tools

Submodule for performing mixed state simulations of qutrit-based quantum circuits.

This submodule is internal and subject to change without a deprecation cycle. Use at your own discretion.

create_initial_state(wires[, …])

Returns an initial state, defaulting to \(\ket{0}\bra{0}\) if no state-prep operator is provided.

apply_operation()

Apply an operation to a given state.

measure(measurementprocess, state[, …])

Apply a measurement process to a state.

measure_with_samples(mp, state, shots[, …])

Returns the samples of the measurement process performed on the given state.

sample_state(state, shots[, …])

Returns a series of computational basis samples of a state.

simulate(circuit[, rng, prng_key, debugger, …])

Simulate a single quantum script.