Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mps 1d Sample / SampledExpectation ops #654

Merged
merged 38 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
43b25b8
initial commit
jaeyoo Jul 29, 2021
d56c35d
Fix simulate_mps_test
jaeyoo Jul 29, 2021
ae41578
Add CheckQubitsIn1D
jaeyoo Aug 3, 2021
b3f6d97
Add Eigen & QSim MPS
jaeyoo Aug 3, 2021
9f8963e
Add mps_1d op and its test
jaeyoo Aug 3, 2021
b85e932
Mike's feedback - add bond_dim as attr
jaeyoo Aug 3, 2021
3974752
Add Attr and its tests
jaeyoo Aug 3, 2021
b0a013d
Rename to mps_1d_expectation
jaeyoo Aug 3, 2021
3c21e16
Fix lint and format
jaeyoo Aug 3, 2021
1dbd1c0
Add import_test
jaeyoo Aug 4, 2021
27a30d3
Fix wheel test - add mps into release/BUILD
jaeyoo Aug 4, 2021
539b58d
Mike's feedback
jaeyoo Aug 6, 2021
4f630c2
WIP where Segmentation fault happens?
jaeyoo Aug 11, 2021
c5a51be
Merge branch 'master' into add_mps_1d
jaeyoo Aug 19, 2021
bb5fc0b
Add Mike's feedback - no gate with control_qubit is allowed
jaeyoo Aug 19, 2021
cd6f239
Revert control_qubit gate validation.
jaeyoo Aug 19, 2021
fc86135
Fix program_resolution_test
jaeyoo Aug 19, 2021
ea1d543
Fix CheckQubitsIn1D()
jaeyoo Aug 19, 2021
8035fc6
Add print messages
jaeyoo Aug 25, 2021
ce16003
Merge branch 'master' into add_mps_1d
jaeyoo Sep 30, 2021
617bc6b
Add debug print
jaeyoo Sep 30, 2021
86b0149
Bump up to qsim==0.10.2
jaeyoo Sep 30, 2021
b0f2daf
Remove tfq::QsimFor and util_qsim.h::ComputeExpectationMPS
jaeyoo Sep 30, 2021
af5b413
Add ComputeExpectationMPSQsim in util_qsim.h
jaeyoo Sep 30, 2021
bf533d5
Add more detailed debug prints
jaeyoo Sep 30, 2021
9257c90
Bump up bond_dim from 2 (default) to 4 to fix segfault error.
jaeyoo Sep 30, 2021
643e46c
Fix how to use ApplyGate in ComputeExpectationMPSQsim
jaeyoo Sep 30, 2021
7057ca8
Uncomment ComputeSmall() and Remove debug outputs
jaeyoo Sep 30, 2021
2adca84
Fix format
jaeyoo Sep 30, 2021
fa51a17
Mike's feedback 1 : use fuses circuit and ApplyFusedGate()
jaeyoo Sep 30, 2021
01ff1c5
Mike's feedback 2 : set default bond_dim to 4
jaeyoo Sep 30, 2021
97c11e9
Fix format
jaeyoo Sep 30, 2021
8578a8b
First commit for mps_1d_samples and mps_1d_sampled_expectation
jaeyoo Oct 1, 2021
d7482fe
Merge branch 'master' into add_mps_1d_sample
jaeyoo Jan 14, 2022
acd7354
Update the latest qsim's MPS Sample()
jaeyoo Jan 15, 2022
7b6cff8
Fix format
jaeyoo Jan 15, 2022
5357250
Fix minor errors
jaeyoo Jan 15, 2022
ea0194a
put things in a working state.
MichaelBroughton Jan 16, 2022
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
2 changes: 2 additions & 0 deletions scripts/import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def test_imports():
_ = tfq.math.inner_product
_ = tfq.math.fidelity
_ = tfq.math.mps_1d_expectation
_ = tfq.math.mps_1d_sample
_ = tfq.math.mps_1d_sampled_expectation

# Noisy simulation ops.
_ = tfq.noise.expectation
Expand Down
4 changes: 4 additions & 0 deletions tensorflow_quantum/core/ops/math_ops/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ cc_binary(
"tfq_inner_product.cc",
"tfq_inner_product_grad.cc",
"tfq_simulate_1d_expectation.cc",
"tfq_simulate_1d_samples.cc",
"tfq_simulate_1d_sampled_expectation.cc",
],
copts = select({
":windows": [
Expand Down Expand Up @@ -127,7 +129,9 @@ py_library(
srcs = ["simulate_mps.py"],
data = [":_tfq_math_ops.so"],
deps = [
"//tensorflow_quantum/core/ops:batch_util",
"//tensorflow_quantum/core/ops:load_module",
"//tensorflow_quantum/core/ops:tfq_utility_ops_py",
],
)

Expand Down
3 changes: 2 additions & 1 deletion tensorflow_quantum/core/ops/math_ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@

from tensorflow_quantum.core.ops.math_ops.fidelity_op import fidelity
from tensorflow_quantum.core.ops.math_ops.inner_product_op import inner_product
from tensorflow_quantum.core.ops.math_ops.simulate_mps import mps_1d_expectation
from tensorflow_quantum.core.ops.math_ops.simulate_mps import (
mps_1d_expectation, mps_1d_sample, mps_1d_sampled_expectation)
88 changes: 88 additions & 0 deletions tensorflow_quantum/core/ops/math_ops/simulate_mps.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os
import tensorflow as tf
from tensorflow_quantum.core.ops.load_module import load_module
from tensorflow_quantum.core.ops import tfq_utility_ops

MATH_OP_MODULE = load_module(os.path.join("math_ops", "_tfq_math_ops.so"))

Expand Down Expand Up @@ -55,3 +56,90 @@ def mps_1d_expectation(programs,
tf.float32),
pauli_sums,
bond_dim=bond_dim)


def mps_1d_sample(programs,
symbol_names,
symbol_values,
num_samples,
bond_dim=4):
"""Generate samples using the C++ MPS 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.
From there we will then sample from the final state.

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.
bond_dim: Integer value used for the bond dimension during simulation.

Returns:
A `tf.RaggedTensor` containing the samples taken from each circuit in
`programs`.
"""
padded_samples = MATH_OP_MODULE.tfq_simulate_mps1d_samples(
programs,
symbol_names,
tf.cast(symbol_values, tf.float32),
num_samples,
bond_dim=bond_dim)

return tfq_utility_ops.padded_to_ragged(padded_samples)


def mps_1d_sampled_expectation(programs,
symbol_names,
symbol_values,
pauli_sums,
num_samples,
bond_dim=4):
"""Calculate the expectation value of circuits using 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.
Them, sample the resulting state `num_samples` times and use these samples
to compute expectation values of the given `pauli_sums`.

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 samples to draw in each term of `pauli_sums[i][j]`
when estimating the expectation. Therefore, `num_samples` must
have the same shape as `pauli_sums`.
bond_dim: Integer value used for the bond dimension during simulation.

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 MATH_OP_MODULE.tfq_simulate_mps1d_sampled_expectation(
programs,
symbol_names,
tf.cast(symbol_values, tf.float32),
pauli_sums,
tf.cast(num_samples, dtype=tf.int32),
bond_dim=bond_dim)
Loading