Skip to content

Commit 1285fa2

Browse files
add some docstring
1 parent cbaaa46 commit 1285fa2

File tree

8 files changed

+156
-2
lines changed

8 files changed

+156
-2
lines changed
File renamed without changes.

examples/vqnhe_h6.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
tc.set_backend("tensorflow")
1212
tc.set_dtype("complex128")
1313

14-
h6h = np.load("./H6_hamiltonian.npy") # reported in 0.99 A
14+
h6h = np.load("./h6_hamiltonian.npy") # reported in 0.99 A
1515
hamiltonian = construct_matrix_v3(h6h.tolist())
1616

1717

tensorcircuit/circuit.py

+9
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ def unitary_kraus(
912912
) -> Tensor:
913913
"""
914914
Apply unitary gates in ``kraus`` randomly based on corresponding ``prob``.
915+
If ``prob`` is ``None``, this is reduced to kraus channel language.
915916
916917
:param kraus: List of ``tc.gates.Gate`` or just Tensors
917918
:type kraus: Sequence[Gate]
@@ -1547,6 +1548,14 @@ def _expectation_ps(
15471548
Shortcut for Pauli string expectation.
15481549
x, y, z list are for X, Y, Z positions
15491550
1551+
:Example:
1552+
1553+
>>> c = tc.Circuit(2)
1554+
>>> c.X(0)
1555+
>>> c.H(1)
1556+
>>> c.expectation_ps(x=[1], z=[0])
1557+
array(-0.99999994+0.j, dtype=complex64)
1558+
15501559
:param x: _description_, defaults to None
15511560
:type x: Optional[Sequence[int]], optional
15521561
:param y: _description_, defaults to None

tensorcircuit/densitymatrix.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ def __init__(
2727
inputs: Optional[Tensor] = None,
2828
dminputs: Optional[Tensor] = None,
2929
) -> None:
30+
"""
31+
The density matrix simulator based on tensornetwork engine.
32+
33+
:param nqubits: Number of qubits
34+
:type nqubits: int
35+
:param empty: if True, nothing initialized, only for internal use, defaults to False
36+
:type empty: bool, optional
37+
:param inputs: the state input for the circuit, defaults to None
38+
:type inputs: Optional[Tensor], optional
39+
:param dminputs: the density matrix input for the circuit, defaults to None
40+
:type dminputs: Optional[Tensor], optional
41+
"""
3042
if not empty:
3143
_prefix = "qb-"
3244
if (inputs is None) and (dminputs is None):
@@ -239,7 +251,17 @@ def apply(self: "DMCircuit", *index: int, **vars: float) -> None:
239251

240252
return apply
241253

242-
def densitymatrix(self, check: bool = False, reuse: bool = True) -> tn.Node.tensor:
254+
def densitymatrix(self, check: bool = False, reuse: bool = True) -> Tensor:
255+
"""
256+
Return the output density matrix of the circuit.
257+
258+
:param check: check whether the final return is a legal density matrix, defaults to False
259+
:type check: bool, optional
260+
:param reuse: whether to reuse previous results, defaults to True
261+
:type reuse: bool, optional
262+
:return: The output densitymatrix in 2D shape tensor form
263+
:rtype: Tensor
264+
"""
243265
nodes, _ = self._copy_dm_tensor(conj=False, reuse=reuse)
244266
# t = contractor(nodes, output_edge_order=d_edges)
245267
dm = backend.reshape(
@@ -254,6 +276,15 @@ def densitymatrix(self, check: bool = False, reuse: bool = True) -> tn.Node.tens
254276
def expectation(
255277
self, *ops: Tuple[tn.Node, List[int]], **kws: Any
256278
) -> tn.Node.tensor:
279+
"""
280+
Compute the expectation of corresponding operators.
281+
282+
:param ops: Operator and its position on the circuit,
283+
eg. ``(tc.gates.z(), [1, ]), (tc.gates.x(), [2, ])`` is for operator :math:`Z_1X_2`.
284+
:type ops: Tuple[tn.Node, List[int]]
285+
:return: Tensor with one element
286+
:rtype: Tensor
287+
"""
257288
# kws is reserved for unsupported feature such as reuse arg
258289
newdm, newdang = self._copy(self._nodes, self._rfront + self._lfront)
259290
occupied = set()

tensorcircuit/quantum.py

+48
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,18 @@ def PauliStringSum2Dense(
11191119
weight: Optional[Sequence[float]] = None,
11201120
numpy: bool = False,
11211121
) -> Tensor:
1122+
"""
1123+
Generate tensorflow dense matrix from Pauli string sum
1124+
1125+
:param ls: 2D Tensor, each row is for a Pauli string,
1126+
e.g. [1, 0, 0, 3, 2] is for :math:`X_0Z_3Y_4`
1127+
:type ls: Sequence[Sequence[int]]
1128+
:param weight: 1D Tensor, each element corresponds the weight for each Pauli string
1129+
defaults to None (all Pauli strings weight 1.0)
1130+
:type weight: Optional[Sequence[float]], optional
1131+
:return: the tensorflow dense matrix
1132+
:rtype: Tensor
1133+
"""
11221134
sparsem = PauliStringSum2COO_numpy(ls, weight)
11231135
if numpy:
11241136
return sparsem.todense()
@@ -1143,6 +1155,18 @@ def _numpy2tf_sparse(a: Tensor) -> Tensor:
11431155
def PauliStringSum2COO_numpy(
11441156
ls: Sequence[Sequence[int]], weight: Optional[Sequence[float]] = None
11451157
) -> Tensor:
1158+
"""
1159+
Generate scipy sparse matrix from Pauli string sum
1160+
1161+
:param ls: 2D Tensor, each row is for a Pauli string,
1162+
e.g. [1, 0, 0, 3, 2] is for :math:`X_0Z_3Y_4`
1163+
:type ls: Sequence[Sequence[int]]
1164+
:param weight: 1D Tensor, each element corresponds the weight for each Pauli string
1165+
defaults to None (all Pauli strings weight 1.0)
1166+
:type weight: Optional[Sequence[float]], optional
1167+
:return: the scipy coo sparse matrix
1168+
:rtype: Tensor
1169+
"""
11461170
# numpy version is 3* faster!
11471171

11481172
nterms = len(ls)
@@ -1165,6 +1189,18 @@ def PauliStringSum2COO_numpy(
11651189
def PauliStringSum2COO(
11661190
ls: Sequence[Sequence[int]], weight: Optional[Sequence[float]] = None
11671191
) -> Tensor:
1192+
"""
1193+
Generate tensorflow sparse matrix from Pauli string sum
1194+
1195+
:param ls: 2D Tensor, each row is for a Pauli string,
1196+
e.g. [1, 0, 0, 3, 2] is for :math:`X_0Z_3Y_4`
1197+
:type ls: Sequence[Sequence[int]]
1198+
:param weight: 1D Tensor, each element corresponds the weight for each Pauli string
1199+
defaults to None (all Pauli strings weight 1.0)
1200+
:type weight: Optional[Sequence[float]], optional
1201+
:return: the tensorflow coo sparse matrix
1202+
:rtype: Tensor
1203+
"""
11681204
nterms = len(ls)
11691205
n = len(ls[0])
11701206
s = 0b1 << n
@@ -1184,6 +1220,18 @@ def PauliStringSum2COO(
11841220

11851221
@compiled_jit
11861222
def PauliString2COO(l: Sequence[int], weight: Optional[float] = None) -> Tensor:
1223+
"""
1224+
Generate tensorflow sparse matrix from Pauli string sum
1225+
1226+
:param l: 1D Tensor representing for a Pauli string,
1227+
e.g. [1, 0, 0, 3, 2] is for :math:`X_0Z_3Y_4`
1228+
:type l: Sequence[int]
1229+
:param weight: the weight for the Pauli string
1230+
defaults to None (all Pauli strings weight 1.0)
1231+
:type weight: Optional[float], optional
1232+
:return: the tensorflow sparse matrix
1233+
:rtype: Tensor
1234+
"""
11871235
n = len(l)
11881236
one = tf.constant(0b1, dtype=tf.int64)
11891237
idx_x = tf.constant(0b0, dtype=tf.int64)

tensorcircuit/templates/blocks.py

+27
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020

2121

2222
def state_centric(f: Callable[..., Circuit]) -> Callable[..., Tensor]:
23+
"""
24+
Function decorator wraps the function with the first input and output in the format of circuit,
25+
the wrapped function has the first input and the output as the state tensor.
26+
27+
:param f: Function with the fist input and the output as ``Circuit`` object.
28+
:type f: Callable[..., Circuit]
29+
:return: Wrapped function with the first input and the output as the state tensor correspondingly.
30+
:rtype: Callable[..., Tensor]
31+
"""
32+
2333
@wraps(f)
2434
def wrapper(s: Tensor, *args: Any, **kws: Any) -> Tensor:
2535
n = backend.sizen(s)
@@ -97,6 +107,23 @@ def QAOA_block(
97107
def example_block(
98108
c: Circuit, param: Tensor, nlayers: int = 2, is_split: bool = False
99109
) -> Circuit:
110+
r"""
111+
The circuit ansatz is firstly one layer of Hadamard gates and
112+
then we have ``nlayers`` blocks of :math:`e^{i\theta Z_iZ_{i+1}}` two-qubit gate in ladder layout,
113+
following rx gate.
114+
115+
:param c: The circuit
116+
:type c: Circuit
117+
:param param: paramter tensor with 2*nlayer*n elements
118+
:type param: Tensor
119+
:param nlayers: number of ZZ+RX blocks, defaults to 2
120+
:type nlayers: int, optional
121+
:param is_split: whether use SVD split to reduce ZZ gate bond dimension,
122+
defaults to False
123+
:type is_split: bool, optional
124+
:return: The circuit with example ansatz attached
125+
:rtype: Circuit
126+
"""
100127
# used for test and demonstrations
101128
if is_split:
102129
split_conf = {

tensorcircuit/templates/dataset.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def amplitude_encoding(
1616
fig: Tensor, nqubits: int, index: Optional[Sequence[int]] = None
1717
) -> Tensor:
1818
# non-batch version
19+
# [WIP]
1920
fig = backend.reshape(fig, shape=[-1])
2021
norm = backend.norm(fig)
2122
fig = fig / norm

tensorcircuit/templates/graphs.py

+38
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,18 @@ def Even1D(n: int, s: int = 0) -> Graph:
5959

6060

6161
class Grid2DCoord:
62+
"""
63+
Two-dimensional grid lattice
64+
"""
65+
6266
def __init__(self, n: int, m: int):
67+
"""
68+
69+
:param n: number of rows
70+
:type n: int
71+
:param m: number of cols
72+
:type m: int
73+
"""
6374
# row first
6475
self.m = m
6576
self.n = n
@@ -74,6 +85,15 @@ def two2one(self, x: int, y: int) -> int:
7485
return x * self.n + y
7586

7687
def all_rows(self, pbc: bool = False) -> Sequence[Tuple[int, int]]:
88+
"""
89+
return all row edge with 1d index encoding
90+
91+
:param pbc: whether to include pbc edges (periodic boundary condition),
92+
defaults to False
93+
:type pbc: bool, optional
94+
:return: list of row edge
95+
:rtype: Sequence[Tuple[int, int]]
96+
"""
7797
r = []
7898
for i in range(self.mn):
7999
if (i + 1) % self.n != 0:
@@ -83,6 +103,15 @@ def all_rows(self, pbc: bool = False) -> Sequence[Tuple[int, int]]:
83103
return r
84104

85105
def all_cols(self, pbc: bool = False) -> Sequence[Tuple[int, int]]:
106+
"""
107+
return all col edge with 1d index encoding
108+
109+
:param pbc: whether to include pbc edges (periodic boundary condition),
110+
defaults to False
111+
:type pbc: bool, optional
112+
:return: list of col edge
113+
:rtype: Sequence[Tuple[int, int]]
114+
"""
86115
r = []
87116
for i in range(self.mn):
88117
if i + self.n < self.mn:
@@ -92,6 +121,15 @@ def all_cols(self, pbc: bool = False) -> Sequence[Tuple[int, int]]:
92121
return r
93122

94123
def lattice_graph(self, pbc: bool = True) -> Graph:
124+
"""
125+
Get the 2D grid lattice in ``nx.Graph`` format
126+
127+
:param pbc: whether to include pbc edges (periodic boundary condition),
128+
defaults to True
129+
:type pbc: bool, optional
130+
:return: _description_
131+
:rtype: Graph
132+
"""
95133
g = nx.Graph()
96134
for i in range(self.mn):
97135
g.add_node(i, weight=0)

0 commit comments

Comments
 (0)