-
Notifications
You must be signed in to change notification settings - Fork 226
Add optional seed parameter to stim.Tableau.random() #1099
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
xguot
wants to merge
1
commit into
quantumlib:main
Choose a base branch
from
xguot:feature/974-tableau-seed
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
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
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,129 @@ | ||
| // Copyright 2021 Google LLC | ||
| // | ||
| // 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. | ||
|
|
||
| #include "stim/stabilizers/tableau_sampler.pybind.h" | ||
|
|
||
| #include "stim/py/base.pybind.h" | ||
|
|
||
| using namespace stim; | ||
| using namespace stim_pybind; | ||
|
|
||
| TableauSampler::TableauSampler(size_t num_qubits, std::mt19937_64 &&rng) | ||
| : num_qubits(num_qubits), rng(std::move(rng)) { | ||
| } | ||
|
|
||
| Tableau<MAX_BITWORD_WIDTH> TableauSampler::next_tableau() { | ||
| return Tableau<MAX_BITWORD_WIDTH>::random(num_qubits, rng); | ||
| } | ||
|
|
||
| std::string TableauSampler::repr() const { | ||
| std::stringstream result; | ||
| result << "stim.TableauSampler(num_qubits="; | ||
| result << num_qubits; | ||
| result << ")"; | ||
| return result.str(); | ||
| } | ||
|
|
||
| pybind11::class_<TableauSampler> stim_pybind::pybind_tableau_sampler(pybind11::module &m) { | ||
| return pybind11::class_<TableauSampler>( | ||
| m, | ||
| "TableauSampler", | ||
| clean_doc_string(R"DOC( | ||
| A tool for pseudo-random tableau sampling. | ||
|
|
||
| Seeds the random number generator once at initialization, then | ||
| produces a reproducible sequence of random tableaus via repeated | ||
| calls to `next_tableau()`. | ||
|
|
||
| Examples: | ||
| >>> import stim | ||
| >>> s = stim.TableauSampler(5, seed=42) | ||
| >>> t1 = s.next_tableau() | ||
| >>> t2 = s.next_tableau() | ||
| )DOC") | ||
| .data()); | ||
| } | ||
|
|
||
| TableauSampler stim_pybind::py_init_tableau_sampler(size_t num_qubits, const pybind11::object &seed) { | ||
| return TableauSampler(num_qubits, make_py_seeded_rng(seed)); | ||
| } | ||
|
|
||
| void stim_pybind::pybind_tableau_sampler_methods( | ||
| pybind11::module &m, pybind11::class_<TableauSampler> &c) { | ||
| c.def( | ||
| pybind11::init(&py_init_tableau_sampler), | ||
| pybind11::arg("num_qubits"), | ||
| pybind11::kw_only(), | ||
| pybind11::arg("seed") = pybind11::none(), | ||
| clean_doc_string(R"DOC( | ||
| Creates a tableau sampler. | ||
|
|
||
| Args: | ||
| num_qubits: The number of qubits each sampled tableau acts on. | ||
| seed: PARTIALLY determines the sequence of sampled tableaus by | ||
| deterministically seeding the random number generator. | ||
|
|
||
| Must be None or an integer in range(2**64). | ||
|
|
||
| Defaults to None. When None, the prng is seeded from system | ||
| entropy. | ||
|
|
||
| When set to an integer, making the exact same series of calls | ||
| on the exact same machine with the exact same version of Stim | ||
| will produce the exact same sequence of tableaus. | ||
|
|
||
| CAUTION: the sequence produced by a specific seed *WILL NOT* | ||
| be consistent between versions of Stim. This restriction is | ||
| present to make it possible to have future optimizations to | ||
| the random sampling, and is enforced by introducing | ||
| intentional differences in the seeding strategy from version | ||
| to version. | ||
|
|
||
| CAUTION: the sequence produced by a specific seed *MAY NOT* | ||
| be consistent across machines that differ in the width of | ||
| supported SIMD instructions. For example, using the same seed | ||
| on a machine that supports AVX instructions and one that only | ||
| supports SSE instructions may produce different sequences. | ||
|
|
||
| Examples: | ||
| >>> import stim | ||
| >>> sampler = stim.TableauSampler(4, seed=12345) | ||
| >>> t = sampler.next_tableau() | ||
| )DOC") | ||
| .data()); | ||
|
|
||
| c.def( | ||
| "next_tableau", | ||
| [](TableauSampler &self) { | ||
| return self.next_tableau(); | ||
| }, | ||
| clean_doc_string(R"DOC( | ||
| Samples a uniformly random tableau. | ||
|
|
||
| Returns: | ||
| A uniformly random `stim.Tableau` over the sampler's `num_qubits`. | ||
|
|
||
| Examples: | ||
| >>> import stim | ||
| >>> sampler = stim.TableauSampler(2, seed=42) | ||
| >>> t1 = sampler.next_tableau() | ||
| >>> t2 = sampler.next_tableau() | ||
| )DOC") | ||
| .data()); | ||
|
|
||
| c.def( | ||
| "__repr__", | ||
| &TableauSampler::repr, | ||
| "Returns a string representation of the `stim.TableauSampler`."); | ||
| } |
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,41 @@ | ||
| // Copyright 2021 Google LLC | ||
| // | ||
| // 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. | ||
|
|
||
| #ifndef _STIM_STABILIZERS_TABLEAU_SAMPLER_PYBIND_H | ||
| #define _STIM_STABILIZERS_TABLEAU_SAMPLER_PYBIND_H | ||
|
|
||
| #include <pybind11/pybind11.h> | ||
|
|
||
| #include "stim/stabilizers/tableau.h" | ||
|
|
||
| namespace stim_pybind { | ||
|
|
||
| struct TableauSampler { | ||
| size_t num_qubits; | ||
| std::mt19937_64 rng; | ||
| TableauSampler() = delete; | ||
| TableauSampler(const TableauSampler &) = delete; | ||
| TableauSampler(TableauSampler &&) = default; | ||
| TableauSampler(size_t num_qubits, std::mt19937_64 &&rng); | ||
| stim::Tableau<stim::MAX_BITWORD_WIDTH> next_tableau(); | ||
| std::string repr() const; | ||
| }; | ||
|
|
||
| pybind11::class_<TableauSampler> pybind_tableau_sampler(pybind11::module &m); | ||
| void pybind_tableau_sampler_methods(pybind11::module &m, pybind11::class_<TableauSampler> &c); | ||
| TableauSampler py_init_tableau_sampler(size_t num_qubits, const pybind11::object &seed); | ||
|
|
||
| } // namespace stim_pybind | ||
|
|
||
| #endif |
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.
Uh oh!
There was an error while loading. Please reload this page.