cirq.ArithmeticOperation

class cirq.ArithmeticOperation[source]

A helper class for implementing reversible classical arithmetic.

Child classes must override the registers, with_registers, and apply
methods.
This class handles the details of ensuring that the scaling of implementing
the operation is O(2^n) instead of O(4^n) where n is the number of qubits
being acted on, by implementing an _apply_unitary_ function in terms of
the registers and the apply function of the child class. It also handles the
boilerplate of implementing the qubits and with_qubits methods.

Examples:

>>> class Add(cirq.ArithmeticOperation):
...     def __init__(self, target_register, input_register):
...         self.target_register = target_register
...         self.input_register = input_register
...
...     def registers(self):
...         return self.target_register, self.input_register
...
...     def with_registers(self, *new_registers):
...         return Add(*new_registers)
...
...     def apply(self, target_value, input_value):
...         return target_value + input_value
>>> cirq.unitary(
...     Add(target_register=cirq.LineQubit.range(2),
...         input_register=1)
... ).astype(np.int32)
array([[0, 0, 0, 1],
       [1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0]], dtype=int32)
>>> c = cirq.Circuit(
...    cirq.X(cirq.LineQubit(3)),
...    cirq.X(cirq.LineQubit(2)),
...    cirq.X(cirq.LineQubit(6)),
...    cirq.measure(*cirq.LineQubit.range(4, 8), key='before:in'),
...    cirq.measure(*cirq.LineQubit.range(4), key='before:out'),
...
...    Add(target_register=cirq.LineQubit.range(4),
...        input_register=cirq.LineQubit.range(4, 8)),
...
...    cirq.measure(*cirq.LineQubit.range(4, 8), key='after:in'),
...    cirq.measure(*cirq.LineQubit.range(4), key='after:out'),
... )
>>> cirq.sample(c).data
   before:in  before:out  after:in  after:out
0          2           3         2          5
__init__()

Initialize self. See help(type(self)) for accurate signature.

Methods

apply(*register_values)

Returns the result of the operation operating on classical values.

controlled_by(*control_qubits[, control_values])

Returns a controlled version of this operation. If no control_qubits

registers()

The data acted upon by the arithmetic operation.

transform_qubits(func)

Returns the same operation, but with different qubits.

validate_args(qubits)

Raises an exception if the qubits don’t match this operation’s qid

with_qubits(*new_qubits)

Returns the same operation, but applied to different qubits.

with_registers(*new_registers)

Returns the same operation targeting different registers.

with_tags(*new_tags)

Creates a new TaggedOperation, with this op and the specified tags.

Attributes

gate

qubits

tags

Returns a tuple of the operation’s tags.

untagged

Returns the underlying operation without any tags.