|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "stim/stabilizers/tableau_sampler.pybind.h" |
| 16 | + |
| 17 | +#include "stim/py/base.pybind.h" |
| 18 | + |
| 19 | +using namespace stim; |
| 20 | +using namespace stim_pybind; |
| 21 | + |
| 22 | +TableauSampler::TableauSampler(size_t num_qubits, std::mt19937_64 &&rng) |
| 23 | + : num_qubits(num_qubits), rng(std::move(rng)) { |
| 24 | +} |
| 25 | + |
| 26 | +Tableau<MAX_BITWORD_WIDTH> TableauSampler::next_tableau() { |
| 27 | + return Tableau<MAX_BITWORD_WIDTH>::random(num_qubits, rng); |
| 28 | +} |
| 29 | + |
| 30 | +std::string TableauSampler::repr() const { |
| 31 | + std::stringstream result; |
| 32 | + result << "stim.TableauSampler(num_qubits="; |
| 33 | + result << num_qubits; |
| 34 | + result << ")"; |
| 35 | + return result.str(); |
| 36 | +} |
| 37 | + |
| 38 | +pybind11::class_<TableauSampler> stim_pybind::pybind_tableau_sampler(pybind11::module &m) { |
| 39 | + return pybind11::class_<TableauSampler>( |
| 40 | + m, |
| 41 | + "TableauSampler", |
| 42 | + clean_doc_string(R"DOC( |
| 43 | + A tool for pseudo-random tableau sampling. |
| 44 | +
|
| 45 | + Seeds the random number generator once at initialization, then |
| 46 | + produces a reproducible sequence of random tableaus via repeated |
| 47 | + calls to `next_tableau()`. |
| 48 | +
|
| 49 | + Examples: |
| 50 | + >>> import stim |
| 51 | + >>> s = stim.TableauSampler(5, seed=42) |
| 52 | + >>> t1 = s.next_tableau() |
| 53 | + >>> t2 = s.next_tableau() |
| 54 | + )DOC") |
| 55 | + .data()); |
| 56 | +} |
| 57 | + |
| 58 | +TableauSampler stim_pybind::py_init_tableau_sampler(size_t num_qubits, const pybind11::object &seed) { |
| 59 | + return TableauSampler(num_qubits, make_py_seeded_rng(seed)); |
| 60 | +} |
| 61 | + |
| 62 | +void stim_pybind::pybind_tableau_sampler_methods( |
| 63 | + pybind11::module &m, pybind11::class_<TableauSampler> &c) { |
| 64 | + c.def( |
| 65 | + pybind11::init(&py_init_tableau_sampler), |
| 66 | + pybind11::arg("num_qubits"), |
| 67 | + pybind11::kw_only(), |
| 68 | + pybind11::arg("seed") = pybind11::none(), |
| 69 | + clean_doc_string(R"DOC( |
| 70 | + Creates a tableau sampler. |
| 71 | +
|
| 72 | + Args: |
| 73 | + num_qubits: The number of qubits each sampled tableau acts on. |
| 74 | + seed: PARTIALLY determines the sequence of sampled tableaus by |
| 75 | + deterministically seeding the random number generator. |
| 76 | +
|
| 77 | + Must be None or an integer in range(2**64). |
| 78 | +
|
| 79 | + Defaults to None. When None, the prng is seeded from system |
| 80 | + entropy. |
| 81 | +
|
| 82 | + When set to an integer, making the exact same series of calls |
| 83 | + on the exact same machine with the exact same version of Stim |
| 84 | + will produce the exact same sequence of tableaus. |
| 85 | +
|
| 86 | + CAUTION: the sequence produced by a specific seed *WILL NOT* |
| 87 | + be consistent between versions of Stim. This restriction is |
| 88 | + present to make it possible to have future optimizations to |
| 89 | + the random sampling, and is enforced by introducing |
| 90 | + intentional differences in the seeding strategy from version |
| 91 | + to version. |
| 92 | +
|
| 93 | + CAUTION: the sequence produced by a specific seed *MAY NOT* |
| 94 | + be consistent across machines that differ in the width of |
| 95 | + supported SIMD instructions. For example, using the same seed |
| 96 | + on a machine that supports AVX instructions and one that only |
| 97 | + supports SSE instructions may produce different sequences. |
| 98 | +
|
| 99 | + Examples: |
| 100 | + >>> import stim |
| 101 | + >>> sampler = stim.TableauSampler(4, seed=12345) |
| 102 | + >>> t = sampler.next_tableau() |
| 103 | + )DOC") |
| 104 | + .data()); |
| 105 | + |
| 106 | + c.def( |
| 107 | + "next_tableau", |
| 108 | + [](TableauSampler &self) { |
| 109 | + return self.next_tableau(); |
| 110 | + }, |
| 111 | + clean_doc_string(R"DOC( |
| 112 | + Samples a uniformly random tableau. |
| 113 | +
|
| 114 | + Returns: |
| 115 | + A uniformly random `stim.Tableau` over the sampler's `num_qubits`. |
| 116 | +
|
| 117 | + Examples: |
| 118 | + >>> import stim |
| 119 | + >>> sampler = stim.TableauSampler(2, seed=42) |
| 120 | + >>> t1 = sampler.next_tableau() |
| 121 | + >>> t2 = sampler.next_tableau() |
| 122 | + )DOC") |
| 123 | + .data()); |
| 124 | + |
| 125 | + c.def( |
| 126 | + "__repr__", |
| 127 | + &TableauSampler::repr, |
| 128 | + "Returns a string representation of the `stim.TableauSampler`."); |
| 129 | +} |
0 commit comments