-
Notifications
You must be signed in to change notification settings - Fork 349
Improve photonics documentation #2502
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
86cb85c
photonics simulator
Omar-ORCA 619c85a
photonics operators
Omar-ORCA c7343f4
executing photonic kernels
Omar-ORCA 5589939
Merge branch 'NVIDIA:main' into photonics_docs
Omar-ORCA 44c9a4c
formatting and spelling
Omar-ORCA af51137
Update annihilate_photonic_gate.py
Omar-ORCA 32e88a2
Merge branch 'main' into photonics_docs
Omar-ORCA 8845762
Add header to beam_splitter_photonic_gate.py
Omar-ORCA ed0831d
Merge branch 'main' into photonics_docs
khalatepradnya d2f52df
patch photonics_tbi*.cpp
Omar-ORCA 518b7ff
Merge branch 'main' into photonics_docs
khalatepradnya f60a312
Merge branch 'main' into photonics_docs
Omar-ORCA 2e4f152
Merge branch 'main' into photonics_docs
khalatepradnya 63869c1
Library mode required
khalatepradnya 4170129
Merge branch 'main' into photonics_docs
khalatepradnya ba698be
Update annihilate_photonic_gate.py
Omar-ORCA b8fd0c8
Merge branch 'main' into photonics_docs
khalatepradnya 3cf669b
Merge branch 'main' into photonics_docs
khalatepradnya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
docs/sphinx/examples/python/executing_photonic_kernels.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "attachments": {}, | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# Executing Quantum Photonic Circuits \n", | ||
| "\n", | ||
| "In CUDA-Q, there are 2 ways in which one can execute quantum photonic kernels: \n", | ||
| "\n", | ||
| "1. `sample`: yields measurement counts \n", | ||
| "3. `get_state`: yields the quantum statevector of the computation \n", | ||
| "\n", | ||
| "## Sample\n", | ||
| "\n", | ||
| "Quantum states collapse upon measurement and hence need to be sampled many times to gather statistics. The CUDA-Q `sample` call enables this: \n", | ||
| "\n" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import cudaq\n", | ||
| "import numpy as np\n", | ||
| "\n", | ||
| "qumode_count = 2\n", | ||
| "\n", | ||
| "# Define the simulation target.\n", | ||
| "cudaq.set_target(\"orca-photonics\")\n", | ||
| "\n", | ||
| "# Define a quantum kernel function.\n", | ||
| "\n", | ||
| "\n", | ||
| "@cudaq.kernel\n", | ||
| "def kernel(qumode_count: int):\n", | ||
| " level = qumode_count + 1\n", | ||
| " qumodes = [qudit(level) for _ in range(qumode_count)]\n", | ||
| "\n", | ||
| " # Apply the create gate to the qumodes.\n", | ||
| " for i in range(qumode_count):\n", | ||
| " create(qumodes[i]) # |00⟩ -> |11⟩\n", | ||
| "\n", | ||
| " # Apply the beam_splitter gate to the qumodes.\n", | ||
| " beam_splitter(qumodes[0], qumodes[1], np.pi / 6)\n", | ||
| "\n", | ||
| " # measure all qumodes\n", | ||
| " mz(qumodes)\n", | ||
| "\n", | ||
| "\n", | ||
| "result = cudaq.sample(kernel, qumode_count, shots_count=1000)\n", | ||
| "\n", | ||
| "print(result)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "\n", | ||
| "## Get state\n", | ||
| "\n", | ||
| "The `get_state` function gives us access to the quantum statevector of the computation." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import cudaq\n", | ||
| "import numpy as np\n", | ||
| "\n", | ||
| "qumode_count = 2\n", | ||
| "\n", | ||
| "# Define the simulation target.\n", | ||
| "cudaq.set_target(\"orca-photonics\")\n", | ||
| "\n", | ||
| "# Define a quantum kernel function.\n", | ||
| "\n", | ||
| "\n", | ||
| "@cudaq.kernel\n", | ||
| "def kernel(qumode_count: int):\n", | ||
| " level = qumode_count + 1\n", | ||
| " qumodes = [qudit(level) for _ in range(qumode_count)]\n", | ||
| "\n", | ||
| " # Apply the create gate to the qumodes.\n", | ||
| " for i in range(qumode_count):\n", | ||
| " create(qumodes[i]) # |00⟩ -> |11⟩\n", | ||
| "\n", | ||
| " # Apply the beam_splitter gate to the qumodes.\n", | ||
| " beam_splitter(qumodes[0], qumodes[1], np.pi / 6)\n", | ||
| "\n", | ||
| " # measure some of all qumodes if need to be measured\n", | ||
| " # mz(qumodes)\n", | ||
| "\n", | ||
| "\n", | ||
| "# Compute the statevector of the kernel\n", | ||
| "result = cudaq.get_state(kernel, qumode_count)\n", | ||
| "\n", | ||
| "print(np.array(result))" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "The statevector generated by the `get_state` command follows little-endian convention for associating numbers with their digit string representations, which places the least significant digit on the right. That is, for the example of a 2-qumode system of level 3 (in which possible states are 0, 1, and 2), we have the following translation between integers and digit string:\n", | ||
| "$$\\begin{matrix} \n", | ||
| "\\text{Integer} & \\text{digit string representation}\\\\\n", | ||
| "& \\text{least significant bit on right}\\\\\n", | ||
| "0 = \\textcolor{blue}{0}*3^1 + \\textcolor{red}{0}*3^0 & \\textcolor{blue}{0}\\textcolor{red}{0} \\\\\n", | ||
| "1 = \\textcolor{blue}{0}*3^1 + \\textcolor{red}{1}*3^0 & \\textcolor{blue}{0}\\textcolor{red}{1}\\\\\n", | ||
| "2 = \\textcolor{blue}{0}*3^1 + \\textcolor{red}{2}*3^0 & \\textcolor{blue}{0}\\textcolor{red}{2}\\\\\n", | ||
| "3 = \\textcolor{blue}{1}*3^1 + \\textcolor{red}{0}*3^0 & \\textcolor{blue}{1}\\textcolor{red}{0} \\\\\n", | ||
| "4 = \\textcolor{blue}{1}*3^1 + \\textcolor{red}{1}*3^0 & \\textcolor{blue}{1}\\textcolor{red}{1} \\\\\n", | ||
| "5 = \\textcolor{blue}{1}*3^1 + \\textcolor{red}{2}*3^0 & \\textcolor{blue}{1}\\textcolor{red}{2} \\\\\n", | ||
| "6 = \\textcolor{blue}{2}*3^1 + \\textcolor{red}{0}*3^0 & \\textcolor{blue}{2}\\textcolor{red}{0} \\\\\n", | ||
| "7 = \\textcolor{blue}{2}*3^1 + \\textcolor{red}{1}*3^0 & \\textcolor{blue}{2}\\textcolor{red}{1} \\\\\n", | ||
| "8 = \\textcolor{blue}{2}*3^1 + \\textcolor{red}{2}*3^0 & \\textcolor{blue}{2}\\textcolor{red}{2} \n", | ||
| "\\end{matrix}\n", | ||
| "$$\n" | ||
| ] | ||
| }, | ||
| { | ||
| "attachments": {}, | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "\n", | ||
| "## Parallelization Techniques\n", | ||
| "\n", | ||
| "The most intensive task in the computation is the execution of the quantum photonic kernel hence each execution function: `sample`, and `get_state` can be parallelized given access to multiple quantum processing units (multi-QPU). We emulate each QPU with a CPU." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "print(cudaq.__version__)" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "Python 3", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.10.12" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 4 | ||
| } |
35 changes: 35 additions & 0 deletions
35
docs/sphinx/snippets/python/using/examples/annihilate_photonic_gate.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # ============================================================================ # | ||
| # Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. # | ||
| # All rights reserved. # | ||
| # # | ||
| # This source code and the accompanying materials are made available under # | ||
| # the terms of the Apache License 2.0 which accompanies this distribution. # | ||
| # ============================================================================ # | ||
|
|
||
| #[Begin Docs] | ||
| import cudaq | ||
|
|
||
| cudaq.set_target("orca-photonics") | ||
|
|
||
|
|
||
| @cudaq.kernel | ||
| def kernel(): | ||
| # A single qumode with 2 levels initialized to the ground / zero state. | ||
| level = 2 | ||
| qumode = qudit(level) | ||
|
|
||
| # Apply the create gate to the qumode. | ||
| create(qumode) # |0⟩ -> |1⟩ | ||
|
|
||
| # Apply the annihilate gate to the qumode. | ||
| annihilate(qumode) # |1⟩ -> |0⟩ | ||
|
|
||
| # Measurement operator. | ||
| mz(qumode) | ||
|
|
||
|
|
||
| # Sample the qumode for 1000 shots to gather statistics. | ||
| # In this case, the results are deterministic and all return state 0. | ||
| result = cudaq.sample(kernel) | ||
| print(result) | ||
| #[End Docs] |
38 changes: 38 additions & 0 deletions
38
docs/sphinx/snippets/python/using/examples/beam_splitter_photonic_gate.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # ============================================================================ # | ||
| # Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. # | ||
| # All rights reserved. # | ||
| # # | ||
| # This source code and the accompanying materials are made available under # | ||
| # the terms of the Apache License 2.0 which accompanies this distribution. # | ||
| # ============================================================================ # | ||
|
|
||
| #[Begin Docs] | ||
| import cudaq | ||
| import math | ||
|
|
||
| cudaq.set_target("orca-photonics") | ||
|
|
||
|
|
||
| @cudaq.kernel | ||
| def kernel(): | ||
| n_modes = 2 | ||
| level = 3 # qudit level | ||
|
|
||
| # Two qumode with 3 levels initialized to the ground / zero state. | ||
| qumodes = [qudit(level) for _ in range(n_modes)] | ||
|
|
||
| # Apply the create gate to the qumodes. | ||
| for i in range(n_modes): | ||
| create(qumodes[i]) # |00⟩ -> |11⟩ | ||
|
|
||
| # Apply the beam_splitter gate to the qumodes. | ||
| beam_splitter(qumodes[0], qumodes[1], math.pi / 4) | ||
|
|
||
| # Measurement operator. | ||
| mz(qumodes) | ||
|
|
||
|
|
||
| # Sample the qumode for 1000 shots to gather statistics. | ||
| result = cudaq.sample(kernel) | ||
| print(result) | ||
| #[End Docs] |
32 changes: 32 additions & 0 deletions
32
docs/sphinx/snippets/python/using/examples/create_photonic_gate.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # ============================================================================ # | ||
| # Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. # | ||
| # All rights reserved. # | ||
| # # | ||
| # This source code and the accompanying materials are made available under # | ||
| # the terms of the Apache License 2.0 which accompanies this distribution. # | ||
| # ============================================================================ # | ||
|
|
||
| #[Begin Docs] | ||
| import cudaq | ||
|
|
||
| cudaq.set_target("orca-photonics") | ||
|
|
||
|
|
||
| @cudaq.kernel | ||
| def kernel(): | ||
| # A single qumode with 2 levels initialized to the ground / zero state. | ||
| level = 2 | ||
| qumode = qudit(level) | ||
|
|
||
| # Apply the create gate to the qumode. | ||
| create(qumode) # |0⟩ -> |1⟩ | ||
|
|
||
| # Measurement operator. | ||
| mz(qumode) | ||
|
|
||
|
|
||
| # Sample the qumode for 1000 shots to gather statistics. | ||
| # In this case, the results are deterministic and all return state 1. | ||
| result = cudaq.sample(kernel) | ||
| print(result) | ||
| #[End Docs] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.