|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/quantum/blob/v0.7.6/tensorflow_quantum/core/ops/math_ops/fidelity_op.py#L20-L96) |

Calculate the fidelity between circuits.

    @tf.function
    tfq.math.fidelity(
        programs, symbol_names, symbol_values, other_programs
    )

Compute (potentially many) fidelities between the given circuits and
the symbol free comparison circuits.

Calculates out\[i\]\[j\] as:

\\\[
\|\\langle \\psi_{ ext{programs\[i\]} }( ext{symbol_values\[i\]}) \\mid
\\psi_{ ext{other_programs\[j\]} }
angle\|\^2
\\\]

    symbols = sympy.symbols('alpha beta')
    qubits = cirq.GridQubit.rect(1, 2)
    reference_circuits = [
        cirq.Circuit((cirq.H**symbols[0]).on_each(qubits)),
        cirq.Circuit(
            cirq.X(qubits[0]) ** symbols[0],
            cirq.Y(qubits[1]) ** symbols[1])
    ]
    other_circuits = [
        cirq.Circuit(cirq.X.on_each(qubits)),
        cirq.Circuit((cirq.Y**0.125).on_each(qubits)),
        cirq.Circuit((cirq.X**0.5).on_each(qubits))
    ]
    reference_tensor = tfq.convert_to_tensor(reference_circuits)
    symbol_tensor = tf.convert_to_tensor([s.name for s in symbols])
    values_tensor = tf.convert_to_tensor(np.arange(4).reshape(2, 2))
    other_tensor = tfq.convert_to_tensor([other_circuits, other_circuits])
    fid = tfq.math.fidelity(reference_tensor, symbol_tensor,
                                values_tensor, other_tensor)
    fid
    tf.Tensor(
        [[ 0., 0.925, 0.25],
         [ 0., 0.036, 0.25]],shape=(2, 3), dtype=float32)

> [!NOTE]
> **Note:** `other_programs` must not contain any free symbols. These can be resolved beforehand with [`tfq.resolve_parameters`](https://www.tensorflow.org/quantum/api_docs/python/tfq/resolve_parameters).

<br />

<br />

<br />

| ## Args ||
|---|---|
| `programs` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) of strings with shape \[batch_size\] containing the string representations of the circuits |
| `symbol_names` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/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`](https://www.tensorflow.org/api_docs/python/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`. |
| `other_programs` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) of strings with shape \[batch_size, n_others\] containing the string representations of the circuits with which to compute the overlap on `programs` with. Must not contain any free symbols. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) with shape \[batch_size, n_others\] where `out[i][j]` is equal to the fidelity of `programs[i]` with `symbol_values[i]` resolved in and `other_programs[i][j]`. ||

<br />