Loading [MathJax]/extensions/tex2jax.js
The Quantum Exact Simulation Toolkit v4.0.0
 v4.0.0
โ€ขAll Classes Functions Variables Typedefs Modules Pages
Expectation values

Functions for calculating expected values of Hermitian observables. More...

Functions

qreal calcExpecFullStateDiagMatr (Qureg qureg, FullStateDiagMatr matr)
 
qreal calcExpecFullStateDiagMatrPower (Qureg qureg, FullStateDiagMatr matr, qreal exponent)
 
qcomp calcExpecNonHermitianFullStateDiagMatr (Qureg qureg, FullStateDiagMatr matr)
 
qcomp calcExpecNonHermitianFullStateDiagMatrPower (Qureg qureg, FullStateDiagMatr matrix, qcomp exponent)
 
qcomp calcExpecNonHermitianPauliStrSum (Qureg qureg, PauliStrSum sum)
 
qreal calcExpecPauliStr (Qureg qureg, PauliStr str)
 
qreal calcExpecPauliStrSum (Qureg qureg, PauliStrSum sum)
 

Detailed Description

Functions for calculating expected values of Hermitian observables.

Function Documentation

โ—† calcExpecFullStateDiagMatr()

qreal calcExpecFullStateDiagMatr ( Qureg qureg,
FullStateDiagMatr matr )
Note
Documentation for this function or struct is under construction!
Attention
This function's input validation has not yet been tested, so erroneous usage may produce unexpected output. Please use with caution!

Definition at line 161 of file calculations.cpp.

161 {
162 validate_quregFields(qureg, __func__);
163 validate_matrixFields(matrix, __func__);
164 validate_matrixAndQuregAreCompatible(matrix, qureg, true, __func__);
165 validate_matrixIsHermitian(matrix, __func__);
166
167 // unused parameters; see calcExpecFullStateDiagMatrPower() for explanation
168 qcomp exponent = qcomp(1,0);
169 bool useRealPow = false;
170
171 qcomp value = (qureg.isDensityMatrix)?
172 localiser_densmatr_calcExpecFullStateDiagMatr(qureg, matrix, exponent, useRealPow):
173 localiser_statevec_calcExpecFullStateDiagMatr(qureg, matrix, exponent, useRealPow);
174
175 // the sub-epsilon imaginary components in matrix never damage the real
176 // component of the statevector expectation value, so we do not validate
177 // imag(value)~0; we can always safely discard it, and only validate for densmatr:
178 if (qureg.isDensityMatrix)
179 validate_densMatrExpecDiagMatrValueIsReal(value, exponent, __func__);
180
181 return std::real(value);
182}

Referenced by TEST_CASE().

โ—† calcExpecFullStateDiagMatrPower()

qreal calcExpecFullStateDiagMatrPower ( Qureg qureg,
FullStateDiagMatr matr,
qreal exponent )
Note
Documentation for this function or struct is under construction!
Attention
This function's input validation has not yet been tested, so erroneous usage may produce unexpected output. Please use with caution!

Definition at line 185 of file calculations.cpp.

185 {
186 validate_quregFields(qureg, __func__);
187 validate_matrixFields(matrix, __func__);
188 validate_matrixAndQuregAreCompatible(matrix, qureg, true, __func__);
189 validate_matrixExpIsHermitian(matrix, exponent, __func__);
190
191 // the backend can use either the pow(qcomp,qcomp) or pow(qreal,qreal) overload;
192 // the former is significantly less accurate when the base is real & negative and
193 // the exponent is real, because complex pow(a,b) = exp(i b Arg(a)) = exp(i b 2 pi),
194 // and the numerical error in pi is compounded by the exponent and the exp(). Because
195 // this function assumes approx/intended Hermiticity, it will always use the real-pow
196 // overload, discarding the imaginary components of 'matrix' during computation - this
197 // is a behaviour unique to this function (other functions collect the erroneous
198 // imaginary components before a final validation and discarding).
199 const bool useRealPow = true;
200
201 qcomp value = (qureg.isDensityMatrix)?
202 localiser_densmatr_calcExpecFullStateDiagMatr(qureg, matrix, exponent, useRealPow):
203 localiser_statevec_calcExpecFullStateDiagMatr(qureg, matrix, exponent, useRealPow);
204
205 // is it impossible for the statevector routine to produce non-zero
206 // imaginary components because of our use of real-pow, the result of
207 // which is multiplied by abs(amp). Alas, density matrices multiply the
208 // result with a complex scalar and can accrue erroneous imaginary
209 // components when the density matrix is non-Hermitian, or due to rounding
210 // errors. As such, we only post-validate density matrix values.
211 if (qureg.isDensityMatrix)
212 validate_densMatrExpecDiagMatrValueIsReal(value, exponent, __func__);
213
214 return std::real(value);
215}

