-
Notifications
You must be signed in to change notification settings - Fork 193
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
Demo TN with LightningTensor #1288
Open
multiphaseCFD
wants to merge
16
commits into
master
Choose a base branch
from
demo_tutorial_lightning_tensor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d500d70
initial commit
multiphaseCFD 8ff8ee8
make format
multiphaseCFD 1c6cffa
Merge branch 'master' into demo_tutorial_lightning_tensor
multiphaseCFD c5eb512
polish the context
multiphaseCFD 9c684b5
make format
multiphaseCFD 205621c
Merge branch 'master' into demo_tutorial_lightning_tensor
multiphaseCFD 990d744
apply some comments
multiphaseCFD 01e8bb3
fix typo
multiphaseCFD 2b94e20
update docs
multiphaseCFD 24d703f
update bio
multiphaseCFD ebf0aef
add pic
multiphaseCFD a9c74e7
Trigger CIs
multiphaseCFD 0e8629e
add json file
multiphaseCFD a4f99e4
fix
multiphaseCFD edd01ac
fix metadata for PR preview building
justinpickering 66db566
Merge branch 'master' into demo_tutorial_lightning_tensor
multiphaseCFD 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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,4 @@ | ||
.. bio:: Shuli Shu | ||
:photo: ../_static/authors/shuli_shu.jpg | ||
|
||
Shuli is a senior quantum software developer at Xanadu, where he focuses on accelerating high-performance quantum software platforms with parallel computation and GPUs. He holds a Ph.D in engineering. |
32 changes: 32 additions & 0 deletions
32
demonstrations/tutorial_How_to_simulate_quantum_circuits_with_lightning_tensor.metadata.json
This file contains 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 @@ | ||
{ | ||
"title": "How to simulate quantum circuits with tensor networks with lightning.tensor", | ||
"authors": [ | ||
{ | ||
"username": "sshu" | ||
} | ||
], | ||
"dateOfPublication": "2025-01-10T00:00:00+00:00", | ||
"dateOfLastModification": "2025-01-10T00:00:00+00:00", | ||
"categories": [ | ||
"Getting Started", | ||
"Quantum Computing", | ||
"Devices and Performance", | ||
"How-to" | ||
], | ||
"tags": [ | ||
"how to" | ||
], | ||
"previewImages": [], | ||
"seoDescription": "Learn how to simulate quantum circuits with tensor networks using the lightning.tensor PennyLane device.", | ||
"doi": "", | ||
"references":[], | ||
"basedOnPapers": [], | ||
"referencedByPapers": [], | ||
"relatedContent": [ | ||
{ | ||
"type": "demonstration", | ||
"id": "tutorial_tn_circuits", | ||
"weight": 1.0 | ||
} | ||
] | ||
} |
154 changes: 154 additions & 0 deletions
154
demonstrations/tutorial_How_to_simulate_quantum_circuits_with_lightning_tensor.py
This file contains 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,154 @@ | ||
r"""How to simulate quantum circuits with tensor networks with lightning.tensor | ||
==================================================================== | ||
|
||
:doc:`Tensor networks </demos/tutorial_tn_circuits>` are widely used for the large-scale quantum circuits simulation. With the approximated represention of quantum states, tensor networks can be used to simulate quantum circuits that state-vector based simulators cannot handle. | ||
The ``lightning.tensor`` device is newly added to Pennylane ecosystem as of `v0.39 release <https://pennylane.ai/blog/2024/11/pennylane-release-0.39>`__, as an alternative to the :class:`~pennylane.devices.default_tensor.DefaultTensor` device. The ``lightning.tensor`` is a high-performance | ||
simulator, which can harness the computational power of Nvidia GPUs. This deivce is built on top of the C/C++ APIs offered by the `cutensornet <https://docs.nvidia.com/cuda/cuquantum/latest/cutensornet/index.html>`__ library, a component of the `Nvidia cuQuantum SDK <https://docs.nvidia.com/cuda/cuquantum/latest/index.html>`__. | ||
The ``lightning.tensor`` device is designed to simulate large-scale quantum circuits efficiently. It can handle circuits with 30+ qubits with a manageable degree of entanglement within only a single GPU. Note that ``lightning.tensor`` is still under active development, and further improvements, | ||
new features, and additional tutorials are expected in future releases. | ||
|
||
In this tutorial, we demonstrate how to simulate quantum circuits using the `lightning.tensor` device in PennyLane. The :class:`~pennylane_lightning.lightning_tensor.LightningTensor` device, similar to the :class:`~pennylane.devices.default_tensor.DefaultTensor` device, supports both Matrix Product State (MPS) and Exact Tensor | ||
Network (ExactTN) representations of quantum states. For more information on the MPS and TN methods, we refer to the `Matrix Product States and Tensor Networks </tutorial_How_to_simulate_quantum_circuits_with_tensor_networks>`. | ||
|
||
Check the latest functionality in the :class:`documentation <.pennylane_lightning.lightning_tensor.LightningTensor>` or pick among other `PennyLane devices <https://pennylane.ai/plugins/#built-in-devices>`__ for your project. | ||
|
||
.. figure:: ../_static/demonstration_assets/how_to_simulate_quantum_circuits_with_tensor_networks/TN_MPS.gif | ||
:align: center | ||
:width: 90% | ||
|
||
""" | ||
|
||
###################################################################### | ||
# Simulating a quantum circuit with the MPS method | ||
# ------------------------------------------------ | ||
# | ||
# Let's start by showing how to simulate a quantum circuit using the MPS method. Generally, the MPS method can be used to simulate quantum circuits that are too large for state-vector simulations or too deep (higher degree of entanglement) for the exact TN contract method. | ||
# The circuit we show here is a simple example to demonstrate the features of a MPS method mentioned above. | ||
# | ||
|
||
import pennylane as qml | ||
import numpy as np | ||
|
||
# Define the keyword arguments for the MPS method | ||
kwargs_mps = { | ||
# Maximum bond dimension of the MPS | ||
"max_bond_dim": 128, | ||
# Cutoff parameter for the singular value decomposition | ||
"cutoff": np.finfo(np.complex128).eps, | ||
} | ||
|
||
# Parameters of the quantum circuit | ||
theta = 0.5 | ||
phi = 0.1 | ||
depth = 10 | ||
n = 1011 | ||
num_qubits = 100 | ||
|
||
# Instantiate the device with the MPS method and the specified kwargs | ||
dev = qml.device("lightning.tensor", wires=num_qubits, method="mps", **kwargs_mps) | ||
|
||
|
||
# Define the quantum circuit | ||
@qml.qnode(dev) | ||
def circuit(theta, phi, n, num_qubits): | ||
for _ in range(1, depth - 1): | ||
for qubit in range(num_qubits - 4): | ||
qml.RX(theta, wires=qubit) | ||
qml.CNOT(wires=[qubit, qubit + 1]) | ||
qml.RY(phi, wires=qubit) | ||
qml.DoubleExcitation(theta, wires=[qubit, qubit + 1, qubit + 3, qubit + 4]) | ||
qml.Toffoli(wires=[qubit + 1, qubit + 3, qubit + 4]) | ||
qml.FlipSign(n, wires=range(num_qubits)) | ||
return qml.expval(qml.X(num_qubits - 1) @ qml.Y(num_qubits - 2) @ qml.Z(num_qubits - 3)) | ||
|
||
|
||
###################################################################### | ||
# We set the maximum bond dimension to 128 and the ``cutoff`` parameter is set to the machine epsilon of the ``numpy.complex128`` data type. | ||
# For this circuit, retaining a maximum of 128 singular values in the singular value decomposition is more than enough to represent the quantum state accurately. | ||
# For an explanation of these parameters, we refer to the :class:`documentation <.pennylane_lightning.lightning_tensor.LightningTensor>` of the ``lightning.tensor`` device. | ||
# Please note that the accepted keyword arguments for ``lightning.tensor`` are slightly different from the ``default.tensor`` device. | ||
# | ||
# In general, a circuit run on a ``lightning.tensor`` device could be faster than on a CPU-based ``default.tensor`` device, given a sufficient large bond dimension is used in the | ||
# calculations. The exact performance of those devices depends on the gates in the specific circuit. For example, the ``lightning.tensor`` device natively supports multi-controlled 1-wire target gates, | ||
# such as the `qml.FlipSign` operator, which is widely used in the Grover algorithm. | ||
# | ||
# | ||
|
||
import time | ||
|
||
# Simulate the circuit for different numbers of qubits | ||
print(f"Number of qubits: {num_qubits}") | ||
start_time = time.time() | ||
result = circuit(theta, phi, n, num_qubits) | ||
multiphaseCFD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end_time = time.time() | ||
print(f"Result: {result}") | ||
print(f"Execution time: {end_time - start_time:.4f} seconds") | ||
|
||
###################################################################### | ||
# Unlike ``default.tensor``, the graph contraction operation is not carried out immediately after each gate application. | ||
# Instead, the tensor network is lazily built up, and the contraction is performed only when the final MPS state calcalution is requested. | ||
# | ||
# To learn more about the MPS method and its theoretical background, we refer to the `Default.Tensor Demo<demos/tutorial_How_to_simulate_quantum_circuits_with_tensor_networks>`. | ||
# | ||
|
||
###################################################################### | ||
# Simulating a quantum circuit with the TN method | ||
# ----------------------------------------------- | ||
# | ||
# The TN method fully captures the entanglement among qubits without approximation and is more accurate than the MPS method. While, it might require more computational and memory resources than the MPS method. | ||
# The memory resource required for the TN method is proportional to the number of entangled qubits. Therefore, the TN method is more suitable for simulating shadow quantum circuits with a less degree of entanglement. | ||
# In the following example, we consider a shadow quantum circuit with a configurable depth and less entangled gates. | ||
# | ||
|
||
import pennylane as qml | ||
import numpy as np | ||
import time | ||
|
||
# Parameters of the quantum circuit | ||
theta = 0.5 | ||
phi = 0.1 | ||
depth = 10 | ||
n = 1011 | ||
num_qubits = 100 | ||
|
||
# Instantiate the device with the TN method and the specified kwargs | ||
dev = qml.device("lightning.tensor", wires=num_qubits, method="tn") | ||
|
||
|
||
@qml.qnode(dev) | ||
def circuit(theta, depth, n, num_qubits): | ||
for i in range(num_qubits): | ||
qml.X(wires=i) | ||
for _ in range(1, depth - 1): | ||
for i in range(0, num_qubits, 2): | ||
qml.CNOT(wires=[i, i + 1]) | ||
for i in range(num_qubits % 5): | ||
qml.RZ(theta, wires=i) | ||
for i in range(1, num_qubits - 1, 2): | ||
qml.CZ(wires=[i, i + 1]) | ||
for i in range(num_qubits - 1): | ||
qml.CNOT(wires=[i, (i + 1)]) | ||
return qml.expval(qml.X(num_qubits - 1)) | ||
|
||
|
||
# Simulate the circuit for different numbers of qubits | ||
print(f"Number of qubits: {num_qubits}") | ||
start_time = time.time() | ||
result = circuit(theta, depth, n, num_qubits) | ||
end_time = time.time() | ||
print(f"Result: {result}") | ||
print(f"Execution time: {end_time - start_time:.4f} seconds") | ||
Comment on lines
+103
to
+140
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. The output of this code is the following:
|
||
|
||
###################################################################### | ||
# Here, we lazily attach each gate to the tensor network and only perform the contraction when a measurement call is requested. | ||
# Note that the TN method could be more memory-intensive than the MPS method, as it requires storing the full tensor network. | ||
|
||
###################################################################### | ||
# Conclusion | ||
# ---------- | ||
# In this tutorial, we have shown how to simulate quantum circuits using the ``lightning.tensor`` device in PennyLane. We have demonstrated how to simulate quantum circuits using the MPS and TN methods, which are supported by the ``lightning.tensor`` device. | ||
# Note that the ``lightning.tensor`` device is still under active development, and further improvements, new features, and additional tutorials/demos are expected in future releases. | ||
|
||
# About the author | ||
# ---------------- | ||
# .. include:: ../_static/authors/shuli_shu.txt |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if we want to add new images for LT? @DSGuala @tomlqc