Skip to content

Fixes #7384 : Made DecompositionContext non-optional in _decompose_with_context_ me… #7385

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

Closed
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
5 changes: 1 addition & 4 deletions cirq-core/cirq/ops/classically_controlled_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ def with_qubits(self, *new_qubits):
*self._conditions
)

def _decompose_(self):
return self._decompose_with_context_()

def _decompose_with_context_(self, context: cirq.DecompositionContext | None = None):
def _decompose_with_context_(self, *, context: cirq.DecompositionContext):
result = protocols.decompose_once(
self._sub_operation, NotImplemented, flatten=False, context=context
)
Expand Down
5 changes: 1 addition & 4 deletions cirq-core/cirq/ops/controlled_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,8 @@ def num_controls(self) -> int:
def _qid_shape_(self) -> tuple[int, ...]:
return self.control_qid_shape + protocols.qid_shape(self.sub_gate)

def _decompose_(self, qubits: tuple[cirq.Qid, ...]) -> None | NotImplementedType | cirq.OP_TREE:
return self._decompose_with_context_(qubits)

def _decompose_with_context_(
self, qubits: tuple[cirq.Qid, ...], context: cirq.DecompositionContext | None = None
self, qubits: tuple[cirq.Qid, ...], *, context: cirq.DecompositionContext
) -> None | NotImplementedType | cirq.OP_TREE:
control_qubits = list(qubits[: self.num_controls()])
controlled_sub_gate = self.sub_gate.controlled(
Expand Down
5 changes: 1 addition & 4 deletions cirq-core/cirq/ops/controlled_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,7 @@ def with_qubits(self, *new_qubits):
new_qubits[:n], self.sub_operation.with_qubits(*new_qubits[n:]), self.control_values
)

def _decompose_(self):
return self._decompose_with_context_()

def _decompose_with_context_(self, context: cirq.DecompositionContext | None = None):
def _decompose_with_context_(self, *, context: cirq.DecompositionContext):
result = protocols.decompose_once_with_qubits(
self.gate, self.qubits, NotImplemented, flatten=False, context=context
)
Expand Down
5 changes: 1 addition & 4 deletions cirq-core/cirq/ops/gate_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,8 @@ def _qid_shape_(self):
def _num_qubits_(self):
return len(self._qubits)

def _decompose_(self) -> cirq.OP_TREE:
return self._decompose_with_context_()

def _decompose_with_context_(
self, context: cirq.DecompositionContext | None = None
self, *, context: cirq.DecompositionContext
) -> cirq.OP_TREE:
return protocols.decompose_once_with_qubits(
self.gate, self.qubits, NotImplemented, flatten=False, context=context
Expand Down
7 changes: 2 additions & 5 deletions cirq-core/cirq/ops/raw_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,11 +828,8 @@ def _from_json_dict_(cls, sub_operation, tags, **kwargs):
def _json_dict_(self) -> dict[str, Any]:
return protocols.obj_to_dict_helper(self, ['sub_operation', 'tags'])

def _decompose_(self) -> cirq.OP_TREE:
return self._decompose_with_context_()

def _decompose_with_context_(
self, context: cirq.DecompositionContext | None = None
self, *, context: cirq.DecompositionContext
) -> cirq.OP_TREE:
return protocols.decompose_once(
self.sub_operation, default=None, flatten=False, context=context
Expand Down Expand Up @@ -986,7 +983,7 @@ def _decompose_(self, qubits):
return self._decompose_with_context_(qubits)

def _decompose_with_context_(
self, qubits: Sequence[cirq.Qid], context: cirq.DecompositionContext | None = None
self, qubits: Sequence[cirq.Qid], *, context: cirq.DecompositionContext
) -> cirq.OP_TREE:
return protocols.inverse(
protocols.decompose_once_with_qubits(self._original, qubits, context=context)
Expand Down
21 changes: 8 additions & 13 deletions cirq-core/cirq/protocols/decompose_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
@runtime_checkable
class OpDecomposerWithContext(Protocol):
def __call__(
self, __op: cirq.Operation, *, context: cirq.DecompositionContext | None = None
self, __op: cirq.Operation, *, context: cirq.DecompositionContext
) -> DecomposeResult: ...


Expand Down Expand Up @@ -126,7 +126,7 @@ def _decompose_(self) -> DecomposeResult:
pass
Copy link
Collaborator

Choose a reason for hiding this comment

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

There were a couple other places in this file too iirc, like some type definitions.


def _decompose_with_context_(
self, *, context: DecompositionContext | None = None
self, *, context: DecompositionContext
) -> DecomposeResult:
pass

Expand Down Expand Up @@ -154,13 +154,13 @@ def _decompose_(self, qubits: tuple[cirq.Qid, ...]) -> DecomposeResult:
pass

def _decompose_with_context_(
self, qubits: tuple[cirq.Qid, ...], *, context: DecompositionContext | None = None
self, qubits: tuple[cirq.Qid, ...], *, context: DecompositionContext
) -> DecomposeResult:
pass


def _try_op_decomposer(
val: Any, decomposer: OpDecomposer | None, *, context: DecompositionContext | None = None
val: Any, decomposer: OpDecomposer | None, *, context: DecompositionContext
) -> DecomposeResult:
if decomposer is None or not isinstance(val, ops.Operation):
return None
Expand All @@ -173,7 +173,7 @@ def _try_op_decomposer(

@dataclasses.dataclass(frozen=True)
class _DecomposeArgs:
context: DecompositionContext | None
context: DecompositionContext
intercepting_decomposer: OpDecomposer | None
fallback_decomposer: OpDecomposer | None
keep: Callable[[cirq.Operation], bool] | None
Expand Down Expand Up @@ -362,14 +362,9 @@ def decompose_once(
TypeError: `val` didn't have a `_decompose_` method (or that method returned
`NotImplemented` or `None`) and `default` wasn't set.
"""
if context is None:
context = DecompositionContext(
ops.SimpleQubitManager(prefix=f'_decompose_protocol_{next(_CONTEXT_COUNTER)}')
)

method = getattr(val, '_decompose_with_context_', None)
decomposed = NotImplemented if method is None else method(*args, **kwargs, context=context)
if decomposed is NotImplemented or decomposed is None:
if context is not None and hasattr(val, '_decompose_with_context_'):
decomposed = val._decompose_with_context_(*args, context=context, **kwargs)
else:
method = getattr(val, '_decompose_', None)
decomposed = NotImplemented if method is None else method(*args, **kwargs)

Expand Down
Loading