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_qubit(A) Spectral decomposition of a \(2\times 2\) 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
CZ CZ gate

Expectations

X Pauli-X matrix
Y Pauli-Y matrix
Z Pauli-Z matrix

Classes

DefaultQubit(wires, *[, shots]) Default qubit device for PennyLane.

Code details

spectral_decomposition_qubit(A)[source]

Spectral decomposition of a \(2\times 2\) Hermitian matrix.

Parameters:A (array) – \(2\times 2\) 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

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
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 for expectations.

Returns:2x2 identity matrix
Return type:array
class DefaultQubit(wires, *, shots=0)[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. A value of 0 yields the exact result.
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]

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(expectation, wires, par)[source]

Return the variance value of an observable.

For plugin developers: this function should return the variance value 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:

expectation value

Return type:

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]

Evaluates an expectation value of the current state.

Parameters:
  • A (array) – \(2^M\times 2^M\) Hermitian matrix corresponding to the observable
  • wires (Sequence[int]) – target subsystems
Returns:

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

Return type:

float

reset()[source]

Reset the device

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]