Source code for pennylane.exceptions

# Copyright 2018-2025 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r"""
This module contains all the custom exceptions and warnings used in PennyLane.

.. warning::

    Unless you are a PennyLane or plugin developer, you will likely not need
    to use these classes directly. They are raised by PennyLane functions
    when errors are encountered.

Contents
--------

The exceptions and warnings are organized by their category of use.

.. currentmodule:: pennylane.exceptions

General Execution Errors
~~~~~~~~~~~~~~~~~~~~~~~~

.. autosummary::
    :toctree: api

    ~CaptureError
    ~DeviceError
    ~QuantumFunctionError
    ~TransformError
    ~ConditionalTransformError
    ~QueuingError
    ~WireError
    ~MeasurementShapeError
    ~AutoGraphError
    ~CompileError
    ~DecompositionError
    ~InvalidCapabilitiesError
    ~NonDifferentiableError

Operator Property Errors
~~~~~~~~~~~~~~~~~~~~~~~~

.. autosummary::
    :toctree: api

    ~OperatorPropertyUndefined
    ~DecompositionUndefinedError
    ~TermsUndefinedError
    ~MatrixUndefinedError
    ~SparseMatrixUndefinedError
    ~EigvalsUndefinedError
    ~DiagGatesUndefinedError
    ~AdjointUndefinedError
    ~PowUndefinedError
    ~GeneratorUndefinedError
    ~ParameterFrequenciesUndefinedError

User Warnings
~~~~~~~~~~~~~

.. autosummary::
    :toctree: api

    ~PennyLaneDeprecationWarning
    ~ExperimentalWarning
    ~AutoGraphWarning

"""  # pragma: no cover

# =============================================================================
# General Execution and Quantum Function Errors
# =============================================================================


[docs] class CaptureError(Exception): """Errors related to PennyLane's Program Capture execution pipeline."""
[docs] class DeviceError(Exception): """Exception raised when it encounters an illegal operation in the quantum circuit."""
[docs] class QuantumFunctionError(Exception): """Exception raised when an illegal operation is defined in a quantum function."""
[docs] class TransformError(Exception): """Raised when there is an error with the transform logic."""
[docs] class ConditionalTransformError(ValueError): """Error for using qml.cond incorrectly"""
[docs] class QueuingError(Exception): """Exception that is raised when there is a queuing error"""
[docs] class WireError(Exception): """Exception raised by a :class:`~.pennylane.wires.Wire` object when it is unable to process wires."""
[docs] class MeasurementShapeError(ValueError): """An error raised when an unsupported operation is attempted with a quantum tape."""
# ============================================================================= # Operator Property Errors # =============================================================================
[docs] class OperatorPropertyUndefined(Exception): """Generic exception to be used for undefined Operator properties or methods."""
[docs] class DecompositionUndefinedError(OperatorPropertyUndefined): """Raised when an Operator's representation as a decomposition is undefined."""
[docs] class TermsUndefinedError(OperatorPropertyUndefined): """Raised when an Operator's representation as a linear combination is undefined."""
[docs] class MatrixUndefinedError(OperatorPropertyUndefined): """Raised when an Operator's matrix representation is undefined."""
[docs] class SparseMatrixUndefinedError(OperatorPropertyUndefined): """Raised when an Operator's sparse matrix representation is undefined."""
[docs] class EigvalsUndefinedError(OperatorPropertyUndefined): """Raised when an Operator's eigenvalues are undefined."""
[docs] class DiagGatesUndefinedError(OperatorPropertyUndefined): """Raised when an Operator's diagonalizing gates are undefined."""
[docs] class AdjointUndefinedError(OperatorPropertyUndefined): """Raised when an Operator's adjoint version is undefined."""
[docs] class PowUndefinedError(OperatorPropertyUndefined): """Raised when an Operator's power is undefined."""
[docs] class GeneratorUndefinedError(OperatorPropertyUndefined): """Exception used to indicate that an operator does not have a generator"""
[docs] class ParameterFrequenciesUndefinedError(OperatorPropertyUndefined): """Exception used to indicate that an operator does not have parameter_frequencies"""
# ============================================================================= # Warnings # =============================================================================
[docs] class PennyLaneDeprecationWarning(UserWarning): """Warning raised when a PennyLane feature is being deprecated."""
[docs] class ExperimentalWarning(UserWarning): """Warning raised to indicate experimental/non-stable feature or support."""
[docs] class AutoGraphWarning(Warning): """Warnings related to PennyLane's AutoGraph submodule."""
# ============================================================================= # Autograph and Compilation Errors # =============================================================================
[docs] class AutoGraphError(Exception): """Errors related to PennyLane's AutoGraph submodule."""
[docs] class CompileError(Exception): """Error encountered in the compilation phase."""
[docs] class DecompositionError(Exception): """Base class for decomposition errors."""
[docs] class InvalidCapabilitiesError(Exception): """Exception raised from invalid TOML files."""
[docs] class NonDifferentiableError(Exception): """Exception raised if attempting to differentiate non-trainable :class:`~.tensor` using Autograd."""