qml.beta.plugins.DefaultTensor

class DefaultTensor(wires, shots=1000, representation='exact', contraction_method='auto')[source]

Bases: pennylane._device.Device

Experimental Tensor Network simulator device for PennyLane.

Short name: default.tensor

This experimental device uses the TensorNetwork library to provide a basic tensor-network-based simulator backend for PennyLane. Tensor network simulators can faster or more efficient for certain types of circuit structures.

To use this device, you will need to install TensorNetwork version 0.3:

pip install tensornetwork==0.3

The default.tensor device supports two types of tensor networks: "exact" and "mps".

The (default) "exact" representation does not make any approximations, using exact dense tensors for the simulator’s quantum states and for the matrices of quantum gates and observables.

The "mps" representation (standing for “matrix product state”) approximates the quantum state using a one-dimensional grid of qubits with nearest-neighbour connectivity. As such, it does not support multi-qubit gates/observables that do not act on nearest-neighbour qubits.

The preferred contraction method can also be specified when using the "exact" representation. Available options are “auto”, “greedy”, “branch”, or “optimal”. See the TensorNetwork documentation for more details.

Example

>>> exact_tensornet = qml.device("default.tensor", wires=2, contraction_method="greedy")
>>> mps_tensornet = qml.device("default.tensor", wires=2, representation="mps")
Parameters
  • wires (int) – number of subsystems in the quantum state represented by the device

  • shots (int) – Number of circuit evaluations/random samples to return when sampling from the device. Defaults to 1000 if not specified.

  • representation (str) – Underlying representation used for the tensor network simulation. Valid options are “exact” (no approximations made) or “mps” (simulated quantum state is approximated as a Matrix Product State).

  • contraction_method (str) – Method used to perform tensor network contractions. Only applicable for the “exact” representation. Valid options are “auto”, “greedy”, “branch”, or “optimal”. See documentation of the TensorNetwork library for more information about contraction methods.

author

backend

contraction_method

The contraction method used by the tensor network.

name

obs_queue

The observables to be measured and returned.

observables

Get the supported set of observables.

op_queue

The operation queue to be applied.

operations

Get the supported set of operations.

parameters

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

pennylane_requires

short_name

shots

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

version

author = 'Xanadu Inc.'
backend = 'numpy'
contraction_method

The contraction method used by the tensor network. Available options are “auto”, “greedy”, “branch”, or “optimal”. See TensorNetwork library documentation for more details.

name = 'PennyLane TensorNetwork simulator plugin'
obs_queue

The observables to be measured and returned.

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

Raises

ValueError – if outside of the execution context

Returns

list[~.operation.Observable]

observables

Get the supported set of observables.

Returns

the set of PennyLane observable names the device supports

Return type

set[str]

op_queue

The operation queue to be applied.

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

Raises

ValueError – if outside of the execution context

Returns

list[~.operation.Operation]

operations

Get the supported set of operations.

Returns

the set of PennyLane operation names the device supports

Return type

set[str]

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().

Raises

ValueError – if outside of the execution context

Returns

the mapping

Return type

dict[int->list[ParameterDependency]]

pennylane_requires = '0.10'
short_name = 'default.tensor'
shots

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

version = '0.10.0'

apply(operation, wires, par)

Apply a quantum operation.

capabilities()

Get the other capabilities of the plugin.

check_validity(queue, observables)

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

ev(obs_nodes, wires)

Expectation value of observables on specified wires.

execute(queue, observables[, parameters])

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

execution_context()

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

expval(observable, wires, par)

Returns the expectation value of observable on specified wires.

post_apply()

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

post_measure()

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

pre_apply()

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

pre_measure()

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

probability([wires])

Return the (marginal) probability of each computational basis state from the last run of the device.

reset()

Reset the device.

sample(observable, wires, par)

Return a sample of an observable.

supports_observable(observable)

Checks if an observable is supported by this device. Raises a ValueError,

supports_operation(operation)

Checks if an operation is supported by this device.

var(observable, wires, par)

Returns the variance of observable on specified wires.

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

classmethod capabilities()

Get the other capabilities of the plugin.

Measurements, batching etc.

Returns

results

Return type

dict[str->*]

check_validity(queue, observables)

Checks whether the operations and observables in queue are all supported by the device. Includes checks for inverse operations.

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

Raises

DeviceError – if there are operations in the queue or observables that the device does not support

ev(obs_nodes, wires)[source]

Expectation value of observables on specified wires.

Parameters
  • obs_nodes (Sequence[tn.Node]) – the observables as TensorNetwork Nodes

  • wires (Sequence[Sequence[int]]) – measured subsystems for each observable

Returns

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

Return type

float

execute(queue, observables, parameters={}, **kwargs)

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[ParameterDependency]]) – Mapping from free parameter index to the list of Operations (in the queue) that depend on it.

Keyword Arguments

return_native_type (bool) – If True, return the result in whatever type the device uses internally, otherwise convert it into array[float]. Default: False.

Raises

QuantumFunctionError – if the value of return_type is not supported

Returns

measured value(s)

Return type

array[float]

execution_context()

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).

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

post_apply()

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

post_measure()

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

pre_apply()

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

pre_measure()

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

probability(wires=None)

Return the (marginal) probability of each computational basis state from the last run of the device.

Parameters

wires (Sequence[int]) – Sequence of wires to return marginal probabilities for. Wires not provided are traced out of the system.

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 device.

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)

Raises

NotImplementedError – if the device does not support sampling

Returns

samples in an array of dimension (n, num_wires)

Return type

array[float]

supports_observable(observable)
Checks if an observable is supported by this device. Raises a ValueError,

if not a subclass or string of an Observable was passed.

Parameters

observable (type or str) – observable to be checked

Raises

ValueError – if observable is not a Observable class or string

Returns

True iff supplied observable is supported

Return type

bool

supports_operation(operation)

Checks if an operation is supported by this device.

Parameters

operation (type or str) – operation to be checked

Raises

ValueError – if operation is not a Operation class or string

Returns

True iff supplied operation is supported

Return type

bool

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)

Raises

NotImplementedError – if the device does not support variance computation

Returns

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

Return type

float