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 observable is supported by this device. execute(queue, observables) 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. observables Get the supported set of observables. apply(operation, wires, par) Apply a quantum operation. expval(observable, wires, par) Return the expectation value of an observable. var(observable, wires, par) Return the variance value of an observable.

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_measure() Called during execute() before the individual observables are measured. post_measure() Called during execute() after the individual observables have been measured. 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, observables) Checks whether the operations and observables 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 = {}

plugin capabilities

Type: dict[str->*]
_circuits = {}

circuit templates associated with this API class

Type: dict[str->Circuit]
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 set[str]
observables

Get the supported set of observables.

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

Get the other capabilities of the plugin.

Measurements, batching etc.

Returns: results dict[str->*]
execute(queue, observables)[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_measure(), expval(), var(), sample(), post_measure(), and execution_context().

Parameters: queue (Iterable[Operation]) – operations to execute on the device observables (Iterable[Observable]) – observables to measure and return measured value(s) 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]
obs_queue

The observables to be measured and returned.

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

Returns: list[~.operation.Observable]
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_measure()[source]

Called during execute() before the individual observables are measured.

post_measure()[source]

Called during execute() after the individual observables have been measured.

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 observable is supported by this device.

Parameters: name (str) – name of the operation or observable True iff it is supported bool
check_validity(queue, observables)[source]

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

Parameters: queue (Iterable[Operation]) – quantum operation objects which are intended to be applied on the device expectations (Iterable[Observable]) – observables which are intended to be evaluated on 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(observable, wires, par)[source]

Return the expectation value of an observable.

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

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

Return the variance value of an observable.

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

Parameters: observable (str) – name of the observable wires (Sequence[int]) – subsystems the observable is to be measured on par (tuple) – parameters for the observable expectation value 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.