Skip to content
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
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/arithmetic_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def _apply_unitary_(self, args: 'cirq.ApplyUnitaryArgs'):


class ArithmeticGate(Gate, metaclass=abc.ABCMeta):
"""A helper gate for implementing reversible classical arithmetic.
r"""A helper gate for implementing reversible classical arithmetic.

Child classes must override the `registers`, `with_registers`, and `apply`
methods.
Expand Down
45 changes: 18 additions & 27 deletions docs/experiments/shor.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,13 @@
"id": "03iL9sDpYArl"
},
"source": [
"Here we discuss an example of defining an arithmetic operation in Cirq, namely modular addition. This operation adds the value of the input register into the target register. More specifically, this operation acts on two qubit registers as\n",
"Here we discuss an example of defining an arithmetic gate in Cirq, namely modular addition. This operation adds the value of the input register into the target register. More specifically, this operation acts on two qubit registers as\n",
"\n",
"$$ |a\\rangle_i |b\\rangle_t \\mapsto |a\\rangle_i |a + b \\text{ mod } N_t \\rangle_t . $$\n",
"\n",
"Here, the subscripts $i$ and $t$ denote <i>i</i>nput and <i>t</i>arget register, respectively, and $N_t$ is the dimension of the target register.\n",
"\n",
"To define this operation, called `Adder`, we inherit from `cirq.ArithmeticOperation` and override the four methods shown below. The main method is the `apply` method which defines the arithmetic. Here, we simply state the expression as $a + b$ instead of the more accurate $a + b \\text{ mod } N_t$ above -- the `cirq.ArithmeticOperation` class is able to deduce what we mean by simply $a + b$ since the operation must be reversible. "
"To define this gate, called `Adder`, we inherit from `cirq.ArithmeticGate` and override the four methods shown below. The main method is the `apply` method which defines the arithmetic. Here, we simply state the expression as $a + b$ instead of the more accurate $a + b \\text{ mod } N_t$ above -- the `cirq.ArithmeticGate` class is able to deduce what we mean by simply $a + b$ since the operation must be reversible. "
]
},
{
Expand All @@ -362,11 +362,11 @@
"outputs": [],
"source": [
"\"\"\"Example of defining an arithmetic (quantum) operation in Cirq.\"\"\"\n",
"class Adder(cirq.ArithmeticOperation):\n",
"class Adder(cirq.ArithmeticGate):\n",
" \"\"\"Quantum addition.\"\"\"\n",
" def __init__(self, target_register, input_register):\n",
" self.input_register = input_register\n",
" self.target_register = target_register\n",
" self.input_register = input_register\n",
" \n",
" def registers(self):\n",
" return self.target_register, self.input_register\n",
Expand Down Expand Up @@ -410,7 +410,7 @@
"circ = cirq.Circuit(\n",
" cirq.X.on(qreg1[0]),\n",
" cirq.X.on(qreg2[1]),\n",
" Adder(input_register=qreg1, target_register=qreg2),\n",
" Adder(input_register=[2,2], target_register=[2,2])(*qreg2, *qreg1),\n",
" cirq.measure_each(*qreg1),\n",
" cirq.measure_each(*qreg2)\n",
")\n",
Expand Down Expand Up @@ -452,7 +452,7 @@
"source": [
"\"\"\"Example of the unitary of an Adder operation.\"\"\"\n",
"cirq.unitary(\n",
" Adder(target_register=cirq.LineQubit.range(2),\n",
" Adder(target_register=[2,2],\n",
" input_register=1)\n",
").real"
]
Expand Down Expand Up @@ -493,7 +493,7 @@
"outputs": [],
"source": [
"\"\"\"Defines the modular exponential operation used in Shor's algorithm.\"\"\"\n",
"class ModularExp(cirq.ArithmeticOperation):\n",
"class ModularExp(cirq.ArithmeticGate):\n",
" \"\"\"Quantum modular exponentiation.\n",
"\n",
" This class represents the unitary which multiplies base raised to exponent\n",
Expand All @@ -515,8 +515,8 @@
" \"\"\"\n",
" def __init__(\n",
" self, \n",
" target: Sequence[cirq.Qid],\n",
" exponent: Union[int, Sequence[cirq.Qid]], \n",
" target: Sequence[int],\n",
" exponent: Union[int, Sequence[int]], \n",
" base: int,\n",
" modulus: int\n",
" ) -> None:\n",
Expand All @@ -534,7 +534,7 @@
" def with_registers(\n",
" self,\n",
" *new_registers: Union[int, Sequence['cirq.Qid']],\n",
" ) -> cirq.ArithmeticOperation:\n",
" ) -> cirq.ArithmeticGate:\n",
" \"\"\"Returns a new ModularExp object with new registers.\"\"\"\n",
" if len(new_registers) != 4:\n",
" raise ValueError(f'Expected 4 registers (target, exponent, base, '\n",
Expand Down Expand Up @@ -582,22 +582,13 @@
" \"\"\"\n",
" assert args.known_qubits is not None\n",
" wire_symbols: List[str] = []\n",
" t, e = 0, 0\n",
" for qubit in args.known_qubits:\n",
" if qubit in self.target:\n",
" if t == 0:\n",
" if isinstance(self.exponent, Sequence):\n",
" e_str = 'e'\n",
" else:\n",
" e_str = str(self.exponent)\n",
" wire_symbols.append(\n",
" f'ModularExp(t*{self.base}**{e_str} % {self.modulus})')\n",
" else:\n",
" wire_symbols.append('t' + str(t))\n",
" t += 1\n",
" if isinstance(self.exponent, Sequence) and qubit in self.exponent:\n",
" wire_symbols.append('e' + str(e))\n",
" e += 1\n",
" if isinstance(self.exponent, Sequence):\n",
" e_str = 'e'\n",
" else:\n",
" e_str = str(self.exponent)\n",
" wire_symbols.append(f'ModularExp(t*{self.base}**{e_str} % {self.modulus})')\n",
" wire_symbols.extend(['t' + str(t) for t in range(1, len(self.target))])\n",
" wire_symbols.extend(['e' + str(e) for e in range(len(self.exponent))])\n",
" return cirq.CircuitDiagramInfo(wire_symbols=tuple(wire_symbols))"
]
},
Expand Down Expand Up @@ -717,7 +708,7 @@
" return cirq.Circuit(\n",
" cirq.X(target[L - 1]),\n",
" cirq.H.on_each(*exponent),\n",
" ModularExp(target, exponent, x, n),\n",
" ModularExp([2]*L, [2]*(2*L+3), x, n).on(*target, *exponent),\n",
" cirq.qft(*exponent, inverse=True),\n",
" cirq.measure(*exponent, key='exponent'),\n",
" )"
Expand Down
2 changes: 1 addition & 1 deletion docs/gatezoo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}
2 changes: 1 addition & 1 deletion docs/start/intro.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2926,4 +2926,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}
2 changes: 1 addition & 1 deletion docs/transform/custom_transformers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}