The Quantum Exact Simulation Toolkit v4.0.0
Loading...
Searching...
No Matches
calculations.h
1/** @file
2 * API signatures for calculating properties of quantum states,
3 * such as probabilities, expectation values and partial traces.
4 *
5 * @author Tyson Jones
6 *
7 * @defgroup calculations Calculations
8 * @ingroup api
9 * @brief Functions for calculating properties of quantum states without modifying them.
10 * @{
11 */
12
13#ifndef CALCULATIONS_H
14#define CALCULATIONS_H
15
16#include "quest/include/types.h"
17#include "quest/include/qureg.h"
18#include "quest/include/paulis.h"
19#include "quest/include/matrices.h"
20
21
22
23/*
24 * These signatures are divided into three partitions; those which are
25 * natively C and C++ compatible (first partition), then those which are
26 * only exposed to C++ (second partition) because they return 'qcomp'
27 * which cannot cross the C++-to-C ABI, and then finally the C++-only
28 * convenience overloads. The first partition defines the doc groups, and
29 * the latter partition functions are added into them.
30 */
31
32
33
34/*
35 * C AND C++ AGNOSTIC FUNCTIONS
36 */
37
38// enable invocation by both C and C++ binaries
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43
44
45/**
46 * @defgroup calc_expec Expectation values
47 * @brief Functions for calculating expected values of Hermitian observables.
48 * @{
49 */
50
51
52/** Calculates the expectation value of the given Pauli string observable @p str under the given
53 * state @p qureg without modifying it.
54 *
55 * @formulae
56 * Let @f$ \pstr = @f$ @p str.
57 * - When @p qureg is a statevector @f$\svpsi@f$, this function returns
58 * @f[
59 \brapsi \pstr \svpsi \in \mathbb{R}.
60 * @f]
61 * - When @p qureg is a density matrix @f$\dmrho@f$, this function returns the real component of
62 * @f[
63 \tr{ \pstr \dmrho }
64 * @f]
65 * which is exact when @f$\dmrho@f$ is physical (specifically Hermitian).
66 *
67 * @constraints
68 * - The returned value is always real, even when @p qureg is an unnormalised density matrix, in
69 * which case the imaginary component of the above expression is neglected.
70 * The full complex value can be obtained using calcExpecNonHermitianPauliStrSum().
71 *
72 * @equivalence
73 * - When @p str is general, this function is equivalent to calling calcExpecPauliStrSum() with a
74 * PauliStrSum composed of only a single PauliStr term and a unity coefficient.
75 * - When @p str @f$ = \id^\otimes @f$, the output is equivalent to that of calcTotalProb().
76 *
77 * @myexample
78 * ```
79 Qureg qureg = createQureg(4);
80 PauliStr str = getPauliStr("XYZ");
81
82 qreal expec = calcExpecPauliStr(qureg, str);
83 reportScalar("expec", expec);
84 * ```
85 *
86 * @see
87 * - calcExpecPauliStrSum()
88 * - calcExpecFullStateDiagMatr()
89 * @param[in] qureg the reference state.
90 * @param[in] str the observable operator.
91 * @returns The real component of the expectation value.
92 * @throws invalidQuESTInputError()
93 * - if @p qureg is uninitialised.
94 * - if @p str contains a (non-identity) Pauli upon a higher-index qubit than exists in @p qureg.
95 * - if the output (with unreturned imaginary component) is not approximately real.
96 * @notvalidated
97 * @author Tyson Jones
98 */
99qreal calcExpecPauliStr(Qureg qureg, PauliStr str);
100
101
102/** Calculates the expectation value of the given Hermitian observable @p sum - a weighted sum of
103 * Pauli strings - under the given state @p qureg, without modifying it.
104 *
105 * @formulae
106 * Let @f$ \hat{H} = @f$ @p sum.
107 * - When @p qureg is a statevector @f$\svpsi@f$, this function returns
108 * @f[
109 \brapsi \hat{H} \svpsi \in \mathbb{R}.
110 * @f]
111 * - When @p qureg is a density matrix @f$\dmrho@f$, this function returns the real component of
112 * @f[
113 \tr{ \hat{H} \dmrho }
114 * @f]
115 * which is the exact expectation value when @f$\dmrho@f$ is physical (specifically Hermitian).
116 *
117 * @constraints
118 * - Hermiticity of @p sum requires that every coefficient within is real.
119 * Validation will check @p sum is _approximately_ Hermitian, i.e. that
120 * @f[
121 |\im{c}| \le \valeps
122 * @f]
123 * for all @f$c \in @f$ `sum.coeffs`. Adjust @f$\valeps@f$ using setValidationEpsilon().
124 * - The returned value is always real, and the imaginary component is neglected even when
125 * Hermiticity validation is relaxed and/or @p qureg is an unnormalised density matrix.
126 * The full complex value can be obtained using calcExpecNonHermitianPauliStrSum().
127 *
128 * @equivalence
129 * - This function is mathematically equivalent to (albeit faster than) calling calcExpecPauliStr() upon
130 * each constituent @p PauliStr within @p sum, weighting each by its corresponding coefficient, and
131 * summing the outputs.
132 * - When @p sum contains only @f$\pauliz@f$ and @f$\id@f$ operators, its corresponding operator matrix
133 * is diagonal, and could be instead effected with calcExpecFullStateDiagMatr(). This may be faster when
134 * @p sum contains very-many terms and operates upon all qubits of the register.
135 *
136 * @myexample
137 * ```
138 Qureg qureg = createQureg(5);
139 PauliStrSum sum = createInlinePauliStrSum(R"(
140 0.123 XXIXX
141 1.234 XYZXZ
142 -1E-2 IIIII
143 )");
144
145 qreal expec = calcExpecPauliStrSum(qureg, sum);
146 reportScalar("expec", expec);
147 * ```
148 * @param[in] qureg the reference state.
149 * @param[in] sum the observable operator.
150 * @returns The real component of the expectation value.
151 * @throws invalidQuESTInputError()
152 * - if @p qureg or @p sum are uninitialised.
153 * - if any PauliStr in @p sum targets a higher-index qubit than exists in @p qureg.
154 * - if @p sum is not approximately Hermitian.
155 * - if the output (with unreturned imaginary component) is not approximately real.
156* @notvalidated
157 * @see
158 * - calcExpecNonHermitianPauliStrSum()
159 * - calcExpecFullStateDiagMatr()
160 * @author Tyson Jones
161 */
162qreal calcExpecPauliStrSum(Qureg qureg, PauliStrSum sum);
163
164
165/// @notdoced
166/// @notvalidated
168
169
170/// @notdoced
171/// @notvalidated
172qreal calcExpecFullStateDiagMatrPower(Qureg qureg, FullStateDiagMatr matr, qreal exponent);
173
174
175/** @} */
176
177
178
179/**
180 * @defgroup calc_prob Probabilities
181 * @brief Functions for non-destructively calculating the probabilities of measurement outcomes.
182 * @{
183 */
184
185
186/// @notdoced
187/// @notvalidated
188qreal calcProbOfBasisState(Qureg qureg, qindex index);
189
190
191/// @notdoced
192/// @notvalidated
193qreal calcProbOfQubitOutcome(Qureg qureg, int qubit, int outcome);
194
195
196/// @notdoced
197/// @notvalidated
198qreal calcProbOfMultiQubitOutcome(Qureg qureg, int* qubits, int* outcomes, int numQubits);
199
200
201/// @notdoced
202/// @notvalidated
203void calcProbsOfAllMultiQubitOutcomes(qreal* outcomeProbs, Qureg qureg, int* qubits, int numQubits);
204
205
206/** @} */
207
208
209
210/**
211 * @defgroup calc_properties Properties
212 * @brief Functions for calculating single-state properties like normalisation and purity.
213 * @{
214 */
215
216
217/// @notdoced
218/// @notvalidated
219qreal calcTotalProb(Qureg qureg);
220
221
222/// @notdoced
223/// @notvalidated
224qreal calcPurity(Qureg qureg);
225
226
227/** @} */
228
229
230
231/**
232 * @defgroup calc_comparisons Comparisons
233 * @brief Functions for comparing multiple quantum states.
234 * @{
235 */
236
237
238/// @notdoced
239/// @notvalidated
240qreal calcFidelity(Qureg qureg, Qureg other);
241
242/// @notdoced
243/// @notvalidated
244qreal calcDistance(Qureg qureg1, Qureg qureg2);
245
246
247/** @} */
248
249
250
251/**
252 * @defgroup calc_partialtrace Partial trace
253 * @brief Functions for calculating reduced density matrices, creating a new output Qureg.
254 * @{
255 */
256
257
258/// @notdoced
259/// @notvalidated
260Qureg calcPartialTrace(Qureg qureg, int* traceOutQubits, int numTraceQubits);
261
262
263/// @notdoced
264/// @notvalidated
265Qureg calcReducedDensityMatrix(Qureg qureg, int* retainQubits, int numRetainQubits);
266
267
268/** @} */
269
270
271// end de-mangler
272#ifdef __cplusplus
273}
274#endif
275
276
277
278/*
279 * C++ ONLY FUNCTIONS
280 *
281 * which are not directly C-compatible because they pass or
282 * return qcomp primitives by-value (rather than by pointer).
283 * This is prohibited because the C and C++ ABI does not agree
284 * on a complex type, though C's _Complex has the same memory
285 * layout as C++'s std::complex<>. To work around this, the
286 * below functions have a C-compatible wrapper defined in
287 * wrappers.h which passes/receives the primitives by pointer;
288 * a qcomp ptr can be safely passed from the C++ source binary
289 * the user's C binary. We manually add these functions to their
290 * respective Doxygen doc groups defined above
291 */
292
293
294/// @ingroup calc_comparisons
295/// @notdoced
296/// @notvalidated
297qcomp calcInnerProduct(Qureg qureg1, Qureg qureg2);
298
299
300/// @ingroup calc_expec
301/// @notdoced
302/// @notvalidated
304
305
306/// @ingroup calc_expec
307/// @notdoced
308/// @notvalidated
310
311
312/// @ingroup calc_expec
313/// @notdoced
314/// @notvalidated
315qcomp calcExpecNonHermitianFullStateDiagMatrPower(Qureg qureg, FullStateDiagMatr matrix, qcomp exponent);
316
317
318
319/*
320 * C++ OVERLOADS
321 *
322 * which are only accessible to C++ binaries, and accept
323 * arguments more natural to C++ (e.g. std::vector). We
324 * manually add these to their respective Doxygen doc groups.
325 */
326
327#ifdef __cplusplus
328
329#include <vector>
330
331
332/// @ingroup calc_prob
333/// @nottested
334/// @notdoced
335/// @notvalidated
336/// @cpponly
337qreal calcProbOfMultiQubitOutcome(Qureg qureg, std::vector<int> qubits, std::vector<int> outcomes);
338
339
340/// @ingroup calc_prob
341/// @nottested
342/// @notdoced
343/// @notvalidated
344/// @cpponly
345std::vector<qreal> calcProbsOfAllMultiQubitOutcomes(Qureg qureg, std::vector<int> qubits);
346
347
348/// @ingroup calc_partialtrace
349/// @nottested
350/// @notdoced
351/// @notvalidated
352/// @cpponly
353Qureg calcPartialTrace(Qureg qureg, std::vector<int> traceOutQubits);
354
355
356/// @ingroup calc_partialtrace
357/// @nottested
358/// @notdoced
359/// @notvalidated
360/// @cpponly
361Qureg calcReducedDensityMatrix(Qureg qureg, std::vector<int> retainQubits);
362
363
364#endif // __cplusplus
365
366
367#endif // CALCULATIONS_H
368
369/** @} */ // (end file-wide doxygen defgroup)
qcomp calcInnerProduct(Qureg qureg1, Qureg qureg2)
qreal calcFidelity(Qureg qureg, Qureg other)
qreal calcDistance(Qureg qureg1, Qureg qureg2)
qreal calcExpecFullStateDiagMatr(Qureg qureg, FullStateDiagMatr matr)
qreal calcExpecFullStateDiagMatrPower(Qureg qureg, FullStateDiagMatr matr, qreal exponent)
qcomp calcExpecNonHermitianFullStateDiagMatr(Qureg qureg, FullStateDiagMatr matr)
qreal calcExpecPauliStrSum(Qureg qureg, PauliStrSum sum)
qcomp calcExpecNonHermitianPauliStrSum(Qureg qureg, PauliStrSum sum)
qcomp calcExpecNonHermitianFullStateDiagMatrPower(Qureg qureg, FullStateDiagMatr matrix, qcomp exponent)
qreal calcExpecPauliStr(Qureg qureg, PauliStr str)
Qureg calcReducedDensityMatrix(Qureg qureg, int *retainQubits, int numRetainQubits)
Qureg calcPartialTrace(Qureg qureg, int *traceOutQubits, int numTraceQubits)
qreal calcProbOfQubitOutcome(Qureg qureg, int qubit, int outcome)
void calcProbsOfAllMultiQubitOutcomes(qreal *outcomeProbs, Qureg qureg, int *qubits, int numQubits)
qreal calcProbOfBasisState(Qureg qureg, qindex index)
qreal calcProbOfMultiQubitOutcome(Qureg qureg, int *qubits, int *outcomes, int numQubits)
qreal calcPurity(Qureg qureg)
qreal calcTotalProb(Qureg qureg)
Definition qureg.h:49