Default qubit plugin

Module name: pennylane.plugins.default_qubit

Short name: "default.qubit"

The default plugin is meant to be used as a template for writing PennyLane device plugins for new qubit-based backends.

It implements the necessary Device methods as well as some built-in qubit operations, and provides a very simple pure state simulation of a qubit-based quantum circuit architecture.

The following is the technical documentation of the implementation of the plugin. You will not need to read and understand this to use this plugin.

Auxiliary functions

spectral_decomposition(A)

Spectral decomposition of a Hermitian matrix.

unitary(*args)

Input validation for an arbitary unitary operation.

hermitian(*args)

Input validation for an arbitary Hermitian expectation.

Gates and operations

Rphi(phi)

One-qubit phase shift.

Rotx(theta)

One-qubit rotation about the x axis.

Roty(theta)

One-qubit rotation about the y axis.

Rotz(theta)

One-qubit rotation about the z axis.

Rot3(a, b, c)

Arbitrary one-qubit rotation using three Euler angles.

X

Pauli-X matrix

Y

Pauli-Y matrix

Z

Pauli-Z matrix

H

Hadamard gate

CNOT

CNOT gate

SWAP

SWAP gate

CSWAP

CSWAP gate

CZ

CZ gate

CRotx(theta)

Two-qubit controlled rotation about the x axis.

CRoty(theta)

Two-qubit controlled rotation about the y axis.

CRotz(theta)

Two-qubit controlled rotation about the z axis.

CRot3(a, b, c)

Arbitrary two-qubit controlled rotation using three Euler angles.

Observables

X

Pauli-X matrix

Y

Pauli-Y matrix

Z

Pauli-Z matrix

Classes

DefaultQubit(wires, *[, shots, analytic])

Default qubit device for PennyLane.

Code details

spectral_decomposition(A)[source]

Spectral decomposition of a Hermitian matrix.

Parameters

A (array) – Hermitian matrix

Returns

(a, P): eigenvalues and hermitian projectors

such that \(A = \sum_k a_k P_k\).

Return type

(vector[float], list[array[complex]])

X = array([[0, 1], [1, 0]])

Pauli-X matrix

Y = array([[ 0.+0.j, -0.-1.j], [ 0.+1.j, 0.+0.j]])

Pauli-Y matrix

Z = array([[ 1, 0], [ 0, -1]])

Pauli-Z matrix

H = array([[ 0.70710678, 0.70710678], [ 0.70710678, -0.70710678]])

Hadamard gate

CNOT = array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])

CNOT gate

SWAP = array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])

SWAP gate

CZ = array([[ 1, 0, 0, 0], [ 0, 1, 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, -1]])

CZ gate

CSWAP = array([[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1]])

CSWAP gate

Rphi(phi)[source]

One-qubit phase shift.

Parameters

phi (float) – phase shift angle

Returns

unitary 2x2 phase shift matrix

Return type

array

Rotx(theta)[source]

One-qubit rotation about the x axis.

Parameters

theta (float) – rotation angle

Returns

unitary 2x2 rotation matrix \(e^{-i \sigma_x \theta/2}\)

Return type

array

Roty(theta)[source]

One-qubit rotation about the y axis.

Parameters

theta (float) – rotation angle

Returns

unitary 2x2 rotation matrix \(e^{-i \sigma_y \theta/2}\)

Return type

array

Rotz(theta)[source]

One-qubit rotation about the z axis.

Parameters

theta (float) – rotation angle

Returns

unitary 2x2 rotation matrix \(e^{-i \sigma_z \theta/2}\)

Return type

array

Rot3(a, b, c)[source]

Arbitrary one-qubit rotation using three Euler angles.

Parameters

a,b,c (float) – rotation angles

Returns

unitary 2x2 rotation matrix rz(c) @ ry(b) @ rz(a)

Return type

array

CRotx(theta)[source]

Two-qubit controlled rotation about the x axis.

Parameters

theta (float) – rotation angle

Returns

unitary 4x4 rotation matrix \(|0\rangle\langle 0|\otimes \mathbb{I}+|1\rangle\langle 1|\otimes R_x(\theta)\)

Return type

array

CRoty(theta)[source]

Two-qubit controlled rotation about the y axis.

Parameters

theta (float) – rotation angle

Returns

unitary 4x4 rotation matrix \(|0\rangle\langle 0|\otimes \mathbb{I}+|1\rangle\langle 1|\otimes R_y(\theta)\)

Return type

array

CRotz(theta)[source]

Two-qubit controlled rotation about the z axis.

Parameters

theta (float) – rotation angle

Returns

unitary 4x4 rotation matrix \(|0\rangle\langle 0|\otimes \mathbb{I}+|1\rangle\langle 1|\otimes R_z(\theta)\)

Return type

array

CRot3(a, b, c)[source]

Arbitrary two-qubit controlled rotation using three Euler angles.

Parameters

a,b,c (float) – rotation angles

Returns

unitary 4x4 rotation matrix \(|0\rangle\langle 0|\otimes \mathbb{I}+|1\rangle\langle 1|\otimes R(a,b,c)\)

Return type

array

unitary(*args)[source]

Input validation for an arbitary unitary operation.

Parameters

args (array) – square unitary matrix

Returns

square unitary matrix

Return type

array

hermitian(*args)[source]

Input validation for an arbitary Hermitian expectation.

Parameters

args (array) – square hermitian matrix

Returns

square hermitian matrix

Return type

array

identity(*_)[source]

Identity matrix observable.

Returns

2x2 identity matrix

Return type

array

class DefaultQubit(wires, *, shots=1000, analytic=True)[source]

Default qubit device for PennyLane.

Parameters
  • wires (int) – the number of modes to initialize the device in

  • shots (int) – How many times the circuit should be evaluated (or sampled) to estimate the expectation values. Defaults to 1000 if not specified. If analytic == True, then the number of shots is ignored in the calculation of expectation values and variances, and only controls the number of samples returned by sample.

  • analytic (bool) – indicates if the device should calculate expectations and variances analytically

pre_apply()[source]

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

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

mat_vec_product(mat, vec, wires)[source]

Apply multiplication of a matrix to subsystems of the quantum state.

Parameters
  • mat (array) – matrix to multiply

  • vec (array) – state vector to multiply

  • wires (Sequence[int]) – target subsystems

Returns

output vector after applying mat to input vec on specified subsystems

Return type

array

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

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.

Parameters
  • observable (str) – name of the observable

  • wires (Sequence[int]) – target subsystems

  • par (tuple[float]) – parameter values

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.

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

Returns

samples in an array of dimension (n, num_wires)

Return type

array[float]

_get_operator_matrix(operation, par)[source]

Get the operator matrix for a given operation or observable.

Parameters
  • operation (str) – name of the operation/observable

  • par (tuple[float]) – parameter values

Returns

matrix representation.

Return type

array

ev(A, wires)[source]

Expectation value of observable on specified wires.

Parameters
  • A (array[float]) – the observable matrix as array

  • wires (Sequence[int]) – target subsystems

Returns

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

Return type

float

reset()[source]

Reset the device

property operations

Get the supported set of operations.

Returns

the set of PennyLane operation names the device supports

Return type

set[str]

property observables

Get the supported set of observables.

Returns

the set of PennyLane observable names the device supports

Return type

set[str]

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]