tfq.get_sampling_op
Stay organized with collections
Save and categorize content based on your preferences.
Get a Tensorflow op that produces samples from given quantum circuits.
tfq.get_sampling_op(
backend=None,
*,
quantum_concurrent=quantum_context.get_quantum_concurrent_op_mode()
)
This function produces a non-differentiable op that will calculate
batches of circuit samples given tensor batches of cirq.Circuit
s,
parameter values, and a scalar telling the op how many samples to take.
# Simulate circuits with cirq.
my_op = tfq.get_sampling_op(backend=cirq.sim.Simulator())
# Simulate circuits with C++.
my_second_op = tfq.get_sampling_op()
# Prepare some inputs.
qubit = cirq.GridQubit(0, 0)
my_symbol = sympy.Symbol('alpha')
my_circuit_tensor = tfq.convert_to_tensor(
[cirq.Circuit(cirq.X(qubit)**my_symbol)])
my_values = np.array([[2.0]])
n_samples = np.array([10])
# This op can now be run to take samples.
output = my_second_op(
my_circuit_tensor, ['alpha'], my_values, n_samples)
output
<tf.RaggedTensor [[[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]]>
Args |
backend
|
Optional Python object that specifies what backend this op
should use when evaluating circuits. Can be any cirq.Sampler . If
not provided the default C++ sampling op is returned.
|
quantum_concurrent
|
Optional Python bool . True indicates that the
returned op should not block graph level parallelism on itself when
executing. False indicates that graph level parallelism on itself
should be blocked. Defaults to value specified in
tfq.get_quantum_concurrent_op_mode which defaults to True
(no blocking). This flag is only needed for advanced users when
using TFQ for very large simulations, or when running on a real
chip.
|
Returns |
A callable with the following signature:
op(programs, symbol_names, symbol_values, num_samples)
|
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 specified by programs, following the ordering
dictated by symbol_names .
|
num_samples
|
tf.Tensor with one element indicating the number of
samples to draw.
|
Returns
|
tf.Tensor with shape
[batch_size, num_samples, n_qubits] that
holds samples (as boolean values) for each circuit.
|
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.get_sampling_op\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/quantum/blob/v0.7.3/tensorflow_quantum/core/ops/circuit_execution_ops.py#L154-L243) |\n\nGet a Tensorflow op that produces samples from given quantum circuits. \n\n tfq.get_sampling_op(\n backend=None,\n *,\n quantum_concurrent=quantum_context.get_quantum_concurrent_op_mode()\n )\n\nThis function produces a non-differentiable op that will calculate\nbatches of circuit samples given tensor batches of [`cirq.Circuit`](https://quantumai.google/reference/python/cirq/Circuit)s,\nparameter values, and a scalar telling the op how many samples to take. \n\n # Simulate circuits with cirq.\n my_op = tfq.get_sampling_op(backend=cirq.sim.Simulator())\n # Simulate circuits with C++.\n my_second_op = tfq.get_sampling_op()\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(cirq.X(qubit)**my_symbol)])\n my_values = np.array([[2.0]])\n n_samples = np.array([10])\n # This op can now be run to take samples.\n output = my_second_op(\n my_circuit_tensor, ['alpha'], my_values, n_samples)\n output\n \u003ctf.RaggedTensor [[[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]]\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `backend` | Optional Python `object` that specifies what backend this op should use when evaluating circuits. Can be any [`cirq.Sampler`](https://quantumai.google/reference/python/cirq/Sampler). If not provided the default C++ sampling op is returned. |\n| `quantum_concurrent` | Optional Python `bool`. True indicates that the returned op should not block graph level parallelism on itself when executing. False indicates that graph level parallelism on itself should be blocked. Defaults to value specified in [`tfq.get_quantum_concurrent_op_mode`](../tfq/get_quantum_concurrent_op_mode) which defaults to True (no blocking). This flag is only needed for advanced users when using TFQ for very large simulations, or when running on a real chip. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| A `callable` with the following signature: \u003cbr /\u003e `op(programs, symbol_names, symbol_values, num_samples)` ||\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 specified by programs, following the ordering dictated by `symbol_names`. |\n| `num_samples` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) with one element indicating the number of samples to draw. |\n| `Returns` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) with shape \\[batch_size, num_samples, n_qubits\\] that holds samples (as boolean values) for each circuit. |\n\n\u003cbr /\u003e"]]