MCPcopy Create free account
hub / github.com/QuEST-Kit/QuEST / calcDistance

Function calcDistance

quest/src/api/calculations.cpp:331–364  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

329
330
331qreal calcDistance(Qureg quregA, Qureg quregB) {
332 validate_quregFields(quregA, __func__);
333 validate_quregFields(quregB, __func__);
334 validate_quregsCanBeProducted(quregA, quregB, __func__);
335
336 bool isDensA = quregA.isDensityMatrix;
337 bool isDensB = quregB.isDensityMatrix;
338
339 // Hilbert-Schmidt = sqrt( Tr((A-B)(A-B)^dagger) = sqrt(sum_ij |A_ij - B_ij|^2)
340 if (isDensA && isDensB) {
341 qreal dif = localiser_densmatr_calcHilbertSchmidtDistance(quregA, quregB); // >= 0
342 return std::sqrt(dif);
343 }
344
345 // Bures = sqrt(2 - 2 |<A|B>|) (even when unnormalised)
346 if (!isDensA && !isDensB) {
347 qcomp prod = localiser_statevec_calcInnerProduct(quregA, quregB);
348 qreal mag = std::abs(prod); // >= 0
349
350 validate_buresDistanceInnerProdIsNormalised(mag, __func__);
351 mag = (mag > 1)? 1 : mag; // forgive eps error to avoid complex
352 return std::sqrt(2 - 2 * mag);
353 }
354
355 // purified distance = sqrt(1 - <psi|rho|psi>)
356 qcomp fid = (quregA.isDensityMatrix)?
357 localiser_densmatr_calcFidelityWithPureState(quregA, quregB, false): // no conj
358 localiser_densmatr_calcFidelityWithPureState(quregB, quregA, false); // no conj
359
360 validate_purifiedDistanceIsNormalised(fid, __func__);
361 qreal re = std::real(fid);
362 re = (re > 1)? 1 : re; // forgive eps error to avoid complex
363 return std::sqrt(1 - re);
364}
365
366
367

Callers

nothing calls this directly

Tested by

no test coverage detected