|
| 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