-
Notifications
You must be signed in to change notification settings - Fork 201
docs: add MPS simulator documentation (fixes #1048) #1064
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
Open
Hadar01
wants to merge
12
commits into
quantumlib:main
Choose a base branch
from
Hadar01:docs/mps-simulator
base: main
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.
+169
−1
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1ec5ada
docs: add MPS simulator documentation
Hadar01 cad38db
docs: remove em dashes from mps.md for cleaner prose
Hadar01 c42cbc9
Update docs/mps.md
Hadar01 6e48ecb
Update docs/mps.md
Hadar01 92e55ea
Update docs/mps.md
Hadar01 9c2cbe8
Merge branch 'main' into docs/mps-simulator
Hadar01 82f98b7
Update docs/mps.md
Hadar01 ffe09ec
Update docs/mps.md
Hadar01 4a6867b
Update docs/mps.md
Hadar01 38b6ac7
Improve clarity on matrix product states and limitations
Hadar01 94c526b
Specify 'Singular Value Decomposition' in MPS steps
Hadar01 8a9c8d4
Merge branch 'main' into docs/mps-simulator
Hadar01 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| # The MPS Simulator | ||
|
|
||
| qsim includes a Matrix Product State (MPS) simulator alongside its standard | ||
| state-vector and hybrid Schrödinger-Feynman simulators. While the full | ||
| state-vector simulator (`qsim`) stores the entire quantum state in memory — which | ||
| grows exponentially with the number of qubits — the MPS simulator takes a | ||
| different approach that can be much more memory-efficient for certain kinds of | ||
| circuits. | ||
|
|
||
| ## What is a Matrix Product State? | ||
|
|
||
| A _matrix product state_ is a way of representing a quantum state as a chain of | ||
| tensors, one per qubit, connected together. Instead of storing one | ||
| exponentially-large vector, you store a sequence of small matrices. The | ||
| _bond dimension_ (often written as χ or `bond_dim`) controls how much | ||
| *entanglement* (quantum correlations between qubits) the representation can | ||
| capture: a higher bond dimension is more accurate but uses more memory and takes | ||
| longer to simulate. | ||
|
|
||
| The catch, and the reason MPS is so useful, is that many quantum circuits of | ||
| practical interest don't generate a lot of entanglement. For those circuits, a | ||
| small bond dimension works well, and you can simulate far more qubits than a | ||
| full state-vector simulator could handle. | ||
|
|
||
| The trade-off is that MPS simulation only provides approximate results for | ||
| highly entangled circuits. When a gate creates entanglement that exceeds the | ||
| bond dimension, the simulator truncates it (using Singular Value Decomposition, | ||
| or SVD). For low-entanglement circuits (e.g., 1D nearest-neighbor circuits, | ||
| QAOA with shallow depth), the results are exact or very close to exact. | ||
|
|
||
| ## Where to find the implementation | ||
|
|
||
| The qsim MPS simulation interface lives in two C++ header files: | ||
|
|
||
| - [`lib/mps_simulator.h`](https://github.com/quantumlib/qsim/blob/main/lib/mps_simulator.h) | ||
| — the `MPSSimulator` class, which applies gates to an MPS. | ||
| - [`lib/mps_statespace.h`](https://github.com/quantumlib/qsim/blob/main/lib/mps_statespace.h) | ||
| — the `MPSStateSpace` class, which manages MPS memory, sampling, and inner products. | ||
|
|
||
| Both live in the `qsim::mps` namespace. | ||
|
|
||
| ## Using MPSStateSpace | ||
|
|
||
| `MPSStateSpace` handles everything related to creating and manipulating the MPS | ||
| object itself. | ||
|
|
||
| ### Creating a state | ||
|
|
||
| ```cpp | ||
| // Requires num_qubits >= 2 and bond_dim >= 2. | ||
| auto state = MPSStateSpace<For, float>::Create(num_qubits, bond_dim); | ||
| ``` | ||
|
|
||
| ### Initializing to |0⟩ | ||
|
|
||
| ```cpp | ||
| MPSStateSpace::SetStateZero(state); | ||
| ``` | ||
|
|
||
| ### Copying a state | ||
|
|
||
| ```cpp | ||
| MPSStateSpace::Copy(src_state, dest_state); | ||
| ``` | ||
|
|
||
| ### Computing inner products | ||
|
|
||
| ```cpp | ||
| // Full complex inner product <state1|state2> | ||
| auto ip = MPSStateSpace::InnerProduct(state1, state2); | ||
|
|
||
| // Real part only | ||
| auto rip = MPSStateSpace::RealInnerProduct(state1, state2); | ||
| ``` | ||
|
|
||
| ### Sampling | ||
|
|
||
| ```cpp | ||
| // Draw one sample | ||
| std::vector<bool> sample; | ||
| MPSStateSpace::SampleOnce(state, scratch, scratch2, &rng, &sample); | ||
|
|
||
| // Draw multiple samples | ||
| std::vector<std::vector<bool>> results(num_samples); | ||
| MPSStateSpace::Sample(state, scratch, scratch2, num_samples, seed, &results); | ||
| ``` | ||
|
|
||
| Note that sampling requires two additional scratch MPS objects of the same size | ||
| as the state. These are used as working memory during the sequential sampling | ||
| process. | ||
|
|
||
| ### Reduced density matrices | ||
|
|
||
| You can compute the 2×2 reduced density matrix (1-RDM) for any single qubit: | ||
|
|
||
| ```cpp | ||
| float rdm[8]; // Use double if fp_type is double | ||
| MPSStateSpace::ReduceDensityMatrix(state, scratch, qubit_index, rdm); | ||
| ``` | ||
|
|
||
| ## Using MPSSimulator | ||
|
|
||
| `MPSSimulator` applies quantum gates to an MPS state. | ||
|
|
||
| ### Applying gates | ||
|
|
||
| ```cpp | ||
| MPSSimulator<For, float> sim(/* ForArgs */); | ||
|
|
||
| // Apply a 1-qubit gate | ||
| sim.ApplyGate({qubit_index}, gate_matrix, state); | ||
|
|
||
| // Apply a 2-qubit gate (must be adjacent) | ||
| sim.ApplyGate({qubit_a, qubit_b}, gate_matrix, state); | ||
| ``` | ||
|
|
||
| When a 2-qubit gate is applied, the simulator: | ||
| 1. Contracts the two neighboring MPS tensors into one combined tensor. | ||
| 2. Applies the gate matrix. | ||
| 3. Uses Singular Value Decomposition (SVD) to split the result back into two tensors. | ||
| 4. Keeps only the top `bond_dim` singular values, truncating the rest. | ||
|
|
||
| Step 4, the truncation step, is where approximation happens. If the true quantum | ||
| state needs more entanglement than `bond_dim` allows, some information is lost. | ||
|
|
||
| ## Current limitations | ||
|
|
||
| The MPS simulator is actively developed but not yet complete. As of now: | ||
|
|
||
| - _Only 1-qubit and 2-qubit gates are supported._ Support for 3+ qubit gates | ||
| is not yet implemented (commented placeholders exist in the source). | ||
| - _Controlled gates are not yet implemented._ `ApplyControlledGate` exists but | ||
| is a stub (`// TODO`). | ||
| - _Expectation values are not yet implemented._ `ExpectationValue` currently | ||
| returns a placeholder value of `(-10, -10)`. | ||
| - _No Python interface._ The MPS simulator is currently only accessible through | ||
| C++. It is not yet exposed via the `qsimcirq` Python package. | ||
| - _2-qubit gates must act on adjacent qubits._ Non-adjacent 2-qubit gates | ||
| require SWAP networks (not handled automatically). | ||
|
|
||
| ## When to use MPS vs. state-vector simulation | ||
|
|
||
| | Situation | Recommended simulator | | ||
| |---|---| | ||
| | Circuit has low entanglement (1D, shallow QAOA, etc.) | MPS | | ||
| | You want exact results for any circuit | qsim (state-vector) | | ||
| | Many qubits, low depth | MPS | | ||
| | Deep random circuits | qsim or qsimh | | ||
| | You need a Python interface | qsim or qsimh (via qsimcirq) | | ||
|
|
||
| ## Further reading | ||
|
|
||
| - [Vidal, G. "Efficient Simulation of One-Dimensional Quantum Many-Body Systems"](https://arxiv.org/abs/quant-ph/0310089) | ||
| — the foundational paper on MPS simulation of quantum circuits. | ||
| - [qsim overview](./overview.md) — description of all simulators in qsim. | ||
| - [C++ template reference](./type_reference.md) — description of template | ||
| parameters like `For`, `fp_type`, etc., used throughout qsim including MPS. |
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.
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.
I am not sure if the MPS portion should be referred to as a separate library, but we should defer to @sergeisakov 's opinion in this matter. Sergei, do you think of it as a separate library, or is it really just a part of qsim?