Skip to content

Commit 5253f54

Browse files
authored
Document private protocol methods for devsite (#3440)
Fixes #2953. Also adds a preview Dockerfile that is a seed for a potential preview service that could run as a GithubAction, deploy an ephemeral Cloud Run container to preview the generated docs and comment on the PR with the link. An example link for this particular change is: https://api-docs-fijpqdu4wa-uc.a.run.app/ While the preview is not "the real thing" - a fullblown devsite preview is not realistic at this point. This preview gives an indication about the intermediate markdown representation generated by tf-docs markdown generator. It's good for: - latex checking in docstrings - e.g. https://api-docs-fijpqdu4wa-uc.a.run.app/cirq/experiments/two_qubit_state_tomography.md - checking whether the private methods really show up: https://api-docs-fijpqdu4wa-uc.a.run.app/cirq/protocols/SupportsActOn.md
1 parent 70aad19 commit 5253f54

26 files changed

+151
-74
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ __pycache__
2323
_build
2424
rtd_docs/generated
2525

26+
# API docs
27+
docs/api_docs
28+
2629
# swap files
2730
*.swp
2831

check/nbformat

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2929
cd "$(git rev-parse --show-toplevel)"
3030

3131
# Install a pinned version of tf-docs
32-
TF_DOCS_COMMIT=ec2bfadbec885fc40268b4193d8b87648ab45791
32+
TF_DOCS_COMMIT=b3dc8a922d8bdc6e998a8ad4f4953359dc6e576a
3333
TF_DOCS_INSTALLED=$(pip list | grep -c $TF_DOCS_COMMIT)
3434

3535
if [ "$TF_DOCS_INSTALLED" != "1" ]; then

cirq/_doc.py

+30
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,33 @@ def document(value: Any, doc_string: str = ''):
5454
# print(traceback.format_stack(limit=2)[0])
5555
pass
5656
return value
57+
58+
59+
# This is based on
60+
# https://github.com/tensorflow/docs/commit/129e54b1a1dc2c2c82ad94bc81e986c7c2be3d6a#diff-85111596b523b2940651a8856939755c8531d470948895c7133deb6a537bc889R295-R324
61+
62+
_DOC_PRIVATE = "_tf_docs_doc_private"
63+
64+
65+
def doc_private(obj):
66+
"""A decorator: Generates docs for private methods/functions.
67+
68+
For example:
69+
```
70+
class Try:
71+
@doc_private
72+
def _private(self):
73+
...
74+
```
75+
As a rule of thumb, private (beginning with `_`) methods/functions are
76+
not documented. This decorator allows to force document a private
77+
method/function.
78+
79+
Args:
80+
obj: The class-attribute to force the documentation for.
81+
Returns:
82+
obj
83+
"""
84+
85+
setattr(obj, _DOC_PRIVATE, None)
86+
return obj

cirq/experiments/qubit_characterizations.py

+14-22
Original file line numberDiff line numberDiff line change
@@ -445,34 +445,26 @@ def two_qubit_state_tomography(sampler: work.Sampler,
445445
The state probabilities $P_{00}, P_{01}, P_{10}$ thus obtained contribute
446446
to the following linear equations (setting $c_{00} = 1$):
447447
448-
$$
449-
c_{03} + c_{30} + c_{33} = 4*P_{00} - 1
450-
$$
451-
452-
$$
453-
-c_{03} + c_{30} - c_{33} = 4*P_{01} - 1
454-
$$
455-
456-
$$
457-
c_{03} - c_{30} - c_{33} = 4*P_{10} - 1
458-
$$
448+
$$
449+
\begin{align}
450+
c_{03} + c_{30} + c_{33} &= 4*P_{00} - 1 \\
451+
-c_{03} + c_{30} - c_{33} &= 4*P_{01} - 1 \\
452+
c_{03} - c_{30} - c_{33} &= 4*P_{10} - 1
453+
\end{align}
454+
$$
459455
460456
And if a Y/2 rotation is applied to the first qubit and a X/2 rotation
461457
is applied to the second qubit before measurement, the measurement
462458
operators are $(I -/+ \sigma_x) \bigotimes (I +/- \sigma_y)$. The
463459
probabilities obtained instead contribute to the following linear equations:
464460
465-
$$
466-
c_{02} - c_{10} - c_{12} = 4*P_{00} - 1
467-
$$
468-
469-
$$
470-
-c_{02} - c_{10} + c_{12} = 4*P_{01} - 1
471-
$$
472-
473-
$$
474-
c_{02} + c_{10} + c_{12} = 4*P_{10} - 1
475-
$$
461+
$$
462+
\begin{align}
463+
c_{02} - c_{10} - c_{12} &= 4*P_{00} - 1 \\
464+
-c_{02} - c_{10} + c_{12} &= 4*P_{01} - 1 \\
465+
c_{02} + c_{10} + c_{12} &= 4*P_{10} - 1
466+
\end{align}
467+
$$
476468
477469
Note that this set of equations has the same form as the first set under
478470
the transformation $c_{03}$ <-> $c_{02}, c_{30}$ <-> $-c_{10}$ and

cirq/protocols/act_on_protocol.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from typing_extensions import Protocol
1919

20-
from cirq._doc import document
20+
from cirq._doc import doc_private
2121
from cirq.type_workarounds import NotImplementedType
2222

2323
if TYPE_CHECKING:
@@ -27,7 +27,7 @@
2727
class SupportsActOn(Protocol):
2828
"""An object that explicitly specifies how to act on simulator states."""
2929

30-
@document
30+
@doc_private
3131
def _act_on_(self, args: Any) -> Union[NotImplementedType, bool]:
3232
"""Applies an action to the given argument, if it is a supported type.
3333

cirq/protocols/apply_channel_protocol.py

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from typing_extensions import Protocol
2020

2121
from cirq import linalg
22+
from cirq._doc import doc_private
2223
from cirq.protocols.apply_unitary_protocol import (
2324
apply_unitary,
2425
ApplyUnitaryArgs,
@@ -115,6 +116,7 @@ def __init__(self, target_tensor: np.ndarray, out_buffer: np.ndarray,
115116
class SupportsApplyChannel(Protocol):
116117
"""An object that can efficiently implement a channel."""
117118

119+
@doc_private
118120
def _apply_channel_(self, args: ApplyChannelArgs
119121
) -> Union[np.ndarray, None, NotImplementedType]:
120122
"""Efficiently applies a channel.

cirq/protocols/apply_mixture_protocol.py

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from typing_extensions import Protocol
2020

2121
from cirq import linalg
22+
from cirq._doc import doc_private
2223
from cirq.protocols.apply_unitary_protocol import (
2324
apply_unitary,
2425
ApplyUnitaryArgs,
@@ -112,6 +113,7 @@ def __init__(self,
112113
class SupportsApplyMixture(Protocol):
113114
"""An object that can efficiently implement a mixture."""
114115

116+
@doc_private
115117
def _apply_mixture_(self, args: ApplyMixtureArgs
116118
) -> Union[np.ndarray, None, NotImplementedType]:
117119
"""Efficiently applies a mixture.

cirq/protocols/apply_unitary_protocol.py

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from typing_extensions import Protocol
3030

3131
from cirq import linalg, qis
32+
from cirq._doc import doc_private
3233
from cirq.protocols import qid_shape_protocol
3334
from cirq.protocols.decompose_protocol import (
3435
_try_decompose_into_operations_and_qubits,)
@@ -219,6 +220,7 @@ def subspace_index(self,
219220
class SupportsConsistentApplyUnitary(Protocol):
220221
"""An object that can be efficiently left-multiplied into tensors."""
221222

223+
@doc_private
222224
def _apply_unitary_(self, args: ApplyUnitaryArgs
223225
) -> Union[np.ndarray, None, NotImplementedType]:
224226
"""Left-multiplies a unitary effect onto a tensor with good performance.

cirq/protocols/approximate_equality_protocol.py

+3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222

2323
from typing_extensions import Protocol
2424

25+
from cirq._doc import doc_private
26+
2527

2628
class SupportsApproximateEquality(Protocol):
2729
"""Object which can be compared approximately."""
2830

31+
@doc_private
2932
def _approx_eq_(self, other: Any, *, atol: Union[int, float]) -> bool:
3033
"""Approximate comparator.
3134

cirq/protocols/channel.py

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import numpy as np
2121
from typing_extensions import Protocol
2222

23+
from cirq._doc import doc_private
2324
from cirq.protocols.decompose_protocol import (
2425
_try_decompose_into_operations_and_qubits,)
2526
from cirq.protocols.mixture_protocol import has_mixture
@@ -42,6 +43,7 @@
4243
class SupportsChannel(Protocol):
4344
"""An object that may be describable as a quantum channel."""
4445

46+
@doc_private
4547
def _channel_(self) -> Union[Sequence[np.ndarray], NotImplementedType]:
4648
r"""A list of matrices describing the quantum channel.
4749
@@ -76,6 +78,8 @@ def _channel_(self) -> Union[Sequence[np.ndarray], NotImplementedType]:
7678
A list of matrices describing the channel (Kraus operators), or
7779
NotImplemented if there is no such matrix.
7880
"""
81+
82+
@doc_private
7983
def _has_channel_(self) -> bool:
8084
"""Whether this value has a channel representation.
8185

cirq/protocols/circuit_diagram_info_protocol.py

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from typing_extensions import Protocol
2222

2323
from cirq import protocols, value
24+
from cirq._doc import doc_private
2425

2526
if TYPE_CHECKING:
2627
import cirq
@@ -304,6 +305,7 @@ def with_args(self, **kwargs):
304305
class SupportsCircuitDiagramInfo(Protocol):
305306
"""A diagrammable operation on qubits."""
306307

308+
@doc_private
307309
def _circuit_diagram_info_(
308310
self, args: CircuitDiagramInfoArgs
309311
) -> Union[str, Iterable[str], CircuitDiagramInfo]:

cirq/protocols/commutes_protocol.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
from typing import Any, TypeVar, Union
1717

1818
import numpy as np
19-
2019
from typing_extensions import Protocol
2120

2221
from cirq import linalg, ops
23-
from cirq._doc import document
22+
from cirq._doc import doc_private
2423
from cirq.protocols import qid_shape_protocol, unitary_protocol
2524
from cirq.type_workarounds import NotImplementedType
2625

@@ -36,7 +35,7 @@
3635
class SupportsCommutes(Protocol):
3736
"""An object that can determine commutation relationships vs others."""
3837

39-
@document
38+
@doc_private
4039
def _commutes_(self, other: Any,
4140
atol: float) -> Union[None, bool, NotImplementedType]:
4241
r"""Determines if this object commutes with the other object.

cirq/protocols/decompose_protocol.py

+3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
Union,
2828
)
2929
from collections import defaultdict
30+
3031
from typing_extensions import Protocol
3132

3233
from cirq import devices, ops
34+
from cirq._doc import doc_private
3335
from cirq.protocols import qid_shape_protocol
3436
from cirq.type_workarounds import NotImplementedType
3537

@@ -92,6 +94,7 @@ class SupportsDecompose(Protocol):
9294
raise a caller-provided error.
9395
"""
9496

97+
@doc_private
9598
def _decompose_(self) -> DecomposeResult:
9699
pass
97100

cirq/protocols/equal_up_to_global_phase_protocol.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from collections.abc import Iterable
1615
import numbers
16+
from collections.abc import Iterable
1717
from typing import Any, Union
18+
1819
import numpy as np
1920
from typing_extensions import Protocol
2021

2122
from cirq import linalg
22-
from cirq._doc import document
23+
from cirq._doc import doc_private
2324
from cirq.protocols.approximate_equality_protocol import approx_eq
2425

2526

2627
class SupportsEqualUpToGlobalPhase(Protocol):
2728
"""Object which can be compared for equality mod global phase."""
2829

29-
@document
30+
@doc_private
3031
def _equal_up_to_global_phase_(self, other: Any, *,
3132
atol: Union[int, float]) -> bool:
3233
"""Approximate comparator.

cirq/protocols/has_unitary_protocol.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
from typing import (
16-
TYPE_CHECKING,
1716
Any,
1817
TypeVar,
1918
Optional,
@@ -22,23 +21,20 @@
2221
import numpy as np
2322
from typing_extensions import Protocol
2423

25-
from cirq._doc import document
24+
from cirq import qis
25+
from cirq._doc import doc_private
2626
from cirq.protocols import qid_shape_protocol
2727
from cirq.protocols.apply_unitary_protocol import ApplyUnitaryArgs
2828
from cirq.protocols.decompose_protocol import (
2929
_try_decompose_into_operations_and_qubits,)
30-
from cirq import qis
31-
32-
if TYPE_CHECKING:
33-
import cirq
3430

3531
TDefault = TypeVar('TDefault')
3632

3733

3834
class SupportsExplicitHasUnitary(Protocol):
3935
"""An object that explicitly specifies whether it has a unitary effect."""
4036

41-
@document
37+
@doc_private
4238
def _has_unitary_(self) -> bool:
4339
"""Determines whether the receiver has a unitary effect.
4440

cirq/protocols/json_serialization.py

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import sympy
3636
from typing_extensions import Protocol
3737

38+
from cirq._doc import doc_private
3839
from cirq.ops import raw_types # Tells mypy that the raw_types module exists
3940
from cirq.type_workarounds import NotImplementedType
4041

@@ -245,6 +246,7 @@ class SupportsJSON(Protocol):
245246
constructor.
246247
"""
247248

249+
@doc_private
248250
def _json_dict_(self) -> Union[None, NotImplementedType, Dict[Any, Any]]:
249251
pass
250252

cirq/protocols/measurement_key_protocol.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
from typing_extensions import Protocol
1919

20-
from cirq._doc import document
21-
20+
from cirq._doc import doc_private
2221
from cirq.protocols.decompose_protocol import \
2322
_try_decompose_into_operations_and_qubits
2423

25-
2624
# This is a special indicator value used by the inverse method to determine
2725
# whether or not the caller provided a 'default' argument.
2826
RaiseTypeErrorIfNotProvided = ([],) # type: Any
@@ -49,7 +47,7 @@ class SupportsMeasurementKey(Protocol):
4947
conditional on the measurement outcome being $k$.
5048
"""
5149

52-
@document
50+
@doc_private
5351
def _measurement_key_(self) -> str:
5452
"""Return the key that will be used to identify this measurement.
5553
@@ -58,7 +56,7 @@ def _measurement_key_(self) -> str:
5856
will be stored.
5957
"""
6058

61-
@document
59+
@doc_private
6260
def _measurement_keys_(self) -> Iterable[str]:
6361
"""Return the keys for measurements performed by the receiving object.
6462

0 commit comments

Comments
 (0)