Base Qiskit device class

This module contains a base class for constructing Qiskit devices for PennyLane.

Classes

QiskitDevice(wires, provider, backend[, shots])

Abstract Qiskit device for PennyLane.

Code details

pennylane_qiskit.qiskit_device.pauli_eigs(n)[source]

Returns the eigenvalues for \(A^{\otimes n}\), where \(A\) is any operator that shares eigenvalues with the Pauli matrices.

Parameters

n (int) – number of wires

Returns

eigenvalues of \(Z^{\otimes n}\)

Return type

array[int]

class pennylane_qiskit.qiskit_device.QiskitDevice(wires, provider, backend, shots=1024, **kwargs)[source]

Abstract Qiskit device for PennyLane.

Parameters
  • wires (int) – The number of qubits of the device

  • provider (Provider) – The Qiskit simulation provider

  • backend (str) – the desired backend

  • shots (int) – Number of circuit evaluations/random samples used to estimate expectation values of observables.

Keyword Arguments
  • name (str) – The name of the circuit. Default 'circuit'.

  • compile_backend (BaseBackend) – The backend used for compilation. If you wish to simulate a device compliant circuit, you can specify a backend here.

  • analytic (bool) – For statevector backends, determines if the expectation values and variances are to be computed analytically. Default value is False.

_state_backends = {'statevector_simulator', 'unitary_simulator'}

Set of backend names that define the backends that support returning the underlying quantum statevector

Type

set[str]

__init__(wires, provider, backend, shots=1024, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

property backend

The Qiskit simulation backend object

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

compile()[source]

Compile the quantum circuit to target the provided compile_backend. If compile_backend is None, then the target is simply the backend.

run(qobj)[source]

Run the compiled circuit, and query the result.

_get_state(result)[source]

Returns the statevector for state simulator backends.

Parameters

result (qiskit.Result) – result object

Returns

size (2**num_wires,) statevector

Return type

array[float]

rotate_basis(obs, wires, par)[source]

Rotates the specified wires such that they are in the eigenbasis of the provided observable.

Parameters
  • observable (str) – the name of an observable

  • wires (List[int]) – wires the observable is measured on

  • par (List[Any]) – parameters of the observable

pre_measure()[source]

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

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)

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

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 (shots,)

Return type

array[float]

probabilities(wires=None)[source]

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]

eigvals(observable, wires, par)[source]

Determine the eigenvalues of observable(s).

Parameters
  • observable (str, List[str]) – the name of an observable, or a list of observables representing a tensor product

  • wires (List[int]) – wires the observable(s) is measured on

  • par (List[Any]) – parameters of the observable(s)

Returns

an array of size (len(wires),) containing the eigenvalues of the observable

Return type

array[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.