Skip to content

Commit 8920c55

Browse files
authored
Gates in the gate set are not considered solved in the graph (#8526)
**Context:** A `KeyError` is raised when trying to access the decomposition rule of an operator that is in the gate set. **Description of the Change:** Now the graph correctly returns `False` when `is_solved_for` is called on an op in the gate set, and the correct error is raised when trying to access the decomposition rule of such an operator. **Benefits:** **Possible Drawbacks:** **Related GitHub Issues:** [sc-102050]
1 parent a047c55 commit 8920c55

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/releases/changelog-dev.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@
196196
when a device that extends the ``LegacyDevice`` does not declare support for mid-circuit measurements.
197197
[(#8486)](https://github.com/PennyLaneAI/pennylane/pull/8486)
198198

199+
* Fixes a bug where a `KeyError` is raised when querying the decomposition rule for an operator in the gate set from a :class:`~pennylane.decomposition.DecompGraphSolution`.
200+
[(#8526)](https://github.com/PennyLaneAI/pennylane/pull/8526)
201+
199202
<h3>Contributors ✍️</h3>
200203

201204
This release contains contributions from (in alphabetical order):

pennylane/decomposition/decomposition_graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def _all_solutions(
552552
def _is_solved(op_node: _OperatorNode):
553553
return (
554554
op_node in self._all_op_indices
555-
and self._all_op_indices[op_node] in visitor.distances
555+
and self._all_op_indices[op_node] in visitor.predecessors
556556
)
557557

558558
def _is_feasible(op_node: _OperatorNode):

tests/decomposition/test_decomposition_graph.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
side_effect=lambda x: decompositions[_to_name(x)],
4343
)
4444
class TestDecompositionGraph:
45-
4645
def test_weighted_graph_solve(self, _):
4746
"""Tests solving a simple graph for the optimal decompositions with weighted gates."""
4847

@@ -230,6 +229,15 @@ def custom_decomp(wires):
230229
# edges from the dummy starting node to the target gate set.
231230
assert len(graph._graph.edges()) == 11
232231

232+
solution = graph.solve()
233+
234+
# verify that is_solved_for returns False for gates in the gate set
235+
assert not solution.is_solved_for(qml.RX(0.5, wires=0))
236+
237+
# verify that the correct error is raised
238+
with pytest.raises(DecompositionError, match="is unsolved in this decomposition graph."):
239+
solution.decomposition(qml.RX(0.5, wires=0))
240+
233241
def test_graph_solve(self, _):
234242
"""Tests solving a simple graph for the optimal decompositions."""
235243

0 commit comments

Comments
 (0)