Skip to content

Commit dea26c6

Browse files
Add tfq.math.mps_1d_expectation for 1D MPS (#610)
* initial commit * Fix simulate_mps_test * Add CheckQubitsIn1D * Add Eigen & QSim MPS * Add mps_1d op and its test * Mike's feedback - add bond_dim as attr * Add Attr and its tests * Rename to mps_1d_expectation * Fix lint and format * Add import_test * Fix wheel test - add mps into release/BUILD * Mike's feedback * WIP where Segmentation fault happens? * Add Mike's feedback - no gate with control_qubit is allowed * Revert control_qubit gate validation. Both Operation with natural born controlled gate (e.g. CNOT) and synthesized ControlledOperation are converted into the same tfq_gate_set with control_qubit. So, we can't filter ControlledOperation in this way, revert the previous commit. * Fix program_resolution_test * Fix CheckQubitsIn1D() * Add print messages * Add debug print * Bump up to qsim==0.10.2 * Remove tfq::QsimFor and util_qsim.h::ComputeExpectationMPS * Add ComputeExpectationMPSQsim in util_qsim.h * Add more detailed debug prints * Bump up bond_dim from 2 (default) to 4 to fix segfault error. * Fix how to use ApplyGate in ComputeExpectationMPSQsim * Uncomment ComputeSmall() and Remove debug outputs * Fix format * Mike's feedback 1 : use fuses circuit and ApplyFusedGate() * Mike's feedback 2 : set default bond_dim to 4 * Fix format * Mike's feedback 3 : bump up to qsim==0.10.3 * bring 1d mps expectation into working state. * empty test. Co-authored-by: Michael Broughton <[email protected]>
1 parent 768ac15 commit dea26c6

14 files changed

+905
-15
lines changed

WORKSPACE

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,32 @@
33

44
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
55

6+
EIGEN_COMMIT = "12e8d57108c50d8a63605c6eb0144c838c128337"
7+
EIGEN_SHA256 = "f689246e342c3955af48d26ce74ac34d21b579a00675c341721a735937919b02"
8+
9+
10+
http_archive(
11+
name = "eigen",
12+
build_file_content = """
13+
cc_library(
14+
name = "eigen3",
15+
textual_hdrs = glob(["Eigen/**", "unsupported/**"]),
16+
visibility = ["//visibility:public"],
17+
)
18+
""",
19+
sha256 = EIGEN_SHA256,
20+
strip_prefix = "eigen-{commit}".format(commit = EIGEN_COMMIT),
21+
urls = [
22+
"https://storage.googleapis.com/mirror.tensorflow.org/gitlab.com/libeigen/eigen/-/archive/{commit}/eigen-{commit}.tar.gz".format(commit = EIGEN_COMMIT),
23+
"https://gitlab.com/libeigen/eigen/-/archive/{commit}/eigen-{commit}.tar.gz".format(commit = EIGEN_COMMIT),
24+
],
25+
)
26+
627
http_archive(
728
name = "qsim",
8-
sha256 = "d39b9c48866ce4d6a095093ae8059444d649e851219497af99e937a74f1e9a45",
9-
strip_prefix = "qsim-0.9.2-dev-20210317",
10-
urls = ["https://github.com/quantumlib/qsim/archive/v0.9.2-dev+20210317.zip"],
29+
sha256 = "91eb09b2697accab9c0f64d5ea6ff482f4772ce000af867670b4efaf06a35224",
30+
strip_prefix = "qsim-0.10.3-dev-20211001",
31+
urls = ["https://github.com/quantumlib/qsim/archive/refs/tags/v0.10.3-dev+20211001.zip"],
1132
)
1233

1334
http_archive(

release/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ sh_binary(
3939
"//tensorflow_quantum/core/ops:tfq_unitary_op_py",
4040
"//tensorflow_quantum/core/ops:tfq_utility_ops_py",
4141
"//tensorflow_quantum/core/ops:tfq_simulate_ops_py",
42-
"//tensorflow_quantum/core/ops/math_ops:inner_product_op_py",
4342
"//tensorflow_quantum/core/ops/math_ops:fidelity_op_py",
43+
"//tensorflow_quantum/core/ops/math_ops:inner_product_op_py",
44+
"//tensorflow_quantum/core/ops/math_ops:simulate_mps_py",
4445
"//tensorflow_quantum/core/ops/noise:noisy_samples_op_py",
4546
"//tensorflow_quantum/core/ops/noise:noisy_expectation_op_py",
4647
"//tensorflow_quantum/core/ops/noise:noisy_sampled_expectation_op_py",

scripts/import_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_imports():
3737
# Math ops.
3838
_ = tfq.math.inner_product
3939
_ = tfq.math.fidelity
40+
_ = tfq.math.mps_1d_expectation
4041

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

tensorflow_quantum/core/ops/math_ops/BUILD

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ cc_binary(
1717
srcs = [
1818
"tfq_inner_product.cc",
1919
"tfq_inner_product_grad.cc",
20+
"tfq_simulate_1d_expectation.cc",
2021
],
2122
copts = select({
2223
":windows": [
@@ -65,7 +66,10 @@ cc_binary(
6566
"//tensorflow_quantum/core/src:adj_util",
6667
"//tensorflow_quantum/core/src:circuit_parser_qsim",
6768
"//tensorflow_quantum/core/src:util_qsim",
69+
"@qsim//lib:mps_simulator",
70+
"@qsim//lib:mps_statespace",
6871
"@qsim//lib:qsim_lib",
72+
"@eigen//:eigen3",
6973
# tensorflow core framework
7074
# tensorflow core lib
7175
# tensorflow core protos
@@ -117,3 +121,22 @@ py_test(
117121
"//tensorflow_quantum/python:util",
118122
],
119123
)
124+
125+
py_library(
126+
name = "simulate_mps_py",
127+
srcs = ["simulate_mps.py"],
128+
data = [":_tfq_math_ops.so"],
129+
deps = [
130+
"//tensorflow_quantum/core/ops:load_module",
131+
],
132+
)
133+
134+
py_test(
135+
name = "simulate_mps_test",
136+
srcs = ["simulate_mps_test.py"],
137+
python_version = "PY3",
138+
deps = [
139+
":simulate_mps_py",
140+
"//tensorflow_quantum/python:util",
141+
],
142+
)

tensorflow_quantum/core/ops/math_ops/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
# ==============================================================================
1515
"""Module for tfq.core.ops.math_ops.*"""
1616

17-
from tensorflow_quantum.core.ops.math_ops.inner_product_op import inner_product
1817
from tensorflow_quantum.core.ops.math_ops.fidelity_op import fidelity
18+
from tensorflow_quantum.core.ops.math_ops.inner_product_op import inner_product
19+
from tensorflow_quantum.core.ops.math_ops.simulate_mps import mps_1d_expectation
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
"""Module to register MPS simulation ops."""
16+
import os
17+
import tensorflow as tf
18+
from tensorflow_quantum.core.ops.load_module import load_module
19+
20+
MATH_OP_MODULE = load_module(os.path.join("math_ops", "_tfq_math_ops.so"))
21+
22+
23+
def mps_1d_expectation(programs,
24+
symbol_names,
25+
symbol_values,
26+
pauli_sums,
27+
bond_dim=4):
28+
"""Calculate the expectation value of circuits wrt some operator(s)
29+
30+
Args:
31+
programs: `tf.Tensor` of strings with shape [batch_size] containing
32+
the string representations of the circuits to be executed.
33+
symbol_names: `tf.Tensor` of strings with shape [n_params], which
34+
is used to specify the order in which the values in
35+
`symbol_values` should be placed inside of the circuits in
36+
`programs`.
37+
symbol_values: `tf.Tensor` of real numbers with shape
38+
[batch_size, n_params] specifying parameter values to resolve
39+
into the circuits specificed by programs, following the ordering
40+
dictated by `symbol_names`.
41+
pauli_sums: `tf.Tensor` of strings with shape [batch_size, n_ops]
42+
containing the string representation of the operators that will
43+
be used on all of the circuits in the expectation calculations.
44+
bond_dim: Integer value used for the bond dimension during simulation.
45+
46+
Returns:
47+
`tf.Tensor` with shape [batch_size, n_ops] that holds the
48+
expectation value for each circuit with each op applied to it
49+
(after resolving the corresponding parameters in).
50+
"""
51+
return MATH_OP_MODULE.tfq_simulate_mps1d_expectation(programs,
52+
symbol_names,
53+
tf.cast(
54+
symbol_values,
55+
tf.float32),
56+
pauli_sums,
57+
bond_dim=bond_dim)

0 commit comments

Comments
 (0)