# 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. supports_operation(operation) Checks if an operation is supported by this device. supports_observable(observable) Checks if an observable is supported by this device. execute(queue, observables[, parameters]) Execute a queue of quantum operations on the device and then measure the given observables. 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) Returns the expectation value of observable on specified wires. var(observable, wires, par) Returns the variance of observable on specified wires.

In addition, the following may also be optionally defined:

 probability() Return the full state probability of each computational basis state from the last run of the device. 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, parameters={})[source]

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

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 parameters (dict[int->list[(int, int)]]) – Mapping from free parameter index to the list of Operations (in the queue) that depend on it. The first element of the tuple is the index of the Operation in the program queue, the second the index of the parameter within the Operation. 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]
parameters

Mapping from free parameter index to the list of Operations in the device queue that depend on it.

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

Returns: the first element of the tuple is the index of the Operation in the program queue, the second the index of the parameter within the Operation. dict[int->list[(int, int)]]
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).

supports_operation(operation)[source]

Checks if an operation is supported by this device.

Parameters: operation (Operation,str) – operation to be checked True iff supplied operation is supported bool
supports_observable(observable)[source]

Checks if an observable is supported by this device.

Parameters: operation (Observable,str) – observable to be checked True iff supplied observable 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]

Returns the expectation value of observable on specified wires.

Parameters: observable (str) – name of the observable wires (Sequence[int]) – target subsystems par (tuple[float]) – parameter values expectation value $$\expect{A} = \bra{\psi}A\ket{\psi}$$ float
var(observable, wires, par)[source]

Returns the variance of observable on specified wires.

Parameters: observable (str) – name of the observable wires (Sequence[int]) – target subsystems par (tuple[float]) – parameter values variance $$\mathrm{var}(A) = \bra{\psi}A^2\ket{\psi} - \bra{\psi}A\ket{\psi}^2$$ float
sample(observable, wires, par, n=None)[source]

Return a sample of an observable.

For plugin developers: this function should return the result of an evaluation 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 n (int) – Number of samples that should be obtained. Defaults to the number of shots given as a parameter to the corresponding Device. samples in an array of dimension (n, num_wires) array[float]
probability()[source]

Return the full state probability of each computational basis state from the last run of the device.

Returns: Dictionary mapping a tuple representing the state to the resulting probability. The dictionary should be sorted such that the state tuples are in lexicographical order. OrderedDict[tuple, 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.