# Quantum Gaussian filter

In this example, We introduce the implementation of quantum Gaussian filter.

### What is quantum Gaussian filter

Gaussian filter is a function localized around the parameter $$\mu$$. Interpreting this parameter as the desired energy level, the quantum Gaussian filter is an operator of the system Hamiltonian which acts as the projection onto an energy subspace near it. Given a Hamiltonian $$H$$, the quantum Gaussian filter is

$$
\exp(-(H-\mu I)^2/\sigma^2).
$$

Here, $$\mu$$ is the parameter for location and $$\sigma^2$$controls the width.

Quantum Gaussian filter is used as a useful subroutine in preparing ground state and solving linear system problems. We refer the interested readers to Ref. [\[1, 2\]](#reference) for more details.

### The equivalent problem in function approximation

Suppose the Hamiltonian is normalized and shifted so that $$0 \prec H \prec I$$. The interval of interest becomes $$x \in (0, 1)$$. Hence, we only need to approximate the following function accurately in the positive half interval to implement the quantum Gaussian filter $$f(x) \propto \exp(- (x - \mu)^2 / \sigma^2)$$. Extending this function as an even function, the target of the approximation is

$$
f(x) \propto \exp(-(|x|-\mu)^2 / \sigma^2).
$$

Because the eigenvalue of the Hamiltonian is in $$x \in (0, 1)$$, it suffices to implement the quantum Gaussian filter $$f(H) \propto \exp(-(H-\mu I)^2/\sigma^2)$$.

In numerical demonstration, we consider an even function $$f(x) = 0.99 \* e^{-(|x|-0.5)^2/0.1^2}$$whose $$L^{\infty}$$ norm over $$\[-1,1]$$ is strictly bounded by $$0.99 \approx 1$$, which is very close to the fully-coherent regime $$\Vert f \Vert\_\infty \approx 1$$.

```matlab
parity = 0;
targ = @(x) 0.99 * exp(-(abs(x)-0.5).^2/0.1^2);
% Compute its Chebyshev coefficients. 
d = 100;
% The second parameter of chebfun should be degree + 1.
% The function chebfun(F, N) characterizes the function F on a size-N grid
% by interpolation. Thus, the degree of the interpolated polynomial is N - 1.
f = chebfun(targ,d + 1);
coef = chebcoeffs(f);
% Only need part of Chebyshev coefficients.
coef = coef(parity+1:2:end);
```

<details>

<summary>visualizing polynomial approximation error</summary>

```matlab
figure()
hold on
targ_value = targ(xlist);
plot(xlist,targ_value,'b-','linewidth',2)
plot(xlist,func_value,'-.')
hold off
xlabel('$x$', 'Interpreter', 'latex')
ylabel('$f(x)$', 'Interpreter', 'latex')
legend('target',  'polynomial',...
  'location','se')

figure()
plot(xlist,func_value-targ_value)
xlabel('$x$', 'Interpreter', 'latex')
ylabel('$f_\mathrm{poly}(x)-f(x)$', 'Interpreter', 'latex')
```

</details>

<div><figure><img src="/files/t187v5ed368CBuSBktWi" alt="" width="375"><figcaption><p>Polynomial approximation of gaussian function</p></figcaption></figure> <figure><img src="/files/3HGQAq7cvohWbQASsuDY" alt="" width="375"><figcaption><p>Polynomial approximation error</p></figcaption></figure></div>

### Set up parameters

```matlab
opts.maxiter = 100;
opts.criteria = 1e-12;
opts.targetPre = true;
opts.method = 'Newton';
% use the real representation to speed up the computation
opts.useReal = true;
```

### Solving phase factors by running the solver

```matlab
[phi_proc,out] = QSP_solver(coef,parity,opts);
```

### Verifying the solution

```matlab
xlist = linspace(0,1,1000)';
func = @(x) ChebyCoef2Func(x, coef, parity, true);
targ_value = targ(xlist);
func_value = func(xlist);
QSP_value = QSPGetEntry(xlist, phi_proc, out);
err= norm(QSP_value-func_value,Inf);
disp('The residual error is');
disp(err);

figure()
plot(xlist,QSP_value-func_value)
xlabel('$x$', 'Interpreter', 'latex')
ylabel('$g(x,\Phi^*)-f_\mathrm{poly}(x)$', 'Interpreter', 'latex')
print(gcf,'quantum_gaussian_filter_error.png','-dpng','-r500');
```

<figure><img src="/files/3HGQAq7cvohWbQASsuDY" alt="" width="563"><figcaption><p>The point-wise error of the solved phase factors.</p></figcaption></figure>

### Reference

1. Lin, L., & Tong, Y. (2020). Optimal polynomial based quantum eigenstate filtering with application to solving quantum linear systems. *Quantum*, *4*, 361.
2. Lin, L., & Tong, Y. (2020). Near-optimal ground state preparation. *Quantum*, *4*, 372.
3. Dong, Y., Meng, X., Whaley, K. B., & Lin, L. (2021). Efficient phase-factor evaluation in quantum signal processing. *Physical Review A*, *103*(4), 042419.

<details>

<summary>Output of the code</summary>

```
iter          err
   1  +2.0557e-01 
   2  +4.6388e-02 
   3  +8.9034e-03 
   4  +8.8606e-04 
   5  +1.3739e-05 
   6  +3.5193e-09 
Stop criteria satisfied.
The residual error is
   8.4377e-15

```

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qsppack.gitbook.io/qsppack/examples/quantum-gaussian-filter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
