Skip to content

Commit

Permalink
Adds tfq.noise.samples op.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBroughton committed Mar 25, 2021
1 parent 6a4abdb commit 02c2d55
Show file tree
Hide file tree
Showing 14 changed files with 853 additions and 16 deletions.
1 change: 1 addition & 0 deletions release/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ sh_binary(
"//tensorflow_quantum/core/ops:tfq_utility_ops_py",
"//tensorflow_quantum/core/ops:tfq_simulate_ops_py",
"//tensorflow_quantum/core/ops/math_ops:inner_product_op_py",
"//tensorflow_quantum/core/ops/noise:noisy_samples_op_py",
"//tensorflow_quantum/core/ops/noise:noisy_expectation_op_py",
"//tensorflow_quantum/core/serialize:serializer",
"//tensorflow_quantum/datasets:cluster_state",
Expand Down
1 change: 1 addition & 0 deletions scripts/import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_imports():

# Noisy simulation ops.
_ = tfq.noise.expectation
_ = tfq.noise.samples

# Util functions.
_ = tfq.convert_to_tensor
Expand Down
23 changes: 23 additions & 0 deletions tensorflow_quantum/core/ops/noise/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cc_binary(
name = "_tfq_noise_ops.so",
srcs = [
"tfq_noisy_expectation.cc",
"tfq_noisy_samples.cc"
],
copts = select({
":windows": [
Expand Down Expand Up @@ -83,3 +84,25 @@ py_test(
"//tensorflow_quantum/python:util",
],
)

py_library(
name = "noisy_samples_op_py",
srcs = ["noisy_samples_op.py"],
data = [":_tfq_noise_ops.so"],
deps = [
"//tensorflow_quantum/core/ops:load_module",
"//tensorflow_quantum/core/ops:tfq_utility_ops_py",
],
)

py_test(
name = "noisy_samples_op_test",
srcs = ["noisy_samples_op_test.py"],
python_version = "PY3",
deps = [
":noisy_samples_op_py",
"//tensorflow_quantum/core/ops:batch_util",
"//tensorflow_quantum/python:util",
],
)

1 change: 1 addition & 0 deletions tensorflow_quantum/core/ops/noise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
"""Module for tfq.core.ops.noise.*"""

from tensorflow_quantum.core.ops.noise.noisy_expectation_op import expectation
from tensorflow_quantum.core.ops.noise.noisy_samples_op import samples
71 changes: 71 additions & 0 deletions tensorflow_quantum/core/ops/noise/noisy_samples_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Module for high performance noisy circuit sampling ops"""
import os
import tensorflow as tf
from tensorflow_quantum.core.ops import tfq_utility_ops
from tensorflow_quantum.core.ops.load_module import load_module

NOISY_OP_MODULE = load_module(os.path.join("noise", "_tfq_noise_ops.so"))


def samples(programs, symbol_names, symbol_values, num_samples):
"""Generate samples using the C++ noisy trajectory simulator.
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. After each simulation is a run a single bitstring
will be drawn. These simulations are repeated `num_samples` times.
>>> # Sample a noisy circuit with C++.
>>> qubit = cirq.GridQubit(0, 0)
>>> my_symbol = sympy.Symbol('alpha')
>>> my_circuit_tensor = tfq.convert_to_tensor([
... cirq.Circuit(
... cirq.X(qubit) ** my_symbol,
... cirq.depolarize(0.01)(qubit)
... )
... ])
>>> my_values = np.array([[0.123]])
>>> my_num_samples = np.array([100])
>>> # This op can now be run with:
>>> output = tfq.noise.samples(
... my_circuit_tensor, ['alpha'], my_values, my_num_samples)
>>> output
<tf.RaggedTensor [[[0], [0], [1], [0], [0], [0], [0], [1], [0], [0]]]>
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 specified by programs, following the ordering
dictated by `symbol_names`.
num_samples: `tf.Tensor` with one element indicating the number of
samples to draw for all circuits in the batch.
Returns:
A `tf.Tensor` containing the samples taken from each circuit in
`programs`.
"""
padded_samples = NOISY_OP_MODULE.tfq_noisy_samples(
programs, symbol_names, tf.cast(symbol_values, tf.float32), num_samples)
return tfq_utility_ops.padded_to_ragged(padded_samples)
Loading

0 comments on commit 02c2d55

Please sign in to comment.