Skip to content

Commit efed9ac

Browse files
Fidelity op (#554)
* [WIP] Fidelity op implementation. * Fix fidelity op test * Fix lint * Fix format * Add wheel build * Fix import_test * Add Mike's feedback Co-authored-by: Michael Broughton <[email protected]>
1 parent 6c29402 commit efed9ac

File tree

8 files changed

+413
-0
lines changed

8 files changed

+413
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ venv/*
4747

4848
# ignore emacs temp files
4949
*#
50+
51+
# vscode
52+
.vscode/*
5053
*~

release/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ sh_binary(
4040
"//tensorflow_quantum/core/ops:tfq_utility_ops_py",
4141
"//tensorflow_quantum/core/ops:tfq_simulate_ops_py",
4242
"//tensorflow_quantum/core/ops/math_ops:inner_product_op_py",
43+
"//tensorflow_quantum/core/ops/math_ops:fidelity_op_py",
4344
"//tensorflow_quantum/core/ops/noise:noisy_samples_op_py",
4445
"//tensorflow_quantum/core/ops/noise:noisy_expectation_op_py",
4546
"//tensorflow_quantum/core/ops/noise:noisy_sampled_expectation_op_py",

scripts/import_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_imports():
3636

3737
# Math ops.
3838
_ = tfq.math.inner_product
39+
_ = tfq.math.fidelity
3940

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

tensorflow_quantum/core/ops/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ py_library(
2828
":tfq_utility_ops_py",
2929
# test addons
3030
"//tensorflow_quantum/core/ops/math_ops:inner_product_op_py",
31+
"//tensorflow_quantum/core/ops/math_ops:fidelity_op_py",
3132
"//tensorflow_quantum/core/ops/noise:noisy_expectation_op_py",
3233
],
3334
)

tensorflow_quantum/core/ops/math_ops/BUILD

+17
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ cc_binary(
7272
],
7373
)
7474

75+
py_library(
76+
name = "fidelity_op_py",
77+
srcs = ["fidelity_op.py"],
78+
deps = [
79+
":inner_product_op_py",
80+
],
81+
)
82+
7583
py_library(
7684
name = "inner_product_op_py",
7785
srcs = ["inner_product_op.py"],
@@ -91,6 +99,15 @@ py_test(
9199
],
92100
)
93101

102+
py_test(
103+
name = "fidelity_op_test",
104+
srcs = ["fidelity_op_test.py"],
105+
deps = [
106+
":fidelity_op_py",
107+
"//tensorflow_quantum/python:util",
108+
],
109+
)
110+
94111
py_test(
95112
name = "inner_product_grad_test",
96113
srcs = ["inner_product_grad_test.py"],

tensorflow_quantum/core/ops/math_ops/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
"""Module for tfq.core.ops.math_ops.*"""
1616

1717
from tensorflow_quantum.core.ops.math_ops.inner_product_op import inner_product
18+
from tensorflow_quantum.core.ops.math_ops.fidelity_op import fidelity
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 for tfq.math.fidelity op."""
16+
import tensorflow as tf
17+
from tensorflow_quantum.core.ops.math_ops import inner_product_op
18+
19+
20+
@tf.function
21+
def fidelity(programs, symbol_names, symbol_values, other_programs):
22+
"""Calculate the fidelity between circuits.
23+
24+
Compute (potentially many) fidelities between the given circuits and
25+
the symbol free comparison circuits.
26+
27+
Calculates out[i][j] = $ | \langle \psi_{\text{programs[i]}} \\
28+
(\text{symbol_values[i]}) | \psi_{\text{other_programs[j]}} \rangle \\
29+
|^2 $
30+
31+
32+
>>> symbols = sympy.symbols('alpha beta')
33+
>>> qubits = cirq.GridQubit.rect(1, 2)
34+
>>> reference_circuits = [
35+
... cirq.Circuit((cirq.H**symbols[0]).on_each(qubits)),
36+
... cirq.Circuit(
37+
... cirq.X(qubits[0]) ** symbols[0],
38+
... cirq.Y(qubits[1]) ** symbols[1])
39+
... ]
40+
>>> other_circuits = [
41+
... cirq.Circuit(cirq.X.on_each(qubits)),
42+
... cirq.Circuit((cirq.Y**0.125).on_each(qubits)),
43+
... cirq.Circuit((cirq.X**0.5).on_each(qubits))
44+
... ]
45+
>>> reference_tensor = tfq.convert_to_tensor(reference_circuits)
46+
>>> symbol_tensor = tf.convert_to_tensor([s.name for s in symbols])
47+
>>> values_tensor = tf.convert_to_tensor(np.arange(4).reshape(2, 2))
48+
>>> other_tensor = tfq.convert_to_tensor([other_circuits, other_circuits])
49+
>>> fid = tfq.math.fidelity(reference_tensor, symbol_tensor,
50+
... values_tensor, other_tensor)
51+
>>> fid
52+
tf.Tensor(
53+
[[ 0., 0.925, 0.25],
54+
[ 0., 0.036, 0.25]],shape=(2, 3), dtype=float32)
55+
56+
57+
58+
Note: `other_programs` must not contain any free symbols. These can
59+
be resolved beforehand with `tfq.resolve_parameters`.
60+
61+
Args:
62+
programs: `tf.Tensor` of strings with shape [batch_size] containing
63+
the string representations of the circuits
64+
symbol_names: `tf.Tensor` of strings with shape [n_params], which
65+
is used to specify the order in which the values in
66+
`symbol_values` should be placed inside of the circuits in
67+
`programs`.
68+
symbol_values: `tf.Tensor` of real numbers with shape
69+
[batch_size, n_params] specifying parameter values to resolve
70+
into the circuits specificed by programs, following the ordering
71+
dictated by `symbol_names`.
72+
other_programs: `tf.Tensor` of strings with shape [batch_size, n_others]
73+
containing the string representations of the circuits with which to
74+
compute the overlap on `programs` with. Must not contain any free
75+
symbols.
76+
Returns:
77+
`tf.Tensor` with shape [batch_size, n_others] where `out[i][j]` is equal
78+
to the fidelity of `programs[i]` with `symbol_values[i]`
79+
resolved in and `other_programs[i][j]`.
80+
"""
81+
ip = inner_product_op.inner_product(programs, symbol_names,
82+
tf.cast(symbol_values, tf.float32),
83+
other_programs)
84+
return tf.math.abs(ip)**2

0 commit comments

Comments
 (0)