Referenced by TEST_CASE().

โ—† calcExpecNonHermitianFullStateDiagMatr()

qcomp calcExpecNonHermitianFullStateDiagMatr ( Qureg qureg,
FullStateDiagMatr matr )
Note
Documentation for this function or struct is under construction!
Attention
This function's input validation has not yet been tested, so erroneous usage may produce unexpected output. Please use with caution!

Definition at line 85 of file calculations.cpp.

85 {
86 validate_quregFields(qureg, __func__);
87 validate_matrixFields(matrix, __func__);
88 validate_matrixAndQuregAreCompatible(matrix, qureg, true, __func__);
89
90 return calcExpecNonHermitianFullStateDiagMatrPower(qureg, matrix, 1); // harmlessly re-validates
91}
qcomp calcExpecNonHermitianFullStateDiagMatrPower(Qureg qureg, FullStateDiagMatr matrix, qcomp exponent)

Referenced by TEST_CASE().

โ—† calcExpecNonHermitianFullStateDiagMatrPower()

qcomp calcExpecNonHermitianFullStateDiagMatrPower ( Qureg qureg,
FullStateDiagMatr matrix,
qcomp exponent )
Note
Documentation for this function or struct is under construction!
Attention
This function's input validation has not yet been tested, so erroneous usage may produce unexpected output. Please use with caution!

Definition at line 98 of file calculations.cpp.

98 {
99 validate_quregFields(qureg, __func__);
100 validate_matrixFields(matrix, __func__);
101 validate_matrixAndQuregAreCompatible(matrix, qureg, true, __func__);
102
103 // this function never uses the qreal-pow overload (because we make
104 // no assumption matrix is Hermitian i.e. real), and instead always
105 // uses the relatively numerically inaccurate qcomp overload
106 const bool useRealPow = false;
107
108 return (qureg.isDensityMatrix)?
109 localiser_densmatr_calcExpecFullStateDiagMatr(qureg, matrix, exponent, useRealPow):
110 localiser_statevec_calcExpecFullStateDiagMatr(qureg, matrix, exponent, useRealPow);
111}

Referenced by calcExpecNonHermitianFullStateDiagMatr(), and TEST_CASE().

โ—† calcExpecNonHermitianPauliStrSum()

qcomp calcExpecNonHermitianPauliStrSum ( Qureg qureg,
PauliStrSum sum )
Note
Documentation for this function or struct is under construction!
Attention
This function's input validation has not yet been tested, so erroneous usage may produce unexpected output. Please use with caution!

Definition at line 68 of file calculations.cpp.

68 {
69 validate_quregFields(qureg, __func__);
70 validate_pauliStrSumFields(sum, __func__);
71 validate_pauliStrSumTargets(sum, qureg, __func__);
72
73 qcomp value = (qureg.isDensityMatrix)?
74 localiser_densmatr_calcExpecPauliStrSum(qureg, sum):
75 localiser_statevec_calcExpecPauliStrSum(qureg, sum);
76
77 return value;
78}

Referenced by TEST_CASE().

โ—† calcExpecPauliStr()

qreal calcExpecPauliStr ( Qureg qureg,
PauliStr str )

Calculates the expectation value of the given Pauli string observable str under the given state qureg without modifying it.

Formulae
Let \( \pstr = \) str.
  • When qureg is a statevector \(\svpsi\), this function returns

    \[ \brapsi \pstr \svpsi \in \mathbb{R}. \]

  • When qureg is a density matrix \(\dmrho\), this function returns the real component of

    \[ \tr{ \pstr \dmrho } \]

    which is exact when \(\dmrho\) is physical (specifically Hermitian).
Constraints
  • The returned value is always real, even when qureg is an unnormalised density matrix, in which case the imaginary component of the above expression is neglected. The full complex value can be obtained using calcExpecNonHermitianPauliStrSum().
