-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit tests for `simulate_one_shot_native_mcm (#6124)
Implement a unit test for `simulate_one_shot_native_mcm` [sc-71560] --------- Co-authored-by: Ali Asadi <10773383+maliasadi@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
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
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
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 @@ | ||
# Copyright 2024 Xanadu Quantum Technologies Inc. | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
Helper functions for testing stochastic processes. | ||
""" | ||
import numpy as np | ||
from scipy.stats import fisher_exact | ||
|
||
|
||
def fisher_exact_test(actual, expected, outcomes=(0, 1), threshold=0.1): | ||
"""Checks that a binary sample matches the expected distribution using the Fisher exact test.""" | ||
|
||
actual, expected = np.asarray(actual), np.asarray(expected) | ||
contingency_table = np.array( | ||
[ | ||
[np.sum(actual == outcomes[0]), np.sum(actual == outcomes[1])], | ||
[np.sum(expected == outcomes[0]), np.sum(expected == outcomes[1])], | ||
] | ||
) | ||
_, p_value = fisher_exact(contingency_table) | ||
assert p_value > threshold, "The sample does not match the expected distribution." |