Skip to content

Commit 8cda8f0

Browse files
Add mps 1d Sample / SampledExpectation ops (#654)
* 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 * First commit for mps_1d_samples and mps_1d_sampled_expectation * Update the latest qsim's MPS Sample() * Fix format * Fix minor errors * put things in a working state. Co-authored-by: Michael Broughton <[email protected]>
1 parent 864f9ce commit 8cda8f0

File tree

9 files changed

+1414
-30
lines changed

9 files changed

+1414
-30
lines changed

scripts/import_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def test_imports():
3838
_ = tfq.math.inner_product
3939
_ = tfq.math.fidelity
4040
_ = tfq.math.mps_1d_expectation
41+
_ = tfq.math.mps_1d_sample
42+
_ = tfq.math.mps_1d_sampled_expectation
4143

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

tensorflow_quantum/core/ops/math_ops/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ cc_binary(
1818
"tfq_inner_product.cc",
1919
"tfq_inner_product_grad.cc",
2020
"tfq_simulate_1d_expectation.cc",
21+
"tfq_simulate_1d_samples.cc",
22+
"tfq_simulate_1d_sampled_expectation.cc",
2123
],
2224
copts = select({
2325
":windows": [
@@ -127,7 +129,9 @@ py_library(
127129
srcs = ["simulate_mps.py"],
128130
data = [":_tfq_math_ops.so"],
129131
deps = [
132+
"//tensorflow_quantum/core/ops:batch_util",
130133
"//tensorflow_quantum/core/ops:load_module",
134+
"//tensorflow_quantum/core/ops:tfq_utility_ops_py",
131135
],
132136
)
133137

tensorflow_quantum/core/ops/math_ops/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616

1717
from tensorflow_quantum.core.ops.math_ops.fidelity_op import fidelity
1818
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
19+
from tensorflow_quantum.core.ops.math_ops.simulate_mps import (
20+
mps_1d_expectation, mps_1d_sample, mps_1d_sampled_expectation)

tensorflow_quantum/core/ops/math_ops/simulate_mps.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import os
1717
import tensorflow as tf
1818
from tensorflow_quantum.core.ops.load_module import load_module
19+
from tensorflow_quantum.core.ops import tfq_utility_ops
1920

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

@@ -55,3 +56,90 @@ def mps_1d_expectation(programs,
5556
tf.float32),
5657
pauli_sums,
5758
bond_dim=bond_dim)
59+
60+
61+
def mps_1d_sample(programs,
62+
symbol_names,
63+
symbol_values,
64+
num_samples,
65+
bond_dim=4):
66+
"""Generate samples using the C++ MPS simulator.
67+
68+
Simulate the final state of `programs` given `symbol_values` are placed
69+
inside of the symbols with the name in `symbol_names` in each circuit.
70+
From there we will then sample from the final state.
71+
72+
Args:
73+
programs: `tf.Tensor` of strings with shape [batch_size] containing
74+
the string representations of the circuits to be executed.
75+
symbol_names: `tf.Tensor` of strings with shape [n_params], which
76+
is used to specify the order in which the values in
77+
`symbol_values` should be placed inside of the circuits in
78+
`programs`.
79+
symbol_values: `tf.Tensor` of real numbers with shape
80+
[batch_size, n_params] specifying parameter values to resolve
81+
into the circuits specified by programs, following the ordering
82+
dictated by `symbol_names`.
83+
num_samples: `tf.Tensor` with one element indicating the number of
84+
samples to draw.
85+
bond_dim: Integer value used for the bond dimension during simulation.
86+
87+
Returns:
88+
A `tf.RaggedTensor` containing the samples taken from each circuit in
89+
`programs`.
90+
"""
91+
padded_samples = MATH_OP_MODULE.tfq_simulate_mps1d_samples(
92+
programs,
93+
symbol_names,
94+
tf.cast(symbol_values, tf.float32),
95+
num_samples,
96+
bond_dim=bond_dim)
97+
98+
return tfq_utility_ops.padded_to_ragged(padded_samples)
99+
100+
101+
def mps_1d_sampled_expectation(programs,
102+
symbol_names,
103+
symbol_values,
104+
pauli_sums,
105+
num_samples,
106+
bond_dim=4):
107+
"""Calculate the expectation value of circuits using samples.
108+
109+
Simulate the final state of `programs` given `symbol_values` are placed
110+
inside of the symbols with the name in `symbol_names` in each circuit.
111+
Them, sample the resulting state `num_samples` times and use these samples
112+
to compute expectation values of the given `pauli_sums`.
113+
114+
Args:
115+
programs: `tf.Tensor` of strings with shape [batch_size] containing
116+
the string representations of the circuits to be executed.
117+
symbol_names: `tf.Tensor` of strings with shape [n_params], which
118+
is used to specify the order in which the values in
119+
`symbol_values` should be placed inside of the circuits in
120+
`programs`.
121+
symbol_values: `tf.Tensor` of real numbers with shape
122+
[batch_size, n_params] specifying parameter values to resolve
123+
into the circuits specificed by programs, following the ordering
124+
dictated by `symbol_names`.
125+
pauli_sums: `tf.Tensor` of strings with shape [batch_size, n_ops]
126+
containing the string representation of the operators that will
127+
be used on all of the circuits in the expectation calculations.
128+
num_samples: `tf.Tensor` with `num_samples[i][j]` is equal to the
129+
number of samples to draw in each term of `pauli_sums[i][j]`
130+
when estimating the expectation. Therefore, `num_samples` must
131+
have the same shape as `pauli_sums`.
132+
bond_dim: Integer value used for the bond dimension during simulation.
133+
134+
Returns:
135+
`tf.Tensor` with shape [batch_size, n_ops] that holds the
136+
expectation value for each circuit with each op applied to it
137+
(after resolving the corresponding parameters in).
138+
"""
139+
return MATH_OP_MODULE.tfq_simulate_mps1d_sampled_expectation(
140+
programs,
141+
symbol_names,
142+
tf.cast(symbol_values, tf.float32),
143+
pauli_sums,
144+
tf.cast(num_samples, dtype=tf.int32),
145+
bond_dim=bond_dim)

0 commit comments

Comments
 (0)