9#include "quest/include/quest.h"
30qcomp getReferenceExpectationValue(qvector state, qmatrix observable) {
31 DEMAND( state.size() == observable.size() );
33 return getInnerProduct(state, observable * state);
36qcomp getReferenceExpectationValue(qmatrix state, qmatrix observable) {
37 DEMAND( state.size() == observable.size() );
39 return getTrace(observable * state);
43qcomp getRefExpecValInner(
auto state,
auto paulis) {
45 int numQubits = getLog2(state.size());
46 qmatrix observable = getMatrix(paulis, numQubits);
47 return getReferenceExpectationValue(state, observable);
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); }
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() );
66 int numQubits = getLog2(state.size());
67 qmatrix projector = getProjector(targets, outcomes, numQubits);
68 qcomp value = getReferenceExpectationValue(state, projector);
69 return std::real(value);
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); }
75qreal getReferenceProbability(qvector state, qindex basisIndex) {
76 DEMAND( basisIndex < (qindex) state.size() );
78 qcomp elem = state[basisIndex];
79 qreal prob = std::norm(elem);
83qreal getReferenceProbability(qmatrix state, qindex basisIndex) {
84 DEMAND( basisIndex < (qindex) state.size() );
86 qcomp elem = state[basisIndex][basisIndex];
87 qreal prob = std::real(elem);
92vector<qreal> getAllRefProbsInner(
auto& state, vector<int> targets) {
94 vector<qreal> out(getPow2(targets.size()));
95 vector<int> outcomes(targets.size());
97 for (
size_t i=0; i<out.size(); i++) {
99 for (
size_t j=0; j<outcomes.size(); j++)
100 outcomes[j] = getBitAt(i, j);
102 out[i] = getReferenceProbability(state, targets, outcomes);
107vector<qreal> getAllReferenceProbabilities(qvector state, vector<int> targets) {
return getAllRefProbsInner(state, targets); }
108vector<qreal> getAllReferenceProbabilities(qmatrix state, vector<int> targets) {
return getAllRefProbsInner(state, targets); }
111qreal getReferenceProbability(qvector state) {
114 for (
auto& elem : state)
115 out += std::norm(elem);
120qreal getReferenceProbability(qmatrix state) {
123 for (
size_t i=0; i<state.size(); i++)
124 out += std::real(state[i][i]);
130qreal getReferencePurity(qmatrix state) {
132 return std::real(getTrace(state * state));
134qreal getReferencePurity(qvector state) {
136 return getReferencePurity(getOuterProduct(state, state));