Skip to content

Commit 2f4613c

Browse files
committed
part 4 draft
1 parent fde3144 commit 2f4613c

File tree

1 file changed

+171
-1
lines changed

1 file changed

+171
-1
lines changed

docs/source/guide/lre-4-low-level.md

Lines changed: 171 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,174 @@ kernelspec:
1212
---
1313

1414

15-
# What happens when I use LRE?
15+
# What happens when I use LRE?
16+
17+
As shown in the figure below, LRE works in two steps, layerwise noise scaling and extrapolation.
18+
19+
The noise-scaled circuits are
20+
created through the functions in {mod}`mitiq.lre.multivariate_scaling.layerwise_folding` while the error-mitigated expectation value is estimated by using the functions in {mod}`mitiq.lre.inference.multivariate_richardson`.
21+
22+
```{figure} ../img/lre_workflow_steps.png
23+
---
24+
width: 700px
25+
name: lre-overview2
26+
---
27+
The diagram shows the workflow of the layerwise Richardson extrapolation (LRE) in Mitiq.
28+
```
29+
30+
31+
**The first step** involves generating and executing layerwise noise-scaled quantum circuits.
32+
- The user provides a `cirq` circuit.
33+
34+
```{danger}
35+
LRE is currently compatible with quantum programs written using `cirq`.
36+
Work on making this technique compatible with other frontends is ongoing. 🚧
37+
```
38+
39+
- Mitiq generates a set of layerwise noise-scaled circuits by applying unitary folding based on a set of pre-determined scale factor vectors.
40+
- The noise-scaled circuits are executed on the noisy backend obtaining a set of noisy expectation values.
41+
42+
**The second step** involves inferring the error mitigated expectation value from the measured results through multivariate richardson extrapolation.
43+
44+
The function {func}`.execute_with_lre` accomplishes both steps behind the scenes to estimate the error mitigate expectation value. Additional information is available in [](lre-1-intro.md).
45+
46+
If one were interested in applying each step individually, the following sections demonstrate how a user can do so.
47+
48+
## First step: generating and executing noise-scaled circuits
49+
50+
### Problem setup
51+
52+
We'll first define a quantum circuit, and a method of executing circuits for demonstration purposes.
53+
54+
For simplicity, we define a circuit whose unitary compiles to the identity operation.
55+
Here we will use a randomized benchmarking circuit on a single qubit, visualized below.
56+
57+
```{code-cell} ipython3
58+
from cirq import LineQubit, Circuit, CZ, CNOT, H
59+
60+
61+
q0, q1, q2, q3 = LineQubit.range(4)
62+
circuit = Circuit(
63+
H(q0),
64+
CNOT.on(q0, q1),
65+
CZ.on(q1, q2),
66+
CNOT.on(q2, q3),
67+
)
68+
69+
70+
print(circuit)
71+
```
72+
73+
We define an [executor](executors.md) which simulates the input circuit subjected to depolarizing noise, and returns the probability of measuring the ground state.
74+
By altering the value for `noise_level`, ideal and noisy expectation values can be obtained.
75+
76+
```{code-cell} ipython3
77+
from cirq import DensityMatrixSimulator, depolarize
78+
79+
80+
def execute(circuit, noise_level=0.025):
81+
noisy_circuit = circuit.with_noise(depolarize(p=noise_level))
82+
rho = DensityMatrixSimulator().simulate(noisy_circuit).final_density_matrix
83+
return rho[0, 0].real
84+
```
85+
86+
Compare the noisy and ideal expectation values:
87+
88+
```{code-cell} ipython3
89+
noisy = execute(circuit)
90+
ideal = execute(circuit, noise_level=0.0)
91+
print(f"Error without mitigation: {abs(ideal - noisy) :.5f}")
92+
```
93+
94+
### Create noise-scaled circuits
95+
96+
We start by creating a number of noise-scaled circuits which we will pass to the executor. {func}`.get_scale_factor_vectors` determines the scale factor vectors while {func}`.multivariate_layer_scaling` creates the noise scaled circuits.
97+
98+
Each noise-scaled circuit is scaled based on some pre-determined values of the scale factor vectors. These vectors depend
99+
on the degree of polynomial chosen for multivariate extrapolation and the fold multiplier for unitary folding.
100+
101+
```{code-cell} ipython3
102+
import numpy as np
103+
from mitiq.lre.multivariate_scaling import get_scale_factor_vectors
104+
105+
degree = 2
106+
fold_multiplier = 2
107+
108+
scale_factors = get_scale_factor_vectors(circuit, degree, fold_multiplier)
109+
110+
# print a randomly chosen scale factor vector
111+
112+
print(f"Example scale factor vector: {scale_factors[2]}")
113+
114+
```
115+
Each value in the scale factor vector corresponds to how unitary folding is applied to a layer in the circuit. If a scale factor value at some position in the scale factor vector is
116+
117+
- greater than 1, then unitary folding is applied to the layer at that position in the circuit.
118+
- 1, then the layer at that position in the circuit is not scaled.
119+
120+
```{code-cell} ipython3
121+
from mitiq.lre.multivariate_scaling import multivariate_layer_scaling
122+
123+
noise_scaled_circuits = multivariate_layer_scaling(circuit, degree, fold_multiplier)
124+
num_scaled_circuits = len(noise_scaled_circuits)
125+
126+
print(f"Total number of noise-scaled circuits for LRE = {num_scaled_circuits}")
127+
```
128+
129+
An example noise-scaled circuit is shown below:
130+
131+
```{code-cell} ipython3
132+
133+
print("Example noise-scaled circuit:", noise_scaled_circuits[2], sep="\n")
134+
```
135+
136+
### Evaluate noise-scaled expectation values
137+
138+
With the many noise-scaled circuits in hand, we can run them through our executor to obtain the expectation values.
139+
140+
```{code-cell} ipython3
141+
noise_scaled_exp_values = [
142+
execute(circuit) for circuit in noise_scaled_circuits
143+
]
144+
```
145+
146+
147+
## Second step: Multivariate Extrapolation for the error-mitigated expectation value
148+
149+
The penultimate step here is to fetch the coefficients we'll use to combine the noisy data we obtained above. Each noise scaled circuit has a coefficient of linear combination and a noisy expectation value associated with it.
150+
151+
```{code-cell} ipython3
152+
from mitiq.lre.inference import multivariate_richardson_coefficients
153+
154+
155+
coefficients = multivariate_richardson_coefficients(
156+
circuit,
157+
fold_multiplier=fold_multiplier,
158+
degree=degree,
159+
)
160+
```
161+
162+
{func}`.sample_matrix` is used by {func}`.multivariate_richardson_coefficients` behind the scenes to calculate these coefficients as discussed in [](lre-5-theory.md).
163+
164+
These coefficients are calculated through solving a system of linear equations $\mathbf{A} c = z$, where each row of the sample matrix $\mathbf{A}$ is formed by the [monomial terms](lre-3-options.md) of the multivariate polynommial evaluated using the values in the scale factor vectors, $z$ is the vector of expectation values and $c$ is the coefficients vector.
165+
166+
For example, if the terms in the monomial basis are given by the following:
167+
168+
$$\{1, λ_1, λ_2, λ_3, λ_4, λ_1^2, λ_1 λ_2, λ_1 λ_3, λ_1 λ_4, λ_2^2, λ_2 λ_3, λ_2 λ_4, λ_3^2, λ_3 λ_4, λ_4^2\}$$
169+
170+
Each row of the sample matrix is defined by these monomial basis terms. Let one of the scale factor vectors be $(1, 5, 1, 1)$. To get to the sample matrix, each row is then evaluated using the scale factor vectors. A row of the sample matrix is then evaluated using $λ_1=1, λ_2=5, λ_3=1, λ_4=1$.
171+
172+
173+
174+
### Combine the results
175+
176+
The error mitigated expectation value is described as a linear combination of the noisy expectation values where the
177+
coefficients of linear combination were calculated in the preceding section.
178+
179+
```{code-cell} ipython3
180+
mitigated = sum(
181+
exp_val * coeff
182+
for exp_val, coeff in zip(noise_scaled_exp_values, coefficients)
183+
)
184+
print(f"Error with mitigation (LRE): {abs(ideal - mitigated):.{3}}")
185+
```

0 commit comments

Comments
 (0)