Skip to content

Commit 3c44e04

Browse files
authored
Merge pull request #39 from simsaidan/aidandocstrings
Add executable API docs and Google-style util docstrings.
2 parents f095610 + 0458349 commit 3c44e04

19 files changed

Lines changed: 471 additions & 27 deletions

.github/workflows/deploy-docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
- name: Install documentation dependencies
3030
run: uv sync --locked --group docs --no-dev
3131
- name: Build site
32-
run: uv run mkdocs build
32+
run: uv run mkdocs build --strict
3333
- uses: actions/upload-pages-artifact@v3
3434
with:
3535
path: site

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ hide:
1010

1111
---
1212

13-
Narrative guides and API reference will land here as the library stabilizes.
13+
Start with the [API reference](reference/utils.md) for standalone utilities.
1414

1515
- :material-github:{ .lg .middle } __Source__
1616

docs/javascript/mathjax.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
window.MathJax = {
2+
tex: {
3+
inlineMath: [["\\(", "\\)"]],
4+
displayMath: [["\\[", "\\]"]],
5+
processEscapes: true,
6+
processEnvironments: true,
7+
},
8+
options: {
9+
ignoreHtmlClass: ".*|",
10+
processHtmlClass: "arithmatex",
11+
},
12+
};
13+
14+
document$.subscribe(() => {
15+
MathJax.typesetPromise();
16+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Combined Hamiltonian matrix
2+
3+
Build a full-system Hamiltonian matrix from local and global terms.
4+
5+
::: shadowsim.core.combined_hamiltonian_matrix.combined_hamiltonian_matrix

docs/reference/utils.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Utils
2+
3+
Standalone matrix and helper utilities.
4+
5+
::: shadowsim.utils.hermitian.hermitian
6+
7+
::: shadowsim.utils.unitary.unitary
8+
9+
::: shadowsim.utils.positive_definite.positive_definite
10+
11+
::: shadowsim.utils.positive_semidefinite.positive_semidefinite
12+
13+
::: shadowsim.utils.negative_definite.negative_definite
14+
15+
::: shadowsim.utils.negative_semidefinite.negative_semidefinite
16+
17+
::: shadowsim.utils.indefinite.indefinite
18+
19+
::: shadowsim.utils.tensor.tensor
20+
21+
::: shadowsim.utils.flip_dict.flip_dict
22+
23+
::: shadowsim.utils.next_power_of_two.next_power_of_two

mkdocs.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ theme:
4040

4141
markdown_extensions:
4242
- pymdownx.superfences
43+
- pymdownx.highlight:
44+
anchor_linenums: true
4345
- pymdownx.inlinehilite
4446
- pymdownx.snippets
4547
- admonition
4648
- pymdownx.details
4749
- attr_list
4850
- md_in_html
51+
- pymdownx.arithmatex:
52+
generic: true
4953
- pymdownx.emoji:
5054
emoji_index: !!python/name:material.extensions.emoji.twemoji
5155
emoji_generator: !!python/name:material.extensions.emoji.to_svg
@@ -54,10 +58,36 @@ markdown_extensions:
5458

5559
nav:
5660
- Home: index.md
61+
- API Reference:
62+
- Utils: reference/utils.md
63+
- Combined Hamiltonian matrix: reference/combined_hamiltonian_matrix.md
5764

5865
plugins:
5966
- meta
6067
- search
68+
- markdown-exec
69+
- mkdocstrings:
70+
handlers:
71+
python:
72+
paths:
73+
- .
74+
options:
75+
show_root_heading: true
76+
show_root_full_path: false
77+
show_source: true
78+
show_symbol_type_heading: true
79+
show_symbol_type_toc: true
80+
heading_level: 2
81+
signature_crossrefs: true
82+
separate_signature: true
83+
docstring_style: google
84+
docstring_section_style: list
85+
show_signature_annotations: true
86+
members_order: source
87+
88+
extra_javascript:
89+
- javascript/mathjax.js
90+
- https://unpkg.com/mathjax@3/es5/tex-mml-chtml.js
6191

6292
extra:
6393
social:

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ dev = [
3939
docs = [
4040
"mkdocs>=1.6",
4141
"mkdocs-material>=9.5",
42+
"mkdocstrings[python]>=0.24",
43+
"markdown-exec[ansi]>=1.7",
44+
# Needed so mkdocstrings can format signatures during the docs build.
45+
"ruff==0.16.0",
4246
]
4347
lint = [
4448
"ruff==0.16.0",
@@ -101,6 +105,9 @@ ignore = ["D407", "D203", "D213", "D416", "PLR0912", "PLR0911", "PLR0915", "PLR2
101105
# E203 -- Whitespace before ':'. This rule conflicts with Black's formatting style.
102106
# It was removing definitions with the same name creating conflicts in test_is_stochastic.py
103107

108+
[tool.ruff.lint.pydocstyle]
109+
convention = "google"
110+
104111
[tool.ruff.lint.pylint]
105112
# Simulator constructors take many positional model parameters (default limit is 5).
106113
max-positional-args = 15

shadowsim/core/combined_hamiltonian_matrix.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,40 @@ def combined_hamiltonian_matrix(
1010
hamiltonians: list[Hamiltonian],
1111
num_qubits: int,
1212
) -> np.ndarray:
13-
"""Sum Hamiltonian terms on the full ``num_qubits``-site tensor space.
13+
r"""Sum Hamiltonian terms on the full ``num_qubits``-site tensor space.
1414
1515
``LocalHamiltonian`` terms are embedded with identities on the remaining
1616
sites; full-domain ``Hamiltonian`` matrices are added as-is. All terms must
1717
match a common Hilbert-space dimension (``local_dim ** num_qubits`` for
1818
locals, or the matrix size of bare terms).
1919
20-
Parameter hamiltonians: The list of Hamiltonians to sum.
21-
Precondition: hamiltonians is a list of Hamiltonian objects.
20+
Args:
21+
hamiltonians: Non-empty list of ``Hamiltonian`` objects to sum.
22+
num_qubits: Positive number of sites in the full system.
23+
24+
Returns:
25+
Complex matrix of shape ``(d, d)`` equal to the sum of the (embedded)
26+
terms, where ``d`` is the shared Hilbert-space dimension.
27+
28+
Raises:
29+
ValueError: If ``LocalHamiltonian`` terms use mixed ``local_dim`` values,
30+
or if term dimensions disagree for the requested ``num_qubits``.
31+
AssertionError: If ``hamiltonians`` / ``num_qubits`` fail basic type and
32+
positivity checks.
33+
34+
Examples:
35+
Embed a single-site \(Z\) into a three-qubit chain and inspect the shape:
36+
37+
```python exec="1" source="above" result="text"
38+
import numpy as np
39+
from shadowsim.core import LocalHamiltonian
40+
from shadowsim.core.combined_hamiltonian_matrix import combined_hamiltonian_matrix
41+
42+
local = LocalHamiltonian(np.diag([1.0, -1.0]), sites=[1], local_dim=2)
43+
out = combined_hamiltonian_matrix([local], num_qubits=3)
44+
print(out.shape)
45+
```
2246
23-
Parameter num_qubits: The number of qubits in the full system.
24-
Precondition: num_qubits is a positive integer.
2547
"""
2648
assert isinstance(hamiltonians, list), "hamiltonians must be a list"
2749
assert all(isinstance(h, Hamiltonian) for h in hamiltonians), "all hamiltonians must be Hamiltonian objects"

shadowsim/utils/flip_dict.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
"""Helpers for reversing dictionary keys (bitstring endianness)."""
22

3+
from typing import Any
34

4-
def flip_dict(d):
5+
6+
def flip_dict(d: dict[str, Any]) -> dict[str, Any]:
57
"""Return a new dictionary with each string key reversed.
68
7-
This is used to switch the endianness of measurement results.
9+
Measurement outcomes are often labeled by bitstrings whose endianness
10+
differs between frameworks; reversing the keys converts between those
11+
conventions.
12+
13+
Args:
14+
d: Mapping whose keys are strings (typically measurement bitstrings).
15+
16+
Returns:
17+
A new dictionary with the same values and each key reversed.
18+
19+
Examples:
20+
Flip Qiskit-style bitstring keys:
21+
22+
```python exec="1" source="above" result="text"
23+
from shadowsim.utils.flip_dict import flip_dict
24+
25+
print(flip_dict({"01": 3, "10": 5}))
26+
```
27+
828
"""
929
return {k[::-1]: v for k, v in d.items()}

shadowsim/utils/hermitian.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,37 @@
33
import numpy as np
44

55

6-
def hermitian(H):
7-
"""Return whether the NumPy matrix ``H`` is Hermitian."""
6+
def hermitian(H: np.ndarray) -> bool:
7+
r"""Return whether the NumPy matrix ``H`` is Hermitian.
8+
9+
A square matrix \(H\) is Hermitian when it equals its conjugate transpose,
10+
\(H = H^\dagger\).
11+
12+
Args:
13+
H: Square complex matrix.
14+
15+
Returns:
16+
``True`` if ``H`` is Hermitian up to numerical tolerance, else ``False``.
17+
18+
Examples:
19+
The Pauli \(Y\) matrix is Hermitian:
20+
21+
```python exec="1" source="above" result="text"
22+
import numpy as np
23+
from shadowsim.utils.hermitian import hermitian
24+
25+
Y = np.array([[0.0, -1j], [1j, 0.0]])
26+
print(hermitian(Y))
27+
```
28+
29+
A non-symmetric complex matrix is not:
30+
31+
```python exec="1" source="above" result="text"
32+
import numpy as np
33+
from shadowsim.utils.hermitian import hermitian
34+
35+
print(hermitian(np.array([[1.0 + 2j, 0.0], [0.0, 0.0]])))
36+
```
37+
38+
"""
839
return np.allclose(H, H.conjugate().T)

0 commit comments

Comments
 (0)