Skip to content

Commit

Permalink
Fix small issues in Shor tutorial (quantumlib#5639)
Browse files Browse the repository at this point in the history
* Fix small issues in Shor tutorial

- use cirq.X instead of cirq.ops.X
- Add a circuit_diagram_info for better printing
- Add a few clarifications here and there.

Also, this changes ArithmeticOperation to use a raw
docstring so it will format better.
  • Loading branch information
dstrain115 authored and rht committed May 1, 2023
1 parent 6ae4e00 commit 14d9837
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
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",
" 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",
"\n",
" Note that the target and exponent should be qubit\n",
" 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

0 comments on commit 14d9837

Please sign in to comment.