ComputeUncompute

class ComputeUncompute(sampler, shots=None, local=False, *, transpiler=None, transpiler_options=None)[source]

Bases: BaseStateFidelity

This class leverages the sampler primitive to calculate the state fidelity of two quantum circuits following the compute-uncompute method (see [1] for further reference). The fidelity can be defined as the state overlap.

\[|\langle\psi(x)|\phi(y)\rangle|^2\]

where \(x\) and \(y\) are optional parametrizations of the states \(\psi\) and \(\phi\) prepared by the circuits circuit_1 and circuit_2, respectively.

Reference: [1] Havlíček, V., Córcoles, A. D., Temme, K., Harrow, A. W., Kandala, A., Chow, J. M., & Gambetta, J. M. (2019). Supervised learning with quantum-enhanced feature spaces. Nature, 567(7747), 209-212. arXiv:1804.11326v2 [quant-ph]

Parameters:
  • sampler (BaseSamplerV2) – Sampler primitive instance.

  • shots (int | None) – Number of shots to be used by the underlying sampler. The order of priority is: number of shots in run method > fidelity’s number of shots > primitive’s default number of shots. Higher priority setting overrides lower priority setting.

  • local (bool) –

    If set to True, the fidelity is averaged over single-qubit projectors

    \[\hat{O} = \frac{1}{N}\sum_{i=1}^N|0_i\rangle\langle 0_i|,\]

    instead of the global projector \(|0\rangle\langle 0|^{\otimes n}\). This coincides with the standard (global) fidelity in the limit of the fidelity approaching 1. Might be used to increase the variance to improve trainability in algorithms such as PVQD.

  • transpiler (Transpiler | None) – An optional object with a run method allowing to transpile the circuits that are produced within this algorithm. If set to None, these won’t be transpiled.

  • transpiler_options (dict[str, Any] | None) – A dictionary of options to be passed to the transpiler’s run method as keyword arguments.

Raises:

ValueError – If the sampler is not an instance of BaseSamplerV2.

Attributes

shots

Return the number of shots used by the run method of the Sampler primitive. If None, the default number of shots of the primitive is used.

Returns:

The default number of shots.

Methods

create_fidelity_circuit(circuit_1, circuit_2)[source]

Combines circuit_1 and circuit_2 to create the fidelity circuit following the compute-uncompute method.

Parameters:
Returns:

The fidelity quantum circuit corresponding to circuit_1 and circuit_2.

Return type:

QuantumCircuit

run(circuits_1, circuits_2, values_1=None, values_2=None, *, shots=None)

Runs asynchronously the state overlap (fidelity) calculation between two (parametrized) circuits (first and second) for a specific set of parameter values (first and second). This calculation depends on the particular fidelity method implementation.

Parameters:
  • circuits_1 (QuantumCircuit | Sequence[QuantumCircuit]) – (Parametrized) quantum circuits preparing \(|\psi\rangle\).

  • circuits_2 (QuantumCircuit | Sequence[QuantumCircuit]) – (Parametrized) quantum circuits preparing \(|\phi\rangle\).

  • values_1 (Sequence[float] | Sequence[Sequence[float]] | None) – Numerical parameters to be bound to the first set of circuits.

  • values_2 (Sequence[float] | Sequence[Sequence[float]] | None) – Numerical parameters to be bound to the second set of circuits.

  • shots (int | Sequence[int] | None) – Number of shots to be used by the underlying sampler. If a single integer is provided, this number will be used for all circuits. If a sequence of integers is provided, they will be used on a per-circuit basis. If none is provided, the fidelity’s default number of shots will be used for all circuits. If this number is also set to None, the underlying primitive’s default number of shots will be used for all circuits.

Returns:

Primitive job for the fidelity calculation. The job’s result is an instance of StateFidelityResult.

Return type:

AlgorithmJob