-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix small issues in Shor tutorial #5639
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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))" | ||
| ] | ||
| }, | ||
| { | ||
|
|
@@ -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", | ||
|
|
@@ -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", | ||
|
|
@@ -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.
|
||
| "\n", | ||
| " Note that the target and exponent should be qubit\n", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
|
@@ -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", | ||
|
|
@@ -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", | ||
|
|
@@ -1096,6 +1119,7 @@ | |
| ], | ||
| "metadata": { | ||
| "colab": { | ||
| "collapsed_sections": [], | ||
| "name": "shor.ipynb", | ||
| "toc_visible": true | ||
| }, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.