This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Update Simulator Release to check for entanglement #710
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fbae09f
Update Simulator Release to check for entanglement
swernli f4591cc
Bring back old exception as deprecated
swernli 54408f6
Merge remote-tracking branch 'origin/main' into swernli/entanglement-…
swernli 15ac878
Simplify entanglement check
swernli 9063bc4
Updates for PR feedback
swernli 0f467c6
Fix explanation in comment
swernli 5c39a21
Update name of local mutex function.
swernli cc45858
Merge branch 'main' into swernli/entanglement-check
swernli 516bf1a
Merge branch 'main' into swernli/entanglement-check
swernli 905e9f0
Merge remote-tracking branch 'origin/main' into swernli/entanglement-…
swernli e3168e3
Merge branch 'main' into swernli/entanglement-check
swernli 1ffc094
Merge branch 'main' into swernli/entanglement-check
swernli 1dfa5bc
Merge branch 'main' into swernli/entanglement-check
swernli 39108ad
Merge remote-tracking branch 'origin/main' into swernli/entanglement-…
swernli 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
19 changes: 19 additions & 0 deletions
19
src/Simulation/Common/Exceptions/ReleasedQubitsAreEntangled.cs
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,19 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.Quantum.Simulation.Simulators.Exceptions | ||
| { | ||
| public class ReleasedQubitsAreEntangled : Exception | ||
| { | ||
| public ReleasedQubitsAreEntangled() | ||
| : base("Released qubits are entangled.") | ||
| { | ||
| } | ||
| } | ||
| } |
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
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 |
|---|---|---|
|
|
@@ -144,6 +144,63 @@ bool isclassical( | |
| return true; | ||
| } | ||
|
|
||
| template <class T, class A> | ||
|
Contributor
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. A doc comment here would be helpful. The naming "is classical" is a bit confusing to me, since aside from in the simulator, I don't think the choice of basis should matter. It being "in computational basis" seems more accurate to me, but then that would be awfully long as name... |
||
| std::pair<bool, bool> is_classical_or_entangled( | ||
| std::vector<std::complex<T>, A> const& wfn, | ||
| std::size_t q, | ||
| T eps = 100. * std::numeric_limits<T>::epsilon()) | ||
| { | ||
| // Iterate through the wave function using offstet and mask to check for both whether the qubit | ||
| // is classical (in the |0⟩ or |1⟩ state) or is entangled with any other qubit. By checking states | ||
| // where the target qubit is the only difference, the "isclassical" check can speed up the iteration | ||
| // which must check the whole vector. If the target qubit has a nonzero probability of being measured | ||
| // as |0⟩ (variable "have0") AND nonzero probability of being measured as |1⟩ (variable "have1") then | ||
| // we know it is not classical with regard to the computational basis. Further, if the target qubit | ||
| // is not classical with regard to the computational basis AND for any two states where the only difference | ||
| // in the states is the target qubit (ie: |101⟩ and |100⟩ for qubit 0) those have nonzero probability, then | ||
| // the qubit cannot be entangled. | ||
| std::size_t offset = 1ull << q; | ||
| bool have0 = false; | ||
| bool have1 = false; | ||
| bool notentangled = false; | ||
|
|
||
| std::size_t maski = ~(offset - 1); | ||
| // When iterating below, the entry in the wave function with index `i + j` and the entry with index | ||
| // `i + j + offset` represent two states where the only difference is the measured value of the target | ||
| // qubit. For example, if the target qubit has id 2 (offset = 4) in wave function of size 4, then for | ||
| // i = 0 and j = 0 the two states checked are 0 and 4, or |0000⟩ and |0100⟩. When i = 8 and j = 1, the | ||
| // two states are 9 and 13, or |1001⟩ and |1101⟩. | ||
| #ifndef _MSC_VER | ||
| #pragma omp parallel for schedule(static) reduction(|| : have0, have1, notentangled) | ||
| for (std::intptr_t i = 0; i < static_cast<std::intptr_t>(wfn.size()); i += 2 * offset) | ||
| for (std::intptr_t j = 0; j < static_cast<std::intptr_t>(offset); ++j) | ||
| { | ||
| bool has0 = std::norm(wfn[i + j]) >= eps; | ||
| bool has1 = std::norm(wfn[i + j + offset]) >= eps; | ||
| have0 = have0 || has0; | ||
| have1 = have1 || has1; | ||
| notentangled = notentangled || (has0 && has1); | ||
| } | ||
swernli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #else | ||
| #pragma omp parallel for schedule(static) reduction(|| : have0, have1, notentangled) | ||
| for (std::intptr_t l = 0; l < static_cast<std::intptr_t>(wfn.size()) / 2; l++) | ||
| { | ||
| std::intptr_t j = l % offset; | ||
| std::intptr_t i = ((l & maski) << 1); | ||
| bool has0 = std::norm(wfn[i + j]) >= eps; | ||
| bool has1 = std::norm(wfn[i + j + offset]) >= eps; | ||
| have0 = have0 || has0; | ||
| have1 = have1 || has1; | ||
| notentangled = notentangled || (has0 && has1); | ||
| } | ||
| #endif | ||
|
|
||
| // isclassical = true IFF have0 XOR have1 | ||
| // isentangled = true IFF have0 AND have1 AND for all pairs of states where only the target qubit | ||
| // differs one of those states has zero probability. | ||
| return std::make_pair<bool, bool>(have0 ^ have1, have0 && have1 && !notentangled); | ||
| } | ||
swernli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| template <class T, class A> | ||
| double jointprobability(std::vector<T, A> const& wfn, std::vector<unsigned> const& qs, bool val = true) | ||
| { | ||
|
|
||
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.
Do you want to refer to the ReleasedQubitsAreEntangled exception?