qml.eigvals

eigvals(op, k=1, which='SA')[source]

The eigenvalues of one or more operations.

Note

  • For a SparseHamiltonian object, the eigenvalues are computed with the efficient scipy.sparse.linalg.eigsh method which returns \(k\) eigenvalues. The default value of \(k\) is \(1\). For an \(N \times N\) sparse matrix, \(k\) must be smaller than \(N - 1\), otherwise scipy.sparse.linalg.eigsh fails. If the requested \(k\) is equal or larger than \(N - 1\), the regular qml.math.linalg.eigvalsh is applied on the dense matrix. For more details see the scipy.sparse.linalg.eigsh documentation.

  • A second-quantized molecular Hamiltonian is independent of the number of electrons and its eigenspectrum contains the energies of the neutral and charged molecules. Therefore, the smallest eigenvalue returned by qml.eigvals for a molecular Hamiltonian might not always correspond to the neutral molecule.

Parameters
  • op (Operator or QNode or QuantumTape or Callable) – A quantum operator or quantum circuit.

  • k (int) – The number of eigenvalues to be returned for a SparseHamiltonian.

  • which (str) – Method for computing the eigenvalues of a SparseHamiltonian. The possible methods are 'LM' (largest in magnitude), 'SM' (smallest in magnitude), 'LA' (largest algebraic), 'SA' (smallest algebraic) and 'BE' (\(k/2\) from each end of the spectrum).

Returns

If an operator is provided as input, the eigenvalues are returned directly in the form of a tensor. Otherwise, the transformed circuit is returned as described in qml.transform. Executing this circuit will provide the eigenvalues as a tensor.

Return type

TensorLike or qnode (QNode) or quantum function (Callable) or tuple[List[QuantumTape], function]

Example

Given an operation, qml.eigvals returns the eigenvalues:

>>> op = qml.Z(0) @ qml.X(1) - 0.5 * qml.Y(1)
>>> qml.eigvals(op)
array([-1.11803399, -1.11803399,  1.11803399,  1.11803399])

It can also be used in a functional form:

>>> x = torch.tensor(0.6, requires_grad=True)
>>> eigval_fn = qml.eigvals(qml.RX)
>>> eigval_fn(x, wires=0)
tensor([0.9553+0.2955j, 0.9553-0.2955j], grad_fn=<LinalgEigBackward>)

In its functional form, it is fully differentiable with respect to gate arguments:

>>> loss = torch.real(torch.sum(eigval_fn(x, wires=0)))
>>> loss.backward()
>>> x.grad
tensor(-0.2955)

This operator transform can also be applied to QNodes, tapes, and quantum functions that contain multiple operations; see Usage Details below for more details.

qml.eigvals can also be used with QNodes, tapes, or quantum functions that contain multiple operations. However, in this situation, eigenvalues may be computed numerically. This can lead to a large computational overhead for a large number of wires.

Consider the following quantum function:

def circuit(theta):
    qml.RX(theta, wires=1)
    qml.Z(0)

We can use qml.eigvals to generate a new function that returns the eigenvalues corresponding to the function circuit:

>>> eigvals_fn = qml.eigvals(circuit)
>>> theta = np.pi / 4
>>> eigvals_fn(theta)
array([ 0.92387953+0.38268343j,  0.92387953-0.38268343j,
       -0.92387953+0.38268343j, -0.92387953-0.38268343j])

Contents

Using PennyLane

Release news

Development

API

Internals