Device

class Device(wires=1, shots=1000)[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. Defaults to 1000 if not specified.
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]
observables

Get the supported set of observables.

Returns:the set of PennyLane observable names the device supports
Return type:set[str]
shots

Number of circuit evaluations/random samples used to estimate expectation values of observables

classmethod capabilities()[source]

Get the other capabilities of the plugin.

Measurements, batching etc.

Returns:results
Return type: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.
Returns:

measured 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]
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.
Return type: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
Returns:True iff supplied operation is supported
Return type:bool
supports_observable(observable)[source]

Checks if an observable is supported by this device.

Parameters:operation (Observable,str) – observable to be checked
Returns:True iff supplied observable is supported
Return type: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
  • observables (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.

Note: all arguments accept _lists_, which indicate a tensor product of observables.

Parameters:
  • observable (str or list[str]) – name of the observable(s)
  • wires (List[int] or List[List[int]]) – subsystems the observable(s) is to be measured on
  • par (tuple or list[tuple]]) – parameters for the observable(s)
Returns:

expectation value \(\expect{A} = \bra{\psi}A\ket{\psi}\)

Return type:

float

var(observable, wires, par)[source]

Returns the variance of observable on specified wires.

Note: all arguments support _lists_, which indicate a tensor product of observables.

Parameters:
  • observable (str or list[str]) – name of the observable(s)
  • wires (List[int] or List[List[int]]) – subsystems the observable(s) is to be measured on
  • par (tuple or list[tuple]]) – parameters for the observable(s)
Returns:

variance \(\mathrm{var}(A) = \bra{\psi}A^2\ket{\psi} - \bra{\psi}A\ket{\psi}^2\)

Return type:

float

sample(observable, wires, par)[source]

Return a sample of an observable.

The number of samples is determined by the value of Device.shots, which can be directly modified.

Note: all arguments support _lists_, which indicate a tensor product of observables.

Parameters:
  • observable (str or list[str]) – name of the observable(s)
  • wires (List[int] or List[List[int]]) – subsystems the observable(s) is to be measured on
  • par (tuple or list[tuple]]) – parameters for the observable(s)
Returns:

samples in an array of dimension (n, num_wires)

Return type:

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.
Return type: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.