Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] The matrix representing a qfunc that contains qml.Barrier gives MatrixUndefinedError #6329

Open
1 task done
isaacdevlugt opened this issue Oct 3, 2024 · 0 comments
Labels
bug 🐛 Something isn't working

Comments

@isaacdevlugt
Copy link
Contributor

Expected behavior

Calling qml.matrix on a qfunc with qml.Barrier present should behave as if Barrier is not present.

Actual behavior

MatrixUndefinedError

Additional information

A circuit or a QNode with only qml.Barrier raises a MatrixUndefinedError when qml.matrix is called. This should return an identity matrix where the number of wires is that given in wire_order. Currently:

@qml.qnode(qml.device("default.qubit"))
def circuit():
    qml.Barrier([0, 1])
    return qml.state()

print(qml.matrix(circuit, wire_order=[0, 1])())
MatrixUndefinedError

Source code

def circuit():
    qml.X(0)
    qml.Barrier([0, 1])

print(qml.matrix(circuit, wire_order=[0, 1])())

Tracebacks

---------------------------------------------------------------------------
MatrixUndefinedError                      Traceback (most recent call last)
File ~/Documents/pennylane/pennylane/ops/functions/matrix.py:229, in matrix(op, wire_order)
    228 try:
--> 229     return op.matrix(wire_order=wire_order)
    230 except:  # pylint: disable=bare-except

File ~/Documents/pennylane/pennylane/operation.py:838, in Operator.matrix(self, wire_order)
    819 r"""Representation of the operator as a matrix in the computational basis.
    820 
    821 If ``wire_order`` is provided, the numerical representation considers the position of the
   (...)
    836     tensor_like: matrix representation
    837 """
--> 838 canonical_matrix = self.compute_matrix(*self.parameters, **self.hyperparameters)
    840 if (
    841     wire_order is None
    842     or self.wires == Wires(wire_order)
   (...)
    846     )
    847 ):

File ~/Documents/pennylane/pennylane/operation.py:807, in Operator.compute_matrix(*params, **hyperparams)
    793 r"""Representation of the operator as a canonical matrix in the computational basis (static method).
    794 
    795 The canonical matrix is the textbook matrix representation that does not consider wires.
   (...)
    805     tensor_like: matrix representation
    806 """
--> 807 raise MatrixUndefinedError

MatrixUndefinedError: 

During handling of the above exception, another exception occurred:

MatrixUndefinedError                      Traceback (most recent call last)
Cell In[16], line 4
      2     qml.X(0)
      3     qml.Barrier([0, 1])
----> 4 print(qml.matrix(circuit, wire_order=[0, 1])())

File ~/Documents/pennylane/pennylane/transforms/core/transform_dispatcher.py:253, in TransformDispatcher._qfunc_transform.<locals>.qfunc_transformed(*args, **kwargs)
    250 transformed_tape = transformed_tapes[0]
    252 if self.is_informative:
--> 253     return processing_fn(transformed_tapes)
    255 for op in transformed_tape.circuit:
    256     qml.apply(op)

File ~/Documents/pennylane/pennylane/ops/functions/matrix.py:263, in _matrix_transform.<locals>.processing_fn(res)
    260     result = matrix(res[0].operations[0], wire_order=wire_order)
    262 for op in res[0].operations[1:]:
--> 263     U = matrix(op, wire_order=wire_order)
    264     # Coerce the matrices U and result and use matrix multiplication. Broadcasted axes
    265     # are handled correctly automatically by ``matmul`` (See e.g. NumPy documentation)
    266     result = qml.math.matmul(*qml.math.coerce([U, result], like=interface), like=interface)

File ~/Documents/pennylane/pennylane/ops/functions/matrix.py:231, in matrix(op, wire_order)
    229     return op.matrix(wire_order=wire_order)
    230 except:  # pylint: disable=bare-except
--> 231     return matrix(QuantumScript(op.decomposition()), wire_order=wire_order or op.wires)

File ~/Documents/pennylane/pennylane/ops/functions/matrix.py:219, in matrix(op, wire_order)
    216     else:
    217         raise TransformError("Input is not an Operator, tape, QNode, or quantum function")
--> 219     return _matrix_transform(op, wire_order=wire_order)
    221 if isinstance(op, qml.operation.Tensor) and wire_order is not None:
    222     op = 1.0 * op  # convert to a Hamiltonian

File ~/Documents/pennylane/pennylane/transforms/core/transform_dispatcher.py:113, in TransformDispatcher.__call__(self, *targs, **tkwargs)
    110         return expand_processing(processed_results)
    112 else:
--> 113     transformed_tapes, processing_fn = self._transform(obj, *targs, **tkwargs)
    115 if self.is_informative:
    116     return processing_fn(transformed_tapes)

File ~/Documents/pennylane/pennylane/ops/functions/matrix.py:239, in _matrix_transform(tape, wire_order, **kwargs)
    234 @partial(transform, is_informative=True)
    235 def _matrix_transform(
    236     tape: QuantumScript, wire_order=None, **kwargs
    237 ) -> tuple[QuantumScriptBatch, PostprocessingFn]:
    238     if not tape.wires:
--> 239         raise qml.operation.MatrixUndefinedError
    241     if wire_order and not set(tape.wires).issubset(wire_order):
    242         raise TransformError(
    243             f"Wires in circuit {list(tape.wires)} are inconsistent with "
    244             f"those in wire_order {list(wire_order)}"
    245         )

MatrixUndefinedError:

System information

Name: PennyLane
Version: 0.38.1
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /Users/isaac/.virtualenvs/pennylane-catalyst/lib/python3.11/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane-Catalyst, PennyLane_Lightning

Platform info:           macOS-14.6.1-arm64-arm-64bit
Python version:          3.11.9
Numpy version:           1.26.4
Scipy version:           1.12.0
Installed devices:
- default.clifford (PennyLane-0.39.0.dev6)
- default.gaussian (PennyLane-0.39.0.dev6)
- default.mixed (PennyLane-0.39.0.dev6)
- default.qubit (PennyLane-0.39.0.dev6)
- default.qubit.autograd (PennyLane-0.39.0.dev6)
- default.qubit.jax (PennyLane-0.39.0.dev6)
- default.qubit.legacy (PennyLane-0.39.0.dev6)
- default.qubit.tf (PennyLane-0.39.0.dev6)
- default.qubit.torch (PennyLane-0.39.0.dev6)
- default.qutrit (PennyLane-0.39.0.dev6)
- default.qutrit.mixed (PennyLane-0.39.0.dev6)
- default.tensor (PennyLane-0.39.0.dev6)
- null.qubit (PennyLane-0.39.0.dev6)
- nvidia.custatevec (PennyLane-Catalyst-0.8.1)
- nvidia.cutensornet (PennyLane-Catalyst-0.8.1)
- oqc.cloud (PennyLane-Catalyst-0.8.1)
- softwareq.qpp (PennyLane-Catalyst-0.8.1)
- lightning.qubit (PennyLane_Lightning-0.38.0)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@isaacdevlugt isaacdevlugt added the bug 🐛 Something isn't working label Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant