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

ControlledQubitUnitary should raise DecompositionUndefinedError #2320

Merged
merged 4 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ The Operator class has undergone a major refactor with the following changes:
decomposition does not exist, the code now raises a custom `NoDecompositionError`
instead of `NotImplementedError`.
[(#2024)](https://github.com/PennyLaneAI/pennylane/pull/2024)
[(#2320)](https://github.com/PennyLaneAI/pennylane/pull/2320)

* The `diagonalizing_gates()` representation has been moved to the highest-level
`Operator` class and is therefore available to all subclasses. A condition
Expand Down
6 changes: 5 additions & 1 deletion pennylane/ops/qubit/matrix_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import numpy as np

import pennylane as qml
from pennylane.operation import AnyWires, Operation
from pennylane.operation import AnyWires, Operation, DecompositionUndefinedError
from pennylane.wires import Wires


Expand Down Expand Up @@ -241,6 +241,10 @@ def __init__(
total_wires = control_wires + wires
super().__init__(*params, wires=total_wires, do_queue=do_queue)

@staticmethod
def compute_decomposition(*params, wires=None, **hyperparameters):
raise DecompositionUndefinedError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add control_wires as a parameter? or save that for later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@puzzleshark hmm the signature should accept that now too, right? Or we could make it more explicit to have it

def compute_decomposition(U, control_wires, u_wires, control_values=None):

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya I was wondering if we should make it more explicit. good with either way though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Let's save it for later, perhaps we can provide some parts of the decomposition down the line and make the signature more explicit too.


@staticmethod
def compute_matrix(
U, control_wires, u_wires, control_values=None
Expand Down
7 changes: 7 additions & 0 deletions tests/ops/qubit/test_matrix_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,13 @@ def test_matrix_representation(self, tol):
assert np.allclose(res_static, expected, atol=tol)
assert np.allclose(res_dynamic, expected, atol=tol)

def test_no_decomp(self):
"""Test that ControlledQubitUnitary raises a decomposition undefined
error."""
mat = qml.PauliX(0).get_matrix()
with pytest.raises(qml.operation.DecompositionUndefinedError):
qml.ControlledQubitUnitary(mat, wires=0, control_wires=1).decomposition()


label_data = [
(X, qml.QubitUnitary(X, wires=0)),
Expand Down