Skip to content

Commit

Permalink
Remove deprecated ArithmeticOperation (#5579)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxfohl authored Jul 7, 2022
1 parent 10abfc2 commit 4b6ee22
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 502 deletions.
1 change: 0 additions & 1 deletion cirq-core/cirq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@
AnyIntegerPowerGateFamily,
AnyUnitaryGateFamily,
ArithmeticGate,
ArithmeticOperation,
asymmetric_depolarize,
AsymmetricDepolarizingChannel,
BaseDensePauliString,
Expand Down
1 change: 0 additions & 1 deletion cirq-core/cirq/interop/quirk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# Imports from cells are only to ensure operation reprs work correctly.
from cirq.interop.quirk.cells import (
QuirkArithmeticGate,
QuirkArithmeticOperation,
QuirkInputRotationOperation,
QuirkQubitPermutationGate,
)
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/interop/quirk/cells/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from cirq.interop.quirk.cells.qubit_permutation_cells import QuirkQubitPermutationGate

from cirq.interop.quirk.cells.arithmetic_cells import QuirkArithmeticGate, QuirkArithmeticOperation
from cirq.interop.quirk.cells.arithmetic_cells import QuirkArithmeticGate

from cirq.interop.quirk.cells.input_rotation_cells import QuirkInputRotationOperation

Expand Down
116 changes: 0 additions & 116 deletions cirq-core/cirq/interop/quirk/cells/arithmetic_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,128 +28,12 @@
)

from cirq import ops, value
from cirq._compat import deprecated_class
from cirq.interop.quirk.cells.cell import Cell, CellMaker, CELL_SIZES

if TYPE_CHECKING:
import cirq


@deprecated_class(deadline='v0.16', fix='Use cirq.QuirkArithmeticGate')
@value.value_equality
class QuirkArithmeticOperation(ops.ArithmeticOperation):
"""Applies arithmetic to a target and some inputs.
Implements Quirk-specific implicit effects like assuming that the presence
of an 'r' input implies modular arithmetic.
In Quirk, modular operations have no effect on values larger than the
modulus. This convention is used because unitarity forces *some* convention
on out-of-range values (they cannot simply disappear or raise exceptions),
and the simplest is to do nothing. This call handles ensuring that happens,
and ensuring the new target register value is normalized modulo the modulus.
"""

def __init__(
self,
identifier: str,
target: Sequence['cirq.Qid'],
inputs: Sequence[Union[Sequence['cirq.Qid'], int]],
):
"""Inits QuirkArithmeticOperation.
Args:
identifier: The quirk identifier string for this operation.
target: The target qubit register.
inputs: Qubit registers (or classical constants) that
determine what happens to the target.
Raises:
ValueError: If given overlapping registers, or the target is too
small for a modular operation with too small modulus.
"""
self.identifier = identifier
self.target: Tuple['cirq.Qid', ...] = tuple(target)
self.inputs: Tuple[Union[Sequence['cirq.Qid'], int], ...] = tuple(
e if isinstance(e, int) else tuple(e) for e in inputs
)

for input_register in self.inputs:
if isinstance(input_register, int):
continue
if set(self.target) & set(input_register):
raise ValueError(f'Overlapping registers: {self.target} {self.inputs}')

if self.operation.is_modular:
r = inputs[-1]
if isinstance(r, int):
over = r > 1 << len(target)
else:
over = len(cast(Sequence, r)) > len(target)
if over:
raise ValueError(f'Target too small for modulus.\nTarget: {target}\nModulus: {r}')

@property
def operation(self) -> '_QuirkArithmeticCallable':
return ARITHMETIC_OP_TABLE[self.identifier]

def _value_equality_values_(self) -> Any:
return self.identifier, self.target, self.inputs

def registers(self) -> Sequence[Union[int, Sequence['cirq.Qid']]]:
return [self.target, *self.inputs]

def with_registers(
self, *new_registers: Union[int, Sequence['cirq.Qid']]
) -> 'QuirkArithmeticOperation':
if len(new_registers) != len(self.inputs) + 1:
raise ValueError(
'Wrong number of registers.\n'
f'New registers: {repr(new_registers)}\n'
f'Operation: {repr(self)}'
)

if isinstance(new_registers[0], int):
raise ValueError(
'The first register is the mutable target. '
'It must be a list of qubits, not the constant '
f'{new_registers[0]}.'
)

return QuirkArithmeticOperation(self.identifier, new_registers[0], new_registers[1:])

def apply(self, *registers: int) -> Union[int, Iterable[int]]:
return self.operation(*registers)

def _circuit_diagram_info_(self, args: 'cirq.CircuitDiagramInfoArgs') -> List[str]:
lettered_args = list(zip(self.operation.letters, self.inputs))

result: List[str] = []

# Target register labels.
consts = ''.join(
f',{letter}={reg}' for letter, reg in lettered_args if isinstance(reg, int)
)
result.append(f'Quirk({self.identifier}{consts})')
result.extend(f'#{i}' for i in range(2, len(self.target) + 1))

# Input register labels.
for letter, reg in lettered_args:
if not isinstance(reg, int):
result.extend(f'{letter.upper()}{i}' for i in range(len(cast(Sequence, reg))))

return result

def __repr__(self) -> str:
return (
'cirq.interop.quirk.QuirkArithmeticOperation(\n'
f' {repr(self.identifier)},\n'
f' target={repr(self.target)},\n'
f' inputs={_indented_list_lines_repr(self.inputs)},\n'
')'
)


@value.value_equality
class QuirkArithmeticGate(ops.ArithmeticGate):
"""Applies arithmetic to a target and some inputs.
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""Gates (unitary and non-unitary), operations, base types, and gate sets.
"""

from cirq.ops.arithmetic_operation import ArithmeticGate, ArithmeticOperation
from cirq.ops.arithmetic_operation import ArithmeticGate

from cirq.ops.clifford_gate import CliffordGate, PauliTransform, SingleQubitCliffordGate

Expand Down
Loading

0 comments on commit 4b6ee22

Please sign in to comment.