Skip to content

Commit

Permalink
Work around pycharm autocomplete bug caused by """ in __doc__ (quantu…
Browse files Browse the repository at this point in the history
…mlib#14)

- Triple-quotes were triggering https://youtrack.jetbrains.com/issue/PY-47605
- Also polish a few more docstrings (add examples, arg names)
  • Loading branch information
Strilanc authored Mar 12, 2021
1 parent 2a1c747 commit 2e6735e
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 68 deletions.
84 changes: 53 additions & 31 deletions src/circuit/circuit.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,29 @@
#include "circuit.pybind.h"

void pybind_circuit(pybind11::module &m) {
pybind11::class_<Circuit>(m, "Circuit", "A mutable stabilizer circuit.")
pybind11::class_<Circuit>(
m,
"Circuit",
R"DOC(
A mutable stabilizer circuit.
Examples:
>>> import stim
>>> c = stim.Circuit()
>>> c.append_operation("X", [0])
>>> c.append_operation("M", [0])
>>> c.compile_sampler().sample(shots=1)
array([[1]], dtype=uint8)
>>> stim.Circuit('''
... H 0
... CNOT 0 1
... M 0 1
... DETECTOR rec[-1] rec[-2]
... ''').compile_detector_sampler().sample(shots=1)
array([[0]], dtype=uint8)
)DOC")
.def(
pybind11::init([](const char *stim_program_text) {
Circuit self;
Expand All @@ -37,21 +59,21 @@ void pybind_circuit(pybind11::module &m) {
Examples:
>>> import stim
>>> empty = stim.Circuit()
>>> not_empty = stim.Circuit("""
>>> not_empty = stim.Circuit('''
... X 0
... CNOT 0 1
... M 1
... """)
... ''')
)DOC")
.def_readonly("num_measurements", &Circuit::num_measurements, R"DOC(
The number of measurement bits produced when sampling from the circuit.
Examples:
>>> import stim
>>> c = stim.Circuit("""
>>> c = stim.Circuit('''
... M 0
... M 0 1
... """)
... ''')
>>> c.num_measurements
3
)DOC")
Expand All @@ -60,15 +82,15 @@ void pybind_circuit(pybind11::module &m) {
Examples:
>>> import stim
>>> c = stim.Circuit("""
>>> c = stim.Circuit('''
... M 0
... M 0 1
... """)
... ''')
>>> c.num_qubits
2
>>> c.append_from_stim_program_text("""
>>> c.append_from_stim_program_text('''
... X 100
... """)
... ''')
>>> c.num_qubits
101
)DOC")
Expand All @@ -82,10 +104,10 @@ void pybind_circuit(pybind11::module &m) {
Examples:
>>> import stim
>>> c = stim.Circuit("""
>>> c = stim.Circuit('''
... X 2
... M 0 1 2
... """)
... ''')
>>> s = c.compile_sampler()
>>> s.sample(shots=1)
array([[0, 0, 1]], dtype=uint8)
Expand All @@ -100,12 +122,12 @@ void pybind_circuit(pybind11::module &m) {
Examples:
>>> import stim
>>> c = stim.Circuit("""
>>> c = stim.Circuit('''
... H 0
... CNOT 0 1
... M 0 1
... DETECTOR rec[-1] rec[-2]
... """)
... ''')
>>> s = c.compile_detector_sampler()
>>> s.sample(shots=1)
array([[0]], dtype=uint8)
Expand All @@ -115,10 +137,10 @@ void pybind_circuit(pybind11::module &m) {
Examples:
>>> import stim
>>> c = stim.Circuit("""
>>> c = stim.Circuit('''
... X 0
... Y 1 2
... """)
... ''')
>>> c.clear()
>>> print(c)
# Circuit [num_qubits=0, num_measurements=0]
Expand All @@ -128,13 +150,13 @@ void pybind_circuit(pybind11::module &m) {
Examples:
>>> import stim
>>> c1 = stim.Circuit("""
>>> c1 = stim.Circuit('''
... X 0
... Y 1 2
... """)
>>> c2 = stim.Circuit("""
... ''')
>>> c2 = stim.Circuit('''
... M 0 1 2
... """)
... ''')
>>> c1 += c2
>>> print(c1)
# Circuit [num_qubits=3, num_measurements=3]
Expand All @@ -157,13 +179,13 @@ void pybind_circuit(pybind11::module &m) {
Examples:
>>> import stim
>>> c1 = stim.Circuit("""
>>> c1 = stim.Circuit('''
... X 0
... Y 1 2
... """)
>>> c2 = stim.Circuit("""
... ''')
>>> c2 = stim.Circuit('''
... M 0 1 2
... """)
... ''')
>>> print(c1 + c2)
# Circuit [num_qubits=3, num_measurements=3]
X 0
Expand All @@ -175,10 +197,10 @@ void pybind_circuit(pybind11::module &m) {
Examples:
>>> import stim
>>> c = stim.Circuit("""
>>> c = stim.Circuit('''
... X 0
... Y 1 2
... """)
... ''')
>>> c *= 3
>>> print(c)
# Circuit [num_qubits=3, num_measurements=0]
Expand All @@ -194,10 +216,10 @@ void pybind_circuit(pybind11::module &m) {
Examples:
>>> import stim
>>> c = stim.Circuit("""
>>> c = stim.Circuit('''
... X 0
... Y 1 2
... """)
... ''')
>>> print(c * 3)
# Circuit [num_qubits=3, num_measurements=0]
X 0
Expand All @@ -214,10 +236,10 @@ void pybind_circuit(pybind11::module &m) {
Examples:
>>> import stim
>>> c = stim.Circuit("""
>>> c = stim.Circuit('''
... X 0
... Y 1 2
... """)
... ''')
>>> print(3 * c)
# Circuit [num_qubits=3, num_measurements=0]
X 0
Expand Down Expand Up @@ -266,13 +288,13 @@ void pybind_circuit(pybind11::module &m) {
Examples:
>>> import stim
>>> c = stim.Circuit()
>>> c.append_from_stim_program_text("""
>>> c.append_from_stim_program_text('''
... H 0 # comment
... CNOT 0 2
...
... M 2
... CNOT rec[-1] 1
... """)
... ''')
>>> print(c)
# Circuit [num_qubits=3, num_measurements=1]
H 0
Expand Down
8 changes: 4 additions & 4 deletions src/py/compiled_measurement_sampler.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ void pybind_compiled_measurement_sampler(pybind11::module &m) {
Examples:
>>> import stim
>>> c = stim.Circuit("""
>>> c = stim.Circuit('''
... X 0 2 3
... M 0 1 2 3
... """)
... ''')
>>> s = c.compile_sampler()
>>> s.sample(shots=1)
array([[1, 0, 1, 1]], dtype=uint8)
Expand All @@ -93,10 +93,10 @@ void pybind_compiled_measurement_sampler(pybind11::module &m) {
Examples:
>>> import stim
>>> c = stim.Circuit("""
>>> c = stim.Circuit('''
... X 0 1 2 3 4 5 6 7 10
... M 0 1 2 3 4 5 6 7 8 9 10
... """)
... ''')
>>> s = c.compile_sampler()
>>> s.sample_bit_packed(shots=1)
array([[255, 4]], dtype=uint8)
Expand Down
Loading

0 comments on commit 2e6735e

Please sign in to comment.