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 * @equivalences
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 @validationerror
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 * @equivalences
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 *
149 * @param[in] qureg the reference state.
150 * @param[in] sum the observable operator.
151 * @returns The real component of the expectation value.
152 * @throws @validationerror
153 * - if @p qureg or @p sum are uninitialised.
154 * - if any PauliStr in @p sum targets a higher-index qubit than exists in @p qureg.
155 * - if @p sum is not approximately Hermitian.
156 * - if the output (with unreturned imaginary component) is not approximately real.
157* @notvalidated
158 * @see
159 * - calcExpecNonHermitianPauliStrSum()
160 * - calcExpecFullStateDiagMatr()
161 * @author Tyson Jones
162 */
163qreal calcExpecPauliStrSum(Qureg qureg, PauliStrSum sum);
164
165
166/// @notdoced
167/// @notvalidated
169
170
171/// @notdoced
172/// @notvalidated
173qreal calcExpecFullStateDiagMatrPower(Qureg qureg, FullStateDiagMatr matr, qreal exponent);
174
175
176/** @} */
177
178
179
180/**
181 * @defgroup calc_prob Probabilities
182 * @brief Functions for non-destructively calculating the probabilities of measurement outcomes.
183 * @{
184 */
185
186
187/// @notdoced
188/// @notvalidated
189qreal calcProbOfBasisState(Qureg qureg, qindex index);
190
191
192/// @notdoced
193/// @notvalidated
194qreal calcProbOfQubitOutcome(Qureg qureg, int qubit, int outcome);
195
196
197/// @notdoced
198/// @notvalidated
199qreal calcProbOfMultiQubitOutcome(Qureg qureg, int* qubits, int* outcomes, int numQubits);
200
201
202/// @notdoced
203/// @notvalidated
204void calcProbsOfAllMultiQubitOutcomes(qreal* outcomeProbs, Qureg qureg, int* qubits, int numQubits);
205
206
207/** @} */
208
209
210
211/**
212 * @defgroup calc_properties Properties
213 * @brief Functions for calculating single-state properties like normalisation and purity.
214 * @{
215 */
216
217
218/// @notdoced
219/// @notvalidated
220qreal calcTotalProb(Qureg qureg);
221
222
223/// @notdoced
224/// @notvalidated
225qreal calcPurity(Qureg qureg);
226
227
228/** @} */
229
230
231
232/**
233 * @defgroup calc_comparisons Comparisons
234 * @brief Functions for comparing multiple quantum states.
235 * @{
236 */
237
238
239/// @notdoced
240/// @notvalidated
241qreal calcFidelity(Qureg qureg, Qureg other);
242
243/// @notdoced
244/// @notvalidated
245qreal calcDistance(Qureg qureg1, Qureg qureg2);
246
247
248/** @} */
249
250
251
252/**
253 * @defgroup calc_partialtrace Partial trace
254 * @brief Functions for calculating reduced density matrices, creating a new output Qureg.
255 * @{
256 */
257
258
259/// @notdoced
260/// @notvalidated
261Qureg calcPartialTrace(Qureg qureg, int* traceOutQubits, int numTraceQubits);
262
263
264/// @notdoced
265/// @notvalidated
266Qureg calcReducedDensityMatrix(Qureg qureg, int* retainQubits, int numRetainQubits);
267
268
269/** @} */
270
271
272// end de-mangler
273#ifdef __cplusplus
274}
275#endif
276
277
278
279/*
280 * C++ ONLY FUNCTIONS
281 *
282 * which are not directly C-compatible because they pass or
283 * return qcomp primitives by-value (rather than by pointer).
284 * This is prohibited because the C and C++ ABI does not agree
285 * on a complex type, though C's _Complex has the same memory
286 * layout as C++'s std::complex<>. To work around this, the
287 * below functions have a C-compatible wrapper defined in
288 * wrappers.h which passes/receives the primitives by pointer;
289 * a qcomp ptr can be safely passed from the C++ source binary
290 * the user's C binary. We manually add these functions to their
291 * respective Doxygen doc groups defined above
292 */
293
294
295/// @ingroup calc_comparisons
296/// @notdoced
297/// @notvalidated
298qcomp calcInnerProduct(Qureg qureg1, Qureg qureg2);
299
300
301/// @ingroup calc_expec
302/// @notdoced
303/// @notvalidated
305
306
307/// @ingroup calc_expec
308/// @notdoced
309/// @notvalidated
311
312
313/// @ingroup calc_expec
314/// @notdoced
315/// @notvalidated
316qcomp calcExpecNonHermitianFullStateDiagMatrPower(Qureg qureg, FullStateDiagMatr matrix, qcomp exponent);
317
318
319
320/*
321 * C++ OVERLOADS
322 *
323 * which are only accessible to C++ binaries, and accept
324 * arguments more natural to C++ (e.g. std::vector). We
325 * manually add these to their respective Doxygen doc groups.
326 */
327
328#ifdef __cplusplus
329
330#include <vector>
331
332
333/// @ingroup calc_prob
334/// @nottested
335/// @notdoced
336/// @notvalidated
337/// @cpponly
338qreal calcProbOfMultiQubitOutcome(Qureg qureg, std::vector<int> qubits, std::vector<int> outcomes);
339
340
341/// @ingroup calc_prob
342/// @nottested
343/// @notdoced
344/// @notvalidated
345/// @cpponly
346std::vector<qreal> calcProbsOfAllMultiQubitOutcomes(Qureg qureg, std::vector<int> qubits);
347
348
349/// @ingroup calc_partialtrace
350/// @nottested
351/// @notdoced
352/// @notvalidated
353/// @cpponly
354Qureg calcPartialTrace(Qureg qureg, std::vector<int> traceOutQubits);
355
356
357/// @ingroup calc_partialtrace
358/// @nottested
359/// @notdoced
360/// @notvalidated
361/// @cpponly
362Qureg calcReducedDensityMatrix(Qureg qureg, std::vector<int> retainQubits);
363
364
365#endif // __cplusplus
366
367
368#endif // CALCULATIONS_H
369
370/** @} */ // (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