Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ http_archive(

http_archive(
name = "qsim",
sha256 = "06c330960edf95d495c3686be9006fa11a5f87c8294d6ef4a2ad0a01660f2e49",
strip_prefix = "qsim-0.9.1",
urls = ["https://github.com/quantumlib/qsim/archive/v0.9.1.zip"],
sha256 = "d39b9c48866ce4d6a095093ae8059444d649e851219497af99e937a74f1e9a45",
strip_prefix = "qsim-0.9.2-dev-20210317",
urls = ["https://github.com/quantumlib/qsim/archive/v0.9.2-dev+20210317.zip"],
)

# Added for crosstool in tensorflow.
Expand Down
2 changes: 2 additions & 0 deletions release/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ sh_binary(
"//tensorflow_quantum/core:__init__.py",
"//tensorflow_quantum/core/ops:__init__.py",
"//tensorflow_quantum/core/ops/math_ops:__init__.py",
"//tensorflow_quantum/core/ops/noise:__init__.py",
"//tensorflow_quantum/core/proto:__init__.py",
"//tensorflow_quantum/core/serialize:__init__.py",

Expand All @@ -39,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_expectation_op_py",
"//tensorflow_quantum/core/serialize:serializer",
"//tensorflow_quantum/datasets:cluster_state",
"//tensorflow_quantum/datasets:spin_system",
Expand Down
3 changes: 3 additions & 0 deletions scripts/import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def test_imports():
# Math ops.
_ = tfq.math.inner_product

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

# Util functions.
_ = tfq.convert_to_tensor
_ = tfq.get_quantum_concurrent_op_mode
Expand Down
3 changes: 3 additions & 0 deletions tensorflow_quantum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
# Import math ops.
from tensorflow_quantum.core import math_ops as math

# Import noise ops.
from tensorflow_quantum.core import noise

# Re-label python module as layers module.
import tensorflow_quantum.python.layers as layers

Expand Down
3 changes: 3 additions & 0 deletions tensorflow_quantum/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@
padded_to_ragged2d, resolve_parameters)
# Import math ops.
from tensorflow_quantum.core.ops import math_ops

# Import noise ops.
from tensorflow_quantum.core.ops import noise
3 changes: 3 additions & 0 deletions tensorflow_quantum/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@

# Import math_ops.
from tensorflow_quantum.core.ops import math_ops

# Import noise ops.
from tensorflow_quantum.core.ops import noise
85 changes: 85 additions & 0 deletions tensorflow_quantum/core/ops/noise/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package(default_visibility = ["//visibility:public"])

licenses(["notice"])

# Export for the PIP package.
exports_files(["__init__.py"])

config_setting(
name = "windows",
constraint_values = ["@bazel_tools//platforms:windows"],
)

cc_binary(
name = "_tfq_noise_ops.so",
srcs = [
"tfq_noisy_expectation.cc",
],
copts = select({
":windows": [
"/D__CLANG_SUPPORT_DYN_ANNOTATION__",
"/D_USE_MATH_DEFINES",
"/DEIGEN_MPL2_ONLY",
"/DEIGEN_MAX_ALIGN_BYTES=64",
"/DEIGEN_HAS_TYPE_TRAITS=0",
"/DTF_USE_SNAPPY",
"/showIncludes",
"/MD",
"/O2",
"/DNDEBUG",
"/w",
"-DWIN32_LEAN_AND_MEAN",
"-DNOGDI",
"/d2ReducedOptimizeHugeFunctions",
"/arch:AVX",
"/std:c++14",
"-DTENSORFLOW_MONOLITHIC_BUILD",
"/DPLATFORM_WINDOWS",
"/DEIGEN_HAS_C99_MATH",
"/DTENSORFLOW_USE_EIGEN_THREADPOOL",
"/DEIGEN_AVOID_STL_ARRAY",
"/Iexternal/gemmlowp",
"/wd4018",
"/wd4577",
"/DNOGDI",
"/UTF_COMPILE_LIBRARY",
],
"//conditions:default": [
"-pthread",
"-std=c++11",
"-D_GLIBCXX_USE_CXX11_ABI=0",
],
}),
features = select({
":windows": ["windows_export_all_symbols"],
"//conditions:default": [],
}),
linkshared = 1,
deps = [
"//tensorflow_quantum/core/ops:parse_context",
"//tensorflow_quantum/core/ops:tfq_simulate_utils",
"//tensorflow_quantum/core/src:util_qsim",
"//tensorflow_quantum/core/src:circuit_parser_qsim",
"@qsim//lib:qsim_lib",
],
)

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

py_test(
name = "noisy_expectation_op_test",
srcs = ["noisy_expectation_op_test.py"],
python_version = "PY3",
deps = [
":noisy_expectation_op_py",
"//tensorflow_quantum/core/ops:batch_util",
"//tensorflow_quantum/python:util",
],
)
17 changes: 17 additions & 0 deletions tensorflow_quantum/core/ops/noise/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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 tfq.core.ops.noise.*"""

from tensorflow_quantum.core.ops.noise.noisy_expectation_op import expectation
65 changes: 65 additions & 0 deletions tensorflow_quantum/core/ops/noise/noisy_expectation_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 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 simulation ops."""
import os
import tensorflow as tf
from tensorflow_quantum.core.ops.load_module import load_module

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


def expectation(programs, symbol_names, symbol_values, pauli_sums, num_samples):
"""Calculate the analytic expectation values using monte-carlo trajectories.

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
analytic expectation calculations with the given `pauli_sums` are calculated
after each run. Once all the runs are finished, these quantities are
averaged together. This process can be thought of as analyical expectation
calculation done using monte carlo state vector simulation to account
for noisy operations in the given circuits.

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).
"""
return NOISY_OP_MODULE.tfq_noisy_expectation(
programs, symbol_names, tf.cast(symbol_values, tf.float32), pauli_sums,
tf.cast(num_samples, dtype=tf.int32))
Loading