tfq.math.fidelity

View source on GitHub

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)

Args

programs tf.Tensor of strings with shape [batch_size] containing the string representations of the circuits
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.
other_programs 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.

Returns

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

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026年01月02日 UTC.