-
Notifications
You must be signed in to change notification settings - Fork 165
Add CPU simulators test suite with 100+ initial tests #2905
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
orpuente-MS
wants to merge
9
commits into
main
Choose a base branch
from
oscarpuente/simulators-tests
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6039718
add simulators test infrastructure
orpuente-MS 3ca55d7
add unit tests
orpuente-MS b994e4f
add within-apply
orpuente-MS 3241744
better tests
orpuente-MS c00d226
add tests showing simulator completes all shots
orpuente-MS 83d1904
add noise intrinsics tests
orpuente-MS 0bb8160
add mz tests
orpuente-MS efe9515
add some missing tests
orpuente-MS dc7e2ac
make `num_results` field optional
orpuente-MS 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -10,11 +10,12 @@ mod tests; | |
| use rand::{Rng, SeedableRng, rngs::StdRng}; | ||
|
|
||
| use crate::{ | ||
| ComplexVector, Error, NoisySimulator, SquareMatrix, TOLERANCE, handle_error, | ||
| ComplexVector, Error, NoisySimulator, SquareMatrix, TOLERANCE, eq_with_tolerance, handle_error, | ||
| instrument::Instrument, kernel::apply_kernel, operation::Operation, | ||
| }; | ||
|
|
||
| /// A vector representing the state of a quantum system. | ||
| #[derive(Debug)] | ||
| pub struct StateVector { | ||
| /// Dimension of the vector. | ||
| dimension: usize, | ||
|
|
@@ -26,6 +27,58 @@ pub struct StateVector { | |
| data: ComplexVector, | ||
| } | ||
|
|
||
| impl PartialEq for StateVector { | ||
| /// Compares two state vectors for equality up to a global phase. | ||
| /// | ||
| /// Two state vectors are considered equal if one can be obtained from the other | ||
| /// by multiplying by a complex number of unit magnitude (a global phase factor). | ||
| fn eq(&self, other: &Self) -> bool { | ||
| use num_complex::Complex; | ||
|
|
||
| if self.dimension != other.dimension || self.number_of_qubits != other.number_of_qubits { | ||
| return false; | ||
| } | ||
|
|
||
| if !eq_with_tolerance(self.trace_change, other.trace_change, TOLERANCE) { | ||
| return false; | ||
| } | ||
|
|
||
| // Find the first non-zero element in self to determine the global phase | ||
| let phase = self | ||
| .data | ||
| .iter() | ||
| .zip(other.data.iter()) | ||
| .find(|(a, _)| a.norm() > TOLERANCE) | ||
| .map(|(a, b)| { | ||
| if b.norm() > TOLERANCE { | ||
| // phase = b / a, so self * phase ≈ other | ||
| b / a | ||
| } else { | ||
| // a is non-zero but b is zero - not equal | ||
| Complex::new(f64::NAN, 0.0) | ||
| } | ||
| }); | ||
|
|
||
| match phase { | ||
| Some(phase) if phase.re.is_nan() => false, | ||
| Some(phase) => { | ||
| // Check that the phase has unit magnitude | ||
| if !eq_with_tolerance(phase.norm(), 1.0, TOLERANCE) { | ||
| return false; | ||
| } | ||
| // Check that all elements match after applying the phase | ||
| self.data | ||
| .iter() | ||
| .zip(other.data.iter()) | ||
| .all(|(a, b)| eq_with_tolerance((a * phase - b).norm(), 0.0, TOLERANCE)) | ||
| } | ||
| // The first vector is the zero vector. | ||
| // We return `true` iff the second vector is also the zero vector. | ||
| None => other.data.iter().all(|b| b.norm() <= TOLERANCE), | ||
|
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. Do you normalize? If so, is this even possible to have all values so small? |
||
| } | ||
| } | ||
| } | ||
|
|
||
| impl StateVector { | ||
| fn new(number_of_qubits: usize) -> Self { | ||
| let dimension = 1 << number_of_qubits; | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| mod clifford_noiseless; | ||
| mod clifford_noisy; | ||
| mod full_state_noiseless; | ||
| mod full_state_noisy; | ||
| mod test_utils; | ||
|
|
||
| /// Seed used for reproducible randomness in tests. | ||
| const SEED: u32 = 42; |
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.
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.
Consider finding the largest by norm. May improve precision.