Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-analysis.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------
2 * Q2NS - Quantum Network Simulator
3 * Copyright (c) 2026 quantuminternet.it
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *---------------------------------------------------------------------------*/
9/**
10 * @file q2ns-analysis.h
11 * @brief Backend-agnostic analysis for q2ns quantum states.
12 */
13
14#pragma once
15
16namespace q2ns {
17
18class QState;
19
20namespace analysis {
21
22/**
23 * @ingroup q2ns_api
24 * @brief Compute fidelity between two QState objects of the same backend type.
25 *
26 * Currently supported backend pairings are:
27 * - QStateKet with QStateKet
28 * - QStateDM with QStateDM
29 * - QStateStab with QStateStab
30 *
31 * Mixed backend comparisons are not yet supported and throw.
32 *
33 * @param a First state.
34 * @param b Second state.
35 * @return Fidelity in the range [0.0, 1.0].
36 *
37 * @throws std::invalid_argument if the states are empty.
38 * @throws std::runtime_error if the states have different sizes, use unsupported
39 * backends, or require unavailable backend-specific functionality.
40 *
41 * @see QState
42 */
43double Fidelity(const QState& a, const QState& b);
44
45/**
46 * @ingroup q2ns_api
47 * @brief Compute the purity of a quantum state.
48 *
49 * Purity is defined as Tr(rho^2), where rho is the density matrix of the state.
50 * It is equal to 1 for pure states and less than 1 for mixed states.
51 *
52 * For backend-specific reasons, this function currently operates only on a
53 * single QState and therefore does not involve cross-backend comparison.
54 *
55 * @param s Input state.
56 * @return Purity in the range [0.0, 1.0], up to numerical tolerance.
57 *
58 * @throws std::invalid_argument if the state is empty.
59 * @throws std::runtime_error if the backend is unsupported or requires
60 * unavailable backend-specific functionality.
61 *
62 * @see IsPure
63 * @see VonNeumannEntropy
64 * @see QState
65 */
66double Purity(const QState& s);
67
68/**
69 * @ingroup q2ns_api
70 * @brief Check whether a quantum state is pure within a numerical tolerance.
71 *
72 * This function evaluates purity and compares it against 1.0. It is intended
73 * as a convenience helper for diagnostics, tests, and analysis code.
74 *
75 * @param s Input state.
76 * @param tol Absolute tolerance for comparing purity against 1.0.
77 * @return True if the state is pure within tolerance, false otherwise.
78 *
79 * @throws std::invalid_argument if the state is empty or if tol is negative.
80 * @throws std::runtime_error if the backend is unsupported or requires
81 * unavailable backend-specific functionality.
82 *
83 * @see Purity
84 * @see QState
85 */
86bool IsPure(const QState& s, double tol = 1e-12);
87
88/**
89 * @ingroup q2ns_api
90 * @brief Compute the von Neumann entropy of a quantum state in bits.
91 *
92 * The von Neumann entropy is defined as
93 * S(rho) = -Tr(rho log2 rho).
94 *
95 * Eigenvalues whose magnitude is below numerical tolerance are treated as zero
96 * and do not contribute to the sum.
97 *
98 * @param s Input state.
99 * @return von Neumann entropy in bits.
100 *
101 * @throws std::invalid_argument if the state is empty.
102 * @throws std::runtime_error if the backend is unsupported, if the backend
103 * requires unavailable functionality, or if the density matrix is not positive
104 * semidefinite within numerical tolerance.
105 *
106 * @see Purity
107 * @see TraceDistance
108 * @see QState
109 */
110double VonNeumannEntropy(const QState& s);
111
112/**
113 * @ingroup q2ns_api
114 * @brief Compute the trace distance between two QState objects of the same backend type.
115 *
116 * The trace distance is defined as
117 * D(rho, sigma) = 0.5 * ||rho - sigma||_1,
118 * where ||.||_1 is the trace norm.
119 *
120 * Currently supported backend pairings are:
121 * - QStateKet with QStateKet
122 * - QStateDM with QStateDM
123 * - QStateStab with QStateStab
124 *
125 * Mixed backend comparisons are not supported and throw.
126 *
127 * @param a First state.
128 * @param b Second state.
129 * @return Trace distance in the range [0.0, 1.0], up to numerical tolerance.
130 *
131 * @throws std::invalid_argument if the states are empty.
132 * @throws std::runtime_error if the states have different sizes, use unsupported
133 * backends, or require unavailable backend-specific functionality.
134 *
135 * @see Fidelity
136 * @see VonNeumannEntropy
137 * @see QState
138 */
139double TraceDistance(const QState& a, const QState& b);
140
141} // namespace analysis
142} // namespace q2ns
double VonNeumannEntropy(const QState &s)
Compute the von Neumann entropy of a quantum state in bits.
double Purity(const QState &s)
Compute the purity of a quantum state.
double Fidelity(const QState &a, const QState &b)
Compute fidelity between two QState objects of the same backend type.
double TraceDistance(const QState &a, const QState &b)
Compute the trace distance between two QState objects of the same backend type.
bool IsPure(const QState &s, double tol)
Check whether a quantum state is pure within a numerical tolerance.