Skip to content

Commit 07c857e

Browse files
authored
Merge branch 'master' into fix-mathjax-docs
2 parents 9205d42 + 65ec707 commit 07c857e

File tree

2 files changed

+83
-13
lines changed

2 files changed

+83
-13
lines changed

tensorflow_quantum/python/layers/circuit_executors/expectation.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@
3030
class Expectation(tf.keras.layers.Layer):
3131
"""A Layer that calculates an expectation value.
3232
33+
Args:
34+
backend (Union[str, cirq.sim.simulator.SimulatesExpectationValues],
35+
optional):
36+
Backend simulator to use. Default is 'noiseless'.
37+
differentiator (Optional[tfq.differentiators.Differentiator]):
38+
Differentiation scheme for gradients.
39+
**kwargs: Additional keyword arguments for the parent class.
40+
41+
Input shape:
42+
- circuits: tf.Tensor or list of shape [batch_size], each entry a
43+
serialized circuit (from tfq.convert_to_tensor).
44+
- symbol_names: list or tf.Tensor of shape [n_symbols], names of
45+
circuit parameters (optional).
46+
- symbol_values: tf.Tensor of shape [batch_size, n_symbols], values for
47+
circuit parameters (optional).
48+
- operators: list or tf.Tensor of shape [n_ops], observables to measure
49+
(optional).
50+
- repetitions: int or tf.Tensor, number of measurement repetitions
51+
(optional, only for noisy backend).
52+
53+
Output shape:
54+
tf.Tensor of shape [batch_size, n_ops], expectation values for each
55+
circuit and operator.
56+
3357
Given an input circuit and set of parameter values, prepare a quantum state
3458
and output expectation values taken on that state with respect to some
3559
observables to the tensorflow graph.
@@ -199,10 +223,9 @@ class Expectation(tf.keras.layers.Layer):
199223
something like `tfq.layers.Expectation()(cirq.Circuit(...), ...)` please
200224
be sure to instead use `tfq.layers.Expectation()(circuit_input, ...)`
201225
where `circuit_input` is a `tf.keras.Input` that is filled with
202-
`tfq.conver_to_tensor([cirq.Circuit(..)] * batch_size)` at runtime. This
226+
`tfq.convert_to_tensor([cirq.Circuit(..)] * batch_size)` at runtime. This
203227
is because compiled Keras models require non keyword layer `call` inputs to
204228
be traceable back to a `tf.keras.Input`.
205-
206229
"""
207230

208231
def __init__(self, backend='noiseless', differentiator=None, **kwargs):
@@ -225,6 +248,7 @@ def __init__(self, backend='noiseless', differentiator=None, **kwargs):
225248
which uses `tfq.differentiators.ParameterShift()`. If
226249
`backend` is also 'noiseless' then default is
227250
`tfq.differentiators.Adjoint`.
251+
**kwargs: Additional keyword arguments for the parent class.
228252
229253
"""
230254
super().__init__(**kwargs)
@@ -273,15 +297,23 @@ def call(self,
273297
initializer=tf.keras.initializers.RandomUniform(0, 2 * np.pi)):
274298
"""Keras call function.
275299
276-
Input options:
277-
`inputs`, `symbol_names`, `symbol_values`:
278-
see `input_checks.expand_circuits`
279-
`operators`: see `input_checks.expand_operators`
280-
281-
Output shape:
282-
`tf.Tensor` with shape [batch_size, n_ops] that holds the
283-
expectation value for each circuit with each op applied to it
284-
(after resolving the corresponding parameters in).
300+
Args:
301+
inputs (tf.Tensor or list): Circuits to execute, shape [batch_size].
302+
symbol_names (list or tf.Tensor, optional): Names of circuit
303+
parameters, shape [n_symbols].
304+
symbol_values (tf.Tensor, optional): Values for circuit parameters,
305+
shape [batch_size, n_symbols].
306+
operators (list or tf.Tensor, optional): Observables to measure,
307+
shape [n_ops].
308+
repetitions (int or tf.Tensor, optional): Number of measurement
309+
repetitions (for noisy backend).
310+
initializer (tf.keras.initializers.Initializer, optional):
311+
Initializer for circuit parameters.
312+
313+
Returns:
314+
tf.Tensor: Tensor of shape [batch_size, n_ops] that holds the
315+
expectation value for each circuit with each op applied to it
316+
(after resolving the corresponding parameters in).
285317
"""
286318
values_empty = False
287319
if symbol_values is None:

tensorflow_quantum/python/layers/high_level/pqc.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,35 @@
2828
class PQC(tf.keras.layers.Layer):
2929
"""Parametrized Quantum Circuit (PQC) Layer.
3030
31+
Args:
32+
model_circuit (cirq.Circuit):
33+
Circuit with sympy.Symbols to be trained.
34+
operators (Union[cirq.PauliSum, cirq.PauliString, list]):
35+
Observable(s) to measure.
36+
repetitions (Optional[int]):
37+
Number of measurement repetitions. If None, analytic
38+
expectation is used.
39+
backend (Union[str, cirq.Sampler,
40+
cirq.sim.simulator.SimulatesExpectationValues]):
41+
Backend simulator.
42+
differentiator (Optional[tfq.differentiators.Differentiator]):
43+
Differentiation scheme.
44+
initializer (tf.keras.initializers.Initializer):
45+
Initializer for circuit parameters.
46+
regularizer (Optional[tf.keras.regularizers.Regularizer]):
47+
Regularizer for circuit parameters.
48+
constraint (Optional[tf.keras.constraints.Constraint]):
49+
Constraint for circuit parameters.
50+
**kwargs: Additional keyword arguments for the parent class.
51+
52+
Input shape:
53+
tf.Tensor of shape [batch_size], each entry a serialized circuit
54+
(from tfq.convert_to_tensor).
55+
56+
Output shape:
57+
tf.Tensor of shape [batch_size, n_operators], expectation values
58+
for each operator.
59+
3160
This layer is for training parameterized quantum models.
3261
Given a parameterized circuit, this layer initializes the parameters
3362
and manages them in a Keras native way.
@@ -146,7 +175,7 @@ def __init__(
146175
"""Instantiate this layer.
147176
148177
Create a layer that will output expectation values of the given
149-
operators when fed quantum data to it's input layer. This layer will
178+
operators when fed quantum data to its input layer. This layer will
150179
accept one input tensor representing a quantum data source (these
151180
circuits must not contain any symbols) and append the model_circuit to
152181
them, execute them and then finally output the expectation values.
@@ -293,7 +322,16 @@ def build(self, input_shape):
293322
super().build(input_shape)
294323

295324
def call(self, inputs):
296-
"""Keras call function."""
325+
"""Keras call function.
326+
327+
Args:
328+
inputs (tf.Tensor): Tensor of shape [batch_size], each entry a
329+
serialized circuit (from tfq.convert_to_tensor).
330+
331+
Returns:
332+
tf.Tensor: Tensor of shape [batch_size, n_operators],
333+
expectation values for each operator.
334+
"""
297335
circuit_batch_dim = tf.gather(tf.shape(inputs), 0)
298336
tiled_up_model = tf.tile(self._model_circuit, [circuit_batch_dim])
299337
model_appended = self._append_layer(inputs, append=tiled_up_model)

0 commit comments

Comments
 (0)