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

Parameters
• op (Operator, pennylane.QNode, QuantumTape, or Callable) – An operator, quantum node, tape, or function that applies quantum operations.

• 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. If a QNode or quantum function is provided as input, a function which accepts the same arguments as the QNode or quantum function is returned. When called, this function will return the unitary matrix in the appropriate autodiff framework (Autograd, TensorFlow, PyTorch, JAX) given its parameters.

Return type

tensor_like or function

Example

Given an operation, qml.eigvals returns the eigenvalues:

>>> op = qml.PauliZ(0) @ qml.PauliX(1) - 0.5 * qml.PauliY(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)


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()
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.PauliZ(wires=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])