Equivalences
Example
Qureg qureg = createQureg(4);
PauliStr str = getPauliStr("XYZ");
qreal expec = calcExpecPauliStr(qureg, str);
reportScalar("expec", expec);
qreal calcExpecPauliStr(Qureg qureg, PauliStr str)
PauliStr getPauliStr(const char *paulis, int *indices, int numPaulis)
Definition paulis.cpp:296
Qureg createQureg(int numQubits)
Definition qureg.cpp:279
void reportScalar(const char *label, qcomp num)
Definition types.cpp:51
Definition qureg.h:49
See also
Parameters
[in]quregthe reference state.
[in]strthe observable operator.
Returns
The real component of the expectation value.
Exceptions
invalidQuESTInputError()
  • if qureg is uninitialised.
  • if str contains a (non-identity) Pauli upon a higher-index qubit than exists in qureg.
  • if the output (with unreturned imaginary component) is not approximately real.
Attention
This function's input validation has not yet been tested, so erroneous usage may produce unexpected output. Please use with caution!
Author
Tyson Jones

Definition at line 133 of file calculations.cpp.

133 {
134 validate_quregFields(qureg, __func__);
135 validate_pauliStrTargets(qureg, str, __func__);
136
137 qcomp value = (qureg.isDensityMatrix)?
138 localiser_densmatr_calcExpecPauliStr(qureg, str):
139 localiser_statevec_calcExpecPauliStr(qureg, str);
140
141 validate_expecPauliStrValueIsReal(value, qureg.isDensityMatrix, __func__);
142 return std::real(value);
143}

Referenced by TEST_CASE().

โ—† calcExpecPauliStrSum()

qreal calcExpecPauliStrSum ( Qureg qureg,
PauliStrSum sum )

Calculates the expectation value of the given Hermitian observable sum - a weighted sum of Pauli strings - under the given state qureg, without modifying it.

Formulae
Let \( \hat{H} = \) sum.
  • When qureg is a statevector \(\svpsi\), this function returns

    \[ \brapsi \hat{H} \svpsi \in \mathbb{R}. \]

  • When qureg is a density matrix \(\dmrho\), this function returns the real component of

    \[ \tr{ \hat{H} \dmrho } \]

    which is the exact expectation value when \(\dmrho\) is physical (specifically Hermitian).
Constraints
  • Hermiticity of sum requires that every coefficient within is real. Validation will check sum is approximately Hermitian, i.e. that

    \[ |\im{c}| \le \valeps \]

    for all \(c \in \) sum.coeffs. Adjust \(\valeps\) using setValidationEpsilon().
  • The returned value is always real, and the imaginary component is neglected even when Hermiticity validation is relaxed and/or qureg is an unnormalised density matrix. The full complex value can be obtained using calcExpecNonHermitianPauliStrSum().
Equivalences
  • This function is mathematically equivalent to (albeit faster than) calling calcExpecPauliStr() upon each constituent PauliStr within sum, weighting each by its corresponding coefficient, and summing the outputs.
  • When sum contains only \(\pauliz\) and \(\id\) operators, its corresponding operator matrix is diagonal, and could be instead effected with calcExpecFullStateDiagMatr(). This may be faster when sum contains very-many terms and operates upon all qubits of the register.
Example
Qureg qureg = createQureg(5);
0.123 XXIXX
1.234 XYZXZ
-1E-2 IIIII
)");
qreal expec = calcExpecPauliStrSum(qureg, sum);
reportScalar("expec", expec);
qreal calcExpecPauliStrSum(Qureg qureg, PauliStrSum sum)
PauliStrSum createInlinePauliStrSum(const char *str)
Definition paulis.cpp:418
Parameters
[in]quregthe reference state.
[in]sumthe observable operator.
Returns
The real component of the expectation value.
Exceptions
invalidQuESTInputError()
  • if qureg or sum are uninitialised.
  • if any PauliStr in sum targets a higher-index qubit than exists in qureg.
  • if sum is not approximately Hermitian.
  • if the output (with unreturned imaginary component) is not approximately real.
Attention
This function's input validation has not yet been tested, so erroneous usage may produce unexpected output. Please use with caution!
See also
Author
Tyson Jones

Definition at line 146 of file calculations.cpp.

146 {
147 validate_quregFields(qureg, __func__);
148 validate_pauliStrSumFields(sum, __func__);
149 validate_pauliStrSumTargets(sum, qureg, __func__);
150 validate_pauliStrSumIsHermitian(sum, __func__);
151
152 qcomp value = (qureg.isDensityMatrix)?
153 localiser_densmatr_calcExpecPauliStrSum(qureg, sum):
154 localiser_statevec_calcExpecPauliStrSum(qureg, sum);
155
156 validate_expecPauliStrSumValueIsReal(value, qureg.isDensityMatrix, __func__);
157 return std::real(value);
158}

Referenced by TEST_CASE().