Device base class

Module name: pennylane._device

This module contains the Device abstract base class. To write a plugin containing a PennyLane-compatible device, Device must be subclassed, and the appropriate class attributes and methods implemented. For examples of subclasses of Device, see DefaultQubit, DefaultGaussian, or the StrawberryFields and ProjectQ plugins.

Device([wires, shots]) Abstract base class for PennyLane devices.

Device attributes and methods

The following methods and attributes are accessible from the PennyLane user interface:

short_name Returns the string used to load the device.
capabilities() Get the other capabilities of the plugin.
supported(name) Checks if an operation or expectation is supported by this device.
execute(queue, expectation) Execute a queue of quantum operations on the device and then measure the given expectation values.
reset() Reset the backend state.

Abstract methods and attributes

The following methods and attributes must be defined for all devices:

name The full name of the device.
short_name Returns the string used to load the device.
pennylane_requires The current API version that the device plugin was made for.
version The current version of the plugin.
author The author(s) of the plugin.
operations Get the supported set of operations.
expectations Get the supported set of expectations.
apply(operation, wires, par) Apply a quantum operation.
expval(expectation, wires, par) Return the expectation value of an expectation.

In addition, the following may also be optionally defined:

pre_apply() Called during execute() before the individual operations are executed.
post_apply() Called during execute() after the individual operations have been executed.
pre_expval() Called during execute() before the individual expectations are executed.
post_expval() Called during execute() after the individual expectations have been executed.
execution_context() The device execution context used during calls to execute().

Internal attributes and methods

The following methods and attributes are used internally by the Device class, to ensure correct operation and internal consistency.

check_validity(queue, expectations) Checks whether the operations and expectations in queue are all supported by the device.

Code details

exception DeviceError[source]

Exception raised by a Device when it encounters an illegal operation in the quantum circuit.

class Device(wires=1, shots=0)[source]

Abstract base class for PennyLane devices.

Parameters:
  • wires (int) – number of subsystems in the quantum state represented by the device. Default 1 if not specified.
  • shots (int) – number of circuit evaluations/random samples used to estimate expectation values of observables. For simulator devices, a value of 0 results in the exact expectation value being returned. Defaults to 0 if not specified.
_capabilities = {}

dict[str->]* – plugin capabilities

_circuits = {}

dict[str->Circuit] – circuit templates associated with this API class

name

The full name of the device.

short_name

Returns the string used to load the device.

pennylane_requires

The current API version that the device plugin was made for.

version

The current version of the plugin.

author

The author(s) of the plugin.

operations

Get the supported set of operations.

Returns:the set of PennyLane operation names the device supports
Return type:set[str]
expectations

Get the supported set of expectations.

Returns:the set of PennyLane expectation names the device supports
Return type:set[str]
classmethod capabilities()[source]

Get the other capabilities of the plugin.

Measurements, batching etc.

Returns:results
Return type:dict[str->*]
execute(queue, expectation)[source]

Execute a queue of quantum operations on the device and then measure the given expectation values.

For plugin developers: Instead of overwriting this, consider implementing a suitable subset of pre_apply(), apply(), post_apply(), pre_expval(), expval(), post_expval(), and execution_context().

Parameters:
  • queue (Iterable[Operation]) – operations to execute on the device
  • expectation (Iterable[Expectation]) – expectations to evaluate and return
Returns:

expectation value(s)

Return type:

array[float]

op_queue

The operation queue to be applied.

Note that this property can only be accessed within the execution context of execute().

Returns:list[~.operation.Operation]
expval_queue

The expectation values to be measured and returned.

Note that this property can only be accessed within the execution context of execute().

Returns:list[~.operation.Expectation]
pre_apply()[source]

Called during execute() before the individual operations are executed.

post_apply()[source]

Called during execute() after the individual operations have been executed.

pre_expval()[source]

Called during execute() before the individual expectations are executed.

post_expval()[source]

Called during execute() after the individual expectations have been executed.

execution_context()[source]

The device execution context used during calls to execute().

You can overwrite this function to return a context manager in case your quantum library requires that; all operations and method calls (including apply() and expval()) are then evaluated within the context of this context manager (see the source of Device.execute() for more details).

supported(name)[source]

Checks if an operation or expectation is supported by this device.

Parameters:name (str) – name of the operation or expectation
Returns:True iff it is supported
Return type:bool
check_validity(queue, expectations)[source]

Checks whether the operations and expectations in queue are all supported by the device.

Parameters:
  • queue (Iterable[Operation]) – quantum operation objects which are intended to be applied in the device
  • expectations (Iterable[Expectation]) – expectations which are intended to be evaluated in the device
apply(operation, wires, par)[source]

Apply a quantum operation.

For plugin developers: this function should apply the operation on the device.

Parameters:
  • operation (str) – name of the operation
  • wires (Sequence[int]) – subsystems the operation is applied on
  • par (tuple) – parameters for the operation
expval(expectation, wires, par)[source]

Return the expectation value of an expectation.

For plugin developers: this function should return the expectation value of the given expectation on the device.

Parameters:
  • expectation (str) – name of the expectation
  • wires (Sequence[int]) – subsystems the expectation value is to be measured on
  • par (tuple) – parameters for the observable
Returns:

expectation value

Return type:

float

reset()[source]

Reset the backend state.

After the reset the backend should be as if it was just constructed. Most importantly the quantum state is reset to its initial value.