Skip to main contentIBM Quantum Documentation Mirror

UnitaryOverlap

class qiskit.circuit.library.UnitaryOverlap(unitary1, unitary2, prefix1='p1', prefix2='p2', insert_barrier=False)

GitHub

Bases: QuantumCircuit

Circuit that returns the overlap between two unitaries U2U1U_2^{\dag} U_1.

The input quantum circuits must represent unitary operations, since they must be invertible. If the inputs will have parameters, they are replaced by ParameterVectors with names “p1” (for circuit unitary1) and “p2” (for circuit unitary_2) in the output circuit.

This circuit is usually employed in computing the fidelity:

0U2U102\left|\langle 0| U_2^{\dag} U_1|0\rangle\right|^{2}

by computing the probability of being in the all-zeros bit-string, or equivalently, the expectation value of projector 00|0\rangle\langle 0|.

Example:

import numpy as np
from qiskit.circuit.library import EfficientSU2, UnitaryOverlap
from qiskit.primitives import Sampler
 
# get two circuit to prepare states of which we compute the overlap
circuit = EfficientSU2(2, reps=1)
unitary1 = circuit.assign_parameters(np.random.random(circuit.num_parameters))
unitary2 = circuit.assign_parameters(np.random.random(circuit.num_parameters))
 
# create the overlap circuit
overlap = UnitaryOverlap(unitary1, unitary2)
 
# sample from the overlap
sampler = Sampler(options={"shots": 100})
result = sampler.run(overlap).result()
 
# the fidelity is the probability to measure 0
fidelity = result.quasi_dists[0].get(0, 0)
Deprecated since version 2.1

The class qiskit.circuit.library.overlap.UnitaryOverlap is deprecated as of Qiskit 2.1. It will be removed in Qiskit 3.0. Use qiskit.circuit.library.unitary_overlap instead.

Parameters

  • unitary1 (QuantumCircuit) – Unitary acting on the ket vector.
  • unitary2 (QuantumCircuit) – Unitary whose inverse operates on the bra vector.
  • prefix1 (str) – The name of the parameter vector associated to unitary1, if it is parameterized. Defaults to "p1".
  • prefix2 (str) – The name of the parameter vector associated to unitary2, if it is parameterized. Defaults to "p2".
  • insert_barrier (bool) – Whether to insert a barrier between the two unitaries.

Raises

  • CircuitError – Number of qubits in unitary1 and unitary2 does not match.
  • CircuitError – Inputs contain measurements and/or resets.

Attributes

name

Type: str

A human-readable name for the circuit.

Example

from qiskit import QuantumCircuit
 
qc = QuantumCircuit(2, 2, name="my_circuit")
print(qc.name)
my_circuit