tfq.noise.sampled_expectation
Stay organized with collections
Save and categorize content based on your preferences.
Estimates (via sampling) expectation values using monte-carlo simulation.
tfq.noise.sampled_expectation(
programs, symbol_names, symbol_values, pauli_sums, num_samples
)
Simulate the final state of programs
given symbol_values
are placed
inside of the symbols with the name in symbol_names
in each circuit.
Channels in this simulation will be "tossed" to a certain realization
during simulation. This simulation is repeated num_samples
times and
bitstring based expectation calculations with the given pauli_sums
are
calculated after each run. Once all the runs are finished, these quantities
are averaged together.
# Prepare some inputs.
qubit = cirq.GridQubit(0, 0)
my_symbol = sympy.Symbol('alpha')
my_circuit_tensor = tfq.convert_to_tensor([
cirq.Circuit(
cirq.H(qubit) ** my_symbol,
cirq.depolarize(0.01)(qubit)
)
])
my_values = np.array([[0.123]])
my_paulis = tfq.convert_to_tensor([[
3.5 * cirq.X(qubit) - 2.2 * cirq.Y(qubit)
]])
my_num_samples = np.array([[100]])
# This op can now be run with:
output = tfq.noise.sampled_expectation(
my_circuit_tensor, ['alpha'], my_values, my_paulis, my_num_samples)
output
tf.Tensor([[0.71530885]], shape=(1, 1), dtype=float32)
In order to make the op differentiable, a tfq.differentiator
object is
needed. see tfq.differentiators
for more details. Below is a simple
example of how to make the from the above code block differentiable:
diff = tfq.differentiators.ForwardDifference()
my_differentiable_op = diff.generate_differentiable_op(
sampled_op=tfq.noise.sampled_expectation
)
Args |
programs
|
tf.Tensor of strings with shape [batch_size] containing
the string representations of the circuits to be executed.
|
symbol_names
|
tf.Tensor of strings with shape [n_params], which
is used to specify the order in which the values in
symbol_values should be placed inside of the circuits in
programs .
|
symbol_values
|
tf.Tensor of real numbers with shape
[batch_size, n_params] specifying parameter values to resolve
into the circuits specificed by programs, following the ordering
dictated by symbol_names .
|
pauli_sums
|
tf.Tensor of strings with shape [batch_size, n_ops]
containing the string representation of the operators that will
be used on all of the circuits in the expectation calculations.
|
num_samples
|
tf.Tensor with num_samples[i][j] is equal to the
number of times programs[i] will be simulated to estimate
pauli_sums[i][j] . Therefore, num_samples must have the same
shape as pauli_sums . Note: internally this quantity can get
rounded up to the nearest multiple of the number of available
threads to TensorFlow. For best performance ensure that the
quantities in num_samples are a multiple of the number of
available threads.
|
Returns |
tf.Tensor with shape [batch_size, n_ops] that holds the
expectation value for each circuit with each op applied to it
(after resolving the corresponding parameters in).
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-05-17 UTC.
[null,null,["Last updated 2024-05-17 UTC."],[],[],null,["# tfq.noise.sampled_expectation\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/quantum/blob/v0.7.3/tensorflow_quantum/core/ops/noise/noisy_sampled_expectation_op.py#L23-L97) |\n\nEstimates (via sampling) expectation values using monte-carlo simulation. \n\n tfq.noise.sampled_expectation(\n programs, symbol_names, symbol_values, pauli_sums, num_samples\n )\n\nSimulate the final state of `programs` given `symbol_values` are placed\ninside of the symbols with the name in `symbol_names` in each circuit.\nChannels in this simulation will be \"tossed\" to a certain realization\nduring simulation. This simulation is repeated `num_samples` times and\nbitstring based expectation calculations with the given `pauli_sums` are\ncalculated after each run. Once all the runs are finished, these quantities\nare averaged together. \n\n # Prepare some inputs.\n qubit = cirq.GridQubit(0, 0)\n my_symbol = sympy.Symbol('alpha')\n my_circuit_tensor = tfq.convert_to_tensor([\n cirq.Circuit(\n cirq.H(qubit) ** my_symbol,\n cirq.depolarize(0.01)(qubit)\n )\n ])\n my_values = np.array([[0.123]])\n my_paulis = tfq.convert_to_tensor([[\n 3.5 * cirq.X(qubit) - 2.2 * cirq.Y(qubit)\n ]])\n my_num_samples = np.array([[100]])\n # This op can now be run with:\n output = tfq.noise.sampled_expectation(\n my_circuit_tensor, ['alpha'], my_values, my_paulis, my_num_samples)\n output\n tf.Tensor([[0.71530885]], shape=(1, 1), dtype=float32)\n\nIn order to make the op differentiable, a `tfq.differentiator` object is\nneeded. see [`tfq.differentiators`](../../tfq/differentiators) for more details. Below is a simple\nexample of how to make the from the above code block differentiable: \n\n diff = tfq.differentiators.ForwardDifference()\n my_differentiable_op = diff.generate_differentiable_op(\n sampled_op=tfq.noise.sampled_expectation\n )\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `programs` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) of strings with shape \\[batch_size\\] containing the string representations of the circuits to be executed. |\n| `symbol_names` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) of strings with shape \\[n_params\\], which is used to specify the order in which the values in `symbol_values` should be placed inside of the circuits in `programs`. |\n| `symbol_values` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) of real numbers with shape \\[batch_size, n_params\\] specifying parameter values to resolve into the circuits specificed by programs, following the ordering dictated by `symbol_names`. |\n| `pauli_sums` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) of strings with shape \\[batch_size, n_ops\\] containing the string representation of the operators that will be used on all of the circuits in the expectation calculations. |\n| `num_samples` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) with `num_samples[i][j]` is equal to the number of times `programs[i]` will be simulated to estimate `pauli_sums[i][j]`. Therefore, `num_samples` must have the same shape as `pauli_sums`. Note: internally this quantity can get rounded up to the nearest multiple of the number of available threads to TensorFlow. For best performance ensure that the quantities in `num_samples` are a multiple of the number of available threads. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) with shape \\[batch_size, n_ops\\] that holds the expectation value for each circuit with each op applied to it (after resolving the corresponding parameters in). ||\n\n\u003cbr /\u003e"]]