The Quantum Exact Simulation Toolkit v4.0.0
Loading...
Searching...
No Matches
measure.cpp
1/** @file
2 * Testing utilities which evaluate measurements upon
3 * reference qvector and qmatrix states. These are slow,
4 * serial, un-optimised, defensively-designed routines.
5 *
6 * @author Tyson Jones
7 */
8
9#include "quest/include/quest.h"
10
11#include "qvector.hpp"
12#include "qmatrix.hpp"
13#include "convert.hpp"
14#include "linalg.hpp"
15#include "macros.hpp"
16
17#include <algorithm>
18#include <complex>
19#include <vector>
20
21using std::vector;
22
23
24
25/*
26 * EXPECTATION VALUES
27 */
28
29
30qcomp getReferenceExpectationValue(qvector state, qmatrix observable) {
31 DEMAND( state.size() == observable.size() );
32
33 return getInnerProduct(state, observable * state);
34}
35
36qcomp getReferenceExpectationValue(qmatrix state, qmatrix observable) {
37 DEMAND( state.size() == observable.size() );
38
39 return getTrace(observable * state);
40}
41
42
43qcomp getRefExpecValInner(auto state, auto paulis) {
44
45 int numQubits = getLog2(state.size());
46 qmatrix observable = getMatrix(paulis, numQubits);
47 return getReferenceExpectationValue(state, observable);
48}
49qcomp getReferenceExpectationValue(qvector state, PauliStr str) { return getRefExpecValInner(state, str); }
50qcomp getReferenceExpectationValue(qmatrix state, PauliStr str) { return getRefExpecValInner(state, str); }
51qcomp getReferenceExpectationValue(qvector state, PauliStrSum sum) { return getRefExpecValInner(state, sum); }
52qcomp getReferenceExpectationValue(qmatrix state, PauliStrSum sum) { return getRefExpecValInner(state, sum); }
53
54
55
56/*
57 * PROBABILITIES
58 */
59
60
61qreal getRefProbInner(auto& state, vector<int> targets, vector<int> outcomes) {
62 DEMAND( getLog2(state.size()) > *std::max_element(targets.begin(), targets.end()) );
63 DEMAND( targets.size() == outcomes.size() );
64
65 // <psi| (|o><o| (x) I) |psi> = Tr( (|o><o| (x) I) rho)
66 int numQubits = getLog2(state.size());
67 qmatrix projector = getProjector(targets, outcomes, numQubits);
68 qcomp value = getReferenceExpectationValue(state, projector); // ~0
69 return std::real(value);
70}
71qreal getReferenceProbability(qvector state, vector<int> targets, vector<int> outcomes) { return getRefProbInner(state, targets, outcomes); }
72qreal getReferenceProbability(qmatrix state, vector<int> targets, vector<int> outcomes) { return getRefProbInner(state, targets, outcomes); }
73
74
75qreal getReferenceProbability(qvector state, qindex basisIndex) {
76 DEMAND( basisIndex < (qindex) state.size() );
77
78 qcomp elem = state[basisIndex];
79 qreal prob = std::norm(elem);
80 return prob;
81}
82
83qreal getReferenceProbability(qmatrix state, qindex basisIndex) {
84 DEMAND( basisIndex < (qindex) state.size() );
85
86 qcomp elem = state[basisIndex][basisIndex];
87 qreal prob = std::real(elem);
88 return prob;
89}
90
91
92vector<qreal> getAllRefProbsInner(auto& state, vector<int> targets) {
93
94 vector<qreal> out(getPow2(targets.size()));
95 vector<int> outcomes(targets.size());
96
97 for (size_t i=0; i<out.size(); i++) {
98
99 for (size_t j=0; j<outcomes.size(); j++)
100 outcomes[j] = getBitAt(i, j);
101
102 out[i] = getReferenceProbability(state, targets, outcomes);
103 }
104
105 return out;
106}
107vector<qreal> getAllReferenceProbabilities(qvector state, vector<int> targets) { return getAllRefProbsInner(state, targets); }
108vector<qreal> getAllReferenceProbabilities(qmatrix state, vector<int> targets) { return getAllRefProbsInner(state, targets); }
109
110
111qreal getReferenceProbability(qvector state) {
112
113 qreal out = 0;
114 for (auto& elem : state)
115 out += std::norm(elem);
116
117 return out;
118}
119
120qreal getReferenceProbability(qmatrix state) {
121
122 qreal out = 0;
123 for (size_t i=0; i<state.size(); i++)
124 out += std::real(state[i][i]);
125
126 return out;
127}
128
129
130qreal getReferencePurity(qmatrix state) {
131
132 return std::real(getTrace(state * state));
133}
134qreal getReferencePurity(qvector state) {
135
136 return getReferencePurity(getOuterProduct(state, state));
137}