# cirq.least_squares_xeb_fidelity_from_expectations

Least squares fidelity estimator.

An XEB experiment collects data from the execution of random circuits subject to noise. The effect of applying a random circuit with unitary U is modeled as U followed by a depolarizing channel. The result is that the initial state |𝜓⟩ is mapped to a density matrix ρ_U as follows:

``````|𝜓⟩ → ρ_U = f |𝜓_U⟩⟨𝜓_U| + (1 - f) I / D
``````

where |𝜓_U⟩ = U|𝜓⟩, D is the dimension of the Hilbert space, I / D is the maximally mixed state, and f is the fidelity with which the circuit is applied. Let O_U be an observable that is diagonal in the computational basis. Then the expectation of O_U on ρ_U is given by

``````Tr(ρ_U O_U) = f ⟨𝜓_U|O_U|𝜓_U⟩ + (1 - f) Tr(O_U / D).
``````

This equation shows how f can be estimated, since Tr(ρ_U O_U) can be estimated from experimental data, and ⟨𝜓_U|O_U|𝜓_U⟩ and Tr(O_U / D) can be computed numerically.

Let e_U = ⟨𝜓_U|O_U|𝜓_U⟩, u_U = Tr(O_U / D), and m_U denote the experimental estimate of Tr(ρ_U O_U). Then we estimate f by performing least squares minimization of the quantity

``````f (e_U - u_U) - (m_U - u_U)
``````

over different random circuits (giving different U). The solution to the least squares problem is given by

``````f = (∑_U (m_U - u_U) * (e_U - u_U)) / (∑_U (e_U - u_U)^2).
``````

`measured_expectations` A sequence of the m_U, the experimental estimates of the observable, one for each circuit U.
`exact_expectations` A sequence of the e_U, the exact value of the observable. The order should match the order of the `measured_expectations` argument.
`uniform_expectations` A sequence of the u_U, the expectation of the observable on a uniformly random bitstring. The order should match the order in the other arguments.

A tuple of two values. The first value is the estimated fidelity. The second value is a list of the residuals

``````f (e_U - u_U) - (m_U - u_U)
``````

of the least squares minimization.

`ValueError` The lengths of the input sequences are not all the same.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }]
[{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]