1 // Experimental average
2 // Source: ./examples/experimental_average.cpp
3 #include <iostream>
4 #include <tuple>
5 
6 #include "qpp.h"
7 
main()8 int main() {
9     using namespace qpp;
10 
11     ket psi = 0_ket; // same as st.z0;
12     cmat X = gt.X;
13     dyn_col_vect<double> evals = hevals(X);
14     cmat evects = hevects(X);
15 
16     long res = 0;
17     idx N = 10000; // number of "measurement experiments"
18     for (idx i = 0; i < N; ++i) {
19         auto measured = measure(psi, evects);
20         idx m = std::get<RES>(measured); // measurement result
21         if (evals[m] < 0)
22             --res; // -1
23         else
24             ++res; // +1
25     }
26     std::cout << ">> N = " << N << " measurements\n";
27     std::cout << ">> The experimental average of the observable X\n";
28     std::cout << disp(X) << '\n';
29     std::cout << "on the state psi\n";
30     std::cout << disp(psi) << '\n';
31     std::cout << "is: " << res / static_cast<double>(N) << '\n';
32     std::cout << ">> Theoretical average <psi | X | psi> = ";
33     std::cout << disp((adjoint(psi) * X * psi).value()) << '\n';
34 }
35