Skip to content
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

Fix small issues in Shor tutorial #5639

Merged
merged 3 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -31,7 +31,7 @@

@deprecated_class(deadline='v0.16', fix='Use cirq.ArithmeticGate')
class ArithmeticOperation(Operation, metaclass=abc.ABCMeta):
"""A helper class for implementing reversible classical arithmetic.
r"""A helper class for implementing reversible classical arithmetic.
Child classes must override the `registers`, `with_registers`, and `apply`
methods.
Expand Down
32 changes: 28 additions & 4 deletions docs/experiments/shor.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,11 @@
" return Adder(*new_registers)\n",
" \n",
" def apply(self, target_value, input_value):\n",
" return target_value + input_value"
" return target_value + input_value\n",
"\n",
" def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs):\n",
" wire_symbols = [' + ' for _ in range(len(self.input_register)+len(self.target_register))]\n",
viathor marked this conversation as resolved.
Show resolved Hide resolved
" return cirq.CircuitDiagramInfo(wire_symbols=tuple(wire_symbols))"
]
},
{
Expand Down Expand Up @@ -404,8 +408,8 @@
"\n",
"# Define the circuit.\n",
"circ = cirq.Circuit(\n",
" cirq.ops.X.on(qreg1[0]),\n",
" cirq.ops.X.on(qreg2[1]),\n",
" cirq.X.on(qreg1[0]),\n",
" cirq.X.on(qreg2[1]),\n",
" Adder(input_register=qreg1, target_register=qreg2),\n",
" cirq.measure_each(*qreg1),\n",
" cirq.measure_each(*qreg2)\n",
Expand Down Expand Up @@ -531,6 +535,7 @@
" self,\n",
" *new_registers: Union[int, Sequence['cirq.Qid']],\n",
" ) -> cirq.ArithmeticOperation:\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",
" f'modulus), but got {len(new_registers)}')\n",
Expand All @@ -547,6 +552,18 @@
" return ModularExp(target, exponent, base, modulus)\n",
"\n",
" def apply(self, *register_values: int) -> int:\n",
" \"\"\"Applies modular exponentiation to the registers.\n",
"\n",
" Four values should be passed in. They are, in order:\n",
" - the target\n",
" - the exponent\n",
" - the base\n",
" - the modulus\n",
viathor marked this conversation as resolved.
Show resolved Hide resolved
"\n",
" Note that the target and exponent should be qubit\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: Strictly speaking exponent can be either a qubit register or a fixed constant, though for specific application in Shor's algorithm we use the former.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I will skip this technicality since this docstring is describing our particular implementation.

" registers, while the base and modulus should be\n",
" constant parameters that control the resulting unitary.\n",
" \"\"\"\n",
" assert len(register_values) == 4\n",
" target, exponent, base, modulus = register_values\n",
" if target >= modulus:\n",
Expand All @@ -557,6 +574,12 @@
" self,\n",
" args: cirq.CircuitDiagramInfoArgs,\n",
" ) -> cirq.CircuitDiagramInfo:\n",
" \"\"\"Returns a 'CircuitDiagramInfo' object for printing circuits.\n",
"\n",
" This function just returns information on how to print this operation\n",
" out in a circuit diagram so that the registers are labeled\n",
" appropriately as exponent ('e') and target ('t').\n",
" \"\"\"\n",
" assert args.known_qubits is not None\n",
" wire_symbols: List[str] = []\n",
" t, e = 0, 0\n",
Expand Down Expand Up @@ -676,7 +699,7 @@
" \"\"\"Returns quantum circuit which computes the order of x modulo n.\n",
"\n",
" The circuit uses Quantum Phase Estimation to compute an eigenvalue of\n",
" the unitary\n",
" the following unitary:\n",
"\n",
" U|y⟩ = |y * x mod n⟩ 0 <= y < n\n",
" U|y⟩ = |y⟩ n <= y\n",
Expand Down Expand Up @@ -1096,6 +1119,7 @@
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "shor.ipynb",
"toc_visible": true
},
Expand